contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
997
Good Subsegments
[ "data structures" ]
null
null
A permutation $p$ of length $n$ is a sequence $p_1, p_2, \ldots, p_n$ consisting of $n$ distinct integers, each of which from $1$ to $n$ ($1 \leq p_i \leq n$) . Let's call the subsegment $[l,r]$ of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers $p_l, p_{l+1}, \dots, p_r$. For example, good segments of permutation $[1, 3, 2, 5, 4]$ are: - $[1, 1]$, - $[1, 3]$, - $[1, 5]$, - $[2, 2]$, - $[2, 3]$, - $[2, 5]$, - $[3, 3]$, - $[4, 4]$, - $[4, 5]$, - $[5, 5]$. You are given a permutation $p_1, p_2, \ldots, p_n$. You need to answer $q$ queries of the form: find the number of good subsegments of the given segment of permutation. In other words, to answer one query, you need to calculate the number of good subsegments $[x \dots y]$ for some given segment $[l \dots r]$, such that $l \leq x \leq y \leq r$.
The first line contains a single integer $n$ ($1 \leq n \leq 120000$) — the number of elements in the permutation. The second line contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ separated by spaces ($1 \leq p_i \leq n$). The third line contains an integer $q$ ($1 \leq q \leq 120000$) — number of queries. The following $q$ lines describe queries, each line contains a pair of integers $l$, $r$ separated by space ($1 \leq l \leq r \leq n$).
Print a $q$ lines, $i$-th of them should contain a number of good subsegments of a segment, given in the $i$-th query.
[ "5\n1 3 2 5 4\n15\n1 1\n1 2\n1 3\n1 4\n1 5\n2 2\n2 3\n2 4\n2 5\n3 3\n3 4\n3 5\n4 4\n4 5\n5 5\n" ]
[ "1\n2\n5\n6\n10\n1\n3\n4\n7\n1\n2\n4\n1\n3\n1\n" ]
none
[]
46
0
0
43,064
571
Campus
[ "binary search", "data structures", "dsu", "trees" ]
null
null
Oscolcovo city has a campus consisting of *n* student dormitories, *n* universities and *n* military offices. Initially, the *i*-th dormitory belongs to the *i*-th university and is assigned to the *i*-th military office. Life goes on and the campus is continuously going through some changes. The changes can be of four types: 1. University *a**j* merges with university *b**j*. After that all the dormitories that belonged to university *b**j* are assigned to to university *a**j*, and university *b**j* disappears. 1. Military office *c**j* merges with military office *d**j*. After that all the dormitories that were assigned to military office *d**j*, are assigned to military office *c**j*, and military office *d**j* disappears. 1. Students of university *x**j* move in dormitories. Lets *k**x**j* is the number of dormitories that belong to this university at the time when the students move in. Then the number of students in each dormitory of university *x**j* increases by *k**x**j* (note that the more dormitories belong to the university, the more students move in each dormitory of the university). 1. Military office number *y**j* conducts raids on all the dormitories assigned to it and takes all students from there. Thus, at each moment of time each dormitory is assigned to exactly one university and one military office. Initially, all the dormitory are empty. Your task is to process the changes that take place in the campus and answer the queries, how many people currently live in dormitory *q**j*.
The first line contains two integers, *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=5·105) — the number of dormitories and the number of queries, respectively. Next *m* lines contain the queries, each of them is given in one of the following formats: - «U *a**j* *b**j*» — merging universities; - «M *c**j* *d**j*» — merging military offices; - «A *x**j*» — students of university *x**j* moving in the dormitories; - «Z *y**j*» — a raid in military office *y**j*; - «Q *q**j*» — a query asking the number of people in dormitory *q**j*.
In the *i*-th line print the answer to the *i*-th query asking the number of people in the dormitory.
[ "2 7\nA 1\nQ 1\nU 1 2\nA 1\nZ 1\nQ 1\nQ 2\n", "5 12\nU 1 2\nM 4 5\nA 1\nQ 1\nA 3\nA 4\nQ 3\nQ 4\nZ 4\nQ 4\nA 5\nQ 5\n" ]
[ "1\n0\n2\n", "2\n1\n1\n0\n1\n" ]
Consider the first sample test: - In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student. - After the third query university 1 owns dormitories 1 and 2. - The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1. After that 3 students live in the first dormitory and 2 students live in the second dormitory. - At the fifth query the number of students living in dormitory 1, assigned to the military office 1, becomes zero.
[]
46
0
0
43,082
223
Two Strings
[ "data structures", "dp", "strings" ]
null
null
A subsequence of length |*x*| of string *s*<==<=*s*1*s*2... *s*|*s*| (where |*s*| is the length of string *s*) is a string *x*<==<=*s**k*1*s**k*2... *s**k*|*x*| (1<=≤<=*k*1<=&lt;<=*k*2<=&lt;<=...<=&lt;<=*k*|*x*|<=≤<=|*s*|). You've got two strings — *s* and *t*. Let's consider all subsequences of string *s*, coinciding with string *t*. Is it true that each character of string *s* occurs in at least one of these subsequences? In other words, is it true that for all *i* (1<=≤<=*i*<=≤<=|*s*|), there is such subsequence *x*<==<=*s**k*1*s**k*2... *s**k*|*x*| of string *s*, that *x*<==<=*t* and for some *j* (1<=≤<=*j*<=≤<=|*x*|) *k**j*<==<=*i*.
The first line contains string *s*, the second line contains string *t*. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2·105.
Print "Yes" (without the quotes), if each character of the string *s* occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
[ "abab\nab\n", "abacaba\naba\n", "abc\nba\n" ]
[ "Yes\n", "No\n", "No\n" ]
In the first sample string *t* can occur in the string *s* as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string *s* occurs at least once. In the second sample the 4-th character of the string *s* doesn't occur in any occurrence of string *t*. In the third sample there is no occurrence of string *t* in string *s*.
[ { "input": "abab\nab", "output": "Yes" }, { "input": "abacaba\naba", "output": "No" }, { "input": "abc\nba", "output": "No" }, { "input": "babbbbbaba\nab", "output": "No" }, { "input": "accbacabaa\nbada", "output": "No" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "Yes" }, { "input": "hqxibotrjfqrgwrydtlpkzeqrkfgzdylfokmaguwafdgwltgvqobnouttrrfuavfkqcoqxkfwsuseomraigoljzzbjukwaxcftvlhfzdypuinnxbluzfxajkabirvyawtxzcrxpoghucjypwinspnnbptsuamkbjqgttooxwcsqxrukwwtgrkxdujioqywqlugkjngfxrybvxjmlwszszljfgyouvgdilzseekxlsiujhod\nnghetuvcotztgttmr", "output": "No" }, { "input": "bacbbcbcacaacbabacbcbacaaaabbabaaccccacbcbbbabcacbacacabaabacacbaaacacbbccbcccbabccaacccccbbcabacbaacabaccccccacbbaccbabaaabaaccabcaaabcccccbbabccccccabacbaaababcbbbccbbabcabbbbaaabbccccbacbaacbcacbbaaccbaabcaaacbccccbcbababccbcccabbbabbba\nbacbbcbcacaacbabacbcbacaaaabbabaaccccacbcbbbabcacbacacabaabacacbaaacacbbccbcccbabccaacccccbbcabacbaacabaccccccacbbaccbabaaabaaccabcaaabcccccbbabccccccabacbaaababcbbbccbbabcabbbbaaabbccccbacbaacbcacbbaaccbaabcaaacbccccbcbababccbcccabbbabbba", "output": "Yes" }, { "input": "adbecbeaddbbebdaa\nadbecbeaddbbebdaa", "output": "Yes" }, { "input": "iiiiiiqqqqqqqqqqaaaaffffllllleeeeeeeekkkkkkkhhhhhhhhhhooooooddddddddlllllllliiiaaaaaaaaaaaaaaaaaooggggggggggllllllffffffcccccccpppppppdddddddddddccccbbbbbbbbbbkkkkfffffiiiiiiipppppppppccccnnnnnnnnnnnnnnkkkkkkkkkkqqqqppppppeeeeeeeeemmmmmmmmbbbbbbbaaaaaaffffllllljjjj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj", "output": "Yes" }, { "input": "cccbbiiiiiqqvvgggwwwwxxxxxxxxoooondddkkkpvvvdddddooqqxxxxxqqqqllllkkkkkkggggfwwwwwkkkfffeeeemmmmmmmqwwwwwwxxxxxxxdddddqqqqqqq\ncbiqvgwxxondkpvdoqxqlkgfwkfemqwxdq", "output": "Yes" }, { "input": "babaabaabb\nbbccb", "output": "No" }, { "input": "ctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxqcruaopjuqsyyorrobnelehjnxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc\nctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc", "output": "No" }, { "input": "adedadcababceeeaddadedddaeaccccbcccdaeeecaaeaebccebddddeedabbddeaaccdacebaeeccdeeddbecbdecddebe\nadedcceecebdccdbe", "output": "No" }, { "input": "cctckkhatkgrhktihcgififfgfctctkrgiakrifazzggfzczfkkahhafhcfgacccfakkarcatkfiktczkficahgiriakccfiztkhkgrfkrimgamighhtamrhxftaadwxgfggytwjccgkdpyyatctfdygxggkyycpjyfxyfdwtgytcacawjddjdctyfgddkfkypyxftxxtaddcxxpgfgxgdfggfdggdcddtgpxpctpddcdcpc\nctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc", "output": "No" }, { "input": "iqqiaiiffiqlqfaaflfieflfillkkhqfolhehedqdqqfddlheifeoqeohhoadqkfiqeleeqdekhhahkaqqqiaqliiqlelkhdfodeafqfhogihlgoqafdiffkaekhqhgqfkcqiaaoodkkfeqkciqfeihkifeodhahdhddghaihkhahghlkcckicehechocfhfcdfeldelgaqhqfepipegklqiafhqglcdfgkfpoggldgfcglfbbpkkkfipipcnnkqpeembaflj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj", "output": "No" }, { "input": "iqqiaiiffiqlqfaaflfieflfillkkhqfolhehedqdqqfddlheifeoqeohhoadqkfiqeleeqdekhhahkaqqqiaqliiqlelkhdfodeafqfhogihlgoqafdiffkaekhqhgqfkcqiaaoodkkfeqkciqfeihkifeodhahdhddghaihkhahghlkcckicehechocfhfcdfeldelgaqhqfepipegklqiafhqglcdfaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj", "output": "Yes" }, { "input": "abaaaba\nabba", "output": "No" }, { "input": "abaca\nabca", "output": "No" }, { "input": "aaa\naaaa", "output": "No" }, { "input": "aa\naaa", "output": "No" }, { "input": "aaaa\naaa", "output": "Yes" }, { "input": "aa\naaaaaaaa", "output": "No" }, { "input": "aaaaaa\naaaaaaa", "output": "No" }, { "input": "aaaaaaaa\naaaaa", "output": "Yes" }, { "input": "abebea\nabeba", "output": "No" }, { "input": "ab\nabcd", "output": "No" }, { "input": "abcdad\nabcd", "output": "No" }, { "input": "abaaaaba\nabba", "output": "No" }, { "input": "ababa\nab", "output": "No" }, { "input": "cabcbac\ncabac", "output": "No" }, { "input": "ababcab\nabbcab", "output": "No" }, { "input": "abaaaaaaba\nabba", "output": "No" }, { "input": "abcbab\nabcab", "output": "No" }, { "input": "abc\nbac", "output": "No" }, { "input": "abcdadbcd\nabcd", "output": "Yes" } ]
92
0
0
43,251
625
Frog Fights
[ "data structures", "greedy" ]
null
null
Ostap Bender recently visited frog farm and was inspired to create his own frog game. Number of frogs are places on a cyclic gameboard, divided into *m* cells. Cells are numbered from 1 to *m*, but the board is cyclic, so cell number 1 goes right after the cell number *m* in the direction of movement. *i*-th frog during its turn can jump for *a**i* cells. Frogs move in turns, game starts with a move by frog 1. On its turn *i*-th frog moves *a**i* cells forward, knocking out all the frogs on its way. If there is a frog in the last cell of the path of the *i*-th frog, that frog is also knocked out. After this the value *a**i* is decreased by the number of frogs that were knocked out during this turn. If *a**i* is zero or goes negative, then *i*-th frog doesn't make moves anymore. After frog number 1 finishes its turn, frog number 2 starts to move, then frog number 3 and so on. After the frog number *n* makes its move, frog 1 starts to move again, then frog 2 and so on this process goes forever. If some frog was already knocked out from the board, we consider that it skips all its moves. Help Ostap to identify, what frogs will stay on the board at the end of a game?
First line of the input contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100000,<=1<=≤<=*m*<=≤<=109,<=*n*<=≤<=*m*) — number of frogs and gameboard size, respectively. Following *n* lines contains frogs descriptions — two integers *p**i* and *a**i* (1<=≤<=*p**i*,<=*a**i*<=≤<=*m*) — the number of cell occupied by *i*-th frog initially and initial jump length. All *p**i* are guaranteed to be distinct.
In the first line output number of frogs on the final gameboard. In the second line output their numbers in any order.
[ "3 5\n2 1\n5 3\n4 3\n", "5 6\n1 2\n3 4\n2 5\n5 1\n6 1\n" ]
[ "1\n3 ", "2\n1 4 " ]
In the first sample first frog jumps 1 cell and finishes in cell number 3. Second frog jumps for 3 cells and finishes on cell number 3, knocking out frog number 1. Current jump length for frog number 2 is now 2. Third frog jumps to cell 2, then second frog jumps to cell 5. Third frog in turn finishes in cell 5 and removes frog 2 from the gameboard. Now, it's the only remaining frog in the game. In the second sample first frog jumps 2 cells and knocks out frogs in cells 2 and 3. Its value *a*<sub class="lower-index">*i*</sub> is now 0. Then fourth frog jumps and knocks out fifth frog and its *a*<sub class="lower-index">*i*</sub> is now 0 too. These two frogs will remains on the gameboard forever.
[]
46
0
0
43,266
101
Buses
[ "binary search", "data structures", "dp" ]
B. Buses
2
265
Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly *n*<=+<=1 bus stops. All of them are numbered with integers from 0 to *n* in the order in which they follow from Gerald's home. The bus stop by Gerald's home has number 0 and the bus stop by the school has number *n*. There are *m* buses running between the house and the school: the *i*-th bus goes from stop *s**i* to *t**i* (*s**i*<=&lt;<=*t**i*), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald's no idiot and he wouldn't get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the *i*-th bus on any stop numbered from *s**i* to *t**i*<=-<=1 inclusive, but he can get off the *i*-th bus only on the bus stop *t**i*. Gerald can't walk between the bus stops and he also can't move in the direction from the school to the house. Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109<=+<=7).
The first line contains two space-separated integers: *n* and *m* (1<=≤<=*n*<=≤<=109,<=0<=≤<=*m*<=≤<=105). Then follow *m* lines each containing two integers *s**i*,<=*t**i*. They are the numbers of starting stops and end stops of the buses (0<=≤<=*s**i*<=&lt;<=*t**i*<=≤<=*n*).
Print the only number — the number of ways to get to the school modulo 1000000007 (109<=+<=7).
[ "2 2\n0 1\n1 2\n", "3 2\n0 1\n1 2\n", "5 5\n0 1\n0 2\n0 3\n0 4\n0 5\n" ]
[ "1\n", "0\n", "16\n" ]
The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two. In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0. In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 2<sup class="upper-index">4</sup> = 16.
[ { "input": "2 2\n0 1\n1 2", "output": "1" }, { "input": "3 2\n0 1\n1 2", "output": "0" }, { "input": "5 5\n0 1\n0 2\n0 3\n0 4\n0 5", "output": "16" }, { "input": "3 3\n1 2\n2 3\n1 3", "output": "0" }, { "input": "10 10\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7\n0 8\n0 9\n0 10", "output": "512" }, { "input": "6 6\n3 4\n2 3\n3 5\n0 1\n1 2\n3 6", "output": "4" }, { "input": "7 7\n0 1\n1 3\n2 3\n4 6\n5 7\n4 5\n5 7", "output": "0" }, { "input": "1000000000 0", "output": "0" }, { "input": "8 8\n0 1\n4 5\n7 8\n3 4\n2 3\n6 7\n5 6\n1 2", "output": "1" }, { "input": "6 1\n0 6", "output": "1" }, { "input": "6 4\n0 3\n1 2\n4 5\n4 6", "output": "0" }, { "input": "5 15\n0 1\n0 2\n0 3\n0 4\n0 5\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5", "output": "360" }, { "input": "5 3\n0 1\n2 3\n4 5", "output": "0" }, { "input": "5 15\n0 1\n1 2\n2 3\n3 4\n4 5\n1 2\n2 3\n3 4\n4 5\n2 3\n3 4\n4 5\n3 4\n4 5\n4 5", "output": "120" }, { "input": "8 94\n2 8\n3 8\n5 6\n1 2\n4 6\n2 7\n2 4\n3 5\n0 2\n0 1\n7 8\n0 7\n0 5\n1 4\n2 7\n3 4\n6 7\n1 5\n4 6\n4 6\n2 8\n4 5\n0 1\n3 8\n5 8\n1 3\n3 4\n1 6\n1 6\n1 7\n1 7\n1 4\n5 6\n5 7\n2 4\n3 8\n0 1\n0 4\n4 8\n1 8\n3 8\n2 4\n5 7\n2 4\n2 7\n3 8\n3 7\n0 6\n1 2\n0 2\n2 7\n0 4\n0 3\n3 6\n0 2\n5 7\n4 8\n3 6\n0 3\n3 5\n2 3\n1 8\n3 7\n0 6\n4 6\n1 8\n1 2\n3 5\n1 5\n1 2\n0 2\n0 3\n4 7\n1 4\n2 5\n5 8\n0 3\n5 7\n5 8\n0 2\n1 5\n4 6\n3 6\n5 6\n0 6\n1 7\n7 8\n2 7\n2 4\n1 7\n0 7\n1 6\n3 8\n0 7", "output": "203624961" }, { "input": "97 53\n21 34\n19 95\n0 6\n28 40\n26 41\n39 41\n47 85\n32 46\n2 17\n55 73\n18 67\n36 85\n77 96\n77 97\n1 53\n12 49\n9 71\n29 92\n35 89\n40 43\n5 78\n13 92\n2 97\n11 22\n4 6\n22 92\n60 87\n25 47\n10 59\n51 70\n13 95\n27 43\n5 71\n48 73\n82 94\n45 51\n85 97\n51 89\n15 66\n44 80\n78 93\n65 84\n9 75\n28 30\n39 69\n50 89\n41 77\n14 31\n12 97\n69 86\n15 18\n14 56\n38 47", "output": "478604297" }, { "input": "33 5\n17 18\n5 27\n18 29\n12 24\n14 31", "output": "0" }, { "input": "93 69\n9 92\n31 37\n58 83\n28 93\n36 44\n22 90\n61 88\n76 83\n19 85\n25 87\n55 84\n45 47\n5 27\n54 82\n4 65\n12 81\n49 55\n16 52\n16 34\n34 44\n17 36\n62 64\n7 34\n19 21\n16 73\n3 55\n12 62\n49 91\n2 36\n47 65\n17 37\n70 80\n52 71\n59 77\n1 17\n23 81\n15 67\n38 67\n14 48\n70 82\n33 51\n31 88\n28 51\n10 54\n6 71\n37 88\n5 60\n2 91\n88 91\n30 91\n17 58\n12 72\n14 77\n34 90\n15 42\n44 47\n54 87\n84 90\n3 49\n26 71\n40 87\n71 74\n20 60\n86 92\n76 83\n40 80\n3 31\n18 33\n5 82", "output": "0" }, { "input": "10 59\n4 7\n4 8\n0 4\n5 7\n6 9\n7 8\n0 9\n6 7\n4 9\n1 10\n5 6\n1 4\n0 4\n4 9\n3 6\n1 7\n4 9\n3 7\n1 2\n0 1\n4 7\n0 8\n8 10\n0 3\n2 5\n0 7\n1 8\n2 10\n0 3\n0 9\n7 8\n2 6\n1 6\n2 10\n3 10\n3 4\n0 2\n0 8\n3 8\n9 10\n1 6\n7 10\n6 9\n2 10\n2 10\n3 5\n9 10\n4 10\n0 8\n5 9\n4 6\n0 10\n6 9\n1 2\n6 7\n1 5\n0 6\n0 7\n0 6", "output": "28167561" }, { "input": "66 35\n49 55\n9 30\n28 54\n44 62\n55 61\n1 21\n6 37\n8 10\n26 33\n19 37\n12 23\n24 42\n34 64\n8 56\n36 40\n16 58\n21 30\n16 36\n36 38\n19 45\n26 49\n6 62\n1 11\n22 48\n33 38\n8 41\n29 53\n58 60\n27 66\n2 19\n48 53\n25 47\n48 56\n61 65\n45 46", "output": "0" }, { "input": "31 26\n15 21\n4 25\n5 19\n16 18\n5 23\n3 25\n7 18\n24 31\n6 9\n8 25\n18 29\n12 27\n15 16\n12 20\n2 7\n14 26\n13 22\n5 19\n5 24\n15 23\n4 7\n8 12\n14 26\n28 30\n1 30\n24 31", "output": "0" }, { "input": "69 68\n49 62\n3 38\n1 43\n42 58\n12 64\n1 37\n35 59\n7 43\n2 29\n8 65\n19 47\n4 27\n41 58\n25 60\n17 37\n34 40\n16 38\n28 52\n35 63\n6 65\n57 58\n38 50\n8 28\n6 8\n10 44\n48 63\n2 42\n46 58\n26 62\n37 45\n7 22\n0 21\n19 48\n6 67\n6 15\n28 38\n19 22\n16 20\n27 40\n0 3\n33 69\n2 66\n10 24\n29 48\n26 69\n15 53\n24 34\n34 58\n20 47\n21 23\n38 68\n34 45\n60 68\n7 15\n21 34\n16 30\n14 58\n2 62\n24 66\n13 27\n24 40\n32 37\n10 37\n22 40\n44 50\n27 31\n0 44\n20 32", "output": "622740890" }, { "input": "1 0", "output": "0" }, { "input": "68 74\n51 54\n3 22\n12 24\n3 27\n32 42\n36 55\n60 64\n1 4\n4 23\n11 64\n54 62\n50 56\n21 34\n27 63\n15 54\n28 61\n13 57\n39 53\n12 32\n32 40\n33 67\n55 61\n33 67\n30 37\n15 49\n27 45\n21 41\n8 42\n24 63\n40 48\n28 41\n30 67\n0 4\n7 15\n27 59\n60 62\n25 65\n30 31\n38 67\n24 43\n14 64\n26 46\n8 12\n34 41\n32 67\n11 42\n11 53\n45 55\n2 47\n7 51\n30 54\n21 44\n7 52\n40 62\n16 50\n10 41\n26 65\n16 51\n6 29\n1 31\n48 54\n9 42\n33 45\n19 59\n25 37\n21 62\n20 58\n23 59\n12 61\n2 46\n19 49\n44 60\n1 20\n19 66", "output": "0" }, { "input": "79 68\n26 47\n55 70\n5 40\n7 45\n16 21\n31 38\n19 62\n40 55\n42 78\n60 61\n43 69\n50 73\n3 77\n2 45\n2 29\n10 58\n2 11\n62 76\n57 70\n65 73\n37 67\n9 24\n4 28\n8 16\n31 44\n10 66\n47 70\n19 45\n17 28\n5 36\n9 68\n2 35\n55 77\n51 71\n1 59\n6 33\n21 53\n39 49\n59 70\n17 44\n18 64\n49 78\n0 52\n24 56\n65 79\n19 51\n42 77\n37 78\n20 39\n47 56\n19 78\n50 78\n3 67\n37 47\n5 27\n40 51\n24 29\n50 54\n45 50\n13 76\n29 31\n0 28\n26 36\n21 44\n71 77\n55 58\n38 61\n22 44", "output": "317376853" }, { "input": "45 51\n2 12\n6 18\n4 17\n8 25\n16 24\n3 23\n29 31\n31 40\n7 26\n5 6\n35 37\n1 36\n9 45\n18 36\n12 27\n5 15\n11 16\n19 29\n8 23\n1 27\n0 30\n25 38\n21 44\n34 39\n10 41\n4 16\n11 36\n0 8\n15 38\n3 33\n11 31\n2 33\n5 34\n24 28\n7 32\n15 25\n2 27\n16 44\n31 40\n35 45\n13 38\n29 42\n18 23\n8 25\n13 21\n3 39\n3 41\n5 6\n13 21\n11 20\n23 42", "output": "493168232" }, { "input": "5 31\n0 2\n3 4\n3 5\n2 4\n1 3\n1 2\n2 5\n1 5\n0 2\n2 5\n1 4\n0 2\n1 3\n0 5\n2 3\n1 5\n1 2\n2 3\n0 1\n0 1\n2 4\n0 4\n1 2\n0 3\n1 2\n3 4\n0 2\n0 4\n1 2\n2 5\n1 5", "output": "8595" }, { "input": "81 52\n33 48\n59 61\n37 77\n58 73\n29 54\n1 17\n8 29\n50 73\n7 26\n35 41\n22 26\n9 22\n0 11\n40 73\n25 57\n35 55\n36 54\n29 41\n56 66\n42 77\n29 48\n41 66\n25 36\n2 55\n58 64\n0 61\n23 31\n9 61\n27 45\n2 71\n14 29\n4 31\n0 35\n31 77\n21 39\n0 54\n46 68\n18 62\n41 45\n12 28\n59 66\n39 71\n10 59\n29 77\n16 48\n13 46\n30 73\n2 41\n42 55\n19 61\n28 29\n20 42", "output": "0" }, { "input": "84 50\n33 46\n19 40\n51 64\n37 45\n35 81\n44 81\n6 57\n57 60\n14 53\n15 49\n4 30\n35 49\n2 51\n8 72\n15 18\n49 51\n14 49\n50 71\n41 59\n28 60\n61 81\n9 12\n34 79\n5 56\n60 67\n21 60\n39 71\n31 60\n13 35\n16 84\n17 33\n48 57\n36 61\n50 55\n5 84\n66 79\n61 70\n42 49\n19 39\n47 49\n3 82\n59 65\n8 44\n71 80\n66 77\n8 65\n1 81\n7 82\n50 74\n10 17", "output": "0" }, { "input": "100 68\n77 89\n19 71\n11 46\n23 70\n16 47\n4 61\n7 96\n38 74\n79 95\n68 75\n14 86\n10 55\n7 13\n88 99\n19 21\n4 94\n17 83\n11 16\n7 50\n58 96\n4 58\n17 72\n44 56\n35 91\n50 88\n9 37\n36 52\n83 89\n8 16\n1 80\n12 75\n3 27\n92 93\n53 88\n37 49\n34 78\n31 66\n39 55\n36 94\n22 67\n47 85\n20 58\n62 98\n41 89\n85 96\n11 73\n39 95\n44 68\n25 33\n36 45\n66 70\n66 93\n17 97\n1 71\n49 53\n47 54\n19 95\n10 12\n38 57\n47 68\n21 70\n32 93\n53 71\n45 59\n27 48\n47 63\n75 76\n8 57", "output": "0" }, { "input": "918949684 6\n351553415 785588657\n423490842 845475457\n351553415 918949684\n740298829 785588657\n351328841 610486484\n423490842 847590951", "output": "0" }, { "input": "863261873 5\n137690029 666186924\n137690029 379800754\n515537329 666186924\n442925959 722302912\n137690029 863261873", "output": "0" }, { "input": "735324925 2\n642054038 735324925\n170935185 642054038", "output": "0" }, { "input": "977743286 6\n317778866 395496218\n395496218 932112884\n98371691 432544933\n440553 922085291\n440553 432544933\n586988624 922085291", "output": "0" }, { "input": "977700285 7\n386643627 467079072\n116215943 914856211\n15183537 386643627\n424146511 977700285\n15183537 620050423\n336304090 947990602\n116215943 914856211", "output": "0" }, { "input": "768016717 4\n242598247 348534209\n33560125 170667468\n348534209 700314158\n700314158 768016717", "output": "0" }, { "input": "814609521 3\n622460875 697824636\n283825432 369448402\n614658965 622460875", "output": "0" }, { "input": "931612300 8\n64655010 186892167\n25283092 580196656\n297609123 628681221\n25283092 186892167\n186892167 221075230\n221075230 634105512\n25283092 156293949\n86333513 156293949", "output": "0" }, { "input": "947714605 4\n23890708 35992029\n35992029 947714605\n93644635 629491402\n23890708 947714605", "output": "0" }, { "input": "768016717 4\n242598247 348534209\n33560125 170667468\n348534209 700314158\n700314158 768016717", "output": "0" }, { "input": "1000000000 2\n0 500000000\n500000000 1000000000", "output": "1" } ]
92
0
0
43,313
409
Big Data
[ "*special" ]
null
null
Little Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (Div.2). The scientists rejoiced at the gift and decided to put some important facts to this array. Here are the first few of the facts: - The highest mountain above sea level in the world is Mount Everest. Its peak rises to 8848 m. - The largest board game tournament consisted of 958 participants playing chapaev.- The largest online maths competition consisted of 12766 participants.- The Nile is credited as the longest river in the world. From its farthest stream in Burundi, it extends 6695 km in length.- While not in flood, the main stretches of the Amazon river in South America can reach widths of up to 1100 km at its widest points.- Angel Falls is the highest waterfall. Its greatest single drop measures 807 m.- The Hotel Everest View above Namche, Nepal — the village closest to Everest base camp – is at a record height of 31962 m- Uranium is the heaviest of all the naturally occurring elements. Its most common isotope has a nucleus containing 146 neutrons.- The coldest permanently inhabited place is the Siberian village of Oymyakon, where the temperature of -68°C was registered in the twentieth century.- The longest snake held in captivity is over 25 feet long. Its name is Medusa.- Colonel Meow holds the world record for longest fur on a cat — almost 134 centimeters.- Sea otters can have up to 10000 hairs per square inch. This is the most dense fur in the animal kingdom.- The largest state of USA is Alaska; its area is 663268 square miles- Alaska has a longer coastline than all of the other 49 U.S. States put together: it is 154103 miles long.- Lake Baikal is the largest freshwater lake in the world. It reaches 1642 meters in depth and contains around one-fifth of the world’s unfrozen fresh water.- The most colorful national flag is the one of Turkmenistan, with 106 colors.
The input will contain a single integer between 1 and 16.
Output a single integer.
[ "1\n", "7\n" ]
[ "1\n", "0\n" ]
none
[ { "input": "1", "output": "1" }, { "input": "7", "output": "0" }, { "input": "13", "output": "1" }, { "input": "3", "output": "0" }, { "input": "8", "output": "1" }, { "input": "16", "output": "0" }, { "input": "11", "output": "0" }, { "input": "2", "output": "0" }, { "input": "5", "output": "0" }, { "input": "10", "output": "1" }, { "input": "9", "output": "1" }, { "input": "15", "output": "1" }, { "input": "4", "output": "1" }, { "input": "12", "output": "0" }, { "input": "6", "output": "1" }, { "input": "14", "output": "0" } ]
31
0
0
43,325
952
Puzzling Language
[ "constructive algorithms" ]
null
null
In this problem you will write a simple code generator for a 2D programming language derived from [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck). The code in this language is a rectangular grid of characters '.' and 'X'. The code is converted to a Brainfuck program as follows: the characters are read in the usual order (top to bottom, left to right), and each 'X' character is converted a Brainfuck instruction to be executed. The instruction is defined by the left, top and right neighbors of the 'X' character using the following conversion table: You are given a string. Output a program in the described language which prints this string. You can download the language interpreter used for judging here: [https://assets.codeforces.com/rounds/952/puzzling-interpreter.cpp](https://assets.codeforces.com/rounds/952/puzzling-interpreter.cpp) (use C++11 to compile the code). Note several implementation details: - The first step of the language interpretation is conversion to a Brainfuck program, which is then executed.- The code must be rectangular, with all lines of the same length. It can have at most 10,000 lines and 10,000 columns, and can have at most 500,000 'X' characters.- The code has toroidal topology, i.e. the 'X' on the first line will have top neighbor in the last line.- Brainfuck interpreter has 30000 memory cells which store integers from 0 to 255 with increment/decrement done modulo 256.- Console input (, command) is allowed in Brainfuck code but has no effect when executed.
The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive.
Output a program in the described language which, when executed, will print the given message.
[ "$$$" ]
[ ".......X.......\n......XXX......\n.....XXXXX.....\n....XXXXXXX....\n...XXXXXXXXX...\n..XXXXXXXXXXX..\n.XXXXXXXXXXXXX.\n...............\nX.............X\nX..............\nX..............\nX..............\n" ]
The example corresponds to the following Brainfuck program: The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines print the '$' character three times.
[ { "input": "$$$", "output": "..\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n..." }, { "input": "Codeforces", "output": "..\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n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\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n..." }, { "input": "\"$&(*,.024", "output": "..\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n..." } ]
46
0
0
43,399
0
none
[ "none" ]
null
null
You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator. Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator. According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order. The elevator has two commands: - Go up or down one floor. The movement takes 1 second. - Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator. Initially the elevator is empty and is located on the floor 1. You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=2000) — the number of employees. The *i*-th of the next *n* lines contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=9, *a**i*<=≠<=*b**i*) — the floor on which an employee initially is, and the floor he wants to reach. The employees are given in the order they came to the elevator.
Print a single integer — the minimal possible time in seconds.
[ "2\n3 5\n5 3\n", "2\n5 3\n3 5\n" ]
[ "10", "12" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/1b7e25450a0703db67675fea25386865f0290049.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 2 <img class="tex-graphics" src="https://espresso.codeforces.com/0f585328ada5689c39e5a7263a792979c00fb01a.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 3 <img class="tex-graphics" src="https://espresso.codeforces.com/61f0c2263487060af2083a06cedf6641d234e31b.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 5 <img class="tex-graphics" src="https://espresso.codeforces.com/4f5d7a22c1f2af851019589a1232851ced924fbf.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 6 <img class="tex-graphics" src="https://espresso.codeforces.com/4602434598e4844e9532caef51583bd84886b067.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 7 <img class="tex-graphics" src="https://espresso.codeforces.com/c1924de28ca19fe1346617380752126a2d4bbb75.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 9 <img class="tex-graphics" src="https://espresso.codeforces.com/2e58cd09a6a459da60780a790c49da336d0b2783.png" style="max-width: 100.0%;max-height: 100.0%;"/> *t* = 10
[]
15
0
0
43,424
839
Winter is here
[ "combinatorics", "dp", "math", "number theory" ]
null
null
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of *n* soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the *i*-th soldier’s strength be *a**i*. For some *k* he calls *i*1,<=*i*2,<=...,<=*i**k* a clan if *i*1<=&lt;<=*i*2<=&lt;<=*i*3<=&lt;<=...<=&lt;<=*i**k* and *gcd*(*a**i*1,<=*a**i*2,<=...,<=*a**i**k*)<=&gt;<=1 . He calls the strength of that clan *k*·*gcd*(*a**i*1,<=*a**i*2,<=...,<=*a**i**k*). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109<=+<=7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
The first line contains integer *n* (1<=≤<=*n*<=≤<=200000) — the size of the army. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1000000) — denoting the strengths of his soldiers.
Print one integer — the strength of John Snow's army modulo 1000000007 (109<=+<=7).
[ "3\n3 3 1\n", "4\n2 3 4 6\n" ]
[ "12\n", "39\n" ]
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12
[ { "input": "3\n3 3 1", "output": "12" }, { "input": "4\n2 3 4 6", "output": "39" } ]
1,434
35,942,400
3
43,433
730
Olympiad in Programming and Sports
[ "dp", "flows", "graphs", "greedy" ]
null
null
There are *n* students at Berland State University. Every student has two skills, each measured as a number: *a**i* — the programming skill and *b**i* — the sports skill. It is announced that an Olympiad in programming and sports will be held soon. That's why Berland State University should choose two teams: one to take part in the programming track and one to take part in the sports track. There should be exactly *p* students in the programming team and exactly *s* students in the sports team. A student can't be a member of both teams. The university management considers that the strength of the university on the Olympiad is equal to the sum of two values: the programming team strength and the sports team strength. The strength of a team is the sum of skills of its members in the corresponding area, so the strength of the programming team is the sum of all *a**i* and the strength of the sports team is the sum of all *b**i* over corresponding team members. Help Berland State University to compose two teams to maximize the total strength of the university on the Olympiad.
The first line contains three positive integer numbers *n*, *p* and *s* (2<=≤<=*n*<=≤<=3000, *p*<=+<=*s*<=≤<=*n*) — the number of students, the size of the programming team and the size of the sports team. The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=3000), where *a**i* is the programming skill of the *i*-th student. The third line contains *n* positive integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=3000), where *b**i* is the sports skill of the *i*-th student.
In the first line, print the the maximum strength of the university on the Olympiad. In the second line, print *p* numbers — the members of the programming team. In the third line, print *s* numbers — the members of the sports team. The students are numbered from 1 to *n* as they are given in the input. All numbers printed in the second and in the third lines should be distinct and can be printed in arbitrary order. If there are multiple solutions, print any of them.
[ "5 2 2\n1 3 4 5 2\n5 3 2 1 4\n", "4 2 2\n10 8 8 3\n10 7 9 4\n", "5 3 1\n5 2 5 1 7\n6 3 1 6 3\n" ]
[ "18\n3 4 \n1 5 \n", "31\n1 2 \n3 4 \n", "23\n1 3 5 \n4 \n" ]
none
[ { "input": "5 2 2\n1 3 4 5 2\n5 3 2 1 4", "output": "18\n3 4 \n1 5 " }, { "input": "4 2 2\n10 8 8 3\n10 7 9 4", "output": "31\n1 2 \n3 4 " }, { "input": "5 3 1\n5 2 5 1 7\n6 3 1 6 3", "output": "23\n1 3 5 \n4 " }, { "input": "2 1 1\n100 101\n1 100", "output": "200\n1 \n2 " }, { "input": "4 1 1\n100 100 1 50\n100 100 50 1", "output": "200\n1 \n2 " }, { "input": "2 1 1\n3 2\n3 2", "output": "5\n1 \n2 " }, { "input": "2 1 1\n9 6\n3 10", "output": "19\n1 \n2 " }, { "input": "2 1 1\n1 17\n5 20", "output": "22\n2 \n1 " }, { "input": "3 1 1\n5 4 2\n1 5 2", "output": "10\n1 \n2 " }, { "input": "3 1 1\n10 5 5\n9 1 4", "output": "14\n1 \n3 " }, { "input": "3 1 1\n17 6 2\n2 19 19", "output": "36\n1 \n2 " }, { "input": "4 1 2\n4 2 4 5\n3 2 5 3", "output": "13\n4 \n1 3 " }, { "input": "4 1 2\n8 7 8 6\n4 5 10 9", "output": "27\n1 \n3 4 " }, { "input": "4 1 3\n6 15 3 9\n2 5 6 8", "output": "31\n2 \n1 3 4 " }, { "input": "5 1 1\n3 2 5 5 1\n3 1 5 4 2", "output": "10\n4 \n3 " }, { "input": "5 2 1\n9 10 1 7 10\n6 10 8 6 3", "output": "29\n1 5 \n2 " }, { "input": "5 2 3\n10 4 19 8 18\n6 16 11 15 3", "output": "74\n3 5 \n1 2 4 " }, { "input": "6 2 1\n4 3 4 3 3 2\n4 4 3 5 3 5", "output": "13\n1 3 \n4 " }, { "input": "6 1 4\n7 9 3 5 9 2\n10 9 10 10 10 1", "output": "49\n2 \n1 3 4 5 " }, { "input": "6 3 3\n15 12 12 19 1 7\n7 2 20 10 4 12", "output": "82\n1 2 4 \n3 5 6 " }, { "input": "7 2 1\n2 2 2 2 2 1 2\n4 2 5 5 2 5 1", "output": "9\n1 2 \n3 " }, { "input": "7 5 1\n1 8 8 6 4 3 9\n4 4 5 8 5 7 1", "output": "42\n2 3 4 5 7 \n6 " }, { "input": "7 2 3\n15 1 5 17 16 9 1\n9 8 5 9 18 14 3", "output": "72\n1 4 \n2 5 6 " }, { "input": "8 3 4\n5 5 4 2 4 1 3 2\n2 5 3 3 2 4 5 1", "output": "30\n1 3 5 \n2 4 6 7 " }, { "input": "8 5 1\n2 4 1 5 8 5 9 7\n10 2 3 1 6 3 8 6", "output": "44\n4 5 6 7 8 \n1 " }, { "input": "8 1 1\n19 14 17 8 16 14 11 16\n12 12 10 4 3 11 10 8", "output": "31\n1 \n2 " }, { "input": "9 1 1\n3 2 3 5 3 1 5 2 3\n1 4 5 4 2 5 4 4 5", "output": "10\n4 \n3 " }, { "input": "9 2 4\n4 3 3 1 1 10 9 8 5\n5 4 4 6 5 10 1 5 5", "output": "43\n7 8 \n1 4 5 6 " }, { "input": "9 2 2\n20 7 6 7 19 15 2 7 8\n15 15 1 13 20 14 13 18 3", "output": "73\n1 6 \n5 8 " }, { "input": "10 5 2\n4 5 3 1 1 5 2 4 1 5\n3 4 2 2 2 3 2 1 2 4", "output": "27\n1 2 6 8 10 \n3 4 " }, { "input": "10 8 2\n5 2 8 6 7 5 2 4 1 10\n4 6 2 1 9 2 9 4 5 6", "output": "61\n1 3 4 5 6 8 9 10 \n2 7 " }, { "input": "10 3 1\n7 11 11 3 19 10 18 7 9 20\n13 9 19 15 13 14 7 12 15 16", "output": "76\n5 7 10 \n3 " }, { "input": "11 4 2\n2 2 4 2 3 5 4 4 5 5 4\n4 4 1 2 1 2 2 5 3 4 3", "output": "28\n3 6 9 10 \n1 8 " }, { "input": "11 1 5\n7 10 1 2 10 8 10 9 5 5 9\n2 1 1 3 5 9 3 4 2 2 3", "output": "34\n2 \n4 5 6 7 8 " }, { "input": "11 6 1\n7 4 7 2 2 12 16 2 5 15 2\n3 12 8 5 7 1 4 19 12 1 14", "output": "81\n1 3 6 7 9 10 \n8 " }, { "input": "12 4 1\n4 5 1 4 3 3 2 4 3 4 3 2\n1 3 5 3 5 5 5 5 3 5 3 2", "output": "22\n1 2 4 8 \n3 " }, { "input": "12 8 1\n4 3 3 5 6 10 10 10 10 8 4 5\n1 7 4 10 8 1 2 4 8 4 4 2", "output": "73\n1 5 6 7 8 9 10 12 \n4 " }, { "input": "12 2 4\n16 17 12 8 18 9 2 9 13 18 3 8\n18 20 9 12 11 19 20 3 13 1 6 9", "output": "113\n5 10 \n1 2 6 7 " }, { "input": "13 1 10\n1 4 5 3 1 3 4 3 1 5 3 2 3\n2 3 5 1 4 3 5 4 2 1 3 4 2", "output": "40\n10 \n1 2 3 5 6 7 8 9 11 12 " }, { "input": "13 2 2\n2 2 6 2 9 5 10 3 10 1 1 1 1\n10 8 3 8 6 6 8 1 4 10 10 1 8", "output": "40\n7 9 \n1 10 " }, { "input": "13 3 1\n16 6 5 11 17 11 13 12 18 5 12 6 12\n12 20 9 9 19 4 19 4 1 12 1 12 4", "output": "71\n1 5 9 \n2 " }, { "input": "14 1 3\n1 1 2 3 4 3 1 3 4 5 3 5 5 5\n3 2 1 1 1 4 2 2 1 4 4 4 5 4", "output": "18\n10 \n6 11 13 " }, { "input": "14 2 1\n3 5 9 5 4 6 1 10 4 10 6 5 10 2\n10 8 8 6 1 8 9 1 6 1 4 5 9 4", "output": "30\n8 10 \n1 " }, { "input": "14 2 8\n20 14 17 18 12 12 19 3 2 20 13 12 17 20\n20 10 3 15 8 15 12 12 14 2 1 15 7 10", "output": "153\n10 14 \n1 2 4 6 7 8 9 12 " }, { "input": "15 7 6\n2 5 4 1 1 3 3 1 4 4 4 3 4 1 1\n5 5 2 5 4 1 4 5 1 5 4 1 4 4 4", "output": "55\n2 3 6 9 11 12 13 \n1 4 5 7 8 10 " }, { "input": "15 1 10\n7 8 1 5 8 8 9 7 4 3 7 4 10 8 3\n3 8 6 5 10 1 9 2 3 8 1 9 3 6 10", "output": "84\n13 \n1 2 3 4 5 7 10 12 14 15 " }, { "input": "15 3 7\n1 11 6 5 16 13 17 6 2 7 19 5 3 13 11\n11 9 6 9 19 4 16 20 11 19 1 10 20 4 7", "output": "161\n6 11 14 \n1 5 7 8 9 10 13 " }, { "input": "16 2 7\n5 4 4 1 5 3 1 1 2 3 3 4 5 5 1 4\n4 5 3 5 4 1 2 2 3 2 2 3 4 5 3 1", "output": "38\n1 5 \n2 3 4 9 12 13 14 " }, { "input": "16 4 8\n2 6 6 4 1 9 5 8 9 10 2 8 9 8 1 7\n8 9 5 2 4 10 9 2 1 5 6 7 1 1 8 1", "output": "98\n8 9 10 13 \n1 2 3 6 7 11 12 15 " }, { "input": "16 4 1\n5 20 3 7 19 19 7 17 18 10 16 11 16 9 15 9\n19 2 13 11 8 19 6 7 16 8 8 5 18 18 20 10", "output": "96\n2 5 6 9 \n15 " }, { "input": "17 1 12\n2 4 5 5 3 3 3 3 1 4 4 1 2 2 3 3 3\n4 1 5 4 2 5 3 4 2 2 5 2 2 5 5 5 3", "output": "54\n10 \n1 3 4 5 6 7 8 11 14 15 16 17 " }, { "input": "17 8 2\n10 5 9 1 7 5 2 9 3 5 8 4 3 5 4 2 4\n9 10 8 10 10 5 6 2 2 4 6 9 10 3 2 5 1", "output": "78\n1 3 5 6 8 10 11 14 \n2 4 " }, { "input": "17 6 5\n18 9 15 14 15 20 18 8 3 9 17 5 2 17 7 10 13\n17 10 7 3 7 11 4 5 18 15 15 15 5 9 7 5 5", "output": "179\n3 4 5 6 7 14 \n1 9 10 11 12 " }, { "input": "18 5 2\n5 3 3 4 1 4 5 3 3 3 4 2 4 2 3 1 4 4\n5 4 3 4 5 1 5 5 2 1 3 2 1 1 1 3 5 5", "output": "32\n1 4 6 7 11 \n5 8 " }, { "input": "18 8 1\n6 10 1 1 10 6 10 2 7 2 3 7 7 7 6 5 8 8\n4 4 4 7 1 5 2 2 7 10 2 7 6 6 2 1 4 3", "output": "77\n2 5 7 9 12 13 17 18 \n10 " }, { "input": "18 5 3\n18 1 8 13 18 1 16 11 11 12 6 14 16 13 10 7 19 17\n14 3 7 18 9 16 3 5 17 8 1 8 2 8 20 1 16 11", "output": "143\n1 5 7 17 18 \n4 9 15 " }, { "input": "19 6 1\n4 5 2 3 4 3 2 3 3 3 5 5 1 4 1 2 4 2 5\n1 2 1 4 1 3 3 2 4 1 3 4 3 3 4 4 4 5 5", "output": "33\n1 2 5 11 12 19 \n18 " }, { "input": "19 14 2\n5 3 4 10 5 7 10 9 2 5 4 3 2 3 10 10 6 4 1\n6 10 5 3 8 9 9 3 1 6 4 4 3 6 8 5 9 3 9", "output": "111\n1 3 4 5 6 7 8 10 11 12 15 16 17 18 \n2 19 " }, { "input": "19 1 4\n2 10 1 3 13 3 6 2 15 15 7 8 1 18 2 12 9 8 14\n15 3 2 15 9 12 19 20 2 18 15 11 18 6 8 16 17 1 12", "output": "93\n14 \n7 8 10 13 " }, { "input": "20 3 6\n3 4 4 5 1 2 2 3 5 5 2 2 1 4 1 5 2 2 1 5\n1 4 5 2 2 2 2 5 3 2 4 5 2 1 3 3 1 3 5 3", "output": "43\n4 9 10 \n2 3 8 11 12 19 " }, { "input": "20 2 5\n9 5 1 8 6 3 5 9 9 9 9 3 4 1 7 2 1 1 3 5\n5 6 4 10 7 9 1 6 9 5 2 1 3 1 5 9 10 8 9 9", "output": "65\n1 8 \n4 6 9 16 17 " }, { "input": "20 1 7\n20 8 10 7 14 9 17 19 19 9 20 6 1 14 11 15 12 10 20 15\n10 3 20 1 16 7 8 19 3 17 9 2 20 14 20 2 20 9 2 4", "output": "152\n1 \n3 5 8 10 13 15 17 " } ]
0
0
-1
43,510
486
Palindrome Transformation
[ "brute force", "greedy", "implementation" ]
null
null
Nam is playing with a string on his computer. The string consists of *n* lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down. There is a cursor pointing at some symbol of the string. Suppose that cursor is at position *i* (1<=≤<=*i*<=≤<=*n*, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position *i*<=-<=1 if *i*<=&gt;<=1 or to the end of the string (i. e. position *n*) otherwise. The same holds when he presses the right arrow key (if *i*<==<=*n*, the cursor appears at the beginning of the string). When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key. Initially, the text cursor is at position *p*. Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
The first line contains two space-separated integers *n* (1<=≤<=*n*<=≤<=105) and *p* (1<=≤<=*p*<=≤<=*n*), the length of Nam's string and the initial position of the text cursor. The next line contains *n* lowercase characters of Nam's string.
Print the minimum number of presses needed to change string into a palindrome.
[ "8 3\naeabcaez\n" ]
[ "6\n" ]
A string is a palindrome if it reads the same forward or reversed. In the sample test, initial Nam's string is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7cd1b7b2498296406fff738215b48ddd9a12898d.png" style="max-width: 100.0%;max-height: 100.0%;"/> (cursor position is shown bold). In optimal solution, Nam may do 6 following steps: The result, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f998673d1ad2128f8e2de7ff01895c8ef9ee34ff.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is now a palindrome.
[ { "input": "8 3\naeabcaez", "output": "6" }, { "input": "8 3\nabcddcbb", "output": "3" }, { "input": "4 4\nrkoa", "output": "14" }, { "input": "39 30\nyehuqwaffoiyxhkmdipxroolhahbhzprioobxfy", "output": "138" }, { "input": "40 23\nvwjzsgpdsopsrpsyccavfkyyahdgkmdxrquhcplw", "output": "169" }, { "input": "10 5\nabcdeedcba", "output": "0" }, { "input": "5 5\npjfjb", "output": "12" }, { "input": "57 9\nibkypcbtpdlhhpmghwrmuwaqoqxxexxqoqawumrwhgmphhldixezvfpqh", "output": "55" }, { "input": "10 6\nabcdefdcba", "output": "1" }, { "input": "167 152\nvqgjxbuxevpqbpnuyxktgpwdgyebnmrxbnitphshuloyykpgxakxadtguqskmhejndzptproeabnlvfwdyjiydfrjkxpvpbzwutsdpfawwcqqqirxwlkrectlnpdeccaoqetcaqcvyjtfoekyupvbsoiyldggycphddecbf", "output": "666" }, { "input": "93 61\nuecrsqsoylbotwcujcsbjohlyjlpjsjsnvttpytrvztqtkpsdcrvsossimwmglumwzpouhaiqvowthzsyonxjjearhniq", "output": "367" }, { "input": "63 4\nwzxjoumbtneztzheqznngprtcqjvawcycwavjqctrpgnnzqehztzentbmuojxzw", "output": "0" }, { "input": "85 19\nblkimwzicvbdkwfodvigvmnujnotwuobkjvugbtaseebxvdiorffqnhllwtwdnfodkuvdofwkdbvcizwmiklb", "output": "187" }, { "input": "198 3\ntuxqalctjyegbvouezfiqoeoazizhmjhpcmvyvjkyrgxkeupwcmvzcosdrrfgtdmxwfttxjxsbaspjwftgpnvsfyfqsrmyjmypdwonbzwsftepwtjlgbilhcsqyfzfzrfvrvfqiwoemthwvqptqnflqqspvqrnmvucnspexpijnivqpavqxjyucufcullevaedlvut", "output": "692" }, { "input": "46 29\nxxzkzsxlyhotmfjpptrilatgtqpyshraiycmyzzlrcllvu", "output": "168" }, { "input": "1 1\na", "output": "0" }, { "input": "2 2\nat", "output": "7" }, { "input": "10 4\nabcddddcef", "output": "11" }, { "input": "8 8\naccedcba", "output": "5" }, { "input": "1 1\nd", "output": "0" } ]
62
132,812,800
-1
43,578
138
Literature Lesson
[ "implementation" ]
null
null
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes. Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels. Two lines rhyme if their suffixes that start from the *k*-th vowels (counting from the end) match. If a line has less than *k* vowels, then such line can't rhyme with any other line. For example, if *k*<==<=1, lines *commit* and *hermit* rhyme (the corresponding suffixes equal *it*), and if *k*<==<=2, they do not rhyme (*ommit*<=≠<=*ermit*). Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines): - Clerihew (*aabb*); - Alternating (*abab*); - Enclosed (*abba*). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by *aaaa*). If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is *aaaa*. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme. Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task.
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=2500, 1<=≤<=*k*<=≤<=5) — the number of quatrains in the poem and the vowel's number, correspondingly. Next 4*n* lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on.
Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes.
[ "1 1\nday\nmay\nsun\nfun\n", "1 1\nday\nmay\ngray\nway\n", "2 1\na\na\na\na\na\na\ne\ne\n", "2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill\n" ]
[ "aabb\n", "aaaa\n", "aabb\n", "NO\n" ]
In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".
[ { "input": "1 1\nday\nmay\nsun\nfun", "output": "aabb" }, { "input": "1 1\nday\nmay\ngray\nway", "output": "aaaa" }, { "input": "2 1\na\na\na\na\na\na\ne\ne", "output": "aabb" }, { "input": "2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill", "output": "NO" }, { "input": "2 5\na\na\na\na\na\na\ne\ne", "output": "NO" }, { "input": "1 1\nrezwbgy\nxakgmv\njogezwbgy\napezwbgy", "output": "NO" }, { "input": "2 1\nnuqfxwrb\napqfkw\nuqfxwrb\nnhcuqfxwrb\nogkznwncmt\nevf\nogkznwncmt\nogkznwncmt", "output": "NO" }, { "input": "1 1\naawjvkxx\nawjvkxx\nxawjvkxx\nawjvkxx", "output": "aaaa" }, { "input": "2 2\nrhcujgxabk\nnjgdqpurul\nueoedt\ncpcfhbyvo\nzmfwnieog\npkpylassbf\nhrfeod\ncdwuil", "output": "NO" }, { "input": "2 1\nol\nol\nol\nzol\nek\nek\nek\nqek", "output": "aaaa" }, { "input": "3 2\nexdaoao\nrdwunurp\ndunurp\ntyqzuxao\ndupocgsps\nzsiravcm\nnqiravcm\nlnupocgsps\niwashk\neepkqcykbv\nyviwashk\neepkqcykbv", "output": "NO" }, { "input": "2 1\ndaihacbnhgfts\nsqihpntjvczkw\nmihpntjvczkw\nvyacbnhgfts\ntsvovdpqajmgvcj\ncexqkwrvctomb\njxbomb\ngnpajmgvcj", "output": "abba" }, { "input": "3 2\netba\ntfecetba\nzkitbgcuuy\nuuy\nbuxeoi\nmekxoi\nblviwoehy\niwoehy\njyfpaqntiz\nqvaqntiz\nhciak\niak", "output": "aabb" }, { "input": "4 3\niixxiojrrdytjcbkvymw\nbjqixxiojrrdytjcbkvymw\nogjixxiojrrdytjcbkvymw\nevixxpfxpgicpg\njkotitixiughfhphliuurx\ngyubkqtonejprfjzvqxbdpn\ndpudxfoqnhekjytbwiuurx\noubkqtonejprfjzvqxbdpn\npgzaendrxjhsfzjmijv\npomuaendrxjhsfzjmijv\nafyuyxueaendrxjhsfzjmijv\naendrxjhsfzjmijv\nyubweicj\ntbnsuxqigmxdfnmbipubweicj\nfuftydlmoo\nmdkuftydlmoo", "output": "NO" }, { "input": "5 2\nqurcmcbxyoddgyyccsmb\nlsdzsqoa\neurcmcbxyoddgyyccsmb\noa\nutyxmdhcvaclynmstwsx\nmkyycelbmkmdrilmbvr\nutyxmdhcvaclynmstwsx\nrduyelbmkmdrilmbvr\nhmguhvqswwciowwgu\nnoe\nzmyncuwrowwgu\nqrhymghavvbmigzsjoe\nbvofhknbzusykztlxwms\nbpbfmvjaimkdeddy\neofhknbzusykztlxwms\nmhivpkxkpazimkdeddy\negvywnhmfngllaknmn\nmblkvhenlggoftwjgk\nzegvywnhmfngllaknmn\ngrdenlggoftwjgk", "output": "abab" }, { "input": "7 3\nferwljzwakxedlgwl\noerwljzwakxedlgwl\nhyqombizhuhxedprb\netptjrizhuhxedprb\nurtuckar\ndkartmwramklcmi\nrurtuckar\nnurartmwramklcmi\niraziomsv\nsaziomsv\nbprapiqpayzurgij\nusyemayzurgij\nztstmeecvmkvuu\nquexlecvmkvuu\nrlhwecvmkvuu\nzecvmkvuu\niikymgbncljtub\nqiikymgbncljtub\nbcavhexqamyszgfya\nojexqamyszgfya\nieyxqinjinjv\nxtiudieyxqinjinjv\nthtceyxqinjinjv\nmuneyxqinjinjv\nwreae\nqylcjhjzfhteae\nozcjthgyuchqo\nfcjozcjthgyuchqo", "output": "NO" }, { "input": "16 1\ni\ni\ni\ni\ni\nu\ni\ni\no\na\na\no\na\ni\na\na\ni\ni\no\no\ni\ni\ni\ni\nu\nu\nu\nu\no\ne\ne\ne\no\ni\no\ni\na\na\na\na\nu\no\no\nu\ni\no\no\ni\na\na\ne\ne\na\na\na\na\na\no\na\na\nu\na\nu\nu", "output": "NO" }, { "input": "16 1\neb\neb\nfe\nce\ner\ner\new\new\nu\ncu\nu\nu\nud\nik\nud\nik\nve\niw\niw\nne\nel\nob\nel\nob\no\neo\no\nyo\nav\nav\nei\nmi\nu\noh\noh\nzu\niw\niw\na\nma\ni\nu\nku\ngi\nac\no\no\nac\ni\ner\nai\ner\nyu\nuf\nuf\nhu\nef\nef\nef\nef\nmu\nu\nqe\nie", "output": "NO" }, { "input": "25 1\nw\ni\nv\nx\nh\ns\nz\ny\no\nn\nh\ni\nf\nf\ny\nr\nb\nu\no\np\nz\nh\nt\no\nw\nx\nh\no\nj\ny\nw\nj\ny\nh\nh\nr\ns\nb\ny\nr\nw\no\nl\nl\nh\nh\nw\nu\na\nv\no\nx\nd\nw\nc\nf\ni\ne\nj\nq\nk\na\ne\nl\nw\nm\nf\na\nc\na\nb\nf\nj\nb\nx\ni\nx\ne\nu\nh\nm\no\ni\nq\nm\nk\nn\nd\nl\np\nc\nw\nu\nz\nc\nk\ng\ny\nj\ny", "output": "NO" }, { "input": "1 1\ne\ne\ne\ne", "output": "aaaa" }, { "input": "1 1\na\ne\ne\ne", "output": "NO" }, { "input": "1 1\ne\na\ne\ne", "output": "NO" }, { "input": "1 1\na\na\ne\ne", "output": "aabb" }, { "input": "1 1\ne\ne\na\ne", "output": "NO" }, { "input": "1 1\na\ne\na\ne", "output": "abab" }, { "input": "1 1\ne\na\na\ne", "output": "abba" }, { "input": "1 1\na\na\na\ne", "output": "NO" }, { "input": "1 1\ne\ne\ne\na", "output": "NO" }, { "input": "1 1\na\ne\ne\na", "output": "abba" }, { "input": "1 1\ne\na\ne\na", "output": "abab" }, { "input": "1 1\na\na\ne\na", "output": "NO" }, { "input": "1 1\ne\ne\na\na", "output": "aabb" }, { "input": "1 1\na\ne\na\na", "output": "NO" }, { "input": "1 1\ne\na\na\na", "output": "NO" }, { "input": "1 1\na\na\na\na", "output": "aaaa" }, { "input": "1 2\neraub\nbee\naab\nttbee", "output": "NO" }, { "input": "10 1\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\ny", "output": "NO" }, { "input": "1 2\neeereaatktb\nbee\niaattb\nottbee", "output": "NO" }, { "input": "1 1\nab\nac\nad\naf", "output": "NO" }, { "input": "1 1\nar\nat\nay\naw", "output": "NO" }, { "input": "2 1\na\ne\na\ne\na\na\na\na", "output": "abab" }, { "input": "1 1\na\ne\na\ni", "output": "NO" }, { "input": "1 1\na\ne\na\ne", "output": "abab" }, { "input": "1 1\nabbbbbbbbbbbbbbbbcbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbfbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbxbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbdbbbbbbbbbbbbbbbb", "output": "NO" }, { "input": "2 1\na\ne\ne\na\na\na\na\na", "output": "abba" }, { "input": "1 1\nbug\nsuy\nluh\ngut", "output": "NO" }, { "input": "1 1\nam\nat\nan\nag", "output": "NO" }, { "input": "2 1\na\na\ne\ne\na\na\na\na", "output": "aabb" }, { "input": "1 4\naieoabcd\naeioabcd\naoeiabcd\naoieabcd", "output": "NO" }, { "input": "1 2\naec\naed\naek\naem", "output": "NO" }, { "input": "1 1\nar\nab\nak\naz", "output": "NO" }, { "input": "2 1\na\na\na\na\na\nb\nb\nb", "output": "NO" } ]
342
7,372,800
3
43,653
815
Karen and Neighborhood
[ "binary search", "constructive algorithms", "implementation" ]
null
null
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood. The neighborhood consists of *n* houses in a straight line, labelled 1 to *n* from left to right, all an equal distance apart. Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one. Note that the first person to arrive always moves into house 1. Karen is the *k*-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
The first and only line of input contains two integers, *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=1018), describing the number of houses in the neighborhood, and that Karen was the *k*-th person to move in, respectively.
Output a single integer on a line by itself, the label of the house Karen will move into.
[ "6 4\n", "39 3\n" ]
[ "2\n", "20\n" ]
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in: 1. The first person moves into house 1. 1. The second person moves into house 6. 1. The third person moves into house 3. 1. The fourth person moves into house 2. In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in: 1. The first person moves into house 1. 1. The second person moves into house 39. 1. The third person moves into house 20.
[ { "input": "6 4", "output": "2" }, { "input": "39 3", "output": "20" }, { "input": "1 1", "output": "1" }, { "input": "1000000000000000000 1", "output": "1" }, { "input": "1000000000000000000 2", "output": "1000000000000000000" }, { "input": "69 69", "output": "68" }, { "input": "101 11", "output": "19" }, { "input": "300 60", "output": "210" }, { "input": "47727 9939", "output": "12324" }, { "input": "499999 3", "output": "250000" }, { "input": "1048577 102030", "output": "583881" }, { "input": "100000000000000 3000000", "output": "52481436729430" }, { "input": "999888777666555 111222333444555", "output": "580499849936606" }, { "input": "12345678910111213 123456789", "output": "8486343040509733" }, { "input": "394041424344454647 4849505152535455", "output": "61169389486139113" }, { "input": "576460752303423488 3712934127391372", "output": "374050384308767616" }, { "input": "430997976774038405 93797162741100400", "output": "132508969799401046" }, { "input": "857888953378997500 138004454633813280", "output": "87204479913701827" }, { "input": "1000000000000000000 100000000000000000", "output": "441768246641462612" }, { "input": "7 5", "output": "3" }, { "input": "11 9", "output": "7" }, { "input": "292220203571590770 243591181971915141", "output": "196272186255771208" }, { "input": "31259010044651407 28307960314947881", "output": "26138278808772852" }, { "input": "490262402520292837 404411178150541217", "output": "344234672616248918" }, { "input": "22240820596964093 16800813768322860", "output": "13098182101177708" }, { "input": "98284504853265819 65462210269503717", "output": "46467200327564851" }, { "input": "564599300518848605 440366176097036033", "output": "321245577409155090" }, { "input": "299568084923339051 269166750771398664", "output": "240982689374573285" }, { "input": "936952405863723813 740701275622245247", "output": "617974976960735622" }, { "input": "1000000000000000000 1000000000000000000", "output": "999999999999999999" }, { "input": "2 1", "output": "1" }, { "input": "2 2", "output": "2" }, { "input": "3 1", "output": "1" }, { "input": "3 2", "output": "3" }, { "input": "3 3", "output": "2" }, { "input": "4 1", "output": "1" }, { "input": "4 2", "output": "4" }, { "input": "4 3", "output": "2" }, { "input": "4 4", "output": "3" }, { "input": "5 1", "output": "1" }, { "input": "5 2", "output": "5" }, { "input": "5 3", "output": "3" }, { "input": "5 4", "output": "2" }, { "input": "5 5", "output": "4" }, { "input": "6 1", "output": "1" }, { "input": "6 2", "output": "6" }, { "input": "6 3", "output": "3" }, { "input": "6 5", "output": "4" }, { "input": "6 6", "output": "5" }, { "input": "576460752303423489 1", "output": "1" }, { "input": "576460752303423489 2", "output": "576460752303423489" }, { "input": "576460752303423489 3", "output": "288230376151711745" }, { "input": "576460752303423489 4", "output": "144115188075855873" }, { "input": "576460752303423489 5", "output": "432345564227567617" }, { "input": "576460752303423489 6", "output": "72057594037927937" }, { "input": "576460752303423489 7", "output": "216172782113783809" }, { "input": "576460752303423489 8", "output": "360287970189639681" }, { "input": "576460752303423489 576460752303423487", "output": "576460752303423484" }, { "input": "576460752303423489 576460752303423488", "output": "576460752303423486" }, { "input": "576460752303423489 576460752303423489", "output": "576460752303423488" }, { "input": "576460752303423489 288230376151711744", "output": "576460752303423483" }, { "input": "576460752303423489 288230376151711745", "output": "576460752303423487" }, { "input": "576460752303423489 288230376151711746", "output": "2" }, { "input": "576460752303423489 432345564227567615", "output": "288230376151711740" }, { "input": "576460752303423489 432345564227567616", "output": "288230376151711742" }, { "input": "576460752303423489 432345564227567617", "output": "288230376151711744" }, { "input": "7 1", "output": "1" }, { "input": "7 2", "output": "7" }, { "input": "7 3", "output": "4" }, { "input": "7 4", "output": "2" }, { "input": "7 6", "output": "5" }, { "input": "7 7", "output": "6" }, { "input": "8 1", "output": "1" }, { "input": "8 2", "output": "8" }, { "input": "8 3", "output": "4" }, { "input": "8 4", "output": "6" }, { "input": "8 5", "output": "2" }, { "input": "8 6", "output": "3" }, { "input": "8 7", "output": "5" }, { "input": "8 8", "output": "7" }, { "input": "1999 1017", "output": "81" }, { "input": "2000 1117", "output": "274" }, { "input": "1000000000000000000 3", "output": "500000000000000000" }, { "input": "1000000000000000000 4", "output": "750000000000000000" }, { "input": "1000000000000000000 5", "output": "250000000000000000" }, { "input": "1000000000000000000 6", "output": "375000000000000000" }, { "input": "1000000000000000000 7", "output": "625000000000000000" }, { "input": "1000000000000000000 8", "output": "875000000000000000" }, { "input": "1000000000000000000 9", "output": "125000000000000000" }, { "input": "1000000000000000000 10", "output": "187500000000000000" }, { "input": "1000000000000000000 1023", "output": "997070312500000000" }, { "input": "1000000000000000000 1024", "output": "999023437500000000" }, { "input": "1000000000000000000 1025", "output": "976562500000000" }, { "input": "1000000000000000000 2016", "output": "968261718750000000" }, { "input": "1000000000000000000 2017", "output": "969238281250000000" }, { "input": "1000000000000000000 2020", "output": "972167968750000000" }, { "input": "1000000000000000000 2047", "output": "998535156250000000" }, { "input": "1000000000000000000 2048", "output": "999511718750000000" }, { "input": "1000000000000000000 2049", "output": "488281250000000" }, { "input": "1000000000000000000 4095", "output": "999267578125000000" }, { "input": "1000000000000000000 4096", "output": "999755859375000000" }, { "input": "1000000000000000000 4097", "output": "244140625000000" }, { "input": "1000000000000000000 8191", "output": "999633789062500000" }, { "input": "1000000000000000000 8192", "output": "999877929687500000" }, { "input": "1000000000000000000 8193", "output": "122070312500000" }, { "input": "1000000000000000000 1048575", "output": "999995231628417968" }, { "input": "1000000000000000000 1048576", "output": "999997138977050781" }, { "input": "1000000000000000000 1048577", "output": "999999046325683593" }, { "input": "1000000000000000000 1048578", "output": "476837158204" }, { "input": "1000000000000000000 1048579", "output": "1430511474610" }, { "input": "1000000000000000000 10000000", "output": "192092716693878173" }, { "input": "1000000000000000000 100000000", "output": "578201510012149810" }, { "input": "1000000000000000000 1000000000", "output": "821395079605281350" }, { "input": "1000000000000000000 10000000000", "output": "198508051398675885" }, { "input": "1000000000000000000 135308871544864767", "output": "999999999999999979" }, { "input": "1000000000000000000 135308871544864768", "output": "999999999999999993" }, { "input": "1000000000000000000 135308871544864769", "output": "7" }, { "input": "1000000000000000000 423539247696576509", "output": "999999999999999977" }, { "input": "1000000000000000000 423539247696576510", "output": "999999999999999984" }, { "input": "1000000000000000000 423539247696576511", "output": "999999999999999991" }, { "input": "1000000000000000000 423539247696576512", "output": "999999999999999998" }, { "input": "1000000000000000000 423539247696576513", "output": "2" }, { "input": "1000000000000000000 423539247696576514", "output": "3" }, { "input": "1000000000000000000 423539247696576515", "output": "5" }, { "input": "1000000000000000000 423539247696576516", "output": "6" }, { "input": "1000000000000000000 423539247696576517", "output": "8" }, { "input": "1000000000000000000 423539247696576518", "output": "9" }, { "input": "1000000000000000000 423539247696576519", "output": "11" }, { "input": "1000000000000000000 423539247696576520", "output": "13" }, { "input": "1000000000000000000 423539247696576521", "output": "15" }, { "input": "1000000000000000000 711769623848288255", "output": "499999999999999997" }, { "input": "1000000000000000000 711769623848288256", "output": "499999999999999999" }, { "input": "1000000000000000000 711769623848288257", "output": "500000000000000001" }, { "input": "1000000000000000000 711769623848288258", "output": "500000000000000002" }, { "input": "1000000000000000000 711769623848288259", "output": "500000000000000004" }, { "input": "1000000000000000000 711769623848288260", "output": "500000000000000005" }, { "input": "1000000000000000000 711769623848288261", "output": "500000000000000007" }, { "input": "1000000000000000000 711769623848288262", "output": "500000000000000008" }, { "input": "1000000000000000000 711769623848288263", "output": "500000000000000010" }, { "input": "1000000000000000000 711769623848288264", "output": "500000000000000012" }, { "input": "1000000000000000000 711769623848288265", "output": "500000000000000014" }, { "input": "1000000000000000000 711769623848288266", "output": "500000000000000015" }, { "input": "1000000000000000000 567654435772432383", "output": "249999999999999997" }, { "input": "1000000000000000000 567654435772432384", "output": "249999999999999999" }, { "input": "1000000000000000000 567654435772432385", "output": "250000000000000001" }, { "input": "1000000000000000000 696969696969696969", "output": "474326219400967539" }, { "input": "1000000000000000000 855884811924144127", "output": "749999999999999997" }, { "input": "1000000000000000000 855884811924144128", "output": "749999999999999999" }, { "input": "1000000000000000000 855884811924144129", "output": "750000000000000001" }, { "input": "1000000000000000000 925717238248123219", "output": "871139949328627287" }, { "input": "1000000000000000000 969696969696969696", "output": "947432621940096746" }, { "input": "1000000000000000000 994128342112375231", "output": "989814297219432918" }, { "input": "1000000000000000000 999799979997999797", "output": "999653020606865314" }, { "input": "1000000000000000000 999999999999999998", "output": "999999999999999995" }, { "input": "1000000000000000000 999999999999999999", "output": "999999999999999997" }, { "input": "9 1", "output": "1" }, { "input": "9 2", "output": "9" }, { "input": "9 3", "output": "5" }, { "input": "9 4", "output": "3" }, { "input": "9 5", "output": "7" }, { "input": "9 6", "output": "2" }, { "input": "9 7", "output": "4" }, { "input": "9 8", "output": "6" }, { "input": "9 9", "output": "8" }, { "input": "10 1", "output": "1" }, { "input": "10 2", "output": "10" }, { "input": "10 3", "output": "5" }, { "input": "10 4", "output": "3" }, { "input": "10 5", "output": "7" }, { "input": "10 6", "output": "2" }, { "input": "10 7", "output": "4" }, { "input": "10 8", "output": "6" }, { "input": "10 9", "output": "8" }, { "input": "10 10", "output": "9" }, { "input": "11 1", "output": "1" }, { "input": "11 2", "output": "11" }, { "input": "11 3", "output": "6" }, { "input": "11 4", "output": "3" }, { "input": "11 5", "output": "8" }, { "input": "11 6", "output": "2" }, { "input": "11 7", "output": "4" }, { "input": "11 8", "output": "5" }, { "input": "11 10", "output": "9" }, { "input": "11 11", "output": "10" }, { "input": "576460752303423489 399581283714723211", "output": "222701815126022932" }, { "input": "581935821395832991 491999931992912314", "output": "403740569928322107" }, { "input": "649986197538098731 549986197538098731", "output": "470310803498245431" }, { "input": "999999999999999999 100010010001001000", "output": "441926504330406603" }, { "input": "999999999999999999 9000909000909000", "output": "998603283072996834" }, { "input": "999999999999999999 423539247696576510", "output": "999999999999999990" }, { "input": "999999999999999999 423539247696576511", "output": "999999999999999997" }, { "input": "999999999999999999 423539247696576512", "output": "2" }, { "input": "999999999999999999 423539247696576513", "output": "3" }, { "input": "999999999999999999 711771585869551553", "output": "500003403564345802" }, { "input": "999999999999999999 711771585869551554", "output": "500003403564345804" }, { "input": "999999999999999999 711771585869551555", "output": "500003403564345805" }, { "input": "999999999999999999 711771585869551556", "output": "500003403564345807" }, { "input": "1000000000000000000 711771585869551553", "output": "500003403564345801" }, { "input": "1000000000000000000 711771585869551554", "output": "500003403564345802" }, { "input": "1000000000000000000 711771585869551555", "output": "500003403564345804" }, { "input": "1000000000000000000 711771585869551556", "output": "500003403564345806" }, { "input": "576460752303423487 288230376151711742", "output": "576460752303423481" }, { "input": "576460752303423487 288230376151711743", "output": "576460752303423485" }, { "input": "576460752303423487 288230376151711744", "output": "2" }, { "input": "576460752303423487 288230376151711745", "output": "3" }, { "input": "576460752303423487 288230376151711746", "output": "5" }, { "input": "576460752303423487 288230376151711747", "output": "7" }, { "input": "800000000000000000 223539247696576509", "output": "799999999999999974" }, { "input": "800000000000000000 223539247696576510", "output": "799999999999999985" }, { "input": "800000000000000000 223539247696576511", "output": "799999999999999991" }, { "input": "800000000000000000 223539247696576512", "output": "799999999999999997" }, { "input": "800000000000000000 223539247696576513", "output": "3" }, { "input": "800000000000000000 223539247696576514", "output": "14" }, { "input": "800000000000000000 223539247696576515", "output": "25" }, { "input": "800000000000000000 288230376151711742", "output": "799999999999999946" }, { "input": "800000000000000000 288230376151711743", "output": "799999999999999957" }, { "input": "800000000000000000 288230376151711744", "output": "799999999999999968" }, { "input": "800000000000000000 288230376151711745", "output": "799999999999999979" }, { "input": "800000000000000000 288230376151711746", "output": "2" }, { "input": "800000000000000000 288230376151711747", "output": "4" }, { "input": "800000000000000000 288230376151711748", "output": "5" }, { "input": "800000000000000000 500000000000000000", "output": "331038989388813566" }, { "input": "800000000000000000 550000000000000000", "output": "409199157824011306" }, { "input": "800000000000000000 555000000000000000", "output": "417015174667531079" }, { "input": "800000000000000000 638192934071238281", "output": "547062649460056776" }, { "input": "800000000000000000 681882351832184278", "output": "615358094480681952" }, { "input": "800000000000000000 781882351832184278", "output": "771678431351077431" }, { "input": "800000000000000000 799999999999999999", "output": "799999999999999998" }, { "input": "800000000000000000 800000000000000000", "output": "799999999999999999" }, { "input": "864691128455135232 288230376151711743", "output": "864691128455135223" }, { "input": "864691128455135232 288230376151711744", "output": "864691128455135229" }, { "input": "864691128455135232 288230376151711745", "output": "3" }, { "input": "864691128455135232 288230376151711746", "output": "2" }, { "input": "864691128455135232 288230376151711747", "output": "4" }, { "input": "864691128455135232 288230376151711748", "output": "5" }, { "input": "864691128455135232 288230376151711749", "output": "7" }, { "input": "864691128455135232 288230376151711750", "output": "8" }, { "input": "864691128455135232 288230376151711751", "output": "10" }, { "input": "864691128455135233 288230376151711743", "output": "864691128455135218" }, { "input": "864691128455135233 288230376151711744", "output": "864691128455135224" }, { "input": "864691128455135233 288230376151711745", "output": "864691128455135230" }, { "input": "864691128455135233 288230376151711746", "output": "2" }, { "input": "864691128455135233 288230376151711747", "output": "3" }, { "input": "864691128455135233 288230376151711748", "output": "5" }, { "input": "864691128455135233 288230376151711749", "output": "6" }, { "input": "864691128455135233 288230376151711750", "output": "8" }, { "input": "864691128455135233 288230376151711751", "output": "9" }, { "input": "864691128455135234 288230376151711743", "output": "864691128455135218" }, { "input": "864691128455135234 288230376151711744", "output": "864691128455135224" }, { "input": "864691128455135234 288230376151711745", "output": "864691128455135230" }, { "input": "864691128455135234 288230376151711746", "output": "864691128455135232" }, { "input": "864691128455135234 288230376151711747", "output": "2" }, { "input": "864691128455135234 288230376151711748", "output": "3" }, { "input": "864691128455135234 288230376151711749", "output": "5" }, { "input": "864691128455135234 288230376151711750", "output": "6" }, { "input": "864691128455135234 288230376151711751", "output": "8" }, { "input": "947531086593 730978659", "output": "342583512902" }, { "input": "147602303331 576571497", "output": "10914909778" }, { "input": "99945033767 2029340787", "output": "87978052670" }, { "input": "9322741888 3453520026", "output": "5669817341" }, { "input": "8317778264483 32491321346", "output": "7413168436512" }, { "input": "8921813537474400 2745173396145970", "output": "2031830901722361" }, { "input": "120130776788356 60065388394178", "output": "17589487325107" }, { "input": "196052871115155 117805115610038", "output": "73995316999954" }, { "input": "55748453191272 34273488567352", "output": "21722094639288" }, { "input": "207631236879517504 36017067697", "output": "10015016252452152" }, { "input": "12736820619632244 2209412020", "output": "367299907991950" }, { "input": "10042739937148808 2394375643", "output": "5571570940569837" }, { "input": "10005672465226802 5957118160", "output": "2701574557902033" }, { "input": "756557929523292800 450435057491", "output": "676795373598503282" }, { "input": "350983625387002432 898517505940", "output": "96681137085208223" }, { "input": "893495239547490944 2287347813242", "output": "35887556727999472" }, { "input": "10009512060680172 56503908814", "output": "5862134490698753" }, { "input": "264908601332820864 4042169048928", "output": "222037294486340774" }, { "input": "121023095108282688 1846665879948", "output": "95207014911958021" }, { "input": "395915528694432960 60341478939902", "output": "283082762830506640" }, { "input": "42108284137792424 6417967404023", "output": "19339359572503200" }, { "input": "998093375643672704 415655752480447", "output": "506173261223031948" }, { "input": "10422063738348552 4340717925176", "output": "9964746178055511" }, { "input": "171228068751990976 83597250702791", "output": "108304172249651330" }, { "input": "406321261919893120 313459025589117", "output": "85011803166018375" }, { "input": "732181334090648576 564954733094636", "output": "2607447157336436" }, { "input": "406454593965502848 650067323415438", "output": "62899472738394651" }, { "input": "354969325322315904 567950920515706", "output": "3153370747553918" }, { "input": "504188717956509312 1967565728610768", "output": "465581154253158528" }, { "input": "768406680474489088 3001588595603473", "output": "93010133267596145" }, { "input": "53117043105703384 653748222839426", "output": "8567254622358339" }, { "input": "328408380190503808 4054424446796343", "output": "312003155640460242" }, { "input": "733722887703270656 11419811481762968", "output": "427607859344774791" }, { "input": "664396727800363008 13490288889347472", "output": "433484369227909381" }, { "input": "81865715143853792 1670728880486812", "output": "39615256460544038" }, { "input": "499095378264387648 13768148365914142", "output": "99816617432770063" }, { "input": "647354401216802304 17982066700466730", "output": "629372334516331643" }, { "input": "196063539413843232 7194992272067642", "output": "26452970383581653" }, { "input": "251653449599656864 9966473251471560", "output": "28537962013795610" }, { "input": "65330213409341952 2613208536373678", "output": "9794325057729016" }, { "input": "205849763444641440 12667677750439474", "output": "83656263298174005" }, { "input": "233461284714024736 14591330294626546", "output": "157424302937962666" }, { "input": "10510729869975688 1136295121078453", "output": "289333696506268" }, { "input": "268798573452166816 29866508161351868", "output": "176849100187519525" }, { "input": "980299045535975808 118824126731633440", "output": "110598003276677332" }, { "input": "684178606868566272 94369463016353968", "output": "428076049041859423" }, { "input": "392296110585284224 56042301512183456", "output": "217915129604204619" }, { "input": "68022271568979712 10883563451036754", "output": "25671302894604935" }, { "input": "403155937375649536 67192656229274920", "output": "335963281146374459" }, { "input": "204114473698081120 38878947371063072", "output": "24269715511763076" }, { "input": "94632947562272896 18926589512454580", "output": "18926589512454713" }, { "input": "593367764922194688 139615944687575216", "output": "556318151640077041" }, { "input": "10582224961916522 2645556240479131", "output": "1850439396614890" }, { "input": "428007475795535168 131694607937087744", "output": "376923717244924605" }, { "input": "10026172142272542 3342057380757514", "output": "4854388023503805" }, { "input": "225217150653026848 77775031920896640", "output": "142372025581529046" }, { "input": "120142123259447360 44505493521742616", "output": "84475028057929720" }, { "input": "22080186679511192 8345526122266806", "output": "18836144692062755" }, { "input": "347152220683835904 139639070140376016", "output": "325587605571559486" }, { "input": "131719758930114320 53774366403531336", "output": "98904317571311960" }, { "input": "239724741881342400 106544329725041056", "output": "18189174713950143" }, { "input": "782068803355263232 349751801476856832", "output": "97428601814566103" }, { "input": "26576419467069112 13288209733534556", "output": "6475752984614668" }, { "input": "10001431202199728 5046001426404996", "output": "986715253670069" }, { "input": "37006976173804192 19720472337309308", "output": "3324279337594061" }, { "input": "318736144596654336 175604017628635680", "output": "57476653028863703" }, { "input": "18229794001307704 10324303654994536", "output": "2603447571387269" }, { "input": "14830228640293486 8497000026502883", "output": "4402655571525565" }, { "input": "745562143588377088 430450504298344000", "output": "231853440220728132" }, { "input": "294608108925127296 177025422449405536", "output": "64425767393969267" }, { "input": "95448208348349872 58186319487812936", "output": "35592675419926572" }, { "input": "711989842033568384 437722919903880064", "output": "251173557597831035" }, { "input": "44603620038240376 28481169899594936", "output": "17558088099976182" }, { "input": "302321262077905536 193166085671707840", "output": "93732995782702686" }, { "input": "17743809448317026 11866000541911046", "output": "6164769365020067" }, { "input": "89204601336551008 60226808765458176", "output": "40593161094307714" }, { "input": "528348167641595392 367110586192868096", "output": "232787436497291822" }, { "input": "766465379659015680 541972867501611648", "output": "406672103760291203" }, { "input": "685275972013532160 520697138055463744", "output": "401223153926817135" }, { "input": "66240432751417232 55701342445521504", "output": "46863879537571564" }, { "input": "987654321123456789 276", "output": "954861111242404512" }, { "input": "987654321123456789 277", "output": "985725308777512537" }, { "input": "987654321123456789 278", "output": "1929012345944252" }, { "input": "987654321123456789 279", "output": "5787037037832755" }, { "input": "987654321123456789 393", "output": "480324074140118633" }, { "input": "987654321123456789 394", "output": "484182098832007136" }, { "input": "987654321123456789 395", "output": "488040123523895639" }, { "input": "987654321123456789 396", "output": "495756172907672646" }, { "input": "987654321123456789 397", "output": "499614197599561149" }, { "input": "987654321123456789 214600813", "output": "292167034454252418" }, { "input": "987654321123456789 268435456", "output": "987654295368363363" }, { "input": "987654321123456789 268435457", "output": "987654310085559606" }, { "input": "987654321123456789 268435458", "output": "1839649531" }, { "input": "987654321123456789 268435459", "output": "5518948591" }, { "input": "987654321123456789 275372170", "output": "25522239785701395" }, { "input": "987654321123456789 301989888", "output": "123456784621483507" }, { "input": "987654321123456789 301989889", "output": "123456788300782568" }, { "input": "987654321123456789 301989890", "output": "123456791980081629" }, { "input": "987654321123456789 301989891", "output": "123456795659380689" }, { "input": "987654321123456789 8833676341", "output": "43291821334438811" }, { "input": "987654321123456789 14150629140", "output": "987654320950989645" }, { "input": "987654321123456789 14150629141", "output": "987654321065967741" }, { "input": "987654321123456789 14150629142", "output": "57489048" }, { "input": "987654321123456789 14150629143", "output": "287445239" }, { "input": "987654321123456789 14590465209", "output": "143404280223170165" }, { "input": "987654321123456789 15665249162", "output": "493827159699392676" }, { "input": "987654321123456789 15665249163", "output": "493827160159305059" }, { "input": "987654321123456789 15665249164", "output": "493827160619217442" }, { "input": "987654321123456789 15665249165", "output": "493827160849173633" }, { "input": "987654321123456789 423179255158", "output": "97674304572857513" }, { "input": "987654321123456789 1099511627776", "output": "987654321120761989" }, { "input": "987654321123456789 1099511627777", "output": "987654321122558522" }, { "input": "987654321123456789 1099511627778", "output": "449134" }, { "input": "987654321123456789 1099511627779", "output": "1347400" }, { "input": "987654321123456789 1544622191104", "output": "399827850967188873" }, { "input": "987654321123456789 1924145348608", "output": "740740740841245192" }, { "input": "987654321123456789 1924145348609", "output": "740740740842143458" }, { "input": "987654321123456789 1924145348610", "output": "740740740843041725" }, { "input": "987654321123456789 1924145348611", "output": "740740740843939991" }, { "input": "987654321123456789 50111190192102960", "output": "934914428012836598" }, { "input": "987654321123456789 288230376151711744", "output": "987654321123456778" }, { "input": "987654321123456789 288230376151711745", "output": "987654321123456785" }, { "input": "987654321123456789 288230376151711746", "output": "12" }, { "input": "987654321123456789 288230376151711747", "output": "19" }, { "input": "987654321123456789 301824571515240242", "output": "109190120243554490" }, { "input": "987654321123456789 349711972485872522", "output": "493827160561728386" }, { "input": "987654321123456789 349711972485872523", "output": "493827160561728393" }, { "input": "987654321123456789 349711972485872524", "output": "493827160561728406" }, { "input": "987654321123456789 349711972485872525", "output": "493827160561728413" }, { "input": "987654321123456789 376845244553514619", "output": "711764681719871143" }, { "input": "987654321123456789 411193568820033300", "output": "987654321123456780" }, { "input": "987654321123456789 411193568820033301", "output": "987654321123456787" }, { "input": "987654321123456789 411193568820033302", "output": "2" }, { "input": "987654321123456789 411193568820033303", "output": "3" }, { "input": "987654321123456789 474364393332759536", "output": "108231024488005064" }, { "input": "987654321123456789 699423944971745044", "output": "493827160561728392" }, { "input": "987654321123456789 699423944971745045", "output": "493827160561728394" }, { "input": "987654321123456789 699423944971745046", "output": "493827160561728396" }, { "input": "987654321123456789 699423944971745047", "output": "493827160561728397" }, { "input": "987654321123456789 877962318087251168", "output": "799718229449393254" }, { "input": "987654321123456789 987654321123456788", "output": "987654321123456786" }, { "input": "987654321123456789 987654321123456789", "output": "987654321123456788" } ]
61
7,168,000
-1
43,668
102
Clothes
[ "brute force" ]
A. Clothes
2
256
A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap. Overall the shop sells *n* clothing items, and exactly *m* pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.
The first input file line contains integers *n* and *m* — the total number of clothing items in the shop and the total number of matching pairs of clothing items (). Next line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=106) — the prices of the clothing items in rubles. Next *m* lines each contain a pair of space-separated integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*,<=*u**i*<=≠<=*v**i*). Each such pair of numbers means that the *u**i*-th and the *v**i*-th clothing items match each other. It is guaranteed that in each pair *u**i* and *v**i* are distinct and all the unordered pairs (*u**i*,<=*v**i*) are different.
Print the only number — the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print "-1" (without the quotes).
[ "3 3\n1 2 3\n1 2\n2 3\n3 1\n", "3 2\n2 3 4\n2 3\n2 1\n", "4 4\n1 1 1 1\n1 2\n2 3\n3 4\n4 1\n" ]
[ "6\n", "-1\n", "-1\n" ]
In the first test there only are three pieces of clothing and they all match each other. Thus, there is only one way — to buy the 3 pieces of clothing; in this case he spends 6 roubles. The second test only has three pieces of clothing as well, yet Gerald can't buy them because the first piece of clothing does not match the third one. Thus, there are no three matching pieces of clothing. The answer is -1. In the third example there are 4 pieces of clothing, but Gerald can't buy any 3 of them simultaneously. The answer is -1.
[ { "input": "3 3\n1 2 3\n1 2\n2 3\n3 1", "output": "6" }, { "input": "3 2\n2 3 4\n2 3\n2 1", "output": "-1" }, { "input": "4 4\n1 1 1 1\n1 2\n2 3\n3 4\n4 1", "output": "-1" }, { "input": "4 3\n10 10 5 1\n2 1\n3 1\n3 4", "output": "-1" }, { "input": "4 0\n9 8 2 10", "output": "-1" }, { "input": "4 3\n5 5 9 6\n3 2\n1 4\n3 4", "output": "-1" }, { "input": "4 3\n5 1 10 1\n2 1\n3 2\n1 4", "output": "-1" }, { "input": "4 3\n1 2 8 6\n1 3\n1 4\n3 4", "output": "15" }, { "input": "4 4\n9 3 3 1\n1 2\n3 1\n3 2\n4 3", "output": "15" }, { "input": "4 3\n6 8 10 1\n2 3\n1 4\n3 4", "output": "-1" }, { "input": "4 5\n4 10 3 9\n1 2\n3 1\n3 2\n2 4\n4 3", "output": "17" }, { "input": "4 2\n2 9 8 4\n1 3\n4 2", "output": "-1" }, { "input": "4 3\n5 3 4 4\n2 1\n4 1\n3 4", "output": "-1" }, { "input": "6 6\n39 15 73 82 37 40\n2 1\n5 1\n1 6\n2 6\n6 3\n4 6", "output": "94" }, { "input": "6 7\n85 2 34 6 83 61\n1 2\n2 3\n4 2\n4 3\n1 5\n4 5\n6 3", "output": "42" }, { "input": "6 8\n64 44 5 31 14 16\n1 2\n1 3\n1 4\n2 5\n3 5\n6 1\n6 3\n6 4", "output": "85" }, { "input": "6 8\n36 19 99 8 52 77\n2 1\n3 1\n4 2\n4 3\n1 5\n5 4\n1 6\n6 2", "output": "132" }, { "input": "6 5\n59 69 52 38 93 53\n4 2\n1 5\n6 1\n4 6\n5 6", "output": "205" }, { "input": "6 11\n95 81 74 94 60 69\n3 2\n1 4\n4 2\n3 4\n1 5\n5 2\n5 3\n1 6\n2 6\n3 6\n4 6", "output": "215" }, { "input": "6 8\n69 36 41 23 91 35\n1 2\n3 1\n3 2\n1 4\n3 4\n3 5\n5 4\n4 6", "output": "133" }, { "input": "6 6\n33 76 98 59 4 69\n1 2\n3 2\n5 1\n2 5\n4 5\n6 5", "output": "113" }, { "input": "6 6\n92 56 15 83 30 28\n3 1\n4 1\n2 5\n5 4\n2 6\n6 3", "output": "-1" }, { "input": "6 10\n17 5 55 24 55 74\n1 3\n2 3\n3 4\n5 1\n5 2\n5 3\n4 5\n6 2\n6 3\n6 5", "output": "115" }, { "input": "3 3\n1000000 1000000 1000000\n2 1\n1 3\n3 2", "output": "3000000" }, { "input": "3 0\n1 1 1", "output": "-1" }, { "input": "3 3\n100000 100000 100001\n1 2\n2 3\n3 1", "output": "300001" }, { "input": "3 3\n1 1 999999\n1 2\n2 3\n3 1", "output": "1000001" }, { "input": "3 3\n999999 1 1\n1 2\n2 3\n3 1", "output": "1000001" }, { "input": "3 3\n1000000 1000000 1000000\n1 2\n2 3\n1 3", "output": "3000000" } ]
154
6,656,000
-1
43,705
711
Directed Roads
[ "combinatorics", "dfs and similar", "graphs", "math" ]
null
null
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of *n* towns numbered from 1 to *n*. There are *n* directed roads in the Udayland. *i*-th of them goes from town *i* to some other town *a**i* (*a**i*<=≠<=*i*). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town *A* to town *B* before the flip, it will go from town *B* to town *A* after. ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns *A*1,<=*A*2,<=...,<=*A**k* (*k*<=&gt;<=1) such that for every 1<=≤<=*i*<=&lt;<=*k* there is a road from town *A**i* to town *A**i*<=+<=1 and another road from town *A**k* to town *A*1. In other words, the roads are confusing if some of them form a directed cycle of some towns. Now ZS the Coder wonders how many sets of roads (there are 2*n* variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing. Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
The first line of the input contains single integer *n* (2<=≤<=*n*<=≤<=2·105) — the number of towns in Udayland. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*,<=*a**i*<=≠<=*i*), *a**i* denotes a road going from town *i* to town *a**i*.
Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109<=+<=7.
[ "3\n2 3 1\n", "4\n2 1 1 1\n", "5\n2 4 2 5 3\n" ]
[ "6\n", "8\n", "28\n" ]
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/> initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip. The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing. <img class="tex-graphics" src="https://espresso.codeforces.com/2e275877797bea4817665ce9cfb5274b837194bc.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "3\n2 3 1", "output": "6" }, { "input": "4\n2 1 1 1", "output": "8" }, { "input": "5\n2 4 2 5 3", "output": "28" }, { "input": "4\n2 1 4 3", "output": "4" }, { "input": "7\n2 3 4 1 6 5 4", "output": "56" }, { "input": "20\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1", "output": "1048574" }, { "input": "2\n2 1", "output": "2" }, { "input": "84\n2 50 67 79 71 45 43 40 57 20 25 8 60 47 52 10 37 23 1 28 22 26 3 42 11 63 61 68 49 32 55 18 5 24 31 70 66 27 38 41 54 12 65 51 15 34 30 35 77 74 21 62 33 16 81 14 19 48 80 73 69 78 39 6 76 46 75 72 84 29 58 59 13 17 82 9 83 4 36 56 53 7 64 44", "output": "428380105" } ]
61
2,867,200
-1
43,745
802
Heidi and Library (easy)
[ "greedy" ]
null
null
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are *n* different books, numbered 1 through *n*. We will look at the library's operation during *n* consecutive days. Heidi knows in advance that on the *i*-th day (1<=≤<=*i*<=≤<=*n*) precisely one person will come to the library, request to borrow the book *a**i*, read it in a few hours, and return the book later on the same day. Heidi desperately wants to please all her guests, so she will make sure to always have the book *a**i* available in the library on the *i*-th day. During the night before the *i*-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books. There is a problem, though. The capacity of the library is *k* – this means that at any time, there can be at most *k* books at the library. If buying a new book would cause Heidi to have more than *k* books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again. You are given *k* and the sequence of requests for books *a*1,<=*a*2,<=...,<=*a**n*. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
The first line of input will contain two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=80). The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*) – the sequence of book requests.
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
[ "4 80\n1 2 2 1\n", "4 1\n1 2 2 1\n", "4 2\n1 2 3 1\n" ]
[ "2\n", "3\n", "3\n" ]
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day. In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day. In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
[ { "input": "4 80\n1 2 2 1", "output": "2" }, { "input": "4 1\n1 2 2 1", "output": "3" }, { "input": "4 2\n1 2 3 1", "output": "3" }, { "input": "11 1\n1 2 3 5 1 10 10 1 1 3 5", "output": "9" }, { "input": "5 2\n1 2 3 1 2", "output": "4" }, { "input": "4 2\n1 2 3 2", "output": "3" }, { "input": "1 1\n1", "output": "1" }, { "input": "80 4\n9 9 2 6 3 10 2 5 4 9 6 7 5 5 3 8 5 3 2 10 7 8 5 3 4 9 4 3 9 5 2 10 8 4 7 3 8 3 5 2 3 7 8 4 2 4 4 7 2 2 5 7 5 8 10 10 5 1 1 3 5 2 10 8 7 9 7 4 8 3 2 8 7 9 10 9 7 1 5 5", "output": "34" }, { "input": "80 4\n10 19 20 18 16 7 13 18 15 5 7 13 16 8 14 8 3 15 19 19 7 13 17 9 18 16 4 14 10 18 1 3 5 3 20 18 9 4 17 19 13 20 16 12 15 5 5 18 17 16 4 5 20 10 18 4 7 19 10 15 8 15 17 3 10 16 19 2 6 6 3 12 10 7 15 3 17 15 6 8", "output": "49" }, { "input": "80 4\n28 34 9 3 29 12 19 17 22 10 21 2 26 18 14 7 7 10 37 39 10 1 9 37 33 4 25 21 23 2 4 2 35 1 11 19 33 31 18 10 23 1 26 20 17 31 18 27 31 22 33 7 2 5 30 24 18 32 1 14 2 33 7 26 2 10 1 10 5 19 37 33 33 34 28 20 1 22 11 14", "output": "58" }, { "input": "80 4\n71 49 41 21 72 71 37 14 51 59 73 11 70 15 36 46 32 57 58 15 72 67 16 75 70 11 67 3 40 36 2 9 63 68 32 22 63 52 67 55 35 19 72 59 22 19 44 55 59 74 4 34 53 3 22 57 32 27 78 12 71 4 26 15 43 21 79 10 67 39 34 74 38 26 31 78 2 78 69 42", "output": "62" }, { "input": "80 8\n16 13 11 16 3 4 1 4 4 16 6 6 1 12 19 18 12 15 2 10 2 18 18 13 3 17 16 15 7 6 19 8 2 14 17 13 1 14 4 2 3 16 2 15 13 15 9 10 7 14 7 2 1 18 19 15 7 3 19 8 9 4 12 4 3 4 9 10 6 5 4 4 9 4 20 8 17 7 1 14", "output": "32" }, { "input": "80 8\n5 17 39 25 40 34 11 23 7 16 20 35 31 14 18 17 32 10 40 9 17 23 5 33 2 9 21 22 8 11 22 7 28 36 3 10 12 21 20 29 25 5 12 30 8 21 18 19 1 29 9 4 19 5 15 36 38 37 10 27 15 13 6 22 31 5 40 30 21 39 23 21 39 32 37 28 29 11 34 16", "output": "51" }, { "input": "80 8\n8 72 32 27 27 20 69 28 77 25 8 4 75 11 41 71 57 17 45 65 79 8 61 15 24 80 39 36 34 13 76 37 16 71 64 77 11 58 30 26 61 23 18 30 68 65 12 47 69 65 3 55 71 3 32 4 20 39 47 25 75 49 34 60 48 56 77 70 59 59 75 6 5 23 55 30 62 66 4 4", "output": "57" }, { "input": "80 12\n9 5 8 1 12 2 6 19 8 20 6 12 9 6 16 1 2 5 11 6 8 4 13 7 2 17 18 12 15 17 13 2 9 8 1 17 10 2 9 12 18 3 5 11 10 16 7 16 8 11 3 18 13 19 8 13 13 2 20 13 11 14 20 3 2 1 17 18 17 8 4 3 12 3 19 18 4 16 6 6", "output": "25" }, { "input": "80 12\n27 12 25 30 13 27 12 17 35 25 1 28 35 16 23 20 38 1 37 2 35 29 16 26 37 4 23 39 24 2 16 21 39 21 23 38 33 9 38 22 40 36 23 39 1 2 4 14 22 26 32 4 31 38 4 5 4 15 35 12 5 32 37 38 11 14 16 26 36 38 2 40 10 15 33 38 36 20 35 12", "output": "37" }, { "input": "80 12\n30 19 34 24 56 38 31 63 57 50 53 69 79 5 6 74 47 47 73 17 18 70 72 49 35 20 65 21 18 4 54 12 67 8 28 25 64 6 31 36 35 54 61 7 45 54 55 49 50 6 3 7 10 29 76 62 50 50 32 66 25 19 17 3 67 17 37 67 58 18 54 25 8 78 35 16 61 19 45 40", "output": "55" }, { "input": "80 16\n4 27 31 28 8 17 28 31 20 7 39 5 40 13 28 6 23 1 16 4 34 2 13 6 6 9 18 1 25 19 33 26 33 16 24 5 13 23 25 9 10 16 25 34 39 8 4 6 33 25 7 40 32 23 13 17 32 20 28 25 33 20 29 2 40 34 23 6 28 2 12 12 9 36 18 39 32 8 11 15", "output": "36" }, { "input": "80 16\n31 26 40 46 75 35 63 29 2 49 51 14 4 65 10 4 8 72 44 67 57 60 69 21 52 40 37 54 27 12 31 24 21 59 61 80 11 76 58 7 77 10 55 9 11 36 7 41 61 13 2 28 28 77 22 57 54 62 65 80 78 32 72 64 41 69 36 46 50 5 48 53 6 76 76 65 57 7 29 67", "output": "53" }, { "input": "80 40\n34 71 32 39 65 8 13 4 7 4 18 66 20 12 57 74 58 50 30 27 31 48 1 6 63 63 7 32 56 48 42 35 45 55 52 76 52 26 40 15 8 38 73 47 55 75 17 22 36 59 28 19 6 79 58 7 40 66 48 39 71 67 55 61 71 24 60 39 63 6 47 70 8 10 68 49 49 67 53 15", "output": "54" }, { "input": "80 60\n70 59 1 24 50 77 11 71 36 26 75 56 30 31 76 51 73 29 49 2 80 55 32 52 51 56 2 28 37 11 37 1 63 33 74 60 59 63 55 28 73 45 5 69 77 8 35 76 47 48 49 77 25 75 38 70 77 80 27 9 13 45 14 30 33 34 32 78 52 4 7 57 55 43 13 1 45 36 79 45", "output": "50" }, { "input": "80 80\n73 32 65 26 35 2 10 73 80 64 67 14 40 34 15 59 8 71 5 9 49 63 32 51 23 1 13 9 34 6 80 31 32 60 31 27 65 20 21 74 26 51 17 11 19 37 4 50 73 22 71 55 76 7 51 21 50 63 6 75 66 55 37 62 10 45 52 21 42 18 80 12 7 28 54 50 9 5 25 42", "output": "53" }, { "input": "80 4\n1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5", "output": "23" }, { "input": "80 8\n1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8", "output": "17" }, { "input": "80 16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12", "output": "20" }, { "input": "80 32\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1 2 3 4 5 6 7 8 9 10 11 12 13 14", "output": "34" }, { "input": "80 80\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80", "output": "80" } ]
108
2,150,400
-1
43,761
825
String Compression
[ "dp", "hashing", "string suffix structures", "strings" ]
null
null
Ivan wants to write a letter to his friend. The letter is a string *s* consisting of lowercase Latin letters. Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string *s* instead of the string itself. The compressed version of string *s* is a sequence of strings *c*1,<=*s*1,<=*c*2,<=*s*2,<=...,<=*c**k*,<=*s**k*, where *c**i* is the decimal representation of number *a**i* (without any leading zeroes) and *s**i* is some string consisting of lowercase Latin letters. If Ivan writes string *s*1 exactly *a*1 times, then string *s*2 exactly *a*2 times, and so on, the result will be string *s*. The length of a compressed version is |*c*1|<=+<=|*s*1|<=+<=|*c*2|<=+<=|*s*2|... |*c**k*|<=+<=|*s**k*|. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length.
The only line of input contains one string *s* consisting of lowercase Latin letters (1<=≤<=|*s*|<=≤<=8000).
Output one integer number — the minimum possible length of a compressed version of *s*.
[ "aaaaaaaaaa\n", "abcab\n", "cczabababab\n" ]
[ "3\n", "6\n", "7\n" ]
In the first example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 10, *s*<sub class="lower-index">1</sub> is a. In the second example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 1, *s*<sub class="lower-index">1</sub> is abcab. In the third example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 2, *s*<sub class="lower-index">1</sub> is c, *c*<sub class="lower-index">2</sub> is 1, *s*<sub class="lower-index">2</sub> is z, *c*<sub class="lower-index">3</sub> is 4, *s*<sub class="lower-index">3</sub> is ab.
[ { "input": "aaaaaaaaaa", "output": "3" }, { "input": "abcab", "output": "6" }, { "input": "cczabababab", "output": "7" }, { "input": "kbyjorwqjk", "output": "11" }, { "input": "baaabbbaba", "output": "9" }, { "input": "aaaaaaaaaa", "output": "3" }, { "input": "cbbbcccbbc", "output": "10" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "4" }, { "input": "hltcdvuobkormkxkbmpfjniilublkrckmvvxemcyietgxcyjgrjwsdsgsfmoqnmbxozfavxopklhldhnsjpxhejxaxuctxeifglx", "output": "101" }, { "input": "agdmdjkbfnleldamiiedfheefgaimecnllgkjdkdcfejainklmhaklcjkgkimgfiiajiiihhdngjedgmefnjmbglghjjejfjkaha", "output": "101" }, { "input": "aaaaaaabaaaabbbbaaaaaaabbaaaaaaaaaabbabaaaaaabaaaaabaaaaaaaabaaaaaaaaaaaaaaaabaaaaaabaaaaaaaaabbaaabaaaaabbaaabaaaaabaaabaaaaaabaaaaaaaaaaabaabaaabaaaaabbbbaaaaaaaaaaaaaaabaaaaaaaaababaaabaaaaaaaaaabaaaaaaaabaaaabbbbaaaaaaabbaaaaaaaaaabbabaaaaaabaaaaabaaaaaaaabaaaaaaaaaaaaaaaabaaaaaabaaaaaaaaabbaaabaaaaabbaaabaaaaabaaabaaaaaabaaaaaaaaaaabaabaaabaaaaabbbbaaaaaaaaaaaaaaabaaaaaaaaababaaabaaaaaaaaaaba", "output": "191" }, { "input": "mulzibhhlxawrjqunzww", "output": "21" } ]
62
4,608,000
0
43,777
431
Random Task
[ "binary search", "bitmasks", "combinatorics", "dp", "math" ]
null
null
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer *n*, that among numbers *n*<=+<=1, *n*<=+<=2, ..., 2·*n* there are exactly *m* numbers which binary representation contains exactly *k* digits one". The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.
The first line contains two space-separated integers, *m* and *k* (0<=≤<=*m*<=≤<=1018; 1<=≤<=*k*<=≤<=64).
Print the required number *n* (1<=≤<=*n*<=≤<=1018). If there are multiple answers, print any of them.
[ "1 1\n", "3 2\n" ]
[ "1\n", "5\n" ]
none
[ { "input": "1 1", "output": "1" }, { "input": "3 2", "output": "5" }, { "input": "3 3", "output": "7" }, { "input": "1 11", "output": "1024" }, { "input": "4 20", "output": "983040" }, { "input": "45902564 24", "output": "6406200698" }, { "input": "330 8", "output": "2033" }, { "input": "10 10", "output": "1023" }, { "input": "0 2", "output": "1" }, { "input": "1000000 55", "output": "504262282264444927" }, { "input": "1 60", "output": "576460752303423488" }, { "input": "1000000000 52", "output": "542648557841154044" }, { "input": "101628400788615604 30", "output": "999999999999995905" }, { "input": "101628400798615604 31", "output": "981546175132942729" }, { "input": "55 55", "output": "36028797018963967" }, { "input": "14240928 10", "output": "999948289" }, { "input": "1000000000 10", "output": "38209103398929" }, { "input": "1111111 11", "output": "7734675" }, { "input": "10000000000000000 35", "output": "247948501945678280" }, { "input": "0 19", "output": "1" }, { "input": "768 10", "output": "9471" }, { "input": "3691 6", "output": "39105" }, { "input": "16 15", "output": "40960" }, { "input": "427 4", "output": "18561" }, { "input": "669 9", "output": "5535" }, { "input": "0 16", "output": "1" }, { "input": "286 11", "output": "8185" }, { "input": "6 16", "output": "64512" }, { "input": "13111 8", "output": "73033" }, { "input": "17 2", "output": "65537" }, { "input": "440 4", "output": "20993" }, { "input": "5733 6", "output": "96257" }, { "input": "3322 6", "output": "34441" }, { "input": "333398 7", "output": "142974977" }, { "input": "19027910 20", "output": "530210696" }, { "input": "73964712 13", "output": "808934145" }, { "input": "33156624 15", "output": "217957249" }, { "input": "406 3", "output": "402653185" }, { "input": "3600 4", "output": "310378497" }, { "input": "133015087 16", "output": "903250260" }, { "input": "14065439 11", "output": "277820673" }, { "input": "135647 6", "output": "612761601" }, { "input": "613794 8", "output": "47611905" }, { "input": "79320883 13", "output": "877746562" }, { "input": "433 3", "output": "603979777" }, { "input": "142129 6", "output": "893386753" }, { "input": "20074910 16", "output": "156957897" }, { "input": "27712 4", "output": "54078379900534785" }, { "input": "109197403264830 17", "output": "530824147803045889" }, { "input": "1767 3", "output": "612489549322387457" }, { "input": "2518095982 9", "output": "835136255900516353" }, { "input": "16184825266581 15", "output": "753750817529397249" }, { "input": "60 2", "output": "576460752303423489" }, { "input": "51908921235703 16", "output": "927684967108968449" }, { "input": "373301530 8", "output": "628568807366983681" }, { "input": "51140330728306 16", "output": "880672956240363521" }, { "input": "78015012688021 17", "output": "237668409087623169" }, { "input": "360651917262546 18", "output": "866841191969193985" }, { "input": "15619605006173 15", "output": "676897611185127425" }, { "input": "296851618 8", "output": "208581753835618305" }, { "input": "1651507249349341 20", "output": "660934198681731073" }, { "input": "234217752433205 18", "output": "333773758789582849" }, { "input": "5004844 6", "output": "488640559569698817" }, { "input": "820882585293 13", "output": "167167411424854017" }, { "input": "0 64", "output": "1" } ]
109
1,228,800
3
43,802
717
Dexterina’s Lab
[ "games", "matrices", "probabilities" ]
null
null
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim. Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0,<=*x*]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game. Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
The first line of the input contains two integers *n* (1<=≤<=*n*<=≤<=109) and *x* (1<=≤<=*x*<=≤<=100) — the number of heaps and the maximum number of objects in a heap, respectively. The second line contains *x*<=+<=1 real numbers, given with up to 6 decimal places each: *P*(0),<=*P*(1),<=... ,<=*P*(*X*). Here, *P*(*i*) is the probability of a heap having exactly *i* objects in start of a game. It's guaranteed that the sum of all *P*(*i*) is equal to 1.
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10<=-<=6.
[ "2 2\n0.500000 0.250000 0.250000\n" ]
[ "0.62500000\n" ]
none
[ { "input": "2 2\n0.500000 0.250000 0.250000", "output": "0.62500000" }, { "input": "9 9\n0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000", "output": "0.93687014" }, { "input": "1000001 5\n0.000000 0.300000 0.000000 0.500000 0.000000 0.200000", "output": "1.00000000" }, { "input": "1000000 5\n0.000000 0.000000 1.000000 0.000000 0.000000 0.000000", "output": "0.00000000" }, { "input": "100 20\n0.065682 0.015963 0.024840 0.025856 0.000807 0.004366 0.020977 0.151841 0.050870 0.009918 0.044345 0.009672 0.077615 0.019520 0.166791 0.028122 0.010873 0.018634 0.172648 0.037614 0.043046", "output": "0.96875000" }, { "input": "1000000 50\n0.034403 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.033928 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.071853 0.000000 0.000000 0.016440 0.000000 0.000000 0.000000 0.000000 0.023167 0.067938 0.000000 0.000000 0.000000 0.000000 0.176349 0.030154 0.180266 0.272662 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.014462 0.000000 0.000000 0.037916 0.040462", "output": "0.98437500" } ]
140
1,945,600
0
43,841
131
Opposites Attract
[ "implementation", "math" ]
null
null
Everybody knows that opposites attract. That is the key principle of the "Perfect Matching" dating agency. The "Perfect Matching" matchmakers have classified each registered customer by his interests and assigned to the *i*-th client number *t**i* (<=-<=10<=≤<=*t**i*<=≤<=10). Of course, one number can be assigned to any number of customers. "Perfect Matching" wants to advertise its services and publish the number of opposite couples, that is, the couples who have opposite values of *t*. Each couple consists of exactly two clients. The customer can be included in a couple an arbitrary number of times. Help the agency and write the program that will find the sought number by the given sequence *t*1,<=*t*2,<=...,<=*t**n*. For example, if *t*<==<=(1,<=<=-<=1,<=1,<=<=-<=1), then any two elements *t**i* and *t**j* form a couple if *i* and *j* have different parity. Consequently, in this case the sought number equals 4. Of course, a client can't form a couple with him/herself.
The first line of the input data contains an integer *n* (1<=≤<=*n*<=≤<=105) which represents the number of registered clients of the "Couple Matching". The second line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n* (<=-<=10<=≤<=*t**i*<=≤<=10), *t**i* — is the parameter of the *i*-th customer that has been assigned to the customer by the result of the analysis of his interests.
Print the number of couples of customs with opposite *t*. The opposite number for *x* is number <=-<=*x* (0 is opposite to itself). Couples that only differ in the clients' order are considered the same. Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
[ "5\n-3 3 0 0 3\n", "3\n0 0 0\n" ]
[ "3\n", "3\n" ]
In the first sample the couples of opposite clients are: (1,2), (1,5) и (3,4). In the second sample any couple of clients is opposite.
[ { "input": "5\n-3 3 0 0 3", "output": "3" }, { "input": "3\n0 0 0", "output": "3" }, { "input": "1\n0", "output": "0" }, { "input": "1\n5", "output": "0" }, { "input": "2\n0 0", "output": "1" }, { "input": "2\n-3 3", "output": "1" }, { "input": "2\n1 5", "output": "0" }, { "input": "4\n1 -1 1 -1", "output": "4" }, { "input": "10\n1 -1 2 -2 3 -3 4 -4 0 0", "output": "5" }, { "input": "5\n0 0 0 0 0", "output": "10" }, { "input": "4\n4 4 4 -1", "output": "0" }, { "input": "2\n10 -10", "output": "1" }, { "input": "2\n-10 10", "output": "1" }, { "input": "3\n-1 -1 2", "output": "0" }, { "input": "2\n-1 2", "output": "0" }, { "input": "7\n0 10 -10 10 -10 10 10", "output": "8" }, { "input": "5\n-3 2 1 -1 3", "output": "2" }, { "input": "4\n-10 10 -10 10", "output": "4" }, { "input": "4\n1 -1 2 -2", "output": "2" } ]
30
0
0
43,859
266
More Queries to Array...
[ "data structures", "math" ]
null
null
You've got an array, consisting of *n* integers: *a*1,<=*a*2,<=...,<=*a**n*. Your task is to quickly run the queries of two types: 1. Assign value *x* to all elements from *l* to *r* inclusive. After such query the values of the elements of array *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* become equal to *x*.1. Calculate and print sum , where *k* doesn't exceed 5. As the value of the sum can be rather large, you should print it modulo 1000000007 (109<=+<=7).
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105), showing, how many numbers are in the array and the number of queries, correspondingly. The second line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109) — the initial values of the array elements. Then *m* queries follow, one per line: 1. The assign query has the following format: "", (1<=≤<=*l*<=≤<=*r*<=≤<=*n*; 0<=≤<=*x*<=≤<=109).1. The query to calculate the sum has the following format: "", (1<=≤<=*l*<=≤<=*r*<=≤<=*n*; 0<=≤<=*k*<=≤<=5). All numbers in the input are integers.
For each query to calculate the sum print an integer — the required sum modulo 1000000007 (109<=+<=7).
[ "4 5\n5 10 2 1\n? 1 2 1\n= 2 2 0\n? 2 4 3\n= 1 4 1\n? 1 4 5\n", "3 1\n1000000000 1000000000 1000000000\n? 1 3 0\n" ]
[ "25\n43\n1300\n", "999999986\n" ]
none
[]
92
1,228,800
0
43,929
715
Complete The Graph
[ "binary search", "constructive algorithms", "graphs", "shortest paths" ]
null
null
ZS the Coder has drawn an undirected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *m* edges between them. Each edge of the graph is weighted, each weight is a positive integer. The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer weight to each of the edges which weights were erased, so that the length of the shortest path between vertices *s* and *t* in the resulting graph is exactly *L*. Can you help him?
The first line contains five integers *n*,<=*m*,<=*L*,<=*s*,<=*t* (2<=≤<=*n*<=≤<=1000,<=<=1<=≤<=*m*<=≤<=10<=000,<=<=1<=≤<=*L*<=≤<=109,<=<=0<=≤<=*s*,<=*t*<=≤<=*n*<=-<=1,<=<=*s*<=≠<=*t*) — the number of vertices, number of edges, the desired length of shortest path, starting vertex and ending vertex respectively. Then, *m* lines describing the edges of the graph follow. *i*-th of them contains three integers, *u**i*,<=*v**i*,<=*w**i* (0<=≤<=*u**i*,<=*v**i*<=≤<=*n*<=-<=1,<=<=*u**i*<=≠<=*v**i*,<=<=0<=≤<=*w**i*<=≤<=109). *u**i* and *v**i* denote the endpoints of the edge and *w**i* denotes its weight. If *w**i* is equal to 0 then the weight of the corresponding edge was erased. It is guaranteed that there is at most one edge between any pair of vertices.
Print "NO" (without quotes) in the only line if it's not possible to assign the weights in a required way. Otherwise, print "YES" in the first line. Next *m* lines should contain the edges of the resulting graph, with weights assigned to edges which weights were erased. *i*-th of them should contain three integers *u**i*, *v**i* and *w**i*, denoting an edge between vertices *u**i* and *v**i* of weight *w**i*. The edges of the new graph must coincide with the ones in the graph from the input. The weights that were not erased must remain unchanged whereas the new weights can be any positive integer not exceeding 1018. The order of the edges in the output doesn't matter. The length of the shortest path between *s* and *t* must be equal to *L*. If there are multiple solutions, print any of them.
[ "5 5 13 0 4\n0 1 5\n2 1 2\n3 2 3\n1 4 0\n4 3 4\n", "2 1 123456789 0 1\n0 1 0\n", "2 1 999999999 1 0\n0 1 1000000000\n" ]
[ "YES\n0 1 5\n2 1 2\n3 2 3\n1 4 8\n4 3 4\n", "YES\n0 1 123456789\n", "NO\n" ]
Here's how the graph in the first sample case looks like : In the first sample case, there is only one missing edge weight. Placing the weight of 8 gives a shortest path from 0 to 4 of length 13. In the second sample case, there is only a single edge. Clearly, the only way is to replace the missing weight with 123456789. In the last sample case, there is no weights to assign but the length of the shortest path doesn't match the required value, so the answer is "NO".
[ { "input": "5 5 13 0 4\n0 1 5\n2 1 2\n3 2 3\n1 4 0\n4 3 4", "output": "YES\n0 1 5\n2 1 2\n3 2 3\n1 4 8\n4 3 4" }, { "input": "2 1 123456789 0 1\n0 1 0", "output": "YES\n0 1 123456789" }, { "input": "2 1 999999999 1 0\n0 1 1000000000", "output": "NO" }, { "input": "4 5 10 1 2\n0 1 3\n1 2 0\n1 3 4\n2 3 4\n2 0 6", "output": "NO" }, { "input": "100 1 123456 99 0\n0 99 123456", "output": "YES\n0 99 123456" }, { "input": "1000 1 5 999 0\n0 999 0", "output": "YES\n0 999 5" }, { "input": "1000 1 1000000000 998 0\n0 999 0", "output": "NO" }, { "input": "4 4 14 1 3\n1 3 13\n2 3 0\n2 0 0\n1 0 12", "output": "NO" }, { "input": "4 4 13 1 3\n1 3 13\n2 3 0\n2 0 0\n1 0 12", "output": "YES\n1 3 13\n2 3 1000000000000000000\n2 0 1000000000000000000\n1 0 12" }, { "input": "4 4 2 1 3\n1 3 13\n2 3 0\n2 0 0\n1 0 0", "output": "NO" }, { "input": "4 4 8 1 3\n1 3 13\n2 3 0\n2 0 0\n1 0 6", "output": "YES\n1 3 13\n2 3 1\n2 0 1\n1 0 6" }, { "input": "5 6 1000000000 0 4\n0 1 1\n2 0 2\n3 0 3\n4 1 0\n4 2 0\n3 4 0", "output": "YES\n0 1 1\n2 0 2\n3 0 3\n4 1 999999999\n4 2 1000000000000000000\n3 4 1000000000000000000" }, { "input": "7 9 320 0 3\n0 1 0\n1 2 0\n2 3 0\n0 4 1\n4 1 1\n1 5 100\n5 2 100\n2 6 59\n6 3 61", "output": "YES\n0 1 1\n1 2 199\n2 3 318\n0 4 1\n4 1 1\n1 5 100\n5 2 100\n2 6 59\n6 3 61" }, { "input": "7 9 319 0 3\n0 1 0\n1 2 0\n2 3 0\n0 4 1\n4 1 1\n1 5 100\n5 2 100\n2 6 59\n6 3 61", "output": "YES\n0 1 1\n1 2 198\n2 3 317\n0 4 1\n4 1 1\n1 5 100\n5 2 100\n2 6 59\n6 3 61" }, { "input": "7 9 999999999 0 3\n0 1 0\n1 2 0\n2 3 0\n0 4 1\n4 1 1\n1 5 499999999\n5 2 499999999\n2 6 1\n6 3 1", "output": "YES\n0 1 1\n1 2 999999996\n2 3 999999997\n0 4 1\n4 1 1\n1 5 499999999\n5 2 499999999\n2 6 1\n6 3 1" }, { "input": "5 5 2 0 2\n0 1 1\n1 2 1\n0 4 0\n4 3 0\n3 2 0", "output": "YES\n0 1 1\n1 2 1\n0 4 1000000000000000000\n4 3 1000000000000000000\n3 2 1000000000000000000" }, { "input": "5 5 1 0 2\n0 1 1\n1 2 1\n0 4 0\n4 3 0\n3 2 0", "output": "NO" }, { "input": "5 5 3 0 2\n0 1 1\n1 2 1\n0 4 0\n4 3 0\n3 2 0", "output": "NO" }, { "input": "8 9 10 1 0\n1 2 1\n2 4 1\n1 3 0\n3 4 0\n4 5 0\n5 6 1\n6 0 1\n5 7 0\n7 0 0", "output": "YES\n1 2 1\n2 4 1\n1 3 1000000000000000000\n3 4 1000000000000000000\n4 5 6\n5 6 1\n6 0 1\n5 7 1000000000000000000\n7 0 1000000000000000000" }, { "input": "4 5 7 0 3\n0 1 0\n1 2 3\n2 3 0\n0 2 5\n1 3 5", "output": "YES\n0 1 2\n1 2 3\n2 3 3\n0 2 5\n1 3 5" } ]
4,000
27,238,400
0
43,963
590
Birthday
[ "graph matchings", "strings" ]
null
null
Today is birthday of a Little Dasha — she is now 8 years old! On this occasion, each of her *n* friends and relatives gave her a ribbon with a greeting written on it, and, as it turned out, all the greetings are different. Dasha gathered all the ribbons and decided to throw away some of them in order to make the remaining set stylish. The birthday girl considers a set of ribbons stylish if no greeting written on some ribbon is a substring of another greeting written on some other ribbon. Let us recall that the substring of the string *s* is a continuous segment of *s*. Help Dasha to keep as many ribbons as possible, so that she could brag about them to all of her friends. Dasha cannot rotate or flip ribbons, that is, each greeting can be read in a single way given in the input.
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=750) — the number of Dasha's relatives and friends. Each of the next *n* lines contains exactly one greeting. Each greeting consists of characters 'a' and 'b' only. The total length of all greetings won't exceed 10<=000<=000 characters.
In the first line print the maximum size of the stylish set. In the second line print the numbers of ribbons involved in it, assuming that they are numbered from 1 to *n* in the order they appear in the input. If there are several stylish sets of the maximum size, print any of them.
[ "5\nabab\naba\naabab\nababb\nbab\n" ]
[ "2\n2 5\n" ]
In the sample, the answer that keeps ribbons 3 and 4 is also considered correct.
[]
2,386
14,950,400
0
44,094
819
Mister B and Astronomers
[ "number theory" ]
null
null
After studying the beacons Mister B decided to visit alien's planet, because he learned that they live in a system of flickering star Moon. Moreover, Mister B learned that the star shines once in exactly *T* seconds. The problem is that the star is yet to be discovered by scientists. There are *n* astronomers numerated from 1 to *n* trying to detect the star. They try to detect the star by sending requests to record the sky for 1 second. The astronomers send requests in cycle: the *i*-th astronomer sends a request exactly *a**i* second after the (*i*<=-<=1)-th (i.e. if the previous request was sent at moment *t*, then the next request is sent at moment *t*<=+<=*a**i*); the 1-st astronomer sends requests *a*1 seconds later than the *n*-th. The first astronomer sends his first request at moment 0. Mister B doesn't know the first moment the star is going to shine, but it's obvious that all moments at which the star will shine are determined by the time of its shine moment in the interval [0,<=*T*). Moreover, this interval can be split into *T* parts of 1 second length each of form [*t*,<=*t*<=+<=1), where *t*<==<=0,<=1,<=2,<=...,<=(*T*<=-<=1). Mister B wants to know how lucky each astronomer can be in discovering the star first. For each astronomer compute how many segments of form [*t*,<=*t*<=+<=1) (*t*<==<=0,<=1,<=2,<=...,<=(*T*<=-<=1)) there are in the interval [0,<=*T*) so that this astronomer is the first to discover the star if the first shine of the star happens in this time interval.
The first line contains two integers *T* and *n* (1<=≤<=*T*<=≤<=109, 2<=≤<=*n*<=≤<=2·105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
Print *n* integers: for each astronomer print the number of time segments describer earlier.
[ "4 2\n2 3\n", "5 4\n1 1 1 1\n" ]
[ "3 1 \n", "2 1 1 1 \n" ]
In the first sample test the first astronomer will send requests at moments *t*<sub class="lower-index">1</sub> = 0, 5, 10, ..., the second — at moments *t*<sub class="lower-index">2</sub> = 3, 8, 13, .... That's why interval [0, 1) the first astronomer will discover first at moment *t*<sub class="lower-index">1</sub> = 0, [1, 2) — the first astronomer at moment *t*<sub class="lower-index">1</sub> = 5, [2, 3) — the first astronomer at moment *t*<sub class="lower-index">1</sub> = 10, and [3, 4) — the second astronomer at moment *t*<sub class="lower-index">2</sub> = 3. In the second sample test interval [0, 1) — the first astronomer will discover first, [1, 2) — the second astronomer, [2, 3) — the third astronomer, [3, 4) — the fourth astronomer, [4, 5) — the first astronomer.
[]
62
0
0
44,119
895
Square Subsets
[ "bitmasks", "combinatorics", "dp", "math" ]
null
null
Petya was late for the lesson too. The teacher gave him an additional task. For some array *a* Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer. Two ways are considered different if sets of indexes of elements chosen by these ways are different. Since the answer can be very large, you should find the answer modulo 109<=+<=7.
First line contains one integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the array. Second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=70) — the elements of the array.
Print one integer — the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109<=+<=7.
[ "4\n1 1 1 1\n", "4\n2 2 2 2\n", "5\n1 2 4 5 8\n" ]
[ "15\n", "7\n", "7\n" ]
In first sample product of elements chosen by any way is 1 and 1 = 1<sup class="upper-index">2</sup>. So the answer is 2<sup class="upper-index">4</sup> - 1 = 15. In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
[ { "input": "4\n1 1 1 1", "output": "15" }, { "input": "4\n2 2 2 2", "output": "7" }, { "input": "5\n1 2 4 5 8", "output": "7" }, { "input": "1\n64", "output": "1" }, { "input": "5\n2 2 2 2 2", "output": "15" }, { "input": "6\n1 2 3 4 5 6", "output": "7" }, { "input": "2\n70 70", "output": "1" }, { "input": "7\n4 9 16 25 36 49 64", "output": "127" }, { "input": "13\n64 65 40 26 36 46 53 31 63 11 2 46 59", "output": "15" }, { "input": "15\n66 34 43 45 61 14 12 67 38 25 55 9 30 41 16", "output": "15" }, { "input": "17\n44 57 54 57 54 65 40 57 59 16 39 51 32 51 20 9 8", "output": "511" }, { "input": "18\n22 41 40 8 36 48 23 5 58 12 26 44 53 49 3 56 58 57", "output": "127" }, { "input": "20\n20 34 51 40 70 64 14 30 24 20 6 1 70 28 38 43 9 60 31 69", "output": "2047" }, { "input": "5\n19 51 55 29 13", "output": "0" }, { "input": "6\n19 60 48 64 56 27", "output": "3" }, { "input": "7\n67 52 58 62 38 26 2", "output": "1" }, { "input": "7\n5 28 46 57 39 26 45", "output": "1" }, { "input": "7\n53 59 56 9 13 1 28", "output": "3" }, { "input": "10\n38 58 51 41 61 12 17 47 18 24", "output": "3" }, { "input": "10\n27 44 40 3 33 38 56 37 43 36", "output": "7" }, { "input": "10\n51 4 25 46 15 21 32 9 43 8", "output": "15" }, { "input": "10\n5 66 19 60 34 27 15 27 42 51", "output": "7" }, { "input": "5\n2 3 5 7 11", "output": "0" }, { "input": "10\n2 3 5 7 11 13 17 19 23 29", "output": "0" }, { "input": "2\n15 45", "output": "0" } ]
0
0
-1
44,302
718
Matvey's Birthday
[ "bitmasks", "graphs" ]
null
null
Today is Matvey's birthday. He never knows what to ask as a present so friends gave him a string *s* of length *n*. This string consists of only first eight English letters: 'a', 'b', ..., 'h'. First question that comes to mind is: who might ever need some string? Matvey is a special boy so he instantly found what to do with this string. He used it to build an undirected graph where vertices correspond to position in the string and there is an edge between distinct positions *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*) if at least one of the following conditions hold: 1. *a* and *b* are neighbouring, i.e. |*a*<=-<=*b*|<==<=1. 1. Positions *a* and *b* contain equal characters, i.e. *s**a*<==<=*s**b*. Then Matvey decided to find the diameter of this graph. Diameter is a maximum distance (length of the shortest path) among all pairs of vertices. Also, Matvey wants to find the number of pairs of vertices such that the distance between them is equal to the diameter of the graph. As he is very cool and experienced programmer he managed to solve this problem very fast. Will you do the same?
The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — the length of the string. The second line contains the string *s* itself. It's guaranteed that *s* consists of only first eight letters of English alphabet.
Print two integers — the diameter of the graph and the number of pairs of positions with the distance equal to the diameter.
[ "3\nabc\n", "7\naaabaaa\n" ]
[ "2 1\n", "2 4\n" ]
Consider the second sample. The maximum distance is 2. It's obtained for pairs (1, 4), (2, 4), (4, 6) and (4, 7).
[]
2,000
27,852,800
0
44,306
767
The Queue
[ "brute force", "greedy" ]
null
null
Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after *t**s* minutes have passed after midnight and closes after *t**f* minutes have passed after midnight (so that (*t**f*<=-<=1) is the last minute when the receptionist is still working). The receptionist spends exactly *t* minutes on each person in the queue. If the receptionist would stop working within *t* minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly *n* visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!
The first line contains three integers: the point of time when the receptionist begins to work *t**s*, the point of time when the receptionist stops working *t**f* and the time the receptionist spends on each visitor *t*. The second line contains one integer *n* — the amount of visitors (0<=≤<=*n*<=≤<=100<=000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that *t**s*<=&lt;<=*t**f*. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.
Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.
[ "10 15 2\n2\n10 13\n", "8 17 3\n4\n3 4 5 8\n" ]
[ "12", "2" ]
In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
[ { "input": "10 15 2\n2\n10 13", "output": "12" }, { "input": "8 17 3\n4\n3 4 5 8", "output": "2" }, { "input": "7 14 3\n2\n1 2", "output": "0" }, { "input": "30 70 10\n3\n30 32 35", "output": "60" }, { "input": "21 56 7\n5\n1 2 3 4 5", "output": "0" }, { "input": "10 1000000000 25\n20\n1 1 5 7 8 10 12 22 44 47 73 77 82 83 89 141 142 168 195 199", "output": "510" }, { "input": "30 60 3\n10\n1 5 6 10 12 13 18 23 24 25", "output": "4" }, { "input": "61 1000000000 13\n55\n29 72 85 94 103 123 125 144 147 153 154 184 189 192 212 234 247 265 292 296 299 304 309 365 378 379 393 401 414 417 421 427 439 441 480 500 509 515 522 539 571 582 623 630 634 635 643 649 654 679 680 686 747 748 775", "output": "360" }, { "input": "117 120 3\n0", "output": "117" }, { "input": "37 3813 32\n117\n1 1 4 5 6 8 10 13 13 16 18 19 20 23 30 32 33 38 49 59 66 69 96 157 160 183 205 292 301 320 349 360 370 372 384 400 410 413 423 434 434 445 451 463 464 490 494 496 497 517 528 532 556 572 607 647 668 689 708 729 748 806 819 879 905 905 915 925 928 931 959 970 1133 1146 1150 1156 1172 1189 1193 1218 1228 1233 1247 1297 1383 1447 1468 1512 1539 1550 1634 1635 1702 1836 1845 1866 1880 2068 2082 2143 2146 2167 2234 2239 2277 2331 2351 2432 2478 2592 2684 2863 2885 3043 3141 3258 3426", "output": "3781" }, { "input": "100000000000 200000000000 10000000000\n10\n1 1 110000000000 110000000000 110000000000 110000000000 110000000000 110000000000 110000000000 110000000000", "output": "109999999999" }, { "input": "1 2 1\n0", "output": "1" }, { "input": "50 230 10\n20\n50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240", "output": "49" }, { "input": "100000000000 100000000005 2\n0", "output": "100000000000" }, { "input": "333 500 5\n1\n3000", "output": "333" } ]
93
13,516,800
3
44,398
584
Dima and Lisa
[ "brute force", "math", "number theory" ]
null
null
Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes. More formally, you are given an odd numer *n*. Find a set of numbers *p**i* (1<=≤<=*i*<=≤<=*k*), such that 1. 1<=≤<=*k*<=≤<=31. *p**i* is a prime1. The numbers *p**i* do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.
The single line contains an odd number *n* (3<=≤<=*n*<=&lt;<=109).
In the first line print *k* (1<=≤<=*k*<=≤<=3), showing how many numbers are in the representation you found. In the second line print numbers *p**i* in any order. If there are multiple possible solutions, you can print any of them.
[ "27\n" ]
[ "3\n5 11 11\n" ]
A prime is an integer strictly larger than one that is divisible only by one and by itself.
[ { "input": "27", "output": "3\n2 2 23" }, { "input": "3", "output": "1\n3" }, { "input": "25", "output": "2\n2 23" }, { "input": "9", "output": "2\n2 7" }, { "input": "91", "output": "2\n2 89" }, { "input": "57", "output": "3\n2 2 53" }, { "input": "31", "output": "1\n31" }, { "input": "555", "output": "3\n3 5 547" }, { "input": "700000001", "output": "1\n700000001" }, { "input": "5", "output": "1\n5" }, { "input": "7", "output": "1\n7" }, { "input": "11", "output": "1\n11" }, { "input": "13", "output": "1\n13" }, { "input": "15", "output": "2\n2 13" }, { "input": "17", "output": "1\n17" }, { "input": "19", "output": "1\n19" }, { "input": "21", "output": "2\n2 19" }, { "input": "23", "output": "1\n23" }, { "input": "29", "output": "1\n29" }, { "input": "79", "output": "1\n79" }, { "input": "99", "output": "2\n2 97" }, { "input": "27", "output": "3\n2 2 23" }, { "input": "55", "output": "2\n2 53" }, { "input": "79", "output": "1\n79" }, { "input": "585", "output": "3\n3 5 577" }, { "input": "245", "output": "3\n2 2 241" }, { "input": "793", "output": "3\n3 3 787" }, { "input": "133", "output": "2\n2 131" }, { "input": "681", "output": "3\n2 2 677" }, { "input": "981399", "output": "2\n2 981397" }, { "input": "867773", "output": "1\n867773" }, { "input": "654141", "output": "3\n3 11 654127" }, { "input": "202985", "output": "3\n2 2 202981" }, { "input": "784533", "output": "3\n3 17 784513" }, { "input": "370359", "output": "3\n19 79 370261" }, { "input": "396831", "output": "3\n19 79 396733" }, { "input": "492211", "output": "3\n19 79 492113" }, { "input": "838347", "output": "3\n19 79 838249" }, { "input": "1098945", "output": "3\n19 79 1098847" }, { "input": "1313565", "output": "3\n19 79 1313467" }, { "input": "1349631", "output": "3\n19 79 1349533" }, { "input": "1357299", "output": "3\n19 79 1357201" }, { "input": "1357323", "output": "3\n13 109 1357201" }, { "input": "1357329", "output": "3\n19 109 1357201" }, { "input": "1388581", "output": "3\n19 79 1388483" }, { "input": "5275", "output": "2\n2 5273" }, { "input": "9515", "output": "3\n2 2 9511" }, { "input": "7847", "output": "3\n3 3 7841" }, { "input": "7077", "output": "3\n3 5 7069" }, { "input": "9531", "output": "3\n3 7 9521" }, { "input": "7865", "output": "3\n5 7 7853" }, { "input": "9675", "output": "3\n3 11 9661" }, { "input": "8909", "output": "3\n3 13 8893" }, { "input": "7147", "output": "3\n5 13 7129" }, { "input": "8487", "output": "3\n3 17 8467" }, { "input": "436273289", "output": "3\n3 277 436273009" }, { "input": "649580445", "output": "3\n3 271 649580171" }, { "input": "944193065", "output": "3\n7 251 944192807" }, { "input": "630045387", "output": "3\n11 239 630045137" }, { "input": "931103229", "output": "3\n3 223 931103003" }, { "input": "950664039", "output": "3\n3 197 950663839" }, { "input": "996104777", "output": "3\n7 173 996104597" }, { "input": "997255617", "output": "3\n7 157 997255453" }, { "input": "999962901", "output": "3\n19 109 999962773" }, { "input": "999995529", "output": "3\n19 79 999995431" }, { "input": "999995339", "output": "3\n5 43 999995291" }, { "input": "999998367", "output": "3\n5 23 999998339" }, { "input": "999999891", "output": "3\n3 5 999999883" }, { "input": "999999935", "output": "3\n3 3 999999929" }, { "input": "999999755", "output": "3\n2 2 999999751" }, { "input": "999999759", "output": "2\n2 999999757" }, { "input": "999999191", "output": "1\n999999191" }, { "input": "999999999", "output": "3\n3 59 999999937" }, { "input": "409449117", "output": "3\n2 2 409449113" }, { "input": "882499837", "output": "3\n3 3 882499831" }, { "input": "765615965", "output": "3\n5 23 765615937" }, { "input": "648732093", "output": "3\n3 11 648732079" }, { "input": "826815517", "output": "3\n3 11 826815503" }, { "input": "4898941", "output": "2\n2 4898939" }, { "input": "182982365", "output": "3\n5 13 182982347" }, { "input": "66098493", "output": "3\n3 41 66098449" }, { "input": "539149213", "output": "1\n539149213" }, { "input": "655957385", "output": "3\n3 13 655957369" }, { "input": "199999581", "output": "3\n19 79 199999483" }, { "input": "199998345", "output": "3\n19 79 199998247" }, { "input": "199991935", "output": "3\n19 79 199991837" }, { "input": "199986207", "output": "3\n19 79 199986109" }, { "input": "499991589", "output": "3\n19 79 499991491" }, { "input": "499984689", "output": "3\n19 79 499984591" }, { "input": "499984159", "output": "3\n19 79 499984061" }, { "input": "499966179", "output": "3\n19 79 499966081" }, { "input": "999995529", "output": "3\n19 79 999995431" }, { "input": "999995085", "output": "3\n19 79 999994987" }, { "input": "999991817", "output": "3\n11 137 999991669" }, { "input": "999991797", "output": "3\n19 109 999991669" }, { "input": "999991791", "output": "3\n13 109 999991669" }, { "input": "748859699", "output": "3\n3 3 748859693" }, { "input": "323845235", "output": "3\n3 3 323845229" }, { "input": "462409937", "output": "3\n2 2 462409933" }, { "input": "618047403", "output": "3\n3 13 618047387" }, { "input": "501148647", "output": "3\n2 2 501148643" }, { "input": "998017623", "output": "2\n2 998017621" }, { "input": "436273289", "output": "3\n3 277 436273009" }, { "input": "999999965", "output": "3\n5 23 999999937" }, { "input": "5", "output": "1\n5" }, { "input": "1000037", "output": "1\n1000037" }, { "input": "989898987", "output": "3\n2 2 989898983" }, { "input": "999999999", "output": "3\n3 59 999999937" }, { "input": "100000003", "output": "3\n3 11 99999989" } ]
46
0
0
44,737
581
Zublicanes and Mumocrates
[ "dp", "trees", "two pointers" ]
null
null
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on *n* main squares of the capital of Berland. Each of the *n* squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties. Some pairs of squares are connected by (*n*<=-<=1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares. The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even. To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.
The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=5000) — the number of squares in the capital of Berland. Next *n*<=-<=1 lines contain the pairs of integers *x*,<=*y* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=*x*<=≠<=*y*) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to *n*. It is guaranteed that the number of dead end squares of the city is even.
Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.
[ "8\n1 4\n2 4\n3 4\n6 5\n7 5\n8 5\n4 5\n", "5\n1 2\n1 3\n1 4\n1 5\n" ]
[ "1\n", "2\n" ]
none
[ { "input": "8\n1 4\n2 4\n3 4\n6 5\n7 5\n8 5\n4 5", "output": "1" }, { "input": "5\n1 2\n1 3\n1 4\n1 5", "output": "2" }, { "input": "11\n1 7\n2 1\n2 9\n6 2\n7 10\n1 3\n5 2\n3 8\n8 11\n2 4", "output": "2" }, { "input": "20\n2 18\n15 18\n18 4\n4 20\n20 6\n8 6\n1 8\n9 6\n11 9\n11 12\n19 4\n3 9\n9 7\n7 13\n10 3\n16 20\n1 5\n5 17\n10 14", "output": "1" }, { "input": "11\n9 2\n9 4\n8 9\n7 9\n3 9\n5 9\n6 9\n10 9\n1 9\n11 9", "output": "5" }, { "input": "15\n4 12\n1 12\n1 6\n9 1\n7 4\n12 5\n15 9\n11 1\n13 9\n14 9\n9 2\n3 5\n10 2\n3 8", "output": "1" }, { "input": "16\n15 6\n5 9\n3 15\n9 11\n7 15\n1 2\n14 6\n8 9\n14 12\n10 16\n3 13\n8 1\n3 1\n9 4\n10 1", "output": "2" }, { "input": "17\n15 6\n2 8\n15 2\n8 3\n16 4\n13 7\n11 5\n10 1\n2 12\n16 8\n12 9\n11 8\n1 8\n5 17\n13 11\n14 13", "output": "2" }, { "input": "18\n5 7\n3 9\n16 17\n18 13\n3 15\n3 18\n17 11\n12 8\n1 2\n5 16\n17 4\n1 4\n8 1\n6 5\n4 18\n10 5\n14 17", "output": "1" }, { "input": "19\n3 19\n13 19\n11 6\n15 19\n7 14\n12 18\n8 16\n7 4\n11 12\n7 10\n11 14\n2 17\n9 7\n3 11\n2 7\n1 7\n16 2\n5 17", "output": "2" }, { "input": "21\n3 19\n3 17\n4 3\n11 3\n3 16\n8 3\n7 3\n3 21\n3 9\n13 3\n18 3\n12 3\n3 1\n10 3\n3 5\n3 20\n3 14\n3 2\n6 3\n3 15", "output": "10" }, { "input": "21\n7 8\n20 7\n7 6\n14 7\n3 7\n7 19\n7 9\n2 7\n4 7\n16 7\n7 5\n7 13\n7 10\n15 7\n7 11\n7 17\n7 21\n18 7\n7 1\n7 12", "output": "10" }, { "input": "23\n3 9\n9 15\n9 19\n10 9\n9 4\n9 8\n1 9\n9 6\n9 21\n9 18\n20 9\n9 13\n9 23\n14 9\n5 9\n12 9\n9 16\n2 9\n11 9\n17 9\n7 9\n22 9", "output": "11" }, { "input": "23\n15 6\n15 23\n15 10\n11 15\n15 19\n15 7\n12 15\n15 8\n4 15\n22 15\n20 15\n17 15\n3 15\n15 13\n21 15\n15 16\n9 15\n5 15\n14 15\n1 15\n15 18\n2 15", "output": "11" } ]
0
0
-1
44,755
533
Board Game
[ "games", "greedy", "implementation", "math" ]
null
null
Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (*x*,<=*y*) to (*x*<=-<=1,<=*y*) or (*x*,<=*y*<=-<=1). Vasiliy can move his pawn from (*x*,<=*y*) to one of cells: (*x*<=-<=1,<=*y*),<=(*x*<=-<=1,<=*y*<=-<=1) and (*x*,<=*y*<=-<=1). Both players are also allowed to skip move. There are some additional restrictions — a player is forbidden to move his pawn to a cell with negative *x*-coordinate or *y*-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0,<=0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well.
The first line contains four integers: *x**p*,<=*y**p*,<=*x**v*,<=*y**v* (0<=≤<=*x**p*,<=*y**p*,<=*x**v*,<=*y**v*<=≤<=105) — Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0,<=0).
Output the name of the winner: "Polycarp" or "Vasiliy".
[ "2 1 2 2\n", "4 7 7 4\n" ]
[ "Polycarp\n", "Vasiliy\n" ]
In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn.
[ { "input": "2 1 2 2", "output": "Polycarp" }, { "input": "4 7 7 4", "output": "Vasiliy" }, { "input": "20 0 7 22", "output": "Polycarp" }, { "input": "80 100 83 97", "output": "Vasiliy" }, { "input": "80 100 77 103", "output": "Vasiliy" }, { "input": "55000 60000 55003 60100", "output": "Polycarp" }, { "input": "100000 100000 100000 99999", "output": "Vasiliy" }, { "input": "100000 99999 100000 100000", "output": "Polycarp" }, { "input": "0 100000 100000 99999", "output": "Polycarp" }, { "input": "0 100000 99999 100000", "output": "Polycarp" }, { "input": "0 90000 89999 89999", "output": "Vasiliy" }, { "input": "0 1 0 2", "output": "Polycarp" }, { "input": "0 1 1 0", "output": "Polycarp" }, { "input": "0 1 1 1", "output": "Polycarp" }, { "input": "0 1 1 2", "output": "Polycarp" }, { "input": "0 1 2 0", "output": "Polycarp" }, { "input": "0 1 2 1", "output": "Polycarp" }, { "input": "0 1 2 2", "output": "Polycarp" }, { "input": "0 2 0 1", "output": "Vasiliy" }, { "input": "0 2 1 0", "output": "Vasiliy" }, { "input": "0 2 1 1", "output": "Vasiliy" }, { "input": "0 2 1 2", "output": "Polycarp" }, { "input": "0 2 2 0", "output": "Polycarp" }, { "input": "0 2 2 1", "output": "Polycarp" }, { "input": "0 2 2 2", "output": "Polycarp" }, { "input": "1 0 0 1", "output": "Polycarp" }, { "input": "1 0 0 2", "output": "Polycarp" }, { "input": "1 0 1 1", "output": "Polycarp" }, { "input": "1 0 1 2", "output": "Polycarp" }, { "input": "1 0 2 0", "output": "Polycarp" }, { "input": "1 0 2 1", "output": "Polycarp" }, { "input": "1 0 2 2", "output": "Polycarp" }, { "input": "1 1 0 1", "output": "Vasiliy" }, { "input": "1 1 0 2", "output": "Polycarp" }, { "input": "1 1 1 0", "output": "Vasiliy" }, { "input": "1 1 1 2", "output": "Polycarp" }, { "input": "1 1 2 0", "output": "Polycarp" }, { "input": "1 1 2 1", "output": "Polycarp" }, { "input": "1 1 2 2", "output": "Polycarp" }, { "input": "1 2 0 1", "output": "Vasiliy" }, { "input": "1 2 0 2", "output": "Vasiliy" }, { "input": "1 2 1 0", "output": "Vasiliy" }, { "input": "1 2 1 1", "output": "Vasiliy" }, { "input": "1 2 2 0", "output": "Vasiliy" }, { "input": "1 2 2 1", "output": "Vasiliy" }, { "input": "1 2 2 2", "output": "Polycarp" }, { "input": "2 0 0 1", "output": "Vasiliy" }, { "input": "2 0 0 2", "output": "Polycarp" }, { "input": "2 0 1 0", "output": "Vasiliy" }, { "input": "2 0 1 1", "output": "Vasiliy" }, { "input": "2 0 1 2", "output": "Polycarp" }, { "input": "2 0 2 1", "output": "Polycarp" }, { "input": "2 0 2 2", "output": "Polycarp" }, { "input": "2 1 0 1", "output": "Vasiliy" }, { "input": "2 1 0 2", "output": "Vasiliy" }, { "input": "2 1 1 0", "output": "Vasiliy" }, { "input": "2 1 1 1", "output": "Vasiliy" }, { "input": "2 1 1 2", "output": "Vasiliy" }, { "input": "2 1 2 0", "output": "Vasiliy" }, { "input": "2 1 2 2", "output": "Polycarp" }, { "input": "2 2 0 1", "output": "Vasiliy" }, { "input": "2 2 0 2", "output": "Vasiliy" }, { "input": "2 2 1 0", "output": "Vasiliy" }, { "input": "2 2 1 1", "output": "Vasiliy" }, { "input": "2 2 1 2", "output": "Vasiliy" }, { "input": "2 2 2 0", "output": "Vasiliy" }, { "input": "2 2 2 1", "output": "Vasiliy" }, { "input": "13118 79593 32785 22736", "output": "Vasiliy" }, { "input": "23039 21508 54113 76824", "output": "Polycarp" }, { "input": "32959 49970 75441 55257", "output": "Polycarp" }, { "input": "91573 91885 61527 58038", "output": "Vasiliy" }, { "input": "70620 15283 74892 15283", "output": "Polycarp" }, { "input": "43308 1372 53325 1370", "output": "Polycarp" }, { "input": "74005 7316 74004 7412", "output": "Vasiliy" }, { "input": "53208 42123 95332 85846", "output": "Polycarp" }, { "input": "14969 66451 81419 29039", "output": "Vasiliy" }, { "input": "50042 34493 84536 17892", "output": "Polycarp" }, { "input": "67949 70623 71979 70623", "output": "Polycarp" }, { "input": "67603 35151 67603 39519", "output": "Polycarp" }, { "input": "27149 26539 53690 17953", "output": "Polycarp" }, { "input": "36711 38307 75018 72040", "output": "Polycarp" }, { "input": "4650 67347 71998 50474", "output": "Polycarp" }, { "input": "4075 33738 4561 33738", "output": "Polycarp" }, { "input": "35868 55066 47754 55066", "output": "Polycarp" }, { "input": "41150 1761 41152 1841", "output": "Polycarp" }, { "input": "63557 16718 38133 80275", "output": "Polycarp" }, { "input": "8956 24932 30356 33887", "output": "Polycarp" }, { "input": "27338 8401 27337 12321", "output": "Vasiliy" }, { "input": "56613 48665 66408 48665", "output": "Polycarp" }, { "input": "34750 34886 34751 44842", "output": "Polycarp" }, { "input": "7591 24141 31732 23276", "output": "Polycarp" }, { "input": "2333 91141 93473 66469", "output": "Vasiliy" }, { "input": "9 0 8 0", "output": "Vasiliy" }, { "input": "0 1000 100 99", "output": "Vasiliy" }, { "input": "4 4 2 2", "output": "Vasiliy" }, { "input": "0 4 4 3", "output": "Polycarp" }, { "input": "100 1 1 100", "output": "Vasiliy" }, { "input": "9 17 14 16", "output": "Vasiliy" }, { "input": "0 3 3 1", "output": "Polycarp" }, { "input": "10 0 0 10", "output": "Polycarp" }, { "input": "5 0 0 4", "output": "Vasiliy" }, { "input": "2 1 1 3", "output": "Polycarp" }, { "input": "4 5 5 5", "output": "Polycarp" }, { "input": "0 3 2 2", "output": "Vasiliy" }, { "input": "3 0 0 10", "output": "Polycarp" } ]
61
6,758,400
0
44,793
629
Famil Door and Roads
[ "combinatorics", "data structures", "dfs and similar", "dp", "probabilities", "trees" ]
null
null
Famil Door’s City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are *n* intersections in the city connected by *n*<=-<=1 bidirectional roads. There are *m* friends of Famil Door living in the city. The *i*-th friend lives at the intersection *u**i* and works at the intersection *v**i*. Everyone in the city is unhappy because there is exactly one simple path between their home and work. Famil Door plans to construct exactly one new road and he will randomly choose one among *n*·(*n*<=-<=1)<=/<=2 possibilities. Note, that he may even build a new road between two cities that are already connected by one. He knows, that each of his friends will become happy, if after Famil Door constructs a new road there is a path from this friend home to work and back that doesn't visit the same road twice. Formally, there is a simple cycle containing both *u**i* and *v**i*. Moreover, if the friend becomes happy, his pleasure is equal to the length of such path (it's easy to see that it's unique). For each of his friends Famil Door wants to know his expected pleasure, that is the expected length of the cycle containing both *u**i* and *v**i* if we consider only cases when such a cycle exists.
The first line of the input contains integers *n* and *m* (2<=≤<=*n*,<= *m*<=≤<=100<=000) — the number of the intersections in the Treeland and the number of Famil Door's friends. Then follow *n*<=-<=1 lines describing bidirectional roads. Each of them contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*) — the indices of intersections connected by the *i*-th road. Last *m* lines of the input describe Famil Door's friends. The *i*-th of these lines contain two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*,<=*u**i*<=≠<=*v**i*) — indices of intersections where the *i*-th friend lives and works.
For each friend you should print the expected value of pleasure if he will be happy. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "4 3\n2 4\n4 1\n3 2\n3 1\n2 3\n4 1\n", "3 3\n1 2\n1 3\n1 2\n1 3\n2 3\n" ]
[ "4.00000000\n3.00000000\n3.00000000\n", "2.50000000\n2.50000000\n3.00000000\n" ]
Consider the second sample. 1. Both roads (1, 2) and (2, 3) work, so the expected length if <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ec563337e40cadafa8449c9571eb5b8c7199e10c.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1. Roads (1, 3) and (2, 3) make the second friend happy. Same as for friend 1 the answer is 2.5 1. The only way to make the third friend happy is to add road (2, 3), so the answer is 3
[ { "input": "4 3\n2 4\n4 1\n3 2\n3 1\n2 3\n4 1", "output": "4.00000000\n3.00000000\n3.00000000" }, { "input": "3 3\n1 2\n1 3\n1 2\n1 3\n2 3", "output": "2.50000000\n2.50000000\n3.00000000" }, { "input": "10 5\n8 10\n3 6\n7 3\n7 4\n8 9\n9 4\n5 1\n10 5\n6 2\n9 1\n7 6\n5 1\n6 9\n10 2", "output": "7.50000000\n6.50000000\n6.00000000\n7.50000000\n9.00000000" } ]
46
0
0
44,854
700
Huffman Coding on Segment
[ "data structures", "greedy" ]
null
null
Alice wants to send an important message to Bob. Message *a*<==<=(*a*1,<=...,<=*a**n*) is a sequence of positive integers (characters). To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function *f*, that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters *a**i* and *a**j* string *f*(*a**i*) is not a prefix of *f*(*a**j*) (and vice versa). The result of the encoding of the message *a*1,<=*a*2,<=...,<=*a**n* is the concatenation of the encoding of each character, that is the string *f*(*a*1)*f*(*a*2)... *f*(*a**n*). Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function *f* is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string *f*(*a*1)*f*(*a*2)... *f*(*a**n*). Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings *a**l**i*... *a**r**i* she wants to know the minimum possible length of the Huffman coding. Help her solve this problem.
The first line of the input contains the single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the length of the initial message. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100<=000) — characters of the message. Next line contains the single integer *q* (1<=≤<=*q*<=≤<=100<=000) — the number of queries. Then follow *q* lines with queries descriptions. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the position of the left and right ends of the *i*-th substring respectively. Positions are numbered from 1. Substrings may overlap in any way. The same substring may appear in the input more than once.
Print *q* lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring *a**l**i*... *a**r**i*.
[ "7\n1 2 1 3 1 2 1\n5\n1 7\n1 3\n3 5\n2 4\n4 4\n" ]
[ "10\n3\n3\n5\n0\n" ]
In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11". Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample).
[]
62
512,000
0
44,871
723
Lakes in Berland
[ "dfs and similar", "dsu", "graphs", "greedy", "implementation" ]
null
null
The map of Berland is a rectangle of the size *n*<=×<=*m*, which consists of cells of size 1<=×<=1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell. You task is to fill up with the earth the minimum number of water cells so that there will be exactly *k* lakes in Berland. Note that the initial number of lakes on the map is not less than *k*.
The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=50, 0<=≤<=*k*<=≤<=50) — the sizes of the map and the number of lakes which should be left on the map. The next *n* lines contain *m* characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least *k* lakes.
In the first line print the minimum number of cells which should be transformed from water to land. In the next *n* lines print *m* symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.
[ "5 4 1\n****\n*..*\n****\n**.*\n..**\n", "3 3 0\n***\n*.*\n***\n" ]
[ "1\n****\n*..*\n****\n****\n..**\n", "1\n***\n***\n***\n" ]
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
[ { "input": "5 4 1\n****\n*..*\n****\n**.*\n..**", "output": "1\n****\n*..*\n****\n****\n..**" }, { "input": "3 3 0\n***\n*.*\n***", "output": "1\n***\n***\n***" }, { "input": "3 5 1\n.**.*\n*.*.*\n***..", "output": "0\n.**.*\n*.*.*\n***.." }, { "input": "3 5 0\n.**.*\n*.*.*\n***..", "output": "1\n.**.*\n***.*\n***.." }, { "input": "3 50 7\n***.********.*********************.**********.****\n*...**..*.**.*.*.*.*.*.*.*..*.*.*.*.*.*.*.*.*.*..*\n****************.*.********.**********************", "output": "8\n***.********.*********************.**********.****\n*...**..****.***.*.*******..*******.*.*.*.*.*.*..*\n****************.*.********.**********************" }, { "input": "50 3 4\n***\n*.*\n*.*\n*.*\n***\n***\n*.*\n***\n.**\n***\n..*\n***\n***\n*.*\n***\n*.*\n***\n***\n*.*\n***\n*.*\n*.*\n*.*\n*.*\n***\n*.*\n*.*\n*.*\n*.*\n***\n***\n*.*\n*.*\n*.*\n*.*\n*.*\n***\n***\n***\n*.*\n***\n***\n***\n*.*\n*.*\n*.*\n***\n***\n***\n***", "output": "8\n***\n***\n***\n***\n***\n***\n***\n***\n.**\n***\n..*\n***\n***\n***\n***\n***\n***\n***\n***\n***\n*.*\n*.*\n*.*\n*.*\n***\n*.*\n*.*\n*.*\n*.*\n***\n***\n*.*\n*.*\n*.*\n*.*\n*.*\n***\n***\n***\n***\n***\n***\n***\n*.*\n*.*\n*.*\n***\n***\n***\n***" }, { "input": "1 1 0\n.", "output": "0\n." }, { "input": "1 1 0\n*", "output": "0\n*" } ]
124
4,096,000
3
44,997
763
Timofey and remoduling
[ "brute force", "implementation", "math", "number theory" ]
null
null
Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime *m*. Also, Timofey likes to look for arithmetical progressions everywhere. One of his birthday presents was a sequence of distinct integers *a*1,<=*a*2,<=...,<=*a**n*. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo *m*, or not. Arithmetical progression modulo *m* of length *n* with first element *x* and difference *d* is sequence of integers *x*,<=*x*<=+<=*d*,<=*x*<=+<=2*d*,<=...,<=*x*<=+<=(*n*<=-<=1)·*d*, each taken modulo *m*.
The first line contains two integers *m* and *n* (2<=≤<=*m*<=≤<=109<=+<=7, 1<=≤<=*n*<=≤<=105, *m* is prime) — Timofey's favorite prime module and the length of the sequence. The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=&lt;<=*m*) — the elements of the sequence.
Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo *m*. Otherwise, print two integers — the first element of the obtained progression *x* (0<=≤<=*x*<=&lt;<=*m*) and its difference *d* (0<=≤<=*d*<=&lt;<=*m*). If there are multiple answers, print any of them.
[ "17 5\n0 2 4 13 15\n", "17 5\n0 2 4 13 14\n", "5 3\n1 2 3\n" ]
[ "13 2\n", "-1\n", "3 4\n" ]
none
[ { "input": "17 5\n0 2 4 13 15", "output": "13 2" }, { "input": "17 5\n0 2 4 13 14", "output": "-1" }, { "input": "5 3\n1 2 3", "output": "3 4" }, { "input": "17 8\n7 9 11 13 15 1 3 5", "output": "1 2" }, { "input": "13 2\n0 11", "output": "0 11" }, { "input": "3 1\n0", "output": "0 1" }, { "input": "2 1\n1", "output": "1 1" }, { "input": "241 120\n235 136 237 28 90 60 27 122 239 59 72 46 180 233 182 21 206 153 97 112 94 219 52 109 31 17 8 152 3 118 191 222 214 119 217 39 33 40 1 164 38 173 42 82 130 229 226 73 44 155 85 41 2 132 95 163 192 62 14 210 137 197 135 142 53 154 148 162 140 35 24 106 56 170 93 201 211 147 124 20 177 76 186 228 102 74 129 156 87 51 67 167 178 146 225 111 125 144 45 151 161 22 174 30 188 98 213 199 207 107 209 200 57 65 123 157 6 204 96 66", "output": "-1" }, { "input": "241 120\n114 147 193 143 206 74 60 15 75 197 192 93 115 166 116 88 14 46 156 229 7 5 233 188 111 179 102 73 187 196 57 211 137 134 201 92 29 224 160 2 110 119 184 202 20 174 124 225 97 105 1 216 155 161 19 125 101 205 78 38 178 123 87 16 142 47 28 215 219 23 70 238 120 210 69 10 55 128 25 6 42 169 79 65 56 175 170 107 214 61 43 228 24 237 96 133 34 83 152 220 234 66 52 51 84 183 146 173 33 164 151 11 207 157 165 129 64 138 37 106", "output": "173 191" }, { "input": "17 17\n15 1 6 3 7 8 10 13 12 16 11 9 14 2 0 4 5", "output": "1 1" }, { "input": "7 3\n3 4 1", "output": "-1" }, { "input": "7 3\n4 0 3", "output": "4 3" }, { "input": "17 2\n5 13", "output": "5 8" }, { "input": "17 2\n8 3", "output": "3 5" }, { "input": "17 5\n15 6 9 8 3", "output": "-1" }, { "input": "17 5\n2 5 14 8 11", "output": "2 3" }, { "input": "17 8\n1 10 6 8 2 16 9 15", "output": "-1" }, { "input": "17 8\n6 0 8 3 14 11 9 12", "output": "8 3" }, { "input": "23 1\n4", "output": "4 1" }, { "input": "23 1\n20", "output": "20 1" }, { "input": "23 11\n11 7 15 20 18 5 13 0 8 3 19", "output": "-1" }, { "input": "23 11\n11 4 0 3 2 14 22 12 13 15 1", "output": "22 12" }, { "input": "239 100\n35 96 173 36 220 188 102 227 226 237 16 97 79 211 156 49 146 222 57 209 121 201 166 123 61 29 67 89 184 72 14 47 193 127 165 76 171 30 120 68 108 228 130 151 221 139 214 18 210 169 87 100 13 52 196 148 122 86 131 208 65 194 43 178 9 118 0 24 183 45 134 55 71 212 111 106 170 80 179 233 180 238 235 75 153 105 182 70 197 143 31 207 145 83 20 113 101 190 6 64", "output": "-1" }, { "input": "239 100\n95 225 152 10 96 20 192 51 150 138 105 206 33 41 29 127 53 75 118 9 21 43 119 52 174 139 86 161 207 19 203 215 228 30 66 64 32 44 217 54 131 0 74 117 22 129 109 171 172 141 181 237 107 151 193 140 142 85 55 182 160 42 185 106 11 97 65 163 195 63 128 226 194 184 173 216 84 130 162 98 76 73 120 196 116 40 62 236 218 8 183 238 149 204 205 108 214 227 31 87", "output": "66 76" }, { "input": "239 100\n178 74 144 43 201 189 40 175 51 31 202 114 12 17 86 78 53 196 235 158 95 224 143 198 170 117 79 81 23 197 73 165 133 166 21 50 148 34 121 223 184 45 54 228 9 238 187 19 218 169 104 62 106 46 209 182 221 61 59 48 160 27 212 123 5 107 82 102 208 151 7 180 35 191 11 70 168 18 145 89 193 87 91 215 42 139 0 29 140 69 194 41 153 231 154 111 157 122 119 127", "output": "-1" }, { "input": "239 100\n9 107 11 203 36 34 35 91 20 3 42 19 225 169 99 41 43 28 113 83 193 219 153 129 155 90 123 17 209 97 235 51 73 27 234 138 49 98 81 106 58 186 139 187 226 25 224 18 65 210 131 121 67 82 137 89 60 145 232 227 179 44 33 1 194 59 195 177 2 74 233 170 50 202 10 201 162 52 105 57 26 161 218 146 171 163 66 115 217 154 122 178 130 75 211 4 12 185 147 114", "output": "224 8" }, { "input": "7 4\n5 2 1 4", "output": "2 3" }, { "input": "7 4\n5 2 1 6", "output": "6 3" }, { "input": "17 16\n11 14 7 15 13 6 3 0 4 10 12 1 16 5 8 9", "output": "3 1" }, { "input": "17 16\n10 0 7 5 13 3 16 14 1 6 8 15 2 9 12 11", "output": "5 1" } ]
217
8,089,600
0
45,038
536
Tavas in Kansas
[ "dp", "games" ]
null
null
Tavas lives in Kansas. Kansas has *n* cities numbered from 1 to *n* connected with *m* bidirectional roads. We can travel from any city to any other city via these roads. Kansas is as strange as Tavas. So there may be a road between a city and itself or more than one road between two cities. Tavas invented a game and called it "Dashti". He wants to play Dashti with his girlfriends, Nafas. In this game, they assign an arbitrary integer value to each city of Kansas. The value of *i*-th city equals to *p**i*. During the game, Tavas is in city *s* and Nafas is in city *t*. They play in turn and Tavas goes first. A player in his/her turn, must choose a non-negative integer *x* and his/her score increases by the sum of values of all cities with (shortest) distance no more than *x* from his/her city. Each city may be used once, or in the other words, after first time a player gets score from a city, city score becomes zero. There is an additional rule: the player must choose *x* such that he/she gets the point of at least one city that was not used before. Note that city may initially have value 0, such city isn't considered as been used at the beginning of the game, i. e. each player may use it to fullfill this rule. The game ends when nobody can make a move. A player's score is the sum of the points he/she earned during the game. The winner is the player with greater score, or there is a draw if players score the same value. Both players start game with zero points. If Tavas wins, he'll break his girlfriend's heart, and if Nafas wins, Tavas will cry. But if their scores are equal, they'll be happy and Tavas will give Nafas flowers. They're not too emotional after all, so they'll play optimally. Your task is to tell Tavas what's going to happen after the game ends.
The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2000, *n*<=-<=1<=≤<=*m*<=≤<=105). The second line of input contains two integers *s* and *t* (1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*). The next line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* separated by spaces (|*p**i*|<=≤<=109). The next *m* lines contain the roads. Each line contains three integers *v*,<=*u*,<=*w* and it means that there's an road with length *w* between cities *v* and *u* (1<=≤<=*u*,<=*v*<=≤<=*n* and 0<=≤<=*w*<=≤<=109). The road may lead from the city to itself, there may be several roads between each pair of cities.
If Tavas wins, print "Break a heart". If Nafas wins print "Cry" and if nobody wins (i. e. the game ended with draw) print "Flowers".
[ "4 4\n1 2\n3 2 5 -11\n1 4 2\n3 4 2\n3 1 5\n3 2 1\n", "5 4\n1 2\n2 2 -5 -4 6\n1 2 4\n2 3 5\n2 4 2\n4 5 2\n", "2 1\n1 2\n-5 -5\n1 2 10\n" ]
[ "Cry\n", "Break a heart\n", "Flowers\n" ]
none
[]
0
0
-1
45,103
543
Remembering Strings
[ "bitmasks", "dp" ]
null
null
You have multiset of *n* strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position *i* and some letter *c* of the English alphabet, such that this string is the only string in the multiset that has letter *c* in position *i*. For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because: - the first string is the only string that has character *c* in position 3; - the second string is the only string that has character *d* in position 2; - the third string is the only string that has character *s* in position 2. You want to change your multiset a little so that it is easy to remember. For *a**ij* coins, you can change character in the *j*-th position of the *i*-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.
The first line contains two integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=20) — the number of strings in the multiset and the length of the strings respectively. Next *n* lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is *m*. Next *n* lines contain *m* integers each, the *i*-th of them contains integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=≤<=*a**ij*<=≤<=106).
Print a single number — the answer to the problem.
[ "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n", "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n", "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1\n" ]
[ "3\n", "2\n", "0\n" ]
none
[ { "input": "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1", "output": "3" }, { "input": "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10", "output": "2" }, { "input": "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1", "output": "0" }, { "input": "5 2\naa\naa\nab\nbb\nbb\n1 100\n100 100\n1 1\n100 100\n100 1", "output": "4" }, { "input": "3 3\nabc\nabc\nabc\n1 100 100\n100 1 100\n100 100 1", "output": "3" }, { "input": "18 3\nfbn\nlkj\nilm\ngfl\ndim\nbef\ncfi\nbma\neak\nkab\nbcn\nebc\nmfh\ncgi\ndeb\nfge\nfce\nglg\n543010 452044 432237\n533026 367079 978125\n571867 7573 259887\n523171 80963 129140\n727509 334751 399501\n656779 1472 523915\n803488 31561 922147\n488639 399532 725926\n301194 418928 306345\n500339 934078 810234\n621770 32854 324219\n35994 611153 973418\n22056 398091 505664\n594841 92510 294841\n285643 766895 214579\n789288 110084 241557\n803788 561404 814295\n454273 109684 485963", "output": "482616" }, { "input": "20 2\ned\nci\ngg\nib\nae\ndd\nka\nce\naf\ngb\nag\nke\ngj\nab\nie\nif\ngb\nkd\njg\neg\n52414 63820\n271794 291903\n838068 130592\n833667 287689\n270004 427864\n609044 320678\n358266 462930\n649990 731767\n805366 699807\n346704 829870\n3088 685256\n841621 526249\n880833 98228\n395075 685300\n693991 152955\n203957 482069\n61588 604920\n869639 800204\n460571 166336\n96179 163290", "output": "3086607" }, { "input": "14 4\neeac\neded\ndaea\nbdcc\nddba\nbbed\nbece\nbade\nbcde\naeaa\nbdac\neeeb\nbdcc\nedbb\n581667 582851 517604 898386\n791884 352385 258349 327447\n676328 157596 467774 112698\n45777 747499 235807 527714\n403040 861287 118443 362125\n263912 559519 730246 226455\n253277 156730 908225 39214\n106859 902383 881709 628437\n654953 279126 620775 491356\n326884 277262 143979 572860\n678172 916575 323805 344966\n788158 845192 910173 583941\n793949 346044 197488 869580\n752215 693122 61084 269351", "output": "1464749" }, { "input": "9 6\nfaggcc\nfaaabb\ngcbeab\nfcfccc\nggfbda\ncdfdca\nafgbfe\ngdfabb\ndfceca\n820666 301493 605608 275034 608561 41415\n38810 371880 894378 995636 233081 97716\n729450 719972 502653 951544 136420 297684\n141643 410761 189875 96642 313469 90309\n327239 909031 256744 641859 919511 632267\n274015 319092 647885 117871 244700 23850\n694455 42862 757559 76928 529026 404811\n169808 705232 622067 341005 732346 273726\n902764 775615 14975 694559 746539 949558", "output": "169808" }, { "input": "20 2\nef\naf\njn\nep\nma\nfl\nls\nja\ndf\nmn\noi\nhl\ncp\nki\nsm\nbr\nkh\nbh\nss\nsn\n994001 86606\n449283 850926\n420642 431355\n661713 265900\n311094 693311\n839793 582465\n218329 404955\n826100 437982\n490781 884023\n543933 661317\n732465 182697\n930091 431572\n899562 219773\n183082 851452\n187385 138584\n208770 505758\n32329 783088\n381254 60719\n81287 322375\n613255 515667", "output": "1399835" }, { "input": "14 3\nach\nema\nnia\nhdb\njdg\nmim\nihn\nlnb\nmea\nkbe\ndhf\nfdm\nihg\ndle\n354035 675597 288100\n834061 16574 159776\n624990 190507 971363\n783873 552492 865853\n23679 404670 333364\n419925 809048 493997\n614400 59207 604857\n291672 219123 261918\n812836 873570 862136\n168628 673613 981460\n811468 388422 605280\n625385 834937 352820\n604930 141476 454908\n638553 112928 340904", "output": "331983" }, { "input": "15 2\nfb\nii\nfh\nca\nhh\nid\nee\ndf\ncb\nah\ndd\neg\nfh\nbf\nfa\n760810 556244\n809829 634461\n387951 792353\n418763 335686\n72714 935647\n998352 931571\n933518 39685\n839205 685365\n478841 923156\n136274 626301\n62152 942551\n652306 897289\n985857 313305\n783929 451818\n614967 944901", "output": "2465167" }, { "input": "11 5\ngbacd\nadgcb\nfegcg\nadegd\necbef\ngaefc\ncfedb\naggcc\ncaeee\ngccbf\nbggfe\n25774 574140 364457 220351 328678\n332976 664320 153656 745374 268524\n976642 503249 891782 487119 60381\n342599 115529 926636 508909 173932\n873361 128784 330500 590205 224003\n744056 583317 746463 96586 490744\n576114 237562 483180 227779 850753\n767095 762144 485730 712642 641107\n754411 247671 390338 690181 587283\n127688 79591 62996 314500 601391\n278604 260035 971558 902808 119517", "output": "591667" }, { "input": "1 1\na\n10", "output": "0" }, { "input": "1 2\nav\n10 10", "output": "0" }, { "input": "1 19\naaaaaaaaaaaaaaaaaaa\n774231 725127 560557 436525 348529 840998 841465 338905 610008 346645 913975 616398 718745 2592 2734 126306 120664 449 493046", "output": "0" }, { "input": "20 1\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\n279537\n272065\n761854\n491903\n772493\n866813\n232656\n138771\n331568\n965561\n251399\n68795\n243844\n195442\n763171\n612582\n137653\n698277\n826156\n42642", "output": "7987621" }, { "input": "20 2\naa\nab\nba\nab\nba\nab\nbb\nba\naa\nba\nbb\nab\nab\nab\nba\naa\naa\nbb\nba\nba\n929272 190859\n322406 81721\n397851 916762\n556069 810114\n510543 404765\n6399 192522\n610299 679861\n251511 669405\n313342 57477\n515837 179823\n576007 834991\n750083 929304\n904444 761847\n551657 584593\n402463 483607\n128054 501477\n484233 342956\n779001 138132\n815387 273216\n717650 927079", "output": "7378839" }, { "input": "3 20\naaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaa\n127368 467778 376749 367872 201445 632491 733098 177711 678142 3202 840006 769776 381139 365553 457474 206005 99000 226191 795686 589632\n785172 213443 482364 357721 191016 596022 882906 601573 917829 966436 129825 285602 706196 722082 206626 338767 795346 360434 596067 839588\n350648 821766 692816 249289 50316 993641 378347 192555 299095 692681 595706 90452 626278 159574 303025 263819 544206 818035 213523 721078", "output": "183343" }, { "input": "1 20\naaaaaaaaaaaaaaaaaaaa\n924705 786913 546594 427698 741583 189683 354089 380570 10694 41046 656300 416271 467032 627269 387941 432085 400210 97543 279672 409814", "output": "0" }, { "input": "2 20\naabbaaaaabbbbbbaabbb\naabaabbbabbaabaaabaa\n129031 157657 633540 354251 528473 948025 107960 614275 976567 779835 628647 672528 278433 254595 676151 992850 419435 163397 816037 682878\n97253 367892 938199 150466 687355 886265 900135 530415 409478 505794 468915 83346 765920 348268 600319 334416 410150 728362 239354 368428", "output": "0" } ]
686
21,504,000
3
45,179
533
Landmarks
[ "data structures", "dp" ]
null
null
We have an old building with *n*<=+<=2 columns in a row. These columns support the ceiling. These columns are located in points with coordinates 0<==<=*x*0<=&lt;<=*x*1<=&lt;<=...<=&lt;<=*x**n*<=&lt;<=*x**n*<=+<=1. The leftmost and the rightmost columns are special, we will call them bearing, the other columns are ordinary. For each column we know its durability *d**i*. Let's consider an ordinary column with coordinate *x*. Let's assume that the coordinate of the closest to it column to the left (bearing or ordinary) is *a* and the coordinate of the closest to it column to the right (also, bearing or ordinary) is *b*. In this task let's assume that this column supports the segment of the ceiling from point to point (here both fractions are considered as real division). If the length of the segment of the ceiling supported by the column exceeds *d**i*, then the column cannot support it and it crashes after a while, and after that the load is being redistributeed between the neighbouring columns according to the same principle. Thus, ordinary columns will be crashing for some time until the process stops at some state. One can prove that the set of the remaining columns doesn't depend on the order in which columns crash. If there are only two bearing columns left in the end, then we assume that the whole construction crashes under the weight of the roof. But if at least one ordinary column stays in addition to the bearing ones, then the building doesn't crash. To make the building stronger, we can add one extra ordinary column of arbitrary durability *d*' at any (not necessarily integer) point 0<=&lt;<=*x*'<=&lt;<=*x**n*<=+<=1. If point *x*' is already occupied by an ordinary column, it is replaced by a new one. Your task is to find out: what minimal durability can the added column have so that the building doesn't crash?
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of ordinary columns. The second line contains *n*<=+<=2 integers *x*0,<=*x*1,<=...,<=*x**n*,<=*x**n*<=+<=1 (*x*0<==<=0, *x**i*<=&lt;<=*x**i*<=+<=1 for 0<=≤<=*i*<=≤<=*n*, *x**n*<=+<=1<=≤<=109) — the coordinates of the columns. The third line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=≤<=*d**i*<=≤<=109).
Print a single number — the minimum possible durability of the column that you need to add in order to make the building stay. If you do not have to add the column, please print 0. Your answer will be checked with the relative or absolute error 10<=-<=4.
[ "2\n0 20 40 100\n15 40\n", "3\n0 4 10 28 30\n9 13 5\n" ]
[ "10\n", "0\n" ]
none
[]
30
0
0
45,385
69
Bets
[ "greedy", "implementation" ]
B. Bets
2
256
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname "Boss". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor. To begin with friends learned the rules: in the race there are *n* sections of equal length and *m* participants. The participants numbered from 1 to *m*. About each participant the following is known: - *l**i* — the number of the starting section, - *r**i* — the number of the finishing section (*l**i*<=≤<=*r**i*),- *t**i* — the time a biathlete needs to complete an section of the path,- *c**i* — the profit in roubles. If the *i*-th sportsman wins on one of the sections, the profit will be given to the man who had placed a bet on that sportsman. The *i*-th biathlete passes the sections from *l**i* to *r**i* inclusive. The competitor runs the whole way in (*r**i*<=-<=*l**i*<=+<=1)·*t**i* time units. It takes him exactly *t**i* time units to pass each section. In case of the athlete's victory on *k* sections the man who has betted on him receives *k*·*c**i* roubles. In each section the winner is determined independently as follows: if there is at least one biathlete running this in this section, then among all of them the winner is the one who has ran this section in minimum time (spent minimum time passing this section). In case of equality of times the athlete with the smaller index number wins. If there are no participants in this section, then the winner in this section in not determined. We have to say that in the summer biathlon all the participants are moving at a constant speed. We should also add that Nikita can bet on each section and on any contestant running in this section. Help the friends find the maximum possible profit.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100). Then follow *m* lines, each containing 4 integers *l**i*, *r**i*, *t**i*, *c**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, 1<=≤<=*t**i*,<=*c**i*<=≤<=1000).
Print a single integer, the maximal profit in roubles that the friends can get. In each of *n* sections it is not allowed to place bets on more than one sportsman.
[ "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20\n", "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20\n" ]
[ "60", "105" ]
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles. In the second test the optimal bet is: on 1 and 5 sections on biathlete 1, in the 2-4 sections on biathlete 2, in the 6-7 sections on athlete 4. There is no winner in the 8 section. Total: profit of 10 rubles for 1 section, the profit of 15 rubles for 2,3,4 section, profit of 10 rubles for a 5 section, profit of 20 rubles for 6, 7 section. Total profit 105 rubles.
[ { "input": "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20", "output": "60" }, { "input": "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20", "output": "105" }, { "input": "2 2\n1 2 3 1\n2 2 3 10", "output": "2" }, { "input": "20 30\n15 17 54 46\n4 18 26 18\n18 20 49 94\n12 12 83 12\n11 13 88 47\n8 8 8 12\n18 18 94 2\n14 17 88 96\n19 19 62 97\n1 12 81 67\n10 12 78 26\n19 20 63 93\n9 20 38 32\n7 9 90 17\n9 10 19 60\n16 16 47 29\n1 6 62 29\n12 18 74 89\n5 5 97 92\n5 17 16 25\n11 19 2 76\n3 15 61 29\n5 7 73 54\n19 20 91 91\n4 17 28 61\n9 13 56 81\n10 11 82 80\n10 11 82 70\n5 10 66 38\n10 19 22 61", "output": "958" }, { "input": "20 30\n4 13 78 11\n13 19 56 41\n15 15 46 83\n4 9 74 72\n17 20 97 7\n15 20 29 48\n8 17 44 85\n4 18 26 46\n16 17 9 90\n16 16 39 89\n13 14 46 63\n14 18 67 18\n12 20 84 48\n10 20 49 32\n10 14 14 11\n6 18 80 84\n3 20 13 97\n12 20 62 42\n12 14 64 71\n5 19 38 17\n17 18 99 18\n11 15 83 22\n4 11 65 99\n8 16 89 45\n11 20 15 39\n8 13 85 26\n5 19 84 3\n10 16 26 45\n13 16 81 37\n3 5 100 42", "output": "1732" }, { "input": "20 30\n4 12 30 83\n3 3 91 46\n5 11 82 84\n20 20 29 36\n3 13 89 29\n11 14 40 80\n9 20 90 21\n14 19 23 74\n8 9 13 88\n12 18 13 95\n13 18 48 29\n8 17 13 15\n7 15 18 51\n9 20 87 51\n12 20 40 32\n4 11 34 11\n3 19 22 20\n19 19 53 5\n16 18 52 30\n5 19 52 71\n19 19 99 95\n14 18 15 28\n20 20 91 64\n15 16 55 47\n1 9 40 40\n9 17 93 82\n7 16 10 75\n1 15 100 24\n10 10 35 84\n1 2 4 7", "output": "1090" }, { "input": "20 30\n20 20 43 41\n5 13 99 35\n9 15 79 12\n4 20 75 16\n20 20 4 94\n14 14 1 1\n5 5 4 92\n14 19 52 30\n19 20 61 14\n10 12 34 89\n11 15 27 12\n14 18 64 25\n11 14 37 14\n19 19 56 20\n19 20 61 11\n13 16 48 36\n14 16 82 73\n16 17 82 26\n1 5 55 91\n10 13 24 33\n3 19 91 70\n10 15 87 53\n3 5 92 80\n10 10 13 24\n9 9 38 20\n13 20 80 38\n5 10 71 23\n6 19 43 90\n13 20 10 55\n11 14 29 62", "output": "1261" }, { "input": "20 30\n15 15 14 51\n17 20 3 20\n14 16 59 66\n14 15 48 22\n18 19 72 26\n13 14 60 72\n8 13 69 57\n4 12 3 82\n1 8 80 37\n18 19 40 33\n9 9 32 55\n13 15 67 5\n10 13 37 1\n19 19 39 11\n17 19 28 88\n8 19 88 87\n16 20 26 2\n18 18 11 46\n14 14 14 20\n15 15 78 100\n18 19 53 32\n12 13 59 66\n11 18 38 36\n5 8 14 97\n8 18 80 97\n6 19 81 17\n13 19 65 93\n8 10 77 3\n20 20 70 60\n17 17 28 35", "output": "1003" }, { "input": "20 30\n5 10 38 50\n17 18 86 42\n4 13 91 90\n20 20 45 31\n3 3 16 11\n16 16 80 66\n19 19 96 26\n15 20 7 84\n9 18 45 36\n5 19 89 6\n9 9 4 58\n9 14 97 31\n6 12 74 90\n4 5 84 2\n12 19 92 48\n16 16 92 55\n9 15 88 38\n6 14 8 66\n14 17 71 91\n20 20 58 20\n8 18 5 47\n7 19 67 43\n19 19 88 80\n9 12 35 86\n4 4 82 22\n7 8 72 82\n8 10 61 92\n20 20 77 93\n15 19 66 20\n20 20 8 10", "output": "911" }, { "input": "20 30\n1 20 49 91\n15 15 60 37\n14 14 3 79\n11 12 81 66\n8 12 71 31\n3 13 8 14\n2 10 11 35\n19 20 40 28\n12 14 6 75\n16 18 100 100\n20 20 89 74\n16 16 27 98\n18 18 21 24\n1 18 69 98\n7 13 70 57\n9 20 41 79\n17 17 75 75\n11 16 19 14\n1 15 62 59\n12 15 33 91\n3 17 10 79\n15 16 100 80\n10 16 5 5\n9 19 53 100\n9 18 65 42\n5 13 34 13\n6 13 61 47\n17 19 68 25\n5 5 42 59\n6 8 48 92", "output": "492" }, { "input": "100 1\n22 59 287 173", "output": "6574" }, { "input": "100 1\n8 31 93 267", "output": "6408" }, { "input": "100 1\n72 82 727 390", "output": "4290" }, { "input": "100 1\n14 25 343 50", "output": "600" }, { "input": "100 1\n73 75 59 176", "output": "528" }, { "input": "21 2\n19 20 253 233\n3 17 23 150", "output": "2716" }, { "input": "47 18\n14 38 376 96\n21 34 749 32\n7 20 409 146\n41 41 740 9\n14 16 526 128\n38 47 518 147\n3 26 7 16\n25 31 155 75\n32 36 164 12\n5 33 436 150\n22 41 477 52\n4 13 166 6\n38 44 664 133\n2 33 452 16\n30 40 623 48\n37 37 250 122\n25 34 506 109\n36 38 716 78", "output": "2091" }, { "input": "1 1\n1 1 1 1", "output": "1" }, { "input": "1 1\n1 1 1000 1000", "output": "1000" } ]
92
0
3.977
45,394
0
none
[ "none" ]
null
null
A newspaper is published in Walrusland. Its heading is *s*1, it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a new word *s*2. It is considered that when Fangy erases some letter, there's no whitespace formed instead of the letter. That is, the string remains unbroken and it still only consists of lowercase Latin letters. For example, the heading is "abc". If we take two such headings and glue them one to the other one, we get "abcabc". If we erase the letters on positions 1 and 5, we get a word "bcac". Which least number of newspaper headings *s*1 will Fangy need to glue them, erase several letters and get word *s*2?
The input data contain two lines. The first line contain the heading *s*1, the second line contains the word *s*2. The lines only consist of lowercase Latin letters (1<=≤<=|*s*1|<=≤<=104,<=1<=≤<=|*s*2|<=≤<=106).
If it is impossible to get the word *s*2 in the above-described manner, print "-1" (without the quotes). Otherwise, print the least number of newspaper headings *s*1, which Fangy will need to receive the word *s*2.
[ "abc\nxyz\n", "abcd\ndabc\n" ]
[ "-1\n", "2\n" ]
none
[ { "input": "abc\nxyz", "output": "-1" }, { "input": "abcd\ndabc", "output": "2" }, { "input": "ab\nbabaaab", "output": "5" }, { "input": "ab\nbaaabba", "output": "6" }, { "input": "fbaaigiihhfaahgdbddgeggjdeigfadhfddja\nhbghjgijijcdafcbgiedichdeebaddfddb", "output": "-1" }, { "input": "ibifgcfdbfdhihbifageaaadegbfbhgeebgdgiafgedchdg\ndedfebcfdigdefdediigcfcafbhhiacgfbeccfchd", "output": "7" }, { "input": "fcagdciidcedeaicgfffgjefaefaachfbfj\naiecchjehdgbjfcdjdefgfhiddjajeddiigidaibejabd", "output": "11" }, { "input": "ehfjaabjfedhddejjfcfijagefhjeahjcddhchahjbagi\nfbfdjbjhibjgjgaaajgdbcfdbhjcajcbbieijhcjgajhgaa", "output": "10" }, { "input": "ifjcedhjhdjhbiaededfefagigggcebfaebf\ngeibbffgeefbaghdbfidbbhabdbdgej", "output": "9" }, { "input": "eidfdfbbgjigghaddbjhcbdechecgghjgfjjcajjhaghdhd\nfcgecaefhgjjegbhjeaffcabifihhcadaibhi", "output": "11" } ]
186
10,240,000
-1
45,440
620
Professor GukiZ and Two Arrays
[ "binary search", "two pointers" ]
null
null
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a *s**a* as close as possible to the sum of the elements in the array b *s**b*. So he wants to minimize the value *v*<==<=|*s**a*<=-<=*s**b*|. In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5,<=1,<=3,<=2,<=4] and the array b is [3,<=3,<=2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2,<=1,<=3,<=2,<=4] and the new array b [3,<=3,<=5]. Professor doesn't want to make more than two swaps. Find the minimal value *v* and some sequence of no more than two swaps that will lead to the such value *v*. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
The first line contains integer *n* (1<=≤<=*n*<=≤<=2000) — the number of elements in the array a. The second line contains *n* integers *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109) — the elements of the array a. The third line contains integer *m* (1<=≤<=*m*<=≤<=2000) — the number of elements in the array b. The fourth line contains *m* integers *b**j* (<=-<=109<=≤<=*b**j*<=≤<=109) — the elements of the array b.
In the first line print the minimal value *v*<==<=|*s**a*<=-<=*s**b*| that can be got with no more than two swaps. The second line should contain the number of swaps *k* (0<=≤<=*k*<=≤<=2). Each of the next *k* lines should contain two integers *x**p*,<=*y**p* (1<=≤<=*x**p*<=≤<=*n*,<=1<=≤<=*y**p*<=≤<=*m*) — the index of the element in the array a and the index of the element in the array b in the *p*-th swap. If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
[ "5\n5 4 3 2 1\n4\n1 1 1 1\n", "5\n1 2 3 4 5\n1\n15\n", "5\n1 2 3 4 5\n4\n1 2 3 4\n" ]
[ "1\n2\n1 1\n4 2\n", "0\n0\n", "1\n1\n3 1\n" ]
none
[ { "input": "5\n5 4 3 2 1\n4\n1 1 1 1", "output": "1\n2\n1 1\n4 2" }, { "input": "5\n1 2 3 4 5\n1\n15", "output": "0\n0" }, { "input": "5\n1 2 3 4 5\n4\n1 2 3 4", "output": "1\n1\n3 1" }, { "input": "1\n-42\n1\n-86", "output": "44\n0" }, { "input": "1\n-21\n10\n-43 6 -46 79 -21 93 -36 -38 -67 1", "output": "1\n1\n1 3" }, { "input": "10\n87 -92 -67 -100 -88 80 -82 -59 81 -72\n10\n-50 30 30 77 65 92 -60 -76 -29 -15", "output": "0\n2\n4 4\n9 6" }, { "input": "6\n1 2 3 4 5 11\n1\n3", "output": "7\n1\n6 1" }, { "input": "2\n-2 -17\n2\n11 -9", "output": "5\n1\n1 1" } ]
30
0
0
45,472
671
Robin Hood
[ "binary search", "greedy" ]
null
null
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor. There are *n* citizens in Kekoland, each person has *c**i* coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in *k* days. He decided to spend these last days with helping poor people. After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too. Your task is to find the difference between richest and poorest persons wealth after *k* days. Note that the choosing at random among richest and poorest doesn't affect the answer.
The first line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=500<=000,<=0<=≤<=*k*<=≤<=109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement. The second line contains *n* integers, the *i*-th of them is *c**i* (1<=≤<=*c**i*<=≤<=109) — initial wealth of the *i*-th person.
Print a single line containing the difference between richest and poorest peoples wealth.
[ "4 1\n1 1 4 2\n", "3 1\n2 2 2\n" ]
[ "2\n", "0\n" ]
Lets look at how wealth changes through day in the first sample. 1. [1, 1, 4, 2] 1. [2, 1, 3, 2] or [1, 2, 3, 2] So the answer is 3 - 1 = 2 In second sample wealth will remain the same for each person.
[ { "input": "4 1\n1 1 4 2", "output": "2" }, { "input": "3 1\n2 2 2", "output": "0" }, { "input": "10 20\n6 4 7 10 4 5 5 3 7 10", "output": "1" }, { "input": "30 7\n3 3 2 2 2 2 3 4 4 5 2 1 1 5 5 3 4 3 2 1 3 4 3 2 2 5 2 5 1 2", "output": "2" }, { "input": "2 0\n182 2", "output": "180" }, { "input": "123 54564\n38 44 41 42 59 3 95 15 45 32 44 69 35 83 94 57 65 85 64 47 24 20 34 86 26 91 98 12 36 96 80 4 70 40 95 38 70 22 58 50 34 84 80 45 14 60 61 43 11 56 19 59 50 63 21 15 97 98 27 13 9 71 32 18 90 10 2 99 75 87 74 83 79 37 89 3 49 27 92 95 49 1 26 50 72 75 81 37 60 98 28 28 10 93 99 63 14 26 69 51 47 59 42 7 20 17 75 44 44 20 44 85 27 32 65 95 47 46 12 22 64 77 21", "output": "1" }, { "input": "111 10\n2 8 6 1 3 5 8 3 8 2 9 9 6 9 8 8 5 2 3 8 8 3 8 3 7 9 4 3 9 7 1 8 3 1 5 5 5 8 2 4 2 7 9 1 4 4 3 1 6 7 7 4 1 3 5 3 9 4 4 4 8 8 7 3 5 6 3 3 8 2 8 4 5 8 1 8 4 1 7 1 4 9 8 9 7 6 5 6 3 7 4 8 9 3 8 9 9 3 5 9 1 3 6 8 9 1 1 3 8 7 6", "output": "8" }, { "input": "10 1000000\n307196 650096 355966 710719 99165 959865 500346 677478 614586 6538", "output": "80333" }, { "input": "5 1000000\n145119584 42061308 953418415 717474449 57984109", "output": "909357107" }, { "input": "100 20\n2 5 3 3 2 7 6 2 2 2 6 7 2 1 8 10 2 4 10 6 10 2 1 1 4 7 1 2 9 7 5 3 7 4 6 3 10 10 3 7 6 8 2 2 10 3 1 2 1 3 1 6 3 1 4 10 3 10 9 5 10 4 3 10 3 3 5 3 10 2 1 5 10 7 8 7 7 2 4 2 1 3 3 8 8 5 7 3 1 1 8 10 5 7 4 4 7 7 1 9", "output": "7" }, { "input": "10 1000\n1000000000 999999994 999999992 1000000000 999999994 999999999 999999990 999999997 999999995 1000000000", "output": "1" }, { "input": "2 100000\n1 3", "output": "0" }, { "input": "4 0\n1 4 4 4", "output": "3" }, { "input": "4 42\n1 1 1 1000000000", "output": "999999943" }, { "input": "3 4\n1 2 7", "output": "1" }, { "input": "4 100\n1 1 10 10", "output": "1" } ]
109
0
0
45,480
41
Pawn
[ "dp" ]
D. Pawn
2
256
On some square in the lowest row of a chessboard a stands a pawn. It has only two variants of moving: upwards and leftwards or upwards and rightwards. The pawn can choose from which square of the lowest row it can start its journey. On each square lay from 0 to 9 peas. The pawn wants to reach the uppermost row having collected as many peas as possible. As there it will have to divide the peas between itself and its *k* brothers, the number of peas must be divisible by *k*<=+<=1. Find the maximal number of peas it will be able to collect and which moves it should make to do it. The pawn cannot throw peas away or leave the board. When a pawn appears in some square of the board (including the first and last square of the way), it necessarily takes all the peas.
The first line contains three integers *n*, *m*, *k* (2<=≤<=*n*,<=*m*<=≤<=100,<=0<=≤<=*k*<=≤<=10) — the number of rows and columns on the chessboard, the number of the pawn's brothers. Then follow *n* lines containing each *m* numbers from 0 to 9 without spaces — the chessboard's description. Each square is described by one number — the number of peas in it. The first line corresponds to the uppermost row and the last line — to the lowest row.
If it is impossible to reach the highest row having collected the number of peas divisible by *k*<=+<=1, print -1. Otherwise, the first line must contain a single number — the maximal number of peas the pawn can collect given that the number must be divisible by *k*<=+<=1. The second line must contain a single number — the number of the square's column in the lowest row, from which the pawn must start its journey. The columns are numbered from the left to the right with integral numbers starting from 1. The third line must contain a line consisting of *n*<=-<=1 symbols — the description of the pawn's moves. If the pawn must move upwards and leftwards, print L, if it must move upwards and rightwards, print R. If there are several solutions to that problem, print any of them.
[ "3 3 1\n123\n456\n789\n", "3 3 0\n123\n456\n789\n", "2 2 10\n98\n75\n" ]
[ "16\n2\nRL\n", "17\n3\nLR\n", "-1\n" ]
none
[ { "input": "3 3 1\n123\n456\n789", "output": "16\n2\nRL" }, { "input": "3 3 0\n123\n456\n789", "output": "17\n3\nLR" }, { "input": "2 2 10\n98\n75", "output": "-1" }, { "input": "3 4 2\n8244\n4768\n4474", "output": "18\n3\nLR" }, { "input": "4 3 10\n194\n707\n733\n633", "output": "22\n3\nLLR" }, { "input": "5 6 0\n564132\n152314\n382748\n956060\n261008", "output": "31\n2\nLRRL" }, { "input": "2 4 2\n3916\n9593", "output": "18\n3\nL" }, { "input": "5 5 6\n78237\n84658\n09523\n48007\n70591", "output": "21\n3\nRLLL" }, { "input": "6 6 0\n962504\n555826\n306365\n336593\n304184\n461978", "output": "42\n4\nRLLLL" }, { "input": "7 7 8\n9178611\n1154936\n5736233\n3683401\n5972844\n1538360\n8915609", "output": "45\n4\nLLRLRR" }, { "input": "10 5 5\n57903\n23822\n16074\n14758\n17503\n85862\n22741\n24624\n91349\n59483", "output": "60\n5\nLLRLLRLRL" }, { "input": "3 10 5\n5982103711\n7068791203\n1573073434", "output": "18\n4\nLL" }, { "input": "22 13 9\n8184281791532\n5803370774001\n6603582781635\n2483939348867\n0830296902280\n3551639607305\n3444831623227\n3091545622824\n6913003961993\n3133646154943\n1940360624827\n6753210603109\n0151850545919\n3740837541625\n5803839641354\n8646937392812\n0603155734470\n7315747209948\n5161762550888\n5911134989142\n5126602312630\n9357303282764", "output": "140\n11\nLLLLLLRRLLRRRLRLLRRRL" }, { "input": "14 23 8\n68504025976030072501641\n56458987321578480010382\n46062870778554718112548\n81908609966761024372750\n76848590874509200408274\n37285110415074847067321\n66805521560779398220121\n50385391753925080239043\n49514980743485792107357\n72577075816570740728649\n39689681512498117328584\n91073140452682825237396\n40514188871545939304976\n13697029058487784430451", "output": "99\n8\nLLLLRLLRRRRRR" }, { "input": "23 2 6\n00\n47\n52\n36\n01\n01\n39\n04\n69\n93\n77\n72\n33\n95\n13\n50\n23\n48\n79\n98\n05\n63\n17", "output": "-1" }, { "input": "23 2 6\n00\n47\n52\n36\n01\n01\n39\n04\n69\n93\n77\n72\n33\n95\n13\n50\n23\n48\n79\n98\n05\n63\n17", "output": "-1" }, { "input": "2 2 3\n15\n52", "output": "-1" }, { "input": "2 2 0\n02\n64", "output": "8\n1\nR" }, { "input": "2 2 9\n82\n68", "output": "-1" }, { "input": "40 10 1\n3662957315\n8667652926\n0833069659\n7030124763\n0285674766\n3253847205\n3183518599\n6584668288\n6016531609\n4094512804\n8169065529\n5526028299\n1251249986\n3970729176\n7534232301\n4643554614\n8544233598\n3618335000\n4458737272\n2014874848\n2052050286\n2523863039\n3367463306\n7570627477\n6504863662\n5673627493\n9683553049\n5087433832\n4895351652\n8976415673\n7744852982\n8880573285\n8601062585\n9914945591\n6101306342\n4477024828\n6711693809\n9518645171\n0320790840\n1660676034", "output": "258\n7\nLLRRRRLRLRLRRLLLLLRLLRLRRRRRRLRLLLLRLLR" }, { "input": "100 2 7\n18\n70\n19\n42\n74\n37\n47\n43\n71\n66\n25\n64\n60\n45\n90\n54\n38\n35\n92\n79\n19\n94\n76\n61\n30\n49\n95\n72\n57\n05\n71\n10\n18\n40\n63\n01\n75\n44\n65\n47\n27\n37\n84\n30\n06\n15\n55\n19\n49\n00\n80\n77\n20\n78\n33\n67\n29\n20\n98\n28\n19\n00\n42\n88\n11\n58\n57\n69\n58\n92\n90\n73\n65\n09\n85\n08\n93\n83\n38\n54\n41\n20\n66\n99\n41\n01\n91\n91\n39\n60\n66\n82\n77\n25\n02\n55\n32\n64\n56\n30", "output": "-1" }, { "input": "100 3 4\n644\n861\n478\n250\n560\n998\n141\n162\n386\n778\n123\n811\n602\n533\n391\n515\n898\n215\n965\n556\n446\n883\n256\n195\n573\n889\n515\n240\n179\n339\n258\n593\n930\n730\n735\n949\n522\n067\n549\n366\n452\n405\n473\n188\n488\n994\n000\n046\n930\n217\n897\n580\n509\n032\n343\n722\n176\n925\n728\n717\n851\n925\n901\n665\n469\n029\n264\n801\n841\n196\n415\n923\n390\n832\n322\n616\n074\n238\n927\n350\n952\n060\n575\n355\n307\n971\n787\n796\n822\n080\n265\n609\n389\n851\n533\n061\n424\n517\n498\n623", "output": "545\n1\nRLRRLLRRLLRLRLRRLLRLRRLLRLRLRRLLRRLLRLRLRLRLRRLLRRLRLLRRLRLRLRLLRLRLRRLLRRLRLLRRLRLLRLRLRRLRLLRLRLR" }, { "input": "2 100 7\n9360286883185741015657297578030499122983212716269549322423994405864643235893094083435861617948832932\n6890233971690210653206070772331643207659524060492980513060276541348578771750981091169346350950048601", "output": "16\n92\nL" }, { "input": "3 100 2\n9274856291089022402330510299964972078374631084698909589848378120688833406094439833480605688734822538\n8559432619222401260831250315191045571941748630289435997646309147962549951488150729159571611224761557\n7459677994197468453434072563284883271493313149578657711970598092555372522009834711876673556425273784", "output": "27\n9\nRL" }, { "input": "4 100 8\n8197214719753093689382933229185566015858043325014460546254750743412353547105592762535428651419733324\n9148500337546694884364549640851337857223054489296090301133259534376331231215539538042806982497493773\n8861823647111079235007692880873989283264269770396047900111206380618089276133969173551645794471217161\n7380214222723596011942700126524470827522028978818427297837353995903366375498632353149447411505503535", "output": "27\n91\nLLL" }, { "input": "3 10 0\n1230000123\n4560000456\n7890000789", "output": "17\n10\nLR" }, { "input": "100 2 1\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99\n99", "output": "900\n2\nLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL" }, { "input": "2 100 5\n9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999\n9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", "output": "18\n100\nL" }, { "input": "100 2 8\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00\n00", "output": "0\n2\nLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRLRL" }, { "input": "2 100 2\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "0\n100\nL" } ]
92
4,915,200
0
45,563
922
Birds
[ "dp" ]
null
null
Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp needs strong magic. There are *n* trees in a row on an alley in a park, there is a nest on each of the trees. In the *i*-th nest there are *c**i* birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him *cost**i* points of mana. However, for each bird summoned, Imp increases his mana capacity by *B* points. Imp summons birds one by one, he can summon any number from 0 to *c**i* birds from the *i*-th nest. Initially Imp stands under the first tree and has *W* points of mana, and his mana capacity equals *W* as well. He can only go forward, and each time he moves from a tree to the next one, he restores *X* points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?
The first line contains four integers *n*, *W*, *B*, *X* (1<=≤<=*n*<=≤<=103,<=0<=≤<=*W*,<=*B*,<=*X*<=≤<=109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (0<=≤<=*c**i*<=≤<=104) — where *c**i* is the number of birds living in the *i*-th nest. It is guaranteed that . The third line contains *n* integers *cost*1,<=*cost*2,<=...,<=*cost**n* (0<=≤<=*cost**i*<=≤<=109), where *cost**i* is the mana cost to summon a bird from the *i*-th nest.
Print a single integer — the maximum number of birds Imp can summon.
[ "2 12 0 4\n3 4\n4 2\n", "4 1000 10 35\n1 2 4 5\n1000 500 250 200\n", "2 10 7 11\n2 10\n6 1\n" ]
[ "6\n", "5\n", "11\n" ]
In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since *B* = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6. In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.
[ { "input": "2 12 0 4\n3 4\n4 2", "output": "6" }, { "input": "4 1000 10 35\n1 2 4 5\n1000 500 250 200", "output": "5" }, { "input": "2 10 7 11\n2 10\n6 1", "output": "11" }, { "input": "5 1 4 6\n3 4 6 5 1\n3 0 10 2 9", "output": "10" }, { "input": "75 262 870 596\n22 13 37 6 15 25 5 1 2 13 6 3 14 7 8 15 6 15 10 3 1 21 33 2 7 14 17 7 4 9 16 29 31 4 31 16 3 11 13 38 3 14 8 18 9 3 1 10 1 5 10 16 7 5 33 9 24 9 8 13 3 31 8 18 33 17 22 15 34 22 14 23 9 11 1\n15 988 494 954 185 43 341 392 828 364 464 33 443 279 410 559 648 522 221 630 446 910 720 680 299 949 898 528 344 530 33 705 562 718 378 603 752 729 874 835 271 809 687 485 383 203 211 967 937 641 222 519 30 437 573 94 818 740 711 628 631 699 803 289 804 722 986 917 286 358 564 740 280 625 104", "output": "221" }, { "input": "1 1000000000 1000000000 1000000000\n10000\n1000000000", "output": "1" }, { "input": "11 182265379 943880823 541186655\n118 395 182 912 3055 1198 972 499 1295 423 951\n919515674 183547374 713185799 495395101 460570056 865323757 856004539 172194086 29017055 573281401 94884106", "output": "30" }, { "input": "30 813763087 667301398 788083179\n358 1 942 255 371 86 22 313 62 220 227 301 164 238 30 18 26 517 1904 293 110 1171 709 412 221 27 355 386 180 81\n364514724 369447241 943009487 777239405 675813054 769495455 824525249 132802164 98954464 131625205 59567398 4498864 777027802 875631890 510915516 4505995 568141671 175640086 253104977 173981164 846365879 8456432 819645904 478042655 137080723 401949614 630869423 140199951 566654245 49531216", "output": "1648" }, { "input": "29 248541711 775857221 991640811\n456 358 735 868 213 24 583 174 381 1357 63 180 903 180 106 56 166 479 434 171 111 200 499 110 16 23 371 525 258\n510246598 566510289 914900360 823548934 762398202 571897078 102256870 717967541 568392967 330484503 755710752 232910352 371570614 781299711 733550623 337438470 36696592 158396947 254877244 719491526 148294199 754511298 655210216 895287270 535726298 235308442 905006463 142116705 96440782", "output": "313" }, { "input": "17 839860324 388827729 364896640\n417 2 8 930 648 3105 434 435 1 1450 58 50 277 646 221 274 1044\n416664151 309840428 823560106 653486427 281743349 81844030 406321182 683147664 445891025 323452426 204464623 21209374 112902821 403197845 977337836 263883864 161229864", "output": "95" } ]
1,000
90,214,400
0
45,581
743
Vladik and cards
[ "binary search", "bitmasks", "brute force", "dp" ]
null
null
Vladik was bored on his way home and decided to play the following game. He took *n* cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions: - the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are *c**k* cards with number *k* on them in the subsequence, than for all pairs of integers the condition |*c**i*<=-<=*c**j*|<=≤<=1 must hold. - if there is at least one card with number *x* on it in the subsequence, then all cards with number *x* in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1,<=1,<=2,<=2] satisfies this condition while the subsequence [1,<=2,<=2,<=1] doesn't. Note that [1,<=1,<=2,<=2] doesn't satisfy the first condition. Please help Vladik to find the length of the longest subsequence that satisfies both conditions.
The first line contains single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of cards in Vladik's sequence. The second line contains the sequence of *n* positive integers not exceeding 8 — the description of Vladik's sequence.
Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.
[ "3\n1 1 1\n", "8\n8 7 6 5 4 3 2 1\n", "24\n1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8\n" ]
[ "1", "8", "17" ]
In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
[ { "input": "3\n1 1 1", "output": "1" }, { "input": "8\n8 7 6 5 4 3 2 1", "output": "8" }, { "input": "24\n1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8", "output": "17" }, { "input": "1\n8", "output": "1" }, { "input": "2\n5 4", "output": "2" }, { "input": "3\n3 3 2", "output": "2" }, { "input": "18\n3 6 6 8 8 1 1 4 4 3 3 5 5 7 7 2 2 3", "output": "16" }, { "input": "5\n2 6 1 2 6", "output": "3" }, { "input": "6\n4 3 1 6 7 4", "output": "5" }, { "input": "7\n8 8 2 6 1 8 5", "output": "5" }, { "input": "8\n2 8 4 7 5 3 6 1", "output": "8" }, { "input": "8\n8 6 3 6 7 5 5 3", "output": "5" }, { "input": "15\n5 2 2 7 5 2 6 4 3 8 1 8 4 2 7", "output": "9" }, { "input": "15\n8 8 1 6 2 2 4 5 4 2 4 8 2 5 2", "output": "6" }, { "input": "16\n8 2 1 5 7 6 2 5 4 4 8 2 2 6 3 8", "output": "10" }, { "input": "16\n2 2 8 8 5 5 3 3 7 7 1 1 6 6 4 4", "output": "16" }, { "input": "18\n4 3 3 3 7 7 5 2 1 1 3 3 6 1 2 4 1 8", "output": "11" }, { "input": "30\n5 5 4 8 6 6 7 7 8 2 2 2 1 4 4 4 8 8 6 3 5 7 7 3 7 1 6 1 1 8", "output": "19" }, { "input": "30\n1 7 2 2 2 3 1 1 1 3 7 3 7 3 7 7 1 7 6 6 6 5 5 5 4 4 4 8 8 8", "output": "24" }, { "input": "120\n6 7 8 5 2 8 5 4 6 4 3 2 5 6 5 7 5 7 1 7 4 6 4 1 4 1 1 7 6 7 3 7 4 7 4 6 4 7 6 6 6 5 5 7 3 5 3 7 2 2 4 2 5 6 8 4 1 2 2 8 3 3 2 5 6 4 3 6 2 4 1 4 2 8 8 3 7 6 4 7 2 7 3 3 8 8 6 8 7 7 6 8 3 2 5 2 6 5 7 5 7 5 3 2 6 2 6 5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 5 5 5 5 5 5 5 5 5 5 5 5", "output": "120" } ]
46
0
0
45,588
177
Party
[ "dfs and similar", "dsu", "graphs" ]
null
null
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: - all his friends should also be invited to the party; - the party shouldn't have any people he dislikes; - all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people *a*1 and *a**p* are connected through a chain of common friends if there exists a sequence of people *a*2,<=*a*3,<=...,<=*a**p*<=-<=1 such that all pairs of people *a**i* and *a**i*<=+<=1 (1<=≤<=*i*<=&lt;<=*p*) are friends. Help the Beaver find the maximum number of acquaintances he can invite.
The first line of input contains an integer *n* — the number of the Beaver's acquaintances. The second line contains an integer *k* — the number of pairs of friends. Next *k* lines contain space-separated pairs of integers *u**i*,<=*v**i* — indices of people who form the *i*-th pair of friends. The next line contains an integer *m* — the number of pairs of people who dislike each other. Next *m* lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: - 2<=≤<=*n*<=≤<=14 The input limitations for getting 100 points are: - 2<=≤<=*n*<=≤<=2000
Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
[ "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9\n" ]
[ "3" ]
Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
[ { "input": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9", "output": "3" }, { "input": "2\n1\n1 2\n0", "output": "2" }, { "input": "2\n0\n1\n1 2", "output": "1" }, { "input": "3\n2\n1 2\n1 3\n1\n2 3", "output": "0" }, { "input": "3\n3\n1 3\n2 1\n2 3\n0", "output": "3" }, { "input": "4\n3\n1 2\n2 3\n3 1\n3\n1 4\n4 2\n3 4", "output": "3" }, { "input": "7\n8\n1 2\n1 3\n1 4\n1 5\n2 4\n2 5\n3 4\n5 6\n3\n2 6\n5 7\n6 7", "output": "1" }, { "input": "14\n20\n1 2\n4 5\n4 6\n4 11\n5 7\n5 8\n5 13\n5 14\n7 8\n7 14\n8 9\n8 11\n8 12\n8 14\n10 11\n10 12\n10 14\n11 13\n11 14\n12 14\n5\n1 8\n1 13\n2 10\n7 12\n8 10", "output": "2" }, { "input": "2\n0\n0", "output": "1" }, { "input": "14\n0\n0", "output": "1" }, { "input": "14\n6\n1 2\n2 3\n3 4\n4 5\n8 9\n9 10\n3\n5 6\n6 7\n7 8", "output": "5" }, { "input": "14\n10\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n2 6\n1\n2 7", "output": "1" }, { "input": "2\n0\n1\n1 2", "output": "1" }, { "input": "13\n78\n11 1\n10 6\n6 2\n10 1\n11 6\n11 3\n5 3\n8 1\n12 11\n4 2\n10 3\n13 8\n9 8\n11 7\n7 5\n11 2\n7 1\n4 1\n11 10\n8 3\n13 11\n9 6\n13 9\n12 7\n12 8\n12 9\n10 2\n5 2\n12 10\n9 2\n9 7\n3 2\n7 4\n11 4\n13 1\n10 5\n11 5\n8 5\n10 4\n8 2\n10 9\n4 3\n9 5\n13 12\n13 5\n7 2\n12 4\n9 1\n10 8\n6 3\n6 4\n7 6\n7 3\n12 3\n5 4\n6 1\n12 5\n8 4\n13 3\n12 1\n9 3\n8 6\n11 9\n9 4\n8 7\n12 6\n5 1\n13 10\n13 6\n10 7\n13 4\n13 7\n13 2\n2 1\n6 5\n12 2\n11 8\n3 1\n0", "output": "13" }, { "input": "13\n0\n78\n11 8\n8 4\n13 9\n6 1\n10 5\n5 1\n9 8\n11 3\n13 12\n6 2\n10 9\n9 1\n10 3\n13 6\n8 1\n11 10\n11 1\n11 9\n12 4\n12 11\n11 4\n8 6\n9 6\n13 10\n13 8\n7 2\n8 3\n10 1\n12 10\n6 5\n8 2\n5 4\n9 2\n13 1\n4 1\n13 2\n12 5\n10 7\n7 4\n8 5\n12 6\n4 3\n13 3\n12 2\n9 3\n11 7\n7 3\n2 1\n10 2\n13 7\n7 5\n13 4\n12 7\n4 2\n12 9\n11 5\n10 8\n11 2\n12 3\n3 1\n7 6\n10 6\n12 1\n10 4\n5 2\n9 4\n11 6\n9 7\n5 3\n7 1\n8 7\n6 3\n13 5\n12 8\n6 4\n13 11\n9 5\n3 2", "output": "1" } ]
92
307,200
3
45,598
34
Collisions
[ "brute force", "implementation", "math" ]
E. Collisions
2
256
On a number line there are *n* balls. At time moment 0 for each ball the following data is known: its coordinate *x**i*, speed *v**i* (possibly, negative) and weight *m**i*. The radius of the balls can be ignored. The balls collide elastically, i.e. if two balls weighing *m*1 and *m*2 and with speeds *v*1 and *v*2 collide, their new speeds will be: Your task is to find out, where each ball will be *t* seconds after.
The first line contains two integers *n* and *t* (1<=≤<=*n*<=≤<=10,<=0<=≤<=*t*<=≤<=100) — amount of balls and duration of the process. Then follow *n* lines, each containing three integers: *x**i*, *v**i*, *m**i* (1<=≤<=|*v**i*|,<=*m**i*<=≤<=100,<=|*x**i*|<=≤<=100) — coordinate, speed and weight of the ball with index *i* at time moment 0. It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;*t*]).
Output *n* numbers — coordinates of the balls *t* seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
[ "2 9\n3 4 5\n0 7 8\n", "3 10\n1 2 3\n4 -5 6\n7 -8 9\n" ]
[ "68.538461538\n44.538461538\n", "-93.666666667\n-74.666666667\n-15.666666667\n" ]
none
[ { "input": "2 9\n3 4 5\n0 7 8", "output": "68.538461538\n44.538461538" }, { "input": "3 10\n1 2 3\n4 -5 6\n7 -8 9", "output": "-93.666666667\n-74.666666667\n-15.666666667" }, { "input": "6 76\n-35 -79 86\n-76 -20 64\n-91 79 10\n12 25 51\n62 6 30\n8 -14 65", "output": "-980.877658851\n-6988.294054054\n-7075.540540541\n879.407407407\n2273.407407407\n-440.712253445" }, { "input": "5 69\n-34 28 72\n54 59 51\n65 42 95\n75 1 59\n99 76 54", "output": "159.714684914\n2255.882270753\n3123.845401174\n3621.987012987\n5343.000000000" }, { "input": "5 65\n58 73 74\n98 85 95\n92 94 96\n66 100 79\n44 80 95", "output": "5298.798816568\n6476.100584492\n6206.443232211\n5534.720179506\n4857.798816568" }, { "input": "5 10\n-98 -98 1\n-100 -100 1\n-99 -100 2\n-97 -100 1\n-95 -100 1", "output": "-1097.000000000\n-1100.000000000\n-1099.000000000\n-1095.000000000\n-1078.000000000" }, { "input": "2 1\n-61 -27 64\n1 22 92", "output": "-88.000000000\n23.000000000" }, { "input": "3 11\n84 48 57\n95 49 86\n-26 -63 16", "output": "612.000000000\n634.000000000\n-719.000000000" }, { "input": "4 21\n28 -78 1\n38 -74 28\n-79 45 70\n-40 11 74", "output": "867.474692604\n1412.313839047\n-757.630867568\n585.377007350" }, { "input": "10 1\n-20 83 94\n52 96 85\n83 96 46\n80 51 91\n77 -51 82\n90 81 28\n61 -15 89\n-74 96 77\n100 -34 96\n63 16 84", "output": "30.399224249\n42.826376492\n154.325168307\n101.265711852\n82.588409123\n170.811063827\n64.233798772\n22.000000000\n171.212593222\n72.891869320" }, { "input": "2 37\n27 59 84\n55 31 88", "output": "1178.558139535\n2186.558139535" }, { "input": "3 4\n97 100 100\n100 100 100\n98 99 100", "output": "494.000000000\n500.000000000\n497.000000000" }, { "input": "5 13\n-96 -97 2\n-99 -96 14\n-95 -98 3\n-100 -98 4\n-98 -81 11", "output": "-1085.126863905\n-1347.000000000\n-1062.031005917\n-1374.000000000\n-1284.150295858" }, { "input": "8 12\n97 100 95\n96 91 86\n74 92 100\n91 100 92\n99 95 96\n81 94 97\n78 93 90\n76 96 98", "output": "1291.698538978\n1235.922963944\n1178.000000000\n1229.665798524\n1296.696335079\n1207.643979043\n1195.599197967\n1192.018045733" }, { "input": "9 61\n-99 -24 6\n-98 -94 8\n-96 -86 5\n-94 -89 11\n-73 -98 2\n-89 -98 17\n-83 -73 4\n-93 -93 9\n-97 -98 5", "output": "-7711.174239455\n-6600.521779932\n-5654.607095573\n-5427.256480611\n-1958.957825813\n-4372.546729461\n-3512.989456453\n-5309.535198648\n-6403.587778627" }, { "input": "10 21\n-70 -54 6\n70 85 2\n16 -23 63\n17 40 36\n18 -31 25\n-2 -77 33\n73 22 15\n-91 98 85\n54 54 84\n-61 56 40", "output": "-631.548217515\n2235.922814695\n920.583456740\n1067.568763844\n1872.906264056\n-62.008559917\n2330.519096877\n-780.876434817\n1925.574133169\n-598.567010035" }, { "input": "10 88\n-100 -86 12\n-88 -62 9\n-96 -73 1\n-89 -100 34\n-80 -57 18\n-81 -91 2\n-84 -100 7\n-77 -84 3\n-97 -96 10\n-52 -60 3", "output": "-9488.033285728\n-7931.120096426\n-8406.936891019\n-8261.349896254\n-5320.095238095\n-5795.666666667\n-6961.166666667\n-5303.428571429\n-8788.118633978\n-3401.000000000" }, { "input": "10 58\n96 87 92\n94 88 97\n73 87 89\n92 52 96\n98 83 94\n60 89 94\n100 75 100\n71 59 96\n88 81 89\n91 97 100", "output": "5198.553945126\n5126.672743602\n4425.443339439\n5114.535101691\n5240.755522578\n3144.101564321\n5767.754126743\n3471.355224628\n4666.552945760\n4878.676613669" }, { "input": "10 48\n99 87 97\n95 94 100\n92 100 93\n98 98 92\n100 99 96\n69 76 97\n96 97 100\n94 100 96\n89 99 95\n97 87 100", "output": "4879.251458116\n4777.276197825\n4266.168616302\n4851.462712686\n4890.385748248\n3717.000000000\n4789.071084833\n4577.088610307\n4262.590963871\n4832.919494246" }, { "input": "10 23\n-100 -90 8\n-95 -94 1\n-55 -92 3\n-99 -88 5\n-94 -100 3\n-77 -98 8\n-96 -100 3\n-79 -92 11\n-88 -93 3\n-97 -91 22", "output": "-2332.142245014\n-2257.990444723\n-2046.030598926\n-2286.343298681\n-2175.663481086\n-2131.947062164\n-2259.563142086\n-2150.817778640\n-2171.497622843\n-2261.332659299" }, { "input": "4 2\n0 1 1\n1 -1 1\n3 1 1\n4 -1 1", "output": "-1.000000000\n2.000000000\n2.000000000\n5.000000000" }, { "input": "4 2\n0 1 1\n2 -1 1\n3 1 1\n4 -1 1", "output": "0.000000000\n2.000000000\n2.000000000\n5.000000000" }, { "input": "10 100\n0 1 1\n1 -1 1\n2 1 1\n3 -1 1\n4 1 1\n5 -1 1\n6 1 1\n7 -1 1\n8 1 1\n9 -1 1", "output": "-99.000000000\n-97.000000000\n-95.000000000\n-93.000000000\n-91.000000000\n100.000000000\n102.000000000\n104.000000000\n106.000000000\n108.000000000" }, { "input": "10 100\n0 10 1\n1 -10 1\n2 10 1\n3 -10 1\n4 10 1\n5 -10 1\n6 10 1\n7 -10 1\n8 10 1\n9 -10 1", "output": "-999.000000000\n-997.000000000\n-995.000000000\n-993.000000000\n-991.000000000\n1000.000000000\n1002.000000000\n1004.000000000\n1006.000000000\n1008.000000000" }, { "input": "10 100\n0 100 1\n1 -100 1\n2 100 1\n3 -100 1\n4 100 1\n5 -100 1\n6 100 1\n7 -100 1\n8 100 1\n9 -100 1", "output": "-9999.000000000\n-9997.000000000\n-9995.000000000\n-9993.000000000\n-9991.000000000\n10000.000000000\n10002.000000000\n10004.000000000\n10006.000000000\n10008.000000000" }, { "input": "10 100\n0 100 100\n1 -100 100\n2 100 100\n3 -100 100\n4 100 100\n5 -100 100\n6 100 100\n7 -100 100\n8 100 100\n9 -100 100", "output": "-9999.000000000\n-9997.000000000\n-9995.000000000\n-9993.000000000\n-9991.000000000\n10000.000000000\n10002.000000000\n10004.000000000\n10006.000000000\n10008.000000000" }, { "input": "2 1\n100 1 1\n-100 -1 1", "output": "101.000000000\n-101.000000000" }, { "input": "2 1\n100 -1 100\n-100 1 100", "output": "99.000000000\n-99.000000000" }, { "input": "2 1\n100 1 1\n-100 -1 100", "output": "101.000000000\n-101.000000000" }, { "input": "2 10\n100 -1 1\n-100 1 100", "output": "90.000000000\n-90.000000000" }, { "input": "2 100\n100 -10 1\n-100 10 100", "output": "2664.356435644\n864.356435644" }, { "input": "2 100\n100 -10 100\n-100 10 1", "output": "-864.356435644\n-2664.356435644" }, { "input": "2 100\n100 -10 1\n-100 100 100", "output": "20486.138613861\n9686.138613861" }, { "input": "2 100\n100 -100 1\n-100 10 100", "output": "11486.138613861\n686.138613861" } ]
310
1,331,200
3.92002
45,632
965
Short Code
[ "data structures", "dp", "greedy", "strings", "trees" ]
null
null
Arkady's code contains $n$ variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code. He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names. A string $a$ is a prefix of a string $b$ if you can delete some (possibly none) characters from the end of $b$ and obtain $a$. Please find this minimum possible total length of new names.
The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of variables. The next $n$ lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than $10^5$. The variable names are distinct.
Print a single integer — the minimum possible total length of new variable names.
[ "3\ncodeforces\ncodehorses\ncode\n", "5\nabba\nabb\nab\naa\naacada\n", "3\ntelegram\ndigital\nresistance\n" ]
[ "6\n", "11\n", "3\n" ]
In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c". In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names.
[ { "input": "3\ncodeforces\ncodehorses\ncode", "output": "6" }, { "input": "5\nabba\nabb\nab\naa\naacada", "output": "11" }, { "input": "3\ntelegram\ndigital\nresistance", "output": "3" }, { "input": "1\na", "output": "1" }, { "input": "10\naaaba\nbabba\nbbba\naaabb\nabba\na\nbbb\nbaa\naaba\naa", "output": "23" }, { "input": "10\naaaaaaaaaaaaaaa\naaaaaaaaaaaaaa\naaaaaaaa\naaa\naaaaaaaaaaaa\naa\naaaaaaa\naaaaaaaaa\naaaaaaaaaaaaa\naaaaaaaaaa", "output": "55" }, { "input": "26\ni\nm\nz\na\nv\nu\nq\nt\nj\nn\nr\nk\nd\ng\ns\nc\no\ne\np\nl\ny\nw\nh\nf\nb\nx", "output": "26" } ]
30
0
0
45,676
405
Unusual Product
[ "implementation", "math" ]
null
null
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix. The dot product of two integer number vectors *x* and *y* of size *n* is the sum of the products of the corresponding components of the vectors. The unusual square of an *n*<=×<=*n* square matrix *A* is defined as the sum of *n* dot products. The *i*-th of them is the dot product of the *i*-th row vector and the *i*-th column vector in the matrix *A*. Fortunately for Chris, he has to work only in *GF*(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix *A* is binary: each element of *A* is either 0 or 1. For example, consider the following matrix *A*: The unusual square of *A* is equal to (1·1<=+<=1·0<=+<=1·1)<=+<=(0·1<=+<=1·1<=+<=1·0)<=+<=(1·1<=+<=0·1<=+<=0·0)<==<=0<=+<=1<=+<=1<==<=0. However, there is much more to the homework. Chris has to process *q* queries; each query can be one of the following: 1. given a row index *i*, flip all the values in the *i*-th row in *A*; 1. given a column index *i*, flip all the values in the *i*-th column in *A*; 1. find the unusual square of *A*. To flip a bit value *w* means to change it to 1<=-<=*w*, i.e., 1 changes to 0 and 0 changes to 1. Given the initial matrix *A*, output the answers for each query of the third type! Can you solve Chris's homework?
The first line of input contains an integer *n* (1<=≤<=*n*<=≤<=1000), the number of rows and the number of columns in the matrix *A*. The next *n* lines describe the matrix: the *i*-th line contains *n* space-separated bits and describes the *i*-th row of *A*. The *j*-th number of the *i*-th line *a**ij* (0<=≤<=*a**ij*<=≤<=1) is the element on the intersection of the *i*-th row and the *j*-th column of *A*. The next line of input contains an integer *q* (1<=≤<=*q*<=≤<=106), the number of queries. Each of the next *q* lines describes a single query, which can be one of the following: - 1 *i* — flip the values of the *i*-th row; - 2 *i* — flip the values of the *i*-th column; - 3 — output the unusual square of *A*. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Let the number of the 3rd type queries in the input be *m*. Output a single string *s* of length *m*, where the *i*-th symbol of *s* is the value of the unusual square of *A* for the *i*-th query of the 3rd type as it appears in the input.
[ "3\n1 1 1\n0 1 1\n1 0 0\n12\n3\n2 3\n3\n2 2\n2 2\n1 3\n3\n3\n1 2\n2 1\n1 1\n3\n" ]
[ "01001\n" ]
none
[ { "input": "3\n1 1 1\n0 1 1\n1 0 0\n12\n3\n2 3\n3\n2 2\n2 2\n1 3\n3\n3\n1 2\n2 1\n1 1\n3", "output": "01001" }, { "input": "1\n1\n9\n1 1\n3\n1 1\n1 1\n3\n1 1\n3\n1 1\n3", "output": "0010" }, { "input": "3\n1 0 1\n0 1 1\n1 0 1\n4\n3\n3\n3\n3", "output": "1111" }, { "input": "1\n0\n5\n1 1\n3\n1 1\n1 1\n3", "output": "11" }, { "input": "2\n0 0\n0 0\n9\n3\n1 2\n2 2\n1 1\n2 1\n3\n1 2\n1 1\n3", "output": "000" }, { "input": "2\n0 1\n1 0\n7\n3\n3\n2 2\n3\n2 2\n2 1\n3", "output": "0011" }, { "input": "5\n1 0 1 0 1\n0 1 0 1 0\n1 0 1 0 1\n0 1 0 1 0\n1 0 1 0 1\n7\n3\n2 4\n1 3\n3\n2 5\n1 5\n3", "output": "111" }, { "input": "2\n1 0\n1 0\n4\n1 2\n3\n1 2\n3", "output": "01" }, { "input": "4\n1 1 0 0\n1 1 0 0\n0 0 0 0\n0 0 0 0\n2\n1 1\n3", "output": "1" }, { "input": "2\n1 1\n1 0\n5\n3\n1 1\n3\n2 1\n3", "output": "101" }, { "input": "2\n1 0\n1 1\n6\n1 2\n3\n2 1\n3\n1 1\n3", "output": "101" }, { "input": "2\n0 0\n0 0\n2\n1 1\n3", "output": "1" }, { "input": "2\n1 0\n0 1\n1\n3", "output": "0" } ]
389
63,078,400
3
45,718
926
Endless Roses Most Beautiful
[]
null
null
Arkady decided to buy roses for his girlfriend. A flower shop has white, orange and red roses, and the total amount of them is *n*. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly *k* roses. For each rose in the shop he knows its beauty and color: the beauty of the *i*-th rose is *b**i*, and its color is *c**i* ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of *k* roses satisfying the constraints above or determine that it is not possible to make such a bouquet.
The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=200<=000) — the number of roses in the show and the number of roses Arkady wants to buy. The second line contains a sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=10<=000), where *b**i* equals the beauty of the *i*-th rose. The third line contains a string *c* of length *n*, consisting of uppercase English letters 'W', 'O' and 'R', where *c**i* denotes the color of the *i*-th rose: 'W' denotes white, 'O'  — orange, 'R' — red.
Print the maximum possible total beauty of a bouquet of *k* roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1.
[ "5 3\n4 3 4 1 6\nRROWW\n", "5 2\n10 20 14 20 11\nRRRRR\n", "11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW\n" ]
[ "11\n", "-1\n", "28\n" ]
In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color.
[ { "input": "5 3\n4 3 4 1 6\nRROWW", "output": "11" }, { "input": "5 2\n10 20 14 20 11\nRRRRR", "output": "-1" }, { "input": "11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW", "output": "28" }, { "input": "15 10\n8560 6244 9607 5137 7187 3217 5527 9919 282 8748 3529 6110 5767 521 3393\nOWRWOORWRORWWRO", "output": "64282" }, { "input": "10 4\n1208 5835 2637 5827 3722 6837 3499 6438 43 5333\nWRRWRWRWRW", "output": "-1" }, { "input": "13 3\n9675 8988 5499 6356 5083 6067 5580 4580 6735 3617 9536 8218 3265\nRRWRRROWRWWWW", "output": "24243" }, { "input": "13 7\n8543 3460 1282 3956 8203 762 6059 9361 4427 8868 5849 3439 8891\nWWOOOOWOWWRWO", "output": "54352" }, { "input": "30 15\n7926 577 5009 7237 4395 3239 8994 4429 8126 2925 139 320 4442 3397 1292 2800 9505 6043 5946 8058 4031 6871 4689 1977 73 440 5320 5290 4707 387\nOOWOWWORRWOWORWRRRRWORROOWWROW", "output": "91633" }, { "input": "1 1\n100\nO", "output": "-1" }, { "input": "1 1\n1059\nO", "output": "-1" }, { "input": "2 2\n9907 4483\nOO", "output": "-1" }, { "input": "1 1\n6750\nW", "output": "-1" }, { "input": "2 2\n144 174\nOW", "output": "318" }, { "input": "3 2\n776 4797 9449\nOWO", "output": "14246" }, { "input": "2 2\n3486 8968\nWW", "output": "-1" }, { "input": "3 2\n2330 2140 3440\nWOW", "output": "5580" }, { "input": "4 2\n1175 8186 4321 1810\nWWOO", "output": "12507" }, { "input": "1 1\n6479\nR", "output": "-1" }, { "input": "2 2\n8512 9903\nOR", "output": "18415" }, { "input": "3 2\n7035 5046 7357\nOOR", "output": "14392" }, { "input": "2 2\n6442 4558\nWR", "output": "-1" }, { "input": "3 2\n9700 698 2122\nOWR", "output": "11822" }, { "input": "4 3\n254 4510 2194 9543\nWOOR", "output": "16247" }, { "input": "3 2\n517 6744 2364\nRWW", "output": "-1" }, { "input": "4 2\n2884 2918 8629 9905\nRWOW", "output": "18534" }, { "input": "5 2\n7882 871 789 4432 7664\nOWORW", "output": "15546" }, { "input": "2 2\n2926 8428\nRR", "output": "-1" }, { "input": "3 2\n7926 1770 3255\nORR", "output": "11181" }, { "input": "4 2\n2578 7910 108 3809\nOROR", "output": "10488" }, { "input": "3 2\n5920 9303 7542\nWRR", "output": "-1" }, { "input": "4 2\n5909 4286 5444 6473\nOWRR", "output": "12382" }, { "input": "5 2\n96 6627 8780 3764 970\nRROWO", "output": "15407" }, { "input": "4 2\n6657 1489 9138 4273\nRRWW", "output": "-1" }, { "input": "5 2\n1598 6700 334 6455 9292\nWORWR", "output": "15992" }, { "input": "6 2\n6231 9178 9845 5932 5477 6659\nORRWOW", "output": "16076" }, { "input": "1 1\n780\nO", "output": "-1" }, { "input": "1 1\n3214\nW", "output": "-1" }, { "input": "2 2\n8455 5432\nOW", "output": "13887" }, { "input": "1 1\n6555\nR", "output": "-1" }, { "input": "2 2\n1120 5670\nOR", "output": "6790" }, { "input": "3 2\n8884 4514 1673\nORW", "output": "13398" }, { "input": "1 1\n6908\nO", "output": "-1" }, { "input": "1 1\n3934\nW", "output": "-1" }, { "input": "2 2\n8856 7299\nWO", "output": "16155" }, { "input": "1 1\n2683\nR", "output": "-1" }, { "input": "2 2\n9094 5772\nRO", "output": "14866" }, { "input": "3 2\n518 9438 7938\nWOR", "output": "17376" }, { "input": "10 4\n9513 754 5917 1337 2337 1387 3499 9873 9138 7647\nWROWWOWWRO", "output": "32950" }, { "input": "20 5\n3747 219 7826 7713 6886 466 1136 7069 1234 7556 3657 6017 9965 2847 6880 362 3179 4676 5934 4913\nWWWOWOWOWOROWOWWWOWW", "output": "40129" } ]
93
0
0
45,920
281
Nearest Fraction
[ "brute force", "implementation", "two pointers" ]
null
null
You are given three positive integers *x*,<=*y*,<=*n*. Your task is to find the nearest fraction to fraction whose denominator is no more than *n*. Formally, you should find such pair of integers *a*,<=*b* (1<=≤<=*b*<=≤<=*n*; 0<=≤<=*a*) that the value is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator.
A single line contains three integers *x*,<=*y*,<=*n* (1<=≤<=*x*,<=*y*,<=*n*<=≤<=105).
Print the required fraction in the format "*a*/*b*" (without quotes).
[ "3 7 6\n", "7 2 4\n" ]
[ "2/5\n", "7/2\n" ]
none
[]
154
1,536,000
0
45,957
87
Beautiful Road
[ "dfs and similar", "dp", "dsu", "graphs", "implementation", "sortings", "trees" ]
D. Beautiful Road
1
256
A long time ago in some country in Asia were civil wars. Each of *n* cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly *n*<=-<=1. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly *n*(*n*<=-<=1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
The first line contains an integer *n* (2<=≤<=*n*<=≤<=105), which represents the number of cities. Next *n*<=-<=1 lines contain three integers each: the numbers of cities *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*), connected by the *i*-th road and the number of days *d**i* the army spends to go on it (1<=≤<=*d**i*<=≤<=109). The lengths of several roads may coincide.
Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to *n*<=-<=1 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
[ "2\n2 1 5\n", "6\n1 2 1\n1 3 5\n3 4 2\n3 5 3\n3 6 4\n" ]
[ "2 1\n1 \n", "16 1\n2 \n" ]
none
[ { "input": "2\n2 1 5", "output": "2 1\n1 " }, { "input": "6\n1 2 1\n1 3 5\n3 4 2\n3 5 3\n3 6 4", "output": "16 1\n2 " }, { "input": "10\n10 6 43981\n4 2 6730\n1 2 35174\n5 3 61951\n8 7 43981\n7 1 6730\n5 8 6730\n9 3 52479\n6 4 18138", "output": "32 1\n4 " }, { "input": "9\n6 4 72697\n9 6 72697\n1 6 38220\n2 6 38220\n6 7 72697\n6 5 72697\n8 6 72697\n3 6 38220", "output": "16 5\n1 2 5 6 7 " }, { "input": "10\n9 2 18232\n3 4 45701\n3 9 13895\n8 9 18232\n7 6 56122\n3 5 45701\n7 1 56122\n8 10 18232\n2 7 91606", "output": "42 1\n9 " }, { "input": "7\n1 2 7485\n6 7 50574\n3 1 50574\n3 4 50574\n5 6 58286\n6 1 58286", "output": "24 1\n6 " }, { "input": "4\n2 3 1914\n4 1 31823\n4 2 26249", "output": "6 1\n2 " }, { "input": "5\n3 2 72460\n3 4 69285\n3 5 69285\n1 3 11694", "output": "8 1\n1 " }, { "input": "9\n5 9 29573\n7 3 72031\n8 5 72031\n6 7 72031\n9 7 27434\n3 2 27434\n4 7 76150\n1 9 27434", "output": "24 1\n2 " }, { "input": "5\n5 4 58958\n2 1 37970\n2 5 37970\n1 3 37970", "output": "8 2\n1 2 " }, { "input": "10\n6 5 30\n2 5 30\n7 8 81\n5 4 46\n9 10 30\n5 7 30\n10 2 46\n8 3 46\n1 10 46", "output": "32 1\n3 " }, { "input": "10\n4 2 73\n10 8 73\n8 7 83\n9 5 83\n2 1 86\n4 9 88\n2 6 30\n8 4 30\n7 3 32", "output": "32 1\n6 " }, { "input": "10\n9 4 75\n2 7 29\n1 8 19\n9 5 20\n4 1 19\n2 4 19\n10 3 29\n2 3 20\n6 4 29", "output": "32 1\n1 " }, { "input": "10\n10 1 74\n8 1 43\n6 3 59\n2 9 58\n6 4 62\n6 7 74\n2 5 62\n1 6 74\n5 3 8", "output": "42 1\n8 " }, { "input": "17\n6 16 69066\n2 5 77345\n7 17 49859\n11 15 77345\n14 2 96308\n3 5 49859\n10 13 16769\n4 17 58915\n15 1 99387\n1 3 77937\n6 8 53782\n13 14 21754\n9 4 49859\n12 8 96308\n11 9 96308\n12 7 96288", "output": "140 1\n9 " }, { "input": "9\n1 6 802882924\n7 5 807576793\n9 4 343794537\n5 3 807576793\n2 7 394413002\n9 7 353519612\n1 7 784711994\n5 8 784711994", "output": "36 1\n2 " } ]
31
0
0
46,070
630
Challenge Pennants
[ "combinatorics", "math" ]
null
null
Because of budget cuts one IT company established new non-financial reward system instead of bonuses. Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table. Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought. In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table. One man can have several pennants of one type and of course he can have pennants of both types on his table. There are *n* tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.
The only line of the input contains one integer *n* (1<=≤<=*n*<=≤<=500) — the number of tables in the IT company.
Output one integer — the amount of ways to place the pennants on *n* tables.
[ "2\n" ]
[ "24" ]
none
[ { "input": "2", "output": "24" }, { "input": "1", "output": "1" }, { "input": "3", "output": "210" }, { "input": "4", "output": "1120" }, { "input": "5", "output": "4410" }, { "input": "6", "output": "14112" }, { "input": "7", "output": "38808" }, { "input": "12", "output": "1589952" }, { "input": "28", "output": "817586560" }, { "input": "43", "output": "21766594410" }, { "input": "139", "output": "212332162372330" }, { "input": "321", "output": "163013183025830865" }, { "input": "100", "output": "15789964684000" }, { "input": "498", "output": "5392730685240975000" }, { "input": "500", "output": "5567867859752100000" } ]
77
2,150,400
-1
46,209
370
Mittens
[ "constructive algorithms", "greedy", "sortings" ]
null
null
A Christmas party in city S. had *n* children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to *m*, and the children are numbered from 1 to *n*. Then the *i*-th child has both mittens of color *c**i*. The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children. The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.
The first line contains two integers *n* and *m* — the number of the children and the number of possible mitten colors (1<=≤<=*n*<=≤<=5000, 1<=≤<=*m*<=≤<=100). The second line contains *n* integers *c*1,<=*c*2,<=... *c**n*, where *c**i* is the color of the mittens of the *i*-th child (1<=≤<=*c**i*<=≤<=*m*).
In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next *n* lines print the way the mittens can be distributed in this case. On the *i*-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the *i*-th child will get. If there are multiple solutions, you can print any of them.
[ "6 3\n1 3 2 2 1 1\n", "4 2\n1 2 1 1\n" ]
[ "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1\n", "2\n1 2\n1 1\n2 1\n1 1\n" ]
none
[ { "input": "6 3\n1 3 2 2 1 1", "output": "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1" }, { "input": "4 2\n1 2 1 1", "output": "2\n1 2\n1 1\n2 1\n1 1" }, { "input": "1 1\n1", "output": "0\n1 1" }, { "input": "4 1\n1 1 1 1", "output": "0\n1 1\n1 1\n1 1\n1 1" }, { "input": "2 2\n2 2", "output": "0\n2 2\n2 2" }, { "input": "2 2\n1 2", "output": "2\n2 1\n1 2" }, { "input": "4 4\n4 3 2 1", "output": "4\n4 3\n3 2\n1 4\n2 1" }, { "input": "4 2\n1 2 1 2", "output": "4\n2 1\n2 1\n1 2\n1 2" }, { "input": "5 3\n1 1 2 1 1", "output": "2\n2 1\n1 1\n1 1\n1 1\n1 2" }, { "input": "6 3\n1 1 2 2 3 3", "output": "6\n1 3\n3 2\n2 1\n2 1\n3 2\n1 3" }, { "input": "10 3\n3 1 2 2 2 1 2 2 2 1", "output": "8\n1 2\n2 1\n3 2\n2 1\n2 1\n2 2\n1 2\n2 3\n2 2\n1 2" }, { "input": "10 3\n3 1 3 1 1 2 2 2 1 3", "output": "10\n2 1\n1 3\n2 1\n1 3\n1 3\n3 2\n2 1\n1 2\n3 2\n3 1" }, { "input": "10 3\n1 2 1 2 2 1 2 3 2 1", "output": "10\n1 2\n2 1\n3 2\n2 1\n2 1\n1 2\n1 2\n2 1\n2 3\n1 2" }, { "input": "10 3\n3 2 3 1 2 2 2 1 1 3", "output": "10\n1 2\n2 3\n1 2\n2 3\n2 3\n3 1\n1 2\n2 1\n3 1\n3 2" }, { "input": "10 3\n3 3 1 3 1 2 2 1 3 2", "output": "10\n1 3\n3 2\n1 3\n3 2\n3 2\n2 1\n1 3\n3 1\n2 1\n2 3" }, { "input": "100 10\n8 9 1 5 7 8 2 6 7 3 1 6 5 5 8 10 5 2 1 7 1 9 8 7 9 9 8 10 8 10 2 9 5 5 1 7 8 5 3 8 6 2 9 2 5 3 8 10 7 1 2 9 5 2 7 8 2 10 8 8 7 6 2 7 3 9 8 9 9 5 10 8 5 7 10 7 2 4 6 7 6 7 5 4 5 3 2 6 5 6 1 5 7 8 3 10 5 9 3 6", "output": "100\n5 8\n5 8\n5 8\n4 5\n1 5\n8 7\n2 6\n10 4\n3 5\n3 5\n8 7\n2 10\n7 9\n7 2\n6 3\n9 6\n1 5\n7 2\n1 5\n8 7\n5 8\n9 2\n2 10\n2 10\n2 10\n5 8\n5 8\n10 1\n5 8\n4 5\n10 1\n10 1\n9 6\n7 9\n2 6\n7 9\n8 7\n5 8\n3 5\n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." } ]
30
512,000
0
46,267
354
Vasya and Beautiful Arrays
[ "brute force", "dp", "number theory" ]
null
null
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers *a* of length *n*. Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array *a* left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by *k* for each number). The seller can obtain array *b* from array *a* if the following conditions hold: *b**i*<=&gt;<=0; 0<=≤<=*a**i*<=-<=*b**i*<=≤<=*k* for all 1<=≤<=*i*<=≤<=*n*. Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=3·105;<=1<=≤<=*k*<=≤<=106). The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=106) — array *a*.
In the single line print a single number — the maximum possible beauty of the resulting array.
[ "6 1\n3 6 10 12 13 16\n", "5 3\n8 21 52 15 77\n" ]
[ "3\n", "7\n" ]
In the first sample we can obtain the array: 3 6 9 12 12 15 In the second sample we can obtain the next array: 7 21 49 14 77
[ { "input": "6 1\n3 6 10 12 13 16", "output": "3" }, { "input": "5 3\n8 21 52 15 77", "output": "7" }, { "input": "13 11\n55 16 26 40 84 80 48 52 25 43 75 21 58", "output": "16" }, { "input": "18 9\n85 29 29 15 17 71 46 69 48 80 44 73 40 55 61 57 22 68", "output": "13" }, { "input": "25 7\n67 18 36 85 64 22 32 66 17 64 66 65 82 36 16 52 19 70 38 51 17 32 85 16 64", "output": "16" }, { "input": "7 1\n12 84 21 60 33 21 45", "output": "4" }, { "input": "1 1\n1", "output": "1" }, { "input": "10 10\n40141 53368 66538 64507 78114 34253 73242 42141 37430 6", "output": "6" }, { "input": "10 7\n869 1293 12421 1 90901 120214 12403 6543 591870 124", "output": "1" }, { "input": "2 84794\n1000000 1000000", "output": "1000000" } ]
389
31,846,400
3
46,292
305
Playing with String
[ "games" ]
null
null
Two people play the following string game. Initially the players have got some string *s*. The players move in turns, the player who cannot make a move loses. Before the game began, the string is written on a piece of paper, one letter per cell. A player's move is the sequence of actions: 1. The player chooses one of the available pieces of paper with some string written on it. Let's denote it is *t*. Note that initially, only one piece of paper is available. 1. The player chooses in the string *t*<==<=*t*1*t*2... *t*|*t*| character in position *i* (1<=≤<=*i*<=≤<=|*t*|) such that for some positive integer *l* (0<=&lt;<=*i*<=-<=*l*; *i*<=+<=*l*<=≤<=|*t*|) the following equations hold: *t**i*<=-<=1<==<=*t**i*<=+<=1, *t**i*<=-<=2<==<=*t**i*<=+<=2, ..., *t**i*<=-<=*l*<==<=*t**i*<=+<=*l*. 1. Player cuts the cell with the chosen character. As a result of the operation, he gets three new pieces of paper, the first one will contain string *t*1*t*2... *t**i*<=-<=1, the second one will contain a string consisting of a single character *t**i*, the third one contains string *t**i*<=+<=1*t**i*<=+<=2... *t*|*t*|. Your task is to determine the winner provided that both players play optimally well. If the first player wins, find the position of character that is optimal to cut in his first move. If there are multiple positions, print the minimal possible one.
The first line contains string *s* (1<=≤<=|*s*|<=≤<=5000). It is guaranteed that string *s* only contains lowercase English letters.
If the second player wins, print in the single line "Second" (without the quotes). Otherwise, print in the first line "First" (without the quotes), and in the second line print the minimal possible winning move — integer *i* (1<=≤<=*i*<=≤<=|*s*|).
[ "abacaba\n", "abcde\n" ]
[ "First\n2\n", "Second\n" ]
In the first sample the first player has multiple winning moves. But the minimum one is to cut the character in position 2. In the second sample the first player has no available moves.
[ { "input": "abacaba", "output": "First\n2" }, { "input": "abcde", "output": "Second" }, { "input": "aaaaa", "output": "First\n3" }, { "input": "aaabbbbbbbbabaaabbaabbbbabbabaabaabbbaabbbbbbabbbabaabaaabaaaabbaaabbbbaabbbaaabababbbbabbabbabaaaaabababbbaabbbaabababaaabababbaaaaaaaabbbbabbabbbababbababbbaabbbbbbbabaababaaabababbabbaabbbbbaabaabaabbbabbabbbbabaabbabaaaabbbbaabaaabbaabbabbaaabbbbaaaaaaabbbbbbbbbbabaaabaabababaabababbbbbbbabaababababbbaababaaaabbaababaaabbaaaabbabbabaaaaaabbabbbbbababbabbabbaaababbbaaababbb", "output": "Second" }, { "input": "abbabbaaababbbbbbabbbbaabbabaababaababbbabbbaaaaaabbabbabbaabbbbaaaabbabaababbbabbaabaabbbaabaaaabbbbbabbbbbaababbabaabaababbbbbbbaaaababbabbaababbabaabbbbbabbbbaabaaabbbababaaabbbbabaaaabbbbbbbbbabbbbababababbbbaabbababaaabbabbaaaabaaabbbbabbbaaaabbabbaabaaaabaaaaabaaabbbbaaabababaaabbbaabbababbaaabbbbaabaabbbaaaaaabbaaabbaabbbaaabbbbbabbbbaabababbbababaaabbbaabbbbbaababaaabaabbbbba", "output": "First\n4" }, { "input": "hwlyeasmdahtbvvguqxrbkzzbladwolhqwnelgatnbcwxpcbzdsuriybdopjiljwyjldjvbcgwdlyrrsbpjkkembkxhsqraqzbtartvbptgsbcibfxqgiosreccmvkfvcxvxxejxtthxnaqhkmzihkjcehlubhdsyhpacwfqetifotriilokxgrjsfpwemrgiyjsbxpvvfiycbxzwcuhivdxvjgyaandwncxqkkrvhfwdnhxnbzilfpkbchsjrer", "output": "Second" }, { "input": "gggggggggggggg", "output": "First\n5" }, { "input": "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogog", "output": "First\n6" } ]
0
0
-1
46,375
817
MEX Queries
[ "binary search", "data structures", "trees" ]
null
null
You are given a set of integer numbers, initially it is empty. You should perform *n* queries. There are three different types of queries: - 1 *l* *r* — Add all missing numbers from the interval [*l*,<=*r*] - 2 *l* *r* — Remove all present numbers from the interval [*l*,<=*r*] - 3 *l* *r* — Invert the interval [*l*,<=*r*] — add all missing and remove all present numbers from the interval [*l*,<=*r*] After each query you should output MEX of the set — the smallest positive (MEX <=≥<=1) integer number which is not presented in the set.
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=105). Next *n* lines contain three integer numbers *t*,<=*l*,<=*r* (1<=≤<=*t*<=≤<=3,<=1<=≤<=*l*<=≤<=*r*<=≤<=1018) — type of the query, left and right bounds.
Print MEX of the set after each query.
[ "3\n1 3 4\n3 1 6\n2 1 3\n", "4\n1 1 3\n3 5 6\n2 4 4\n3 1 6\n" ]
[ "1\n3\n1\n", "4\n4\n4\n1\n" ]
Here are contents of the set after each query in the first example: 1. {3, 4} — the interval [3, 4] is added 1. {1, 2, 5, 6} — numbers {3, 4} from the interval [1, 6] got deleted and all the others are added 1. {5, 6} — numbers {1, 2} got deleted
[ { "input": "3\n1 3 4\n3 1 6\n2 1 3", "output": "1\n3\n1" }, { "input": "4\n1 1 3\n3 5 6\n2 4 4\n3 1 6", "output": "4\n4\n4\n1" }, { "input": "1\n1 1 89", "output": "90" }, { "input": "10\n1 1 9\n2 2 10\n2 1 10\n1 8 10\n3 8 10\n2 1 8\n3 9 10\n1 7 10\n1 4 10\n3 10 10", "output": "10\n2\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "10\n2 9 59\n2 8 72\n3 19 41\n1 1 62\n1 50 74\n1 53 66\n2 59 69\n3 66 77\n2 62 63\n3 57 69", "output": "1\n1\n1\n63\n75\n75\n59\n59\n59\n57" }, { "input": "20\n3 20 37\n1 1 32\n1 22 70\n3 40 61\n3 50 50\n3 52 59\n1 65 66\n3 59 73\n3 72 76\n2 73 75\n1 72 75\n2 74 75\n1 65 78\n2 74 80\n2 76 79\n3 72 84\n2 84 87\n3 87 87\n1 81 83\n2 81 85", "output": "1\n38\n71\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40\n40" }, { "input": "3\n1 1 1000000000000000000\n3 1 999999999999999999\n2 1000000000000000000 1000000000000000000", "output": "1000000000000000001\n1\n1" }, { "input": "3\n1 999999999999999999 1000000000000000000\n3 999999999999999999 999999999999999999\n2 1000000000000000000 1000000000000000000", "output": "1\n1\n1" } ]
93
2,969,600
-1
46,489
610
Harmony Analysis
[ "constructive algorithms" ]
null
null
The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or <=-<=1 and any two vectors are orthogonal. Just as a reminder, two vectors in *n*-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2*k* vectors in 2*k*-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?
The only line of the input contains a single integer *k* (0<=≤<=*k*<=≤<=9).
Print 2*k* lines consisting of 2*k* characters each. The *j*-th character of the *i*-th line must be equal to '<=*<=' if the *j*-th coordinate of the *i*-th vector is equal to <=-<=1, and must be equal to '<=+<=' if it's equal to <=+<=1. It's guaranteed that the answer always exists. If there are many correct answers, print any.
[ "2\n" ]
[ "++**\n+*+*\n++++\n+**+" ]
Consider all scalar products in example: - Vectors 1 and 2: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( + 1) + ( - 1)·( - 1) = 0 - Vectors 1 and 3: ( + 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) + ( - 1)·( + 1) = 0 - Vectors 1 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( - 1)·( - 1) + ( - 1)·( + 1) = 0 - Vectors 2 and 3: ( + 1)·( + 1) + ( - 1)·( + 1) + ( + 1)·( + 1) + ( - 1)·( + 1) = 0 - Vectors 2 and 4: ( + 1)·( + 1) + ( - 1)·( - 1) + ( + 1)·( - 1) + ( - 1)·( + 1) = 0 - Vectors 3 and 4: ( + 1)·( + 1) + ( + 1)·( - 1) + ( + 1)·( - 1) + ( + 1)·( + 1) = 0
[ { "input": "2", "output": "++++\n+*+*\n++**\n+**+" }, { "input": "1", "output": "++\n+*" }, { "input": "3", "output": "++++++++\n+*+*+*+*\n++**++**\n+**++**+\n++++****\n+*+**+*+\n++****++\n+**+*++*" }, { "input": "0", "output": "+" }, { "input": "4", "output": "++++++++++++++++\n+*+*+*+*+*+*+*+*\n++**++**++**++**\n+**++**++**++**+\n++++****++++****\n+*+**+*++*+**+*+\n++****++++****++\n+**+*++*+**+*++*\n++++++++********\n+*+*+*+**+*+*+*+\n++**++****++**++\n+**++**+*++**++*\n++++********++++\n+*+**+*+*+*++*+*\n++****++**++++**\n+**+*++**++*+**+" }, { "input": "5", "output": "++++++++++++++++++++++++++++++++\n+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*\n++**++**++**++**++**++**++**++**\n+**++**++**++**++**++**++**++**+\n++++****++++****++++****++++****\n+*+**+*++*+**+*++*+**+*++*+**+*+\n++****++++****++++****++++****++\n+**+*++*+**+*++*+**+*++*+**+*++*\n++++++++********++++++++********\n+*+*+*+**+*+*+*++*+*+*+**+*+*+*+\n++**++****++**++++**++****++**++\n+**++**+*++**++*+**++**+*++**++*\n++++********++++++++********++++\n+*+**+*+*+*++*+*+*+**+*+*+*++*+*\n++****++**++++**++****++**++++**\n+..." }, { "input": "6", "output": "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*\n++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**\n+**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**+\n++++****++++****++++****++++****++++****++++****++++****++++****\n+*+**+*++*+**+*++*+**+*++*+**+*++*+**+*++*+**+*++*+**+*++*+**+*+\n++****++++****++++****++++****++++****++++****++++****++++****++\n+**+*++*+**+*++*+**+*++*+**+*++*+**+*++*+**+*++*+..." }, { "input": "7", "output": "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*\n++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**\n+**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++..." }, { "input": "8", "output": "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+..." }, { "input": "9", "output": "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++..." } ]
139
6,963,200
3
46,578
628
Bear and Fair Set
[ "flows", "graphs" ]
null
null
Limak is a grizzly bear. He is big and dreadful. You were chilling in the forest when you suddenly met him. It's very unfortunate for you. He will eat all your cookies unless you can demonstrate your mathematical skills. To test you, Limak is going to give you a puzzle to solve. It's a well-known fact that Limak, as every bear, owns a set of numbers. You know some information about the set: - The elements of the set are distinct positive integers. - The number of elements in the set is *n*. The number *n* is divisible by 5. - All elements are between 1 and *b*, inclusive: bears don't know numbers greater than *b*. - For each *r* in {0,<=1,<=2,<=3,<=4}, the set contains exactly elements that give remainder *r* when divided by 5. (That is, there are elements divisible by 5, elements of the form 5*k*<=+<=1, elements of the form 5*k*<=+<=2, and so on.) Limak smiles mysteriously and gives you *q* hints about his set. The *i*-th hint is the following sentence: "If you only look at elements that are between 1 and *upTo**i*, inclusive, you will find exactly *quantity**i* such elements in my set." In a moment Limak will tell you the actual puzzle, but something doesn't seem right... That smile was very strange. You start to think about a possible reason. Maybe Limak cheated you? Or is he a fair grizzly bear? Given *n*, *b*, *q* and hints, check whether Limak can be fair, i.e. there exists at least one set satisfying the given conditions. If it's possible then print ''fair". Otherwise, print ''unfair".
The first line contains three integers *n*, *b* and *q* (5<=≤<=*n*<=≤<=*b*<=≤<=104, 1<=≤<=*q*<=≤<=104, *n* divisible by 5) — the size of the set, the upper limit for numbers in the set and the number of hints. The next *q* lines describe the hints. The *i*-th of them contains two integers *upTo**i* and *quantity**i* (1<=≤<=*upTo**i*<=≤<=*b*, 0<=≤<=*quantity**i*<=≤<=*n*).
Print ''fair" if there exists at least one set that has all the required properties and matches all the given hints. Otherwise, print ''unfair".
[ "10 20 1\n10 10\n", "10 20 3\n15 10\n5 0\n10 5\n", "10 20 2\n15 3\n20 10\n" ]
[ "fair\n", "fair\n", "unfair\n" ]
In the first example there is only one set satisfying all conditions: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. In the second example also there is only one set satisfying all conditions: {6, 7, 8, 9, 10, 11, 12, 13, 14, 15}. Easy to see that there is no set satisfying all conditions from the third example. So Limak lied to you :-(
[ { "input": "10 20 1\n10 10", "output": "fair" }, { "input": "10 20 3\n15 10\n5 0\n10 5", "output": "fair" }, { "input": "10 20 2\n15 3\n20 10", "output": "unfair" }, { "input": "15 27 2\n6 4\n23 5", "output": "unfair" }, { "input": "50 7654 4\n1273 11\n6327 38\n1244 3\n5208 22", "output": "fair" }, { "input": "50 7654 4\n2899 15\n3848 26\n2718 12\n5511 36", "output": "fair" }, { "input": "50 7654 4\n4881 20\n4957 6\n4764 50\n944 44", "output": "unfair" }, { "input": "50 6457 1\n945 41", "output": "fair" }, { "input": "500 5000 5\n1289 221\n694 178\n2179 454\n160 11\n1398 232", "output": "fair" }, { "input": "500 5000 10\n2905 421\n573 82\n1602 205\n4523 491\n970 100\n3810 453\n2553 418\n2033 364\n1664 245\n1924 311", "output": "fair" }, { "input": "500 5000 20\n875 49\n73 16\n2405 136\n811 33\n2477 140\n3475 303\n4640 496\n4025 369\n4482 440\n3475 272\n3594 346\n3945 368\n3807 346\n2605 159\n4045 382\n2861 270\n4488 448\n1894 61\n2388 113\n4071 383", "output": "unfair" }, { "input": "500 5000 50\n687 73\n3816 389\n4333 436\n1660 177\n2238 231\n2936 312\n899 96\n541 55\n4218 425\n4512 457\n1302 132\n2322 239\n688 73\n4423 449\n2765 284\n3755 382\n4192 422\n2718 277\n2254 231\n1354 140\n4891 490\n2722 277\n344 35\n4774 479\n988 101\n2530 256\n3679 375\n3258 341\n1870 201\n1391 146\n643 68\n1040 105\n2607 266\n906 97\n4790 480\n2390 245\n3101 326\n2616 267\n1064 107\n1091 110\n1735 187\n2434 247\n3887 397\n1335 137\n2073 219\n450 45\n480 47\n3519 359\n157 16\n4316 434", "output": "fair" }, { "input": "15 40 3\n2 0\n13 9\n4 1", "output": "fair" }, { "input": "15 41 3\n16 8\n14 2\n40 9", "output": "unfair" }, { "input": "15 40 3\n8 0\n38 14\n28 9", "output": "fair" }, { "input": "15 40 3\n1 9\n24 0\n35 7", "output": "unfair" }, { "input": "15 40 2\n23 4\n36 7", "output": "unfair" }, { "input": "15 41 2\n19 12\n2 0", "output": "fair" }, { "input": "15 40 2\n35 13\n36 14", "output": "fair" }, { "input": "15 40 2\n15 4\n24 4", "output": "fair" }, { "input": "5 6 2\n4 4\n5 4", "output": "unfair" }, { "input": "10 20 4\n3 3\n5 3\n9 7\n17 7", "output": "unfair" }, { "input": "5 10 3\n2 1\n5 4\n7 4", "output": "unfair" }, { "input": "5 30 10\n1 1\n5 1\n6 2\n10 2\n11 3\n15 3\n16 4\n20 4\n21 5\n30 5", "output": "unfair" }, { "input": "10 13 2\n3 3\n5 4", "output": "unfair" }, { "input": "5 10 3\n1 1\n5 1\n9 5", "output": "unfair" }, { "input": "10 14 2\n4 4\n5 4", "output": "unfair" } ]
233
9,011,200
3
46,809
379
New Year Letter
[ "bitmasks", "brute force", "dp" ]
null
null
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, *s*1 anf *s*2, consisting of uppercase English letters. Then the boy makes string *s**k*, using a recurrent equation *s**n*<==<=*s**n*<=-<=2<=+<=*s**n*<=-<=1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string *s**k* on a piece of paper, puts it in the envelope and sends in to Santa. Vasya is absolutely sure that Santa will bring him the best present if the resulting string *s**k* has exactly *x* occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string *s*1 should have length *n*, and string *s*2 should have length *m*. Vasya hasn't decided anything else. At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, *s*1 and *s*2 in the required manner. Help Vasya.
The first line contains four integers *k*,<=*x*,<=*n*,<=*m* (3<=≤<=*k*<=≤<=50; 0<=≤<=*x*<=≤<=109; 1<=≤<=*n*,<=*m*<=≤<=100).
In the first line print string *s*1, consisting of *n* uppercase English letters. In the second line print string *s*2, consisting of *m* uppercase English letters. If there are multiple valid strings, print any of them. If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
[ "3 2 2 2\n", "3 3 2 2\n", "3 0 2 2\n", "4 3 2 1\n", "4 2 2 1\n" ]
[ "AC\nAC\n", "Happy new year!\n", "AA\nAA\n", "Happy new year!\n", "Happy new year!\n" ]
none
[ { "input": "3 2 2 2", "output": "AC\nAC" }, { "input": "3 3 2 2", "output": "Happy new year!" }, { "input": "3 0 2 2", "output": "AA\nAA" }, { "input": "4 3 2 1", "output": "Happy new year!" }, { "input": "4 2 2 1", "output": "Happy new year!" }, { "input": "3 0 1 1", "output": "A\nA" }, { "input": "50 0 100 100", "output": "AZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA\nAZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA" }, { "input": "50 1 100 100", "output": "Happy new year!" }, { "input": "4 32 23 32", "output": "ACACZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACZA" }, { "input": "5 45 23 32", "output": "AZZZZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACZA" }, { "input": "6 54 23 32", "output": "ACACACZZZZZZZZZZZZZZZZA\nACACACACACACACACACZZZZZZZZZZZZZA" }, { "input": "7 120 23 32", "output": "AZZZZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACZA" }, { "input": "8 664 100 100", "output": "ACACACACACZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACZZZA" }, { "input": "8 661 100 99", "output": "ACACACZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACA" }, { "input": "8 704 99 100", "output": "ACACACACACACACACACACZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA\nACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACZZZA" }, { "input": "10 189 44 100", "output": "ACACACACACACACACACZZZZZZZZZZZZZZZZZZZZZZZZZA\nAZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA" }, { "input": "3 1 1 6", "output": "A\nACZZZA" }, { "input": "3 1 6 1", "output": "ACZZZA\nA" }, { "input": "50 1000000000 100 100", "output": "Happy new year!" }, { "input": "6 4 3 2", "output": "AZA\nCA" }, { "input": "5 4 3 2", "output": "ACA\nCB" }, { "input": "3 3 3 3", "output": "ACA\nCAC" }, { "input": "48 512559680 100 100", "output": "Happy new year!" }, { "input": "3 1 1 1", "output": "A\nC" }, { "input": "5 4 2 3", "output": "CA\nACA" }, { "input": "3 2 3 1", "output": "ACA\nC" }, { "input": "5 2 1 1", "output": "A\nC" }, { "input": "10 143 4 6", "output": "CACA\nCACACA" }, { "input": "7 3 1 3", "output": "B\nCZA" }, { "input": "5 1 10 10", "output": "AZZZZZZZZB\nCZZZZZZZZA" }, { "input": "6 4 2 2", "output": "AA\nCA" }, { "input": "9 33 2 2", "output": "CA\nCA" }, { "input": "5 1 2 2", "output": "AB\nCA" }, { "input": "5 4 2 2", "output": "CA\nCA" }, { "input": "5 1 1 1", "output": "C\nA" }, { "input": "4 4 3 3", "output": "ACA\nCAC" }, { "input": "33 100034454 87 58", "output": "Happy new year!" }, { "input": "7 4 1 2", "output": "C\nAA" }, { "input": "6 7 3 2", "output": "ACA\nCA" }, { "input": "7 12 2 2", "output": "CA\nCA" }, { "input": "48 512559680 2 2", "output": "Happy new year!" }, { "input": "6 1 2 2", "output": "AB\nCA" }, { "input": "6 3 1 1", "output": "A\nC" }, { "input": "4 6 3 5", "output": "ACA\nCACAC" }, { "input": "13 6325 61 51", "output": "Happy new year!" }, { "input": "8 7 1 1", "output": "Happy new year!" } ]
716
0
3
46,815
135
Zero-One
[ "constructive algorithms", "games", "greedy" ]
null
null
Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him. Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it. An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
[ "????\n", "1010\n", "1?1\n" ]
[ "00\n01\n10\n11\n", "10\n", "01\n11\n" ]
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes. In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
[ { "input": "????", "output": "00\n01\n10\n11" }, { "input": "1010", "output": "10" }, { "input": "1?1", "output": "01\n11" }, { "input": "111?", "output": "11" }, { "input": "000?", "output": "00" }, { "input": "1??1?", "output": "00\n01\n10\n11" }, { "input": "?111111?00?", "output": "10\n11" }, { "input": "??????????0????????????????0000000000000", "output": "00\n10\n11" }, { "input": "1?1?1?1?0?10", "output": "00\n10\n11" }, { "input": "?0101", "output": "00\n01" }, { "input": "??0101", "output": "00\n01\n11" }, { "input": "??1010", "output": "00\n10\n11" }, { "input": "00", "output": "00" }, { "input": "11", "output": "11" }, { "input": "1110110101111111001111111110111011100001110111011001011111111111011010011111?111111?011111110001010110011111010111100111?111101010011101010?011111101010111111111101111010111?1111111100110110001111101111100100110011101111011110111000111?011110?111110?1011010111101111101111010100111110101111011101110011101001?11?011101110101101111001111111101100011?1011111111011100011101111100010110110101011?10111001101101011101110110110?10100111011", "output": "11" }, { "input": "1010", "output": "10" }, { "input": "0101", "output": "01" }, { "input": "00000?01?111100001100100110101000110110110001001011000001010110110001101100101110000110011110011111011111000011001011101101011000110100010000001111000110110100111000001101101100011001011011100000011001111101111000010110101111000010001000011100100100110110011010010010111110101001000111110111011011100", "output": "00\n10\n11" }, { "input": "1000101111011000001100011100100101100110110101010110000000011010011011010010110110100110010001001111011001011111101110011001100010001011000000011011001110100100110101010011001011111000001001100111111?010100101001101000011111011010000?000101010001110100110101000101001011001110001001011110111101110111", "output": "00\n01\n11" }, { "input": "100111111101011110100010111111100011001101101000010111101111100100001100101110110100010010110000001111010101011110000000110100110010110111001100110100100010111011110100100010010001?0000101101001101000100010110001100000111000001001010001000100001000010101000111110111100010101110111111100101111101110?", "output": "00\n01\n10\n11" }, { "input": "010111110110100010111110?01101100010110011111001001010110?11110100001111000110010001000110001111110100011001100100100110001100000011101111111111001110110001001101111001001101001101011110011110100110011010100111100110010000011010010011000101010000010110100110001100111000100011111000000010011010000", "output": "00\n10\n11" }, { "input": "110011111011101010100001000111111000000110110010000101111100011100000110001?0010110110001111101101111101110000001000000010110010000001001011101001100011000100110100001110100101100110101011000?110101111111011010110110001010100111101010001101100111100110101000001110011101001001011110011101101100001", "output": "00\n01\n11" }, { "input": "0011000100000011110100010001110011101011111100111111101010110111100100110100110110000010111111?011010001001001100101011100010110010011101011011011001100101001101100100100010100001000001110101111011001100011111011010101000110110101101111011010001101000000111000111000100100110010010001100101011000?", "output": "00\n01\n10\n11" }, { "input": "100110000000100000101010010001010011010001010110011011111001110111101011010111111000000111110001000101110101111111101110000000001000101101100111110111110110000100011010011001111011101011101111100111101001111110101000100000001010010111000100001111111000011101011011100110010001100010000111000000000", "output": "10" }, { "input": "000111101111111101011001100000101011101111010101000000100110011000101010001001010001100001011110100011011100010001000000110110101010000000011001011001101011100111010111111000100111110110001101100101000010011001010001111111001001100101011000011000010011111011011111100110010001011100000111111011011", "output": "01" }, { "input": "01101010111100111010100110001101000110111010100000111111111010011011111101001001101001100110100110010110110001010110001101000011100111110000001000100010100001010010100100101111000110010011111100?10000100110101101011010110101010100110001111110000011011101001000011111010011100000010010110111100", "output": "10\n11" }, { "input": "001011110111010100111111100100100010100001011001000100101110011001101101000101010110111011011111110010110101000100011000010111110110000111101100100000010100111010000011010011000101000111000000100101111011?0011011011101111000010110101100111111010010010000000001110101001101000111110110110001011", "output": "01\n11" }, { "input": "0100101010100000000011001010010011011011111000100000101110101000111110000011101100001111000010010010101110011011101111010001001111110000101101000100100110110011110100110101100100001111111011110110011101101011011111001111110000010110100100000011110100100000110011111111000011101100000100011001?", "output": "10\n11" }, { "input": "001000101010110101111111101111001011000100100000111010111010001001100000000101001010111011001000010010111000011010000?1101100001001111000100100001011011100100111110101110100010111110100110100100110001110101100100000101111101010001101010101011011000110110011111001001111100011001101001110111100", "output": "00\n10" }, { "input": "00001101010100101111100001001010000001100110001101010110100011011010111011111011000011011100011110001011111001001110000110011010101001101000100101000000011101111101110010010111110001101001101010111011011?01011001001010110111011101100000101110001000010111110100010110100111000000111000110100001", "output": "00\n01" }, { "input": "0101011100110100101000101010001100011100011111001011011101000010100001100010101111111010000011011001100111101010001001101100100001011111101101001000001000100110000111111101101101010011001010101010110110110100011110101000111111101011101010001111011100010001010011001010001100001000101011100100?", "output": "00\n01" }, { "input": "111100101001111101010101000011000010010011111100011001111011110011010100000010000001011000110101?1100110001100100100110111110010010101000000000100010101101101111001001101000010011010000010101111010110111111111110101110100010100011000101111110111000110000011001011000101101001101001100110110", "output": "00\n10" }, { "input": "0001010101001001100001101101100000111110110011010001111000010001001001011?011000011111001001110000111001100110010011100100000101100010100011110101000111011110100010010100101101110000110001011101101101010001100101011110110111011001110011111110111100001110101011111001010100000100100100100101", "output": "00\n01" }, { "input": "1010111110011001100111110000101001101000110001001101011100011011011110110001000000010101100110010010111001000010111000110000100001011110000000101011111100000110010110100111111001010011100001000001100110111000010011110111011100010010011111001100111111011010011101101011000010011001001011101?", "output": "00\n01" }, { "input": "1011110101010001110000110111011011101011010100001001101?10101010111001011000110111011111010000110001000011100101011100011111100100100110011100101000011110111010011011010110111111010000000001010100100000111000000110100011110101111110010011010011100100011100000000011110111101010010010000", "output": "10\n11" }, { "input": "00101010011010001111101100110000110001111001001010110001?1001010111100110010110001101000011000001011110101011100111011100111101101110110000110110111111010101011110110000001001101011100111101001010101110001101101001000101101110100000001011101000110101000010111110100000101000000100100101", "output": "01\n11" }, { "input": "000101010011000011101111101110010000100000001111110100000111010011000100000011101101100111011110001010000001111100001000100011110100111111010000010111000010100101011101111001010101100100000111011101111110000100011001101101010010101110101111000111101100000010011101000111111111100010110?", "output": "10\n11" }, { "input": "1100110110011001101101000110100011001000001110111010010010000001011111001000111111000110100010111001111010000100100011101000001000011001111111000111001001111110011101010111010011111011001110011000101000100011001100110011111101100100100110011010001001011001011010101000001011001110", "output": "10" }, { "input": "1000001000110000111000000001011100001100100011010000111010110100111111101001101100011101000011110010110110100110000010110101111101110100011101111001010110110010011001100001101110010010110001100000110010000011111100110101110101111010000001100100110100001111011010111001101100110111", "output": "01" }, { "input": "0101011011010000111111111101101010000110100010000010111101000101010111101011111001010110001000000010010110110100011010100110100000110101010010010001101110011110011011111011001100110001011011000001011100110110010100001011100011111010100110011001001010110111000111000001100", "output": "00" }, { "input": "1010011100111101001000110100011011100100100010011000000111011011011010110110010111011010111001000100001001001011011100110001000110111001101100111100111010010000001110001000001010111001111111011111011001111101111000010100100010011001000001010000101011101011110010100010011", "output": "00" }, { "input": "??", "output": "00\n01\n10\n11" }, { "input": "??????????????????????????????????????????????????????????????????????????????????????0", "output": "00\n10\n11" }, { "input": "??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1", "output": "00\n01\n11" }, { "input": "10?", "output": "00\n01" }, { "input": "100???????????????????????????0000000??????????????????????????????0", "output": "00\n10\n11" } ]
92
0
0
46,856
286
Lucky Permutation
[ "constructive algorithms", "math" ]
null
null
A permutation *p* of size *n* is the sequence *p*1,<=*p*2,<=...,<=*p**n*, consisting of *n* distinct integers, each of them is from 1 to *n* (1<=≤<=*p**i*<=≤<=*n*). A lucky permutation is such permutation *p*, that any integer *i* (1<=≤<=*i*<=≤<=*n*) meets this condition *p**p**i*<==<=*n*<=-<=*i*<=+<=1. You have integer *n*. Find some lucky permutation *p* of size *n*.
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the required permutation size.
Print "-1" (without the quotes) if the lucky permutation *p* of size *n* doesn't exist. Otherwise, print *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*) after a space — the required permutation. If there are multiple answers, you can print any of them.
[ "1\n", "2\n", "4\n", "5\n" ]
[ "1 \n", "-1\n", "2 4 1 3 \n", "2 5 3 1 4 \n" ]
none
[ { "input": "1", "output": "1 " }, { "input": "2", "output": "-1" }, { "input": "4", "output": "2 4 1 3 " }, { "input": "5", "output": "2 5 3 1 4 " }, { "input": "3", "output": "-1" }, { "input": "100000", "output": "2 100000 4 99998 6 99996 8 99994 10 99992 12 99990 14 99988 16 99986 18 99984 20 99982 22 99980 24 99978 26 99976 28 99974 30 99972 32 99970 34 99968 36 99966 38 99964 40 99962 42 99960 44 99958 46 99956 48 99954 50 99952 52 99950 54 99948 56 99946 58 99944 60 99942 62 99940 64 99938 66 99936 68 99934 70 99932 72 99930 74 99928 76 99926 78 99924 80 99922 82 99920 84 99918 86 99916 88 99914 90 99912 92 99910 94 99908 96 99906 98 99904 100 99902 102 99900 104 99898 106 99896 108 99894 110 99892 112 99890 114..." }, { "input": "6", "output": "-1" }, { "input": "7", "output": "-1" }, { "input": "8", "output": "2 8 4 6 3 5 1 7 " }, { "input": "9", "output": "2 9 4 7 5 3 6 1 8 " }, { "input": "99997", "output": "2 99997 4 99995 6 99993 8 99991 10 99989 12 99987 14 99985 16 99983 18 99981 20 99979 22 99977 24 99975 26 99973 28 99971 30 99969 32 99967 34 99965 36 99963 38 99961 40 99959 42 99957 44 99955 46 99953 48 99951 50 99949 52 99947 54 99945 56 99943 58 99941 60 99939 62 99937 64 99935 66 99933 68 99931 70 99929 72 99927 74 99925 76 99923 78 99921 80 99919 82 99917 84 99915 86 99913 88 99911 90 99909 92 99907 94 99905 96 99903 98 99901 100 99899 102 99897 104 99895 106 99893 108 99891 110 99889 112 99887 114 ..." }, { "input": "10000", "output": "2 10000 4 9998 6 9996 8 9994 10 9992 12 9990 14 9988 16 9986 18 9984 20 9982 22 9980 24 9978 26 9976 28 9974 30 9972 32 9970 34 9968 36 9966 38 9964 40 9962 42 9960 44 9958 46 9956 48 9954 50 9952 52 9950 54 9948 56 9946 58 9944 60 9942 62 9940 64 9938 66 9936 68 9934 70 9932 72 9930 74 9928 76 9926 78 9924 80 9922 82 9920 84 9918 86 9916 88 9914 90 9912 92 9910 94 9908 96 9906 98 9904 100 9902 102 9900 104 9898 106 9896 108 9894 110 9892 112 9890 114 9888 116 9886 118 9884 120 9882 122 9880 124 9878 126 9..." }, { "input": "10001", "output": "2 10001 4 9999 6 9997 8 9995 10 9993 12 9991 14 9989 16 9987 18 9985 20 9983 22 9981 24 9979 26 9977 28 9975 30 9973 32 9971 34 9969 36 9967 38 9965 40 9963 42 9961 44 9959 46 9957 48 9955 50 9953 52 9951 54 9949 56 9947 58 9945 60 9943 62 9941 64 9939 66 9937 68 9935 70 9933 72 9931 74 9929 76 9927 78 9925 80 9923 82 9921 84 9919 86 9917 88 9915 90 9913 92 9911 94 9909 96 9907 98 9905 100 9903 102 9901 104 9899 106 9897 108 9895 110 9893 112 9891 114 9889 116 9887 118 9885 120 9883 122 9881 124 9879 126 9..." }, { "input": "10002", "output": "-1" }, { "input": "10003", "output": "-1" }, { "input": "10004", "output": "2 10004 4 10002 6 10000 8 9998 10 9996 12 9994 14 9992 16 9990 18 9988 20 9986 22 9984 24 9982 26 9980 28 9978 30 9976 32 9974 34 9972 36 9970 38 9968 40 9966 42 9964 44 9962 46 9960 48 9958 50 9956 52 9954 54 9952 56 9950 58 9948 60 9946 62 9944 64 9942 66 9940 68 9938 70 9936 72 9934 74 9932 76 9930 78 9928 80 9926 82 9924 84 9922 86 9920 88 9918 90 9916 92 9914 94 9912 96 9910 98 9908 100 9906 102 9904 104 9902 106 9900 108 9898 110 9896 112 9894 114 9892 116 9890 118 9888 120 9886 122 9884 124 9882 126..." }, { "input": "25", "output": "2 25 4 23 6 21 8 19 10 17 12 15 13 11 14 9 16 7 18 5 20 3 22 1 24 " }, { "input": "29", "output": "2 29 4 27 6 25 8 23 10 21 12 19 14 17 15 13 16 11 18 9 20 7 22 5 24 3 26 1 28 " }, { "input": "33", "output": "2 33 4 31 6 29 8 27 10 25 12 23 14 21 16 19 17 15 18 13 20 11 22 9 24 7 26 5 28 3 30 1 32 " }, { "input": "9", "output": "2 9 4 7 5 3 6 1 8 " }, { "input": "13", "output": "2 13 4 11 6 9 7 5 8 3 10 1 12 " }, { "input": "17", "output": "2 17 4 15 6 13 8 11 9 7 10 5 12 3 14 1 16 " }, { "input": "50001", "output": "2 50001 4 49999 6 49997 8 49995 10 49993 12 49991 14 49989 16 49987 18 49985 20 49983 22 49981 24 49979 26 49977 28 49975 30 49973 32 49971 34 49969 36 49967 38 49965 40 49963 42 49961 44 49959 46 49957 48 49955 50 49953 52 49951 54 49949 56 49947 58 49945 60 49943 62 49941 64 49939 66 49937 68 49935 70 49933 72 49931 74 49929 76 49927 78 49925 80 49923 82 49921 84 49919 86 49917 88 49915 90 49913 92 49911 94 49909 96 49907 98 49905 100 49903 102 49901 104 49899 106 49897 108 49895 110 49893 112 49891 114 ..." }, { "input": "20001", "output": "2 20001 4 19999 6 19997 8 19995 10 19993 12 19991 14 19989 16 19987 18 19985 20 19983 22 19981 24 19979 26 19977 28 19975 30 19973 32 19971 34 19969 36 19967 38 19965 40 19963 42 19961 44 19959 46 19957 48 19955 50 19953 52 19951 54 19949 56 19947 58 19945 60 19943 62 19941 64 19939 66 19937 68 19935 70 19933 72 19931 74 19929 76 19927 78 19925 80 19923 82 19921 84 19919 86 19917 88 19915 90 19913 92 19911 94 19909 96 19907 98 19905 100 19903 102 19901 104 19899 106 19897 108 19895 110 19893 112 19891 114 ..." }, { "input": "30001", "output": "2 30001 4 29999 6 29997 8 29995 10 29993 12 29991 14 29989 16 29987 18 29985 20 29983 22 29981 24 29979 26 29977 28 29975 30 29973 32 29971 34 29969 36 29967 38 29965 40 29963 42 29961 44 29959 46 29957 48 29955 50 29953 52 29951 54 29949 56 29947 58 29945 60 29943 62 29941 64 29939 66 29937 68 29935 70 29933 72 29931 74 29929 76 29927 78 29925 80 29923 82 29921 84 29919 86 29917 88 29915 90 29913 92 29911 94 29909 96 29907 98 29905 100 29903 102 29901 104 29899 106 29897 108 29895 110 29893 112 29891 114 ..." }, { "input": "99996", "output": "2 99996 4 99994 6 99992 8 99990 10 99988 12 99986 14 99984 16 99982 18 99980 20 99978 22 99976 24 99974 26 99972 28 99970 30 99968 32 99966 34 99964 36 99962 38 99960 40 99958 42 99956 44 99954 46 99952 48 99950 50 99948 52 99946 54 99944 56 99942 58 99940 60 99938 62 99936 64 99934 66 99932 68 99930 70 99928 72 99926 74 99924 76 99922 78 99920 80 99918 82 99916 84 99914 86 99912 88 99910 90 99908 92 99906 94 99904 96 99902 98 99900 100 99898 102 99896 104 99894 106 99892 108 99890 110 99888 112 99886 114 ..." }, { "input": "99999", "output": "-1" }, { "input": "101", "output": "2 101 4 99 6 97 8 95 10 93 12 91 14 89 16 87 18 85 20 83 22 81 24 79 26 77 28 75 30 73 32 71 34 69 36 67 38 65 40 63 42 61 44 59 46 57 48 55 50 53 51 49 52 47 54 45 56 43 58 41 60 39 62 37 64 35 66 33 68 31 70 29 72 27 74 25 76 23 78 21 80 19 82 17 84 15 86 13 88 11 90 9 92 7 94 5 96 3 98 1 100 " }, { "input": "505", "output": "2 505 4 503 6 501 8 499 10 497 12 495 14 493 16 491 18 489 20 487 22 485 24 483 26 481 28 479 30 477 32 475 34 473 36 471 38 469 40 467 42 465 44 463 46 461 48 459 50 457 52 455 54 453 56 451 58 449 60 447 62 445 64 443 66 441 68 439 70 437 72 435 74 433 76 431 78 429 80 427 82 425 84 423 86 421 88 419 90 417 92 415 94 413 96 411 98 409 100 407 102 405 104 403 106 401 108 399 110 397 112 395 114 393 116 391 118 389 120 387 122 385 124 383 126 381 128 379 130 377 132 375 134 373 136 371 138 369 140 367 142 ..." }, { "input": "565", "output": "2 565 4 563 6 561 8 559 10 557 12 555 14 553 16 551 18 549 20 547 22 545 24 543 26 541 28 539 30 537 32 535 34 533 36 531 38 529 40 527 42 525 44 523 46 521 48 519 50 517 52 515 54 513 56 511 58 509 60 507 62 505 64 503 66 501 68 499 70 497 72 495 74 493 76 491 78 489 80 487 82 485 84 483 86 481 88 479 90 477 92 475 94 473 96 471 98 469 100 467 102 465 104 463 106 461 108 459 110 457 112 455 114 453 116 451 118 449 120 447 122 445 124 443 126 441 128 439 130 437 132 435 134 433 136 431 138 429 140 427 142 ..." } ]
404
28,262,400
3
47,077
575
Party
[ "bitmasks", "brute force", "graph matchings" ]
null
null
Note the unusual memory limit for the problem. People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday. There are *N* people working in MDCS and there are *N* clubs in the city. Unfortunately, if there is more than one Microsoft employee in night club, level of coolness goes infinitely high and party is over, so club owners will never let more than one Microsoft employee enter their club in the same week (just to be sure). You are organizing night life for Microsoft employees and you have statistics about how much every employee likes Friday and Saturday parties for all clubs. You need to match people with clubs maximizing overall sum of their happiness (they are happy as much as they like the club), while half of people should go clubbing on Friday and the other half on Saturday.
The first line contains integer *N* — number of employees in MDCS. Then an *N*<=×<=*N* matrix follows, where element in *i*-th row and *j*-th column is an integer number that represents how much *i*-th person likes *j*-th club’s Friday party. Then another *N*<=×<=*N* matrix follows, where element in *i*-th row and *j*-th column is an integer number that represents how much *i*-th person likes *j*-th club’s Saturday party. - 2<=≤<=*N*<=≤<=20 - *N* is even - 0<=≤<= level of likeness <=≤<=106 - All values are integers
Output should contain a single integer — maximum sum of happiness possible.
[ "4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3\n5 8 7 1\n6 9 81 3\n55 78 1 6\n1 1 1 1\n" ]
[ "167\n" ]
Here is how we matched people with clubs: Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness). Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness). 4+4+81+78 = 167
[]
46
0
-1
47,114
383
Vowels
[ "combinatorics", "divide and conquer", "dp" ]
null
null
Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of *n* 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (*a* to *x*). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the *xor* of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number.
The first line contains one integer, *n* (1<=≤<=*n*<=≤<=104). Each of the next *n* lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words.
Print one number, the *xor* of the squared answers to the queries.
[ "5\nabc\naaa\nada\nbcd\ndef\n" ]
[ "0\n" ]
none
[ { "input": "5\nabc\naaa\nada\nbcd\ndef", "output": "0" }, { "input": "100\namd\namj\natr\nbcp\nbjm\ncna\ncpj\ncse\ndij\ndjp\ndlv\nebk\nedf\nelw\nfbr\nfcl\nfhs\nflo\nfmj\ngcg\ngen\nghg\ngvb\ngxx\nhbe\nhbf\nhgu\nhlv\nhqa\nibg\nifp\nima\nitt\nivl\nixu\njle\njli\nket\nkit\nkws\nlep\nles\nleu\nmbp\nmci\nmdv\nmhf\nmih\nmll\nmop\nndp\nnfs\nngl\nnng\noic\nomo\nooj\noti\npax\npfo\npjd\npup\nqer\nrad\nrdg\nrfq\nrvt\nrwa\nrxj\nshc\nsjv\nswx\ntcu\ntlm\ntmb\ntml\ntmw\ntvr\ntvx\nuid\nuir\nukf\nulg\nvce\nves\nvfb\nvok\nvut\nvvi\nvwb\nwab\nwba\nwdf\nweq\nwog\nwsl\nxbk\nxiq\nxop\nxpp", "output": "13888" }, { "input": "100\naip\nbfl\nbld\nblh\nbpk\nbqd\nbtk\ncfu\nciv\nckf\ncog\ncro\nctt\ncve\ncvn\ndlj\neer\negw\negx\nffi\nfld\nggk\ngis\ngkv\ngnq\ngvj\nhdo\nhgf\nhgu\nhjt\nhla\nhni\nhnk\nifa\niir\niml\njfa\njgl\nkbf\nliv\nlqo\nmlw\nmot\nmpx\nnas\nnlo\nobt\nodo\nodx\nolr\nolw\nonc\npac\npdp\nphn\npku\npng\npsd\nptl\npuq\npvk\npvx\nqjj\nqju\nqpf\nqqv\nqsf\nrac\nrgj\nrrg\nsbm\nsdf\nsif\nsil\nsnv\nspt\nsxt\ntou\nttj\nufi\nuht\nujm\null\nupm\nuqf\nvof\nvpq\nwae\nwck\nwed\nwhd\nwjn\nwpp\nwvd\nxbx\nxdv\nxeh\nxmq\nxsm\nxsp", "output": "8624" }, { "input": "10\nhjk\nkkw\nmsw\nnht\noqu\npcx\npet\nshd\nutb\nwbw", "output": "0" }, { "input": "20\netf\nffq\ngqe\nhpj\nido\niep\nkbv\nlgs\nlkl\nlvg\nmhs\nocr\nonc\nonv\npmv\nqhk\nrck\nrgj\nsib\nuox", "output": "0" }, { "input": "30\nagf\naov\ncac\ncdq\nclc\ncue\ndmh\ndrr\ndxv\nfrv\njmg\nkih\nkii\nkqm\nkwc\nnri\nohw\nrfk\nrrd\nrrk\ntmp\ntsc\nuhg\nuhx\nujw\nvms\nvrg\nwer\nxml\nxuv", "output": "0" }, { "input": "40\nbhw\nblh\ncal\nccg\ncdd\ncsm\ndir\ndux\nefp\nfnw\ngcr\nhuc\niaf\nipv\niva\niwl\njeb\njwk\nlot\nmcf\nmnk\nnak\nopl\norb\noxj\nqws\nrbl\nsmo\nsuw\nsws\ntgt\numg\nvhn\nvud\nwml\nwqg\nxbv\nxgj\nxlm\nxxv", "output": "944" }, { "input": "50\nagj\nbnk\nbtg\ncqt\ncxs\ndjv\neft\neqt\nfbf\nfbp\nfko\nfrg\ngdb\ngdw\ngie\ngvv\nhdw\nijo\nixc\njif\njph\nkad\nkje\nlel\nles\nlhw\nlkw\nmht\nnii\nnsb\nnuo\nnwp\nolm\nomb\noti\notm\nove\npnl\npqf\npwc\nrfq\nrkl\nsrm\nthb\ntje\ntpw\nugo\nwhk\nwwq\nxpx", "output": "1184" }, { "input": "50\naah\naoh\naqc\nauo\ncnk\ndfa\ndok\nfvd\nhxk\nibb\nicl\nigj\nird\njjv\njmv\nkbo\nkgj\nkji\nkxp\nlnf\nlqe\nndq\nnoi\nohh\noro\npdg\npio\npjq\npkw\npsg\npvt\nqdi\nqmo\nrba\nrkh\nrpk\nrrm\nrxs\nssu\ntcn\ntea\ntjb\ntkr\nuuh\nvmn\nvqd\nwaj\nwnl\nwqp\nxtw", "output": "2736" }, { "input": "50\nabh\navn\nbrx\ndcp\ndqe\nedr\neub\nfmg\ngda\ngmm\ngpn\nhbd\nhnw\nhta\nhuk\nhun\nieo\nifc\niwn\nixm\njpc\njsr\nkrj\nksc\nlie\nljj\nllb\nlqp\nmap\nmkx\nnob\nogl\nokh\noxq\npqu\npxk\nqfv\nqkt\nrjw\nseu\ntpe\nupe\nvlk\nwbw\nwce\nxae\nxqk\nxsv\nxve\nxvk", "output": "224" }, { "input": "50\nbpx\ncpq\ncqo\ndct\ndhh\ndid\ndlr\ndpl\neie\nesj\nfnc\nfse\nfxp\ngat\nghq\ngmg\nhan\nhdq\nhqn\nhse\nhwt\nibk\njbg\njda\nkgi\nkrr\nkrt\nkvo\nlwe\nmuh\nmve\nnfp\noac\nodw\nofq\npdr\nqlr\nrjm\nsdl\nsfj\nshs\ntae\ntdt\nual\nukf\nuup\nvkw\nvnj\nwbh\nxsp", "output": "3200" }, { "input": "50\nbfu\nbqa\ncew\nclt\ncnx\ncor\ncvq\nddq\ndgm\ndme\nehr\neua\newd\nfhq\nhep\nill\njmp\njnc\njng\njts\njtt\njww\nkei\nkjr\nkmk\nkoq\nkxi\nmgu\nnbb\nnqa\nnrp\nntq\nnwg\nost\notf\noxc\npia\nqgo\nqli\nqqa\nrrx\nrug\nsaj\nsjc\ntqm\nvoh\nvoo\nvwd\nwke\nwqg", "output": "2432" }, { "input": "100\nacs\nako\naqn\navw\naxm\nbea\nbmw\nbro\nbrw\nbvn\nciv\ncpn\ndas\ndex\ndjo\ndwq\neat\nedq\negu\neqw\nfkt\nflt\nfqv\nfrf\nfwg\ngab\nhcs\nhfw\nhoq\nhwu\nicq\niji\nins\nirs\nivn\njga\njng\nkcq\nkfe\nkox\nkps\nkts\nlmt\nlok\nlvm\nlwt\nmfd\nmlc\nmnm\nmwu\nnad\nnai\nnot\nogr\nope\noqm\nosd\novq\nprj\nqad\nqoh\nqqk\nrnq\nrqx\nrsh\nrug\nrxg\nsar\nsbn\nsbu\nsbw\nseg\nskp\nsqm\nssx\ntoo\nttm\nuch\nuek\nuhm\nuhn\nusv\nvaw\nvcw\nvkm\nvsj\nvwi\nwbm\nwcg\nwqr\nwri\nwsw\nxbs\nxcn\nxhw\nxip\nxoq\nxue\nxuk\nxvg", "output": "7488" }, { "input": "100\naie\naoq\nban\nbdw\ncdk\ncgw\ncls\ncoq\ncsp\ncwi\ndmg\negd\negi\nejd\nfbs\nfiv\nfjv\nfrp\nfto\ngcf\ngfb\ngkg\ngvg\nhfe\nhfr\nhgi\nhgx\nhpe\nhwt\nhxn\nibd\nifb\nihu\nipf\niwe\njds\njfe\njkb\njkx\njvq\nkdr\nkjh\nkll\nkog\nltk\nmik\nmsb\nnci\nndl\nnfo\nnfp\nnio\nnkr\nnmi\nnpk\noch\nogx\noka\nolf\nopm\norv\nphm\npmd\npuo\npxq\nqae\nqik\nqlp\nqna\nqst\nqth\nqxm\nrak\nrpj\nrqd\nsbq\nsfv\nstw\ntaj\nteh\ntlw\ntmj\ntmm\ntqv\nujn\nuko\nunb\nuvm\nvdb\nvjd\nvtp\nvvt\nwme\nwnq\nwqs\nwwj\nxan\nxdn\nxjg\nxkd", "output": "8960" }, { "input": "100\nahd\nahw\narc\naro\natd\naui\nbas\nbeg\nblc\nbmu\nboo\nbpt\nbqa\ncds\nchn\ncni\ncsh\nddt\ndjb\ndkh\neal\near\necr\neea\nefr\nekf\nekq\netb\neui\nfau\nfcr\nfdc\nfhp\nfpc\nfwv\ngaf\ngoo\ngut\nhek\nheu\nhfq\nhjk\nhjx\nhmk\nhqp\nhsa\niax\nijm\njlf\njlw\njok\njqi\njss\njte\nknb\nkrt\nlbi\nlej\nlqu\nlva\nlxf\nmll\nndb\nndf\nngc\nolh\nope\npds\npli\npuk\nqec\nqgi\nqkr\nqqu\nrks\nrsj\nscb\nsig\nsnj\ntdc\ntpa\ntro\nttc\ntwn\nuef\nuhh\nujb\nujn\nuka\nulk\nuss\nuwa\nuwu\nvmr\nvmt\nvoq\nwug\nwvh\nxef\nxrk", "output": "6624" }, { "input": "100\nagg\nals\naxf\nbdd\nbex\nbsx\nchb\nclr\ncmm\ndaf\ndbf\nddw\ndng\nduw\nebp\nech\neex\neff\nefg\neqt\nerp\nexg\nfbd\nffg\nfif\nfta\nghv\ngqn\ngrf\nhcc\nhdc\nhos\nhqh\nims\nipf\niro\nixu\njhx\njil\njqn\njuh\nkeb\nknl\nkol\nksj\nksl\nkxn\nlbn\nlci\nlfr\nliw\nlpc\nmdq\nmhx\nmts\nmwl\nnde\nnik\nnlo\nnnk\nnpc\nntt\nohr\nona\npap\npfb\npgm\npgo\npql\npsd\npvd\nqax\nqcj\nqfj\nqiv\nqke\nqks\nrhu\nrrg\nseo\nskr\ntjp\ntlt\ntof\ntop\ntpn\ntxe\nvfl\nvpn\nvrh\nwbd\nwet\nwgo\nwlm\nwox\nwwi\nxas\nxmg\nxng\nxqj", "output": "13408" }, { "input": "100\navm\nbir\nbmx\nbve\nbvx\ncbr\nccq\nckn\ncmd\ncuu\ncxh\nddw\ndfb\ndgt\ndmo\ndqd\neon\nerm\nerp\neux\newl\nfau\nfek\nfss\nftg\nfvb\ngfu\ngkw\nguj\ngwe\nhjf\nhrq\nibk\njjs\njmp\njqs\nkbu\nklu\nkqw\nkqx\nlaa\nlbe\nmek\nmga\nmio\nmle\nmls\nmma\nmoj\nmpb\nmxu\nnfs\nnht\noap\nods\noee\nokc\noqr\npdh\npdt\nphq\nphw\npwa\nqgt\nqji\nqnj\nqqt\nqvu\nqwb\nqwc\nrdv\nrfq\nrnx\nrse\nruq\nrvs\nsoo\nsxe\nthh\ntop\ntrg\ntud\ntur\nugu\nupj\nupt\nvak\nver\nvhu\nvul\nwes\nwkm\nwqc\nwuf\nxbk\nxdf\nxlh\nxnv\nxqo\nxvu", "output": "10864" } ]
46
0
0
47,115
1,009
Allowed Letters
[ "bitmasks", "flows", "graph matchings", "graphs", "greedy" ]
null
null
Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup! Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent. In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there. Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?) More formally, you are given a string consisting of lowercase Latin letters from "a" to "f". You can swap letters at any positions arbitrary number of times (zero swaps is also possible). What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters? If Polycarp can't produce any valid name then print "Impossible".
The first line is the string $s$ ($1 \le |s| \le 10^5$) — the name Polycarp has came up with. The string consists only of lowercase Latin letters from "a" to "f". The second line contains a single integer $m$ ($0 \le m \le |s|$) — the number of investors. The $i$-th of the next $m$ lines contain an integer number $pos_i$ and a non-empty string of allowed characters for $pos_i$ ($1 \le pos_i \le |s|$). Each string contains pairwise distinct letters from "a" to "f". $pos_1, pos_2, \dots, pos_m$ are pairwise distinct. If any position of the string doesn't appear in the investors demands then any letter can go in this position.
If Polycarp can't produce any valid name then print "Impossible". Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string $s$ such that the letter at every position is among the allowed ones.
[ "bedefead\n5\n2 e\n1 dc\n5 b\n7 ef\n6 ef\n", "abacaba\n0\n", "fc\n2\n1 cfab\n2 f\n" ]
[ "deadbeef\n", "aaaabbc\n", "cf\n" ]
none
[ { "input": "bedefead\n5\n2 e\n1 dc\n5 b\n7 ef\n6 ef", "output": "deadbeef" }, { "input": "abacaba\n0", "output": "aaaabbc" }, { "input": "fc\n2\n1 cfab\n2 f", "output": "cf" }, { "input": "bbcbbc\n6\n1 c\n2 c\n3 b\n4 ab\n5 ab\n6 ab", "output": "ccbbbb" }, { "input": "abcdefffffffffffffff\n5\n20 abcde\n19 abcde\n18 abcde\n17 abcde\n16 abcde", "output": "fffffffffffffffabcde" }, { "input": "abcdefffffffffffffff\n20\n1 acf\n2 cdef\n3 ef\n4 def\n5 adef\n6 acdef\n7 bdef\n8 abdf\n9 bcdf\n10 abf\n11 abf\n12 bcdf\n13 df\n14 df\n15 abcdf\n16 abcde\n17 abcde\n18 abcde\n19 abcde\n20 abcde", "output": "fffffffffffffffabcde" }, { "input": "aaeff\n5\n2 afbdce\n5 c\n1 dbc\n4 afcbde\n3 ef", "output": "Impossible" }, { "input": "cdff\n1\n2 ae", "output": "Impossible" }, { "input": "dfb\n2\n1 c\n3 cae", "output": "Impossible" }, { "input": "cefe\n2\n4 ca\n1 da", "output": "Impossible" }, { "input": "cdccc\n5\n2 fae\n1 dabc\n4 dcfabe\n3 abc\n5 bdcafe", "output": "Impossible" }, { "input": "bdc\n3\n1 f\n3 fdacb\n2 eb", "output": "Impossible" }, { "input": "effa\n3\n3 ca\n2 bd\n4 abfdce", "output": "Impossible" }, { "input": "bfd\n2\n2 aecf\n3 dfb", "output": "bfd" }, { "input": "bfb\n3\n1 f\n3 acdef\n2 cdefab", "output": "Impossible" }, { "input": "fce\n3\n3 abdecf\n1 efdcba\n2 ac", "output": "ecf" }, { "input": "ded\n1\n2 aedc", "output": "dde" }, { "input": "a\n1\n1 b", "output": "Impossible" } ]
233
10,137,600
3
47,118
171
Mysterious numbers - 2
[ "*special" ]
null
null
The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=≤<=*a*1,<=*a*2,<=*a*3<=≤<=20), separated by spaces. Output a single integer.
The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=≤<=*a*1,<=*a*2,<=*a*3<=≤<=20), separated by spaces.
Output a single integer.
[ "2 3 2\n", "13 14 1\n", "14 5 9\n", "17 18 3\n" ]
[ "5\n", "14\n", "464\n", "53\n" ]
none
[ { "input": "2 3 2", "output": "5" }, { "input": "13 14 1", "output": "14" }, { "input": "14 5 9", "output": "464" }, { "input": "17 18 3", "output": "53" }, { "input": "1 1 1", "output": "1" }, { "input": "4 6 7", "output": "110" }, { "input": "1 1 20", "output": "10946" }, { "input": "20 20 1", "output": "20" }, { "input": "20 20 20", "output": "218920" }, { "input": "12 9 18", "output": "42420" }, { "input": "1 19 15", "output": "11967" }, { "input": "5 5 5", "output": "40" }, { "input": "10 11 12", "output": "2474" }, { "input": "3 7 17", "output": "14140" }, { "input": "8 2 9", "output": "236" } ]
122
0
3
47,176
232
Fence
[ "binary search", "data structures", "string suffix structures" ]
null
null
John Doe has a crooked fence, consisting of *n* rectangular planks, lined up from the left to the right: the plank that goes *i*-th (1<=≤<=*i*<=≤<=*n*) (from left to right) has width 1 and height *h**i*. We will assume that the plank that goes *i*-th (1<=≤<=*i*<=≤<=*n*) (from left to right) has index *i*. A piece of the fence from *l* to *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) is a sequence of planks of wood with indices from *l* to *r* inclusive, that is, planks with indices *l*,<=*l*<=+<=1,<=...,<=*r*. The width of the piece of the fence from *l* to *r* is value *r*<=-<=*l*<=+<=1. Two pieces of the fence from *l*1 to *r*1 and from *l*2 to *r*2 are called matching, if the following conditions hold: - the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; - the pieces are of the same width; - for all *i* (0<=≤<=*i*<=≤<=*r*1<=-<=*l*1) the following condition holds: *h**l*1<=+<=*i*<=+<=*h**l*2<=+<=*i*<==<=*h**l*1<=+<=*h**l*2. John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of wood planks in the fence. The second line contains *n* space-separated integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≤<=*h**i*<=≤<=109) — the heights of fence planks. The third line contains integer *q* (1<=≤<=*q*<=≤<=105) — the number of queries. Next *q* lines contain two space-separated integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the boundaries of the *i*-th piece of the fence.
For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.
[ "10\n1 2 2 1 100 99 99 100 100 100\n6\n1 4\n1 2\n3 4\n1 5\n9 10\n10 10\n" ]
[ "1\n2\n2\n0\n2\n9\n" ]
none
[]
46
0
0
47,181
0
none
[ "none" ]
null
null
Programmer Vasya is studying a new programming language &amp;K*. The &amp;K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages. There is a very powerful system of pointers on &amp;K* — you can add an asterisk to the right of the existing type *X* — that will result in new type *X*<=*<=. That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of *X*, which is a pointer, you can add an ampersand — that will result in a type &amp;*X*, to which refers *X*. That is called a dereference operation. The &amp;K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof. - The operator "typedef *A* *B*" defines a new data type *B*, which is equivalent to *A*. *A* can have asterisks and ampersands, and *B* cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**.- The operator "typeof *A*" returns type of *A*, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**. An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype*<==<=&amp;errtype<==<=errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype. Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change. Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &amp;*T*<=*<= is always equal to *T*. Note, that the operators are executed consecutively one by one. If we have two operators "typedef &amp;void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &amp;void* = void (see sample 2). Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of operators. Then follow *n* lines with operators. Each operator is of one of two types: either "typedef *A* *B*", or "typeof *A*". In the first case the *B* type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.
For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.
[ "5\ntypedef void* ptv\ntypeof ptv\ntypedef &amp;&amp;ptv node\ntypeof node\ntypeof &amp;ptv\n", "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &amp;b b\ntypeof b\ntypeof c\ntypedef &amp;&amp;b* c\ntypeof c\ntypedef &amp;b* c\ntypeof c\ntypedef &amp;void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &amp;&amp;b* c\ntypeof c\n" ]
[ "void*\nerrtype\nvoid\n", "void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype\n" ]
Let's look at the second sample. After the first two queries typedef the b type is equivalent to void*, and с — to void**. The next query typedef redefines b — it is now equal to &amp;b = &amp;void* = void. At that, the с type doesn't change. After that the с type is defined as &amp;&amp;b* = &amp;&amp;void* = &amp;void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &amp;void* = void. Then the b type is again redefined as &amp;void = errtype. Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &amp;void******* = void******. The same happens in the last typedef.
[ { "input": "5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv", "output": "void*\nerrtype\nvoid" }, { "input": "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &&b* c\ntypeof c", "output": "void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype" }, { "input": "10\ntypeof void\ntypedef void voiddd\ntypeof &&&&&voiddd*********\ntypeof &&&&&voidddd*********\ntypedef aaaa bbbb\ntypeof bbbb\ntypeof aaaa\ntypedef void** aaaa\ntypeof aaaa\ntypeof bbbb", "output": "void\nvoid****\nerrtype\nerrtype\nerrtype\nvoid**\nerrtype" }, { "input": "10\ntypedef &errtype********* xekls\ntypeof &xekls*\ntypedef xekls*** xekls\ntypeof &xekls********\ntypedef &void*** xekls\ntypeof &xekls\ntypedef &errtype******* dwkmly\ntypeof &&dwkmly******\ntypedef void******* zkpahsnsumbnnzi\ntypeof zkpahsnsumbnnzi*", "output": "errtype\nerrtype\nvoid*\nerrtype\nvoid********" }, { "input": "10\ntypedef errtype** ucywcaykzh\ntypeof &ucywcaykzh*********\ntypedef &&&&&&void********* ucywcaykzh\ntypeof &&&ucywcaykzh******\ntypedef &errtype vfqmtssewklwhiukrz\ntypeof &&vfqmtssewklwhiukrz********\ntypedef &errtype********** xvhxopvh\ntypeof &xvhxopvh\ntypedef &void****** kieypzcclmsvce\ntypeof &&&&&kieypzcclmsvce**", "output": "errtype\nvoid******\nerrtype\nerrtype\nvoid**" }, { "input": "10\ntypedef &&&&&void* mbmrmohbydctgukqbueu\ntypeof &mbmrmohbydctgukqbueu*******\ntypedef &&mbmrmohbydctgukqbueu******* xollxoxrhif\ntypeof xollxoxrhif********\ntypedef &&&&void** zijybfnyxmodoahvvikd\ntypeof zijybfnyxmodoahvvikd****\ntypedef &void**** to\ntypeof &to*******\ntypedef &void******* yhhoqgpnysxvft\ntypeof &&&yhhoqgpnysxvft*******", "output": "errtype\nerrtype\nerrtype\nvoid*********\nvoid**********" }, { "input": "10\ntypedef &&&&void******* xqldryeid\ntypeof &xqldryeid*\ntypedef &void****** frgqt\ntypeof &&frgqt*********\ntypedef &void******* xqldryeid\ntypeof xqldryeid*\ntypedef errtype* xqldryeid\ntypeof &xqldryeid****\ntypedef &&&&&xqldryeid***** cuyhdvkkfyjzjmdkgcf\ntypeof cuyhdvkkfyjzjmdkgcf********", "output": "void***\nvoid************\nvoid*******\nerrtype\nerrtype" }, { "input": "10\ntypedef void**** iizqen\ntypeof iizqen****\ntypedef &void**** gdq\ntypeof &&gdq*********\ntypedef &&errtype******** lhndwyohjckrcew\ntypeof &lhndwyohjckrcew*\ntypedef &&&&void********** ccuoxbgeui\ntypeof ccuoxbgeui\ntypedef &&&&gdq******** gdq\ntypeof gdq******", "output": "void********\nvoid**********\nerrtype\nvoid******\nvoid*************" }, { "input": "10\ntypedef &void**** youdyfpinzk\ntypeof &youdyfpinzk***\ntypedef &&&&youdyfpinzk****** nfbsgpjzhxzskmxc\ntypeof nfbsgpjzhxzskmxc***\ntypedef &&&void*** puerqioirikxej\ntypeof &puerqioirikxej********\ntypedef &puerqioirikxej******** xzgzsamjdufuyxz\ntypeof &xzgzsamjdufuyxz******\ntypedef &&&&xzgzsamjdufuyxz******* hbyqffrbitdgott\ntypeof hbyqffrbitdgott*****", "output": "void*****\nvoid********\nvoid*******\nvoid************\nvoid***************" }, { "input": "10\ntypedef &errtype*** oomxdcottaxn\ntypeof &oomxdcottaxn*********\ntypedef oomxdcottaxn**** bqbigpn\ntypeof &&bqbigpn**********\ntypedef &&&void******** ilrltx\ntypeof &&ilrltx**********\ntypedef void*** yo\ntypeof yo**********\ntypedef oomxdcottaxn*** bqbigpn\ntypeof &bqbigpn****", "output": "errtype\nerrtype\nvoid*************\nvoid*************\nerrtype" }, { "input": "15\ntypedef &void pt\ntypeof pt\ntypeof pt*\ntypedef pt**** err\ntypeof err\ntypeof &err\ntypeof err*\ntypedef &void*** pt\ntypeof err\ntypeof &err\ntypeof err*\ntypeof pt\ntypeof pt*\ntypeof &&pt*\ntypeof &&&pt", "output": "errtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nvoid**\nvoid***\nvoid*\nerrtype" }, { "input": "1\ntypeof a", "output": "errtype" }, { "input": "2\ntypedef void errtypea\ntypeof errtypea", "output": "void" }, { "input": "4\ntypedef void voida\ntypedef voida* voidb\ntypedef voidb* voidc\ntypeof voidc", "output": "void**" } ]
280
0
3
47,199
734
Anton and Digits
[ "brute force", "greedy", "implementation", "math" ]
null
null
Recently Anton found a box with digits in his room. There are *k*2 digits 2, *k*3 digits 3, *k*5 digits 5 and *k*6 digits 6. Anton's favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task! Each digit can be used no more than once, i.e. the composed integers should contain no more than *k*2 digits 2, *k*3 digits 3 and so on. Of course, unused digits are not counted in the sum.
The only line of the input contains four integers *k*2, *k*3, *k*5 and *k*6 — the number of digits 2, 3, 5 and 6 respectively (0<=≤<=*k*2,<=*k*3,<=*k*5,<=*k*6<=≤<=5·106).
Print one integer — maximum possible sum of Anton's favorite integers that can be composed using digits from the box.
[ "5 1 3 4\n", "1 1 1 1\n" ]
[ "800\n", "256\n" ]
In the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer. In the second sample, the optimal answer is to create on integer 256, thus the answer is 256.
[ { "input": "5 1 3 4", "output": "800" }, { "input": "1 1 1 1", "output": "256" }, { "input": "10 2 1 5", "output": "320" }, { "input": "4 2 7 2", "output": "576" }, { "input": "489 292 127 263", "output": "41856" }, { "input": "9557 5242 1190 7734", "output": "472384" }, { "input": "1480320 1969946 1158387 3940412", "output": "306848928" }, { "input": "0 0 0 0", "output": "0" }, { "input": "5000000 5000000 5000000 5000000", "output": "1280000000" }, { "input": "1048576 256 1048576 1048576", "output": "268435456" }, { "input": "2073144 2073145 0 0", "output": "66340608" }, { "input": "1000000 0 0 1", "output": "0" }, { "input": "2 1 1 1", "output": "288" }, { "input": "0 5000000 5000000 5000000", "output": "0" }, { "input": "4494839 1140434 3336818 4921605", "output": "890719296" }, { "input": "2363223 3835613 926184 3190201", "output": "283088352" }, { "input": "198044 2268164 2811743 1458798", "output": "50699264" }, { "input": "5 5 1 0", "output": "160" }, { "input": "1 1 1 4", "output": "256" }, { "input": "3 3 4 4", "output": "768" }, { "input": "1 2 0 5", "output": "32" }, { "input": "1207814 1649617 2347252 3136345", "output": "309200384" }, { "input": "78025 2308643 78025 4943733", "output": "19974400" }, { "input": "3046068 2548438 2676145 4789979", "output": "696930656" }, { "input": "4755258 2724358 2030900 4801065", "output": "607089856" }, { "input": "1359689 3792971 4451626 4497236", "output": "348080384" }, { "input": "3484483 3995744 87159 4941393", "output": "131027072" }, { "input": "1273630 1273630 980163 1711706", "output": "260312672" }, { "input": "2010798 1111442 4014004 4430228", "output": "514764288" }, { "input": "1714940 133067 3346537 3346537", "output": "439024640" }, { "input": "3731658 4548347 3731658 3731658", "output": "955304448" }, { "input": "601597 2632066 450558 450558", "output": "120176096" }, { "input": "726573 158002 568571 568571", "output": "150610240" }, { "input": "407729 4510137 3425929 3425929", "output": "104378624" }, { "input": "1781608 3826276 4384744 4384744", "output": "456091648" }, { "input": "4486284 4486284 3249460 3249460", "output": "871440128" }, { "input": "4759823 3520376 4743363 4743363", "output": "1214827648" }, { "input": "386719 4643763 2749163 1701105", "output": "99000064" }, { "input": "3173901 1042250 4102237 3173901", "output": "812518656" }, { "input": "2681845 4558270 4388852 13014", "output": "88734176" }, { "input": "1152975 910150 2776412 242825", "output": "91288000" }, { "input": "2005366 3807065 4174270 2471686", "output": "513373696" }, { "input": "2092196 2406694 3664886 85601", "output": "86124896" }, { "input": "4542228 4542228 3992410 1039690", "output": "378241856" }, { "input": "3093105 2256347 675644 570209", "output": "218176608" }, { "input": "0 1 1 1", "output": "0" }, { "input": "10 20 10 20", "output": "2560" }, { "input": "0 0 5 1", "output": "0" }, { "input": "10 0 10 11", "output": "2560" }, { "input": "2 1 32768 65536", "output": "512" }, { "input": "2 3 5 6", "output": "512" }, { "input": "5 3 2 2", "output": "608" }, { "input": "1 0 2 3", "output": "256" }, { "input": "1 0 1 1", "output": "256" }, { "input": "2 0 3 3", "output": "512" }, { "input": "2 2 2 0", "output": "64" }, { "input": "0 0 1 1", "output": "0" }, { "input": "1 0 2 2", "output": "256" }, { "input": "4 3 4 4", "output": "1024" }, { "input": "5 1 5 6100", "output": "1280" } ]
46
0
3
47,279
82
General Mobilization
[ "data structures", "dfs and similar", "sortings" ]
C. General Mobilization
2
256
The Berland Kingdom is a set of *n* cities connected with each other with *n*<=-<=1 railways. Each road connects exactly two different cities. The capital is located in city 1. For each city there is a way to get from there to the capital by rail. In the *i*-th city there is a soldier division number *i*, each division is characterized by a number of *a**i*. It represents the priority, the smaller the number, the higher the priority of this division. All values of *a**i* are different. One day the Berland King Berl Great declared a general mobilization, and for that, each division should arrive in the capital. Every day from every city except the capital a train departs. So there are exactly *n*<=-<=1 departing trains each day. Each train moves toward the capital and finishes movement on the opposite endpoint of the railway on the next day. It has some finite capacity of *c**j*, expressed in the maximum number of divisions, which this train can transport in one go. Each train moves in the direction of reducing the distance to the capital. So each train passes exactly one railway moving from a city to the neighboring (where it stops) toward the capital. In the first place among the divisions that are in the city, division with the smallest number of *a**i* get on the train, then with the next smallest and so on, until either the train is full or all the divisions are be loaded. So it is possible for a division to stay in a city for a several days. The duration of train's progress from one city to another is always equal to 1 day. All divisions start moving at the same time and end up in the capital, from where they don't go anywhere else any more. Each division moves along a simple path from its city to the capital, regardless of how much time this journey will take. Your goal is to find for each division, in how many days it will arrive to the capital of Berland. The countdown begins from day 0.
The first line contains the single integer *n* (1<=≤<=*n*<=≤<=5000). It is the number of cities in Berland. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* represents the priority of the division, located in the city number *i*. All numbers *a*1,<=*a*2,<=...,<=*a**n* are different (1<=≤<=*a**i*<=≤<=109). Then *n*<=-<=1 lines contain the descriptions of the railway roads. Each description consists of three integers *v**j*,<=*u**j*,<=*c**j*, where *v**j*, *u**j* are number of cities connected by the *j*-th rail, and *c**j* stands for the maximum capacity of a train riding on this road (1<=≤<=*v**j*,<=*u**j*<=≤<=*n*,<=*v**j*<=≠<=*u**j*, 1<=≤<=*c**j*<=≤<=*n*).
Print sequence *t*1,<=*t*2,<=...,<=*t**n*, where *t**i* stands for the number of days it takes for the division of city *i* to arrive to the capital. Separate numbers with spaces.
[ "4\n40 10 30 20\n1 2 1\n2 3 1\n4 2 1\n", "5\n5 4 3 2 1\n1 2 1\n2 3 1\n2 4 1\n4 5 1\n" ]
[ "0 1 3 2 ", "0 1 4 2 3 " ]
none
[]
2,000
15,257,600
0
47,347
39
Tram
[]
I. Tram
2
64
In a Berland city S*** there is a tram engine house and only one tram. Three people work in the house — the tram driver, the conductor and the head of the engine house. The tram used to leave the engine house every morning and drove along his loop route. The tram needed exactly *c* minutes to complete the route. The head of the engine house controlled the tram’s movement, going outside every *c* minutes when the tram drove by the engine house, and the head left the driver without a bonus if he was even one second late. It used to be so. Afterwards the Berland Federal Budget gave money to make more tramlines in S***, and, as it sometimes happens, the means were used as it was planned. The tramlines were rebuilt and as a result they turned into a huge network. The previous loop route may have been destroyed. S*** has *n* crossroads and now *m* tramlines that links the pairs of crossroads. The traffic in Berland is one way so the tram can move along each tramline only in one direction. There may be several tramlines between two crossroads, which go same way or opposite ways. Every tramline links two different crossroads and for each crossroad there is at least one outgoing tramline. So, the tramlines were built but for some reason nobody gave a thought to increasing the number of trams in S***! The tram continued to ride alone but now the driver had an excellent opportunity to get rid of the unending control of the engine house head. For now due to the tramline network he could choose the route freely! Now at every crossroad the driver can arbitrarily choose the way he can go. The tram may even go to the parts of S*** from where it cannot return due to one way traffic. The driver is not afraid of the challenge: at night, when the city is asleep, he can return to the engine house safely, driving along the tramlines in the opposite direction. The city people were rejoicing for some of the had been waiting for the tram to appear on their streets for several years. However, the driver’s behavior enraged the engine house head. Now he tries to carry out an insidious plan of installing cameras to look after the rebellious tram. The plan goes as follows. The head of the engine house wants to install cameras at some crossroads, to choose a period of time *t* and every *t* minutes turn away from the favourite TV show to check where the tram is. Also the head of the engine house wants at all moments of time, divisible by *t*, and only at such moments the tram to appear on a crossroad under a camera. There must be a camera on the crossroad by the engine house to prevent possible terrorist attacks on the engine house head. Among all the possible plans the engine house head chooses the plan with the largest possible value of *t* (as he hates being distracted from his favourite TV show but he has to). If such a plan is not unique, pick the plan that requires the minimal possible number of cameras. Find such a plan.
The first line contains integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=105) — the number of crossroads and tramlines in S*** respectively. The next *m* lines contain the descriptions of the tramlines in "*u* *v*" format, where *u* is the initial tramline crossroad and *v* is its final crossroad. The crossroads are numbered with integers from 1 to *n*, and the engine house is at the crossroad number 1.
In the first line output the value of *t*. In the next line output the value of *k* — the required number of the cameras. In the next line output space-separated numbers of the crossroads, where the cameras should be installed. Output the numbers in increasing order.
[ "4 5\n1 2\n2 3\n3 4\n4 1\n1 4\n" ]
[ "2\n2\n1 3\n" ]
none
[]
62
0
0
47,666
359
Pair of Numbers
[ "binary search", "brute force", "data structures", "math", "two pointers" ]
null
null
Simon has an array *a*1,<=*a*2,<=...,<=*a**n*, consisting of *n* positive integers. Today Simon asked you to find a pair of integers *l*,<=*r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*), such that the following conditions hold: 1. there is integer *j* (*l*<=≤<=*j*<=≤<=*r*), such that all integers *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* are divisible by *a**j*; 1. value *r*<=-<=*l* takes the maximum value among all pairs for which condition 1 is true; Help Simon, find the required pair of numbers (*l*,<=*r*). If there are multiple required pairs find all of them.
The first line contains integer *n* (1<=≤<=*n*<=≤<=3·105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=106).
Print two integers in the first line — the number of required pairs and the maximum value of *r*<=-<=*l*. On the following line print all *l* values from optimal pairs in increasing order.
[ "5\n4 6 9 3 6\n", "5\n1 3 5 7 9\n", "5\n2 3 5 7 11\n" ]
[ "1 3\n2 \n", "1 4\n1 \n", "5 0\n1 2 3 4 5 \n" ]
In the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3. In the second sample all numbers are divisible by number 1. In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).
[ { "input": "5\n4 6 9 3 6", "output": "1 3\n2 " }, { "input": "5\n1 3 5 7 9", "output": "1 4\n1 " }, { "input": "5\n2 3 5 7 11", "output": "5 0\n1 2 3 4 5 " }, { "input": "1\n1343", "output": "1 0\n1 " }, { "input": "1\n1000000", "output": "1 0\n1 " }, { "input": "1\n5", "output": "1 0\n1 " }, { "input": "1\n1", "output": "1 0\n1 " }, { "input": "2\n1 1000000", "output": "1 1\n1 " }, { "input": "2\n999999 1000000", "output": "2 0\n1 2 " }, { "input": "5\n3 3 6 2 2", "output": "2 2\n1 3 " } ]
794
63,385,600
3
47,848
346
Doodle Jump
[ "math", "number theory" ]
null
null
In Doodle Jump the aim is to guide a four-legged creature called "The Doodler" up a never-ending series of platforms without falling. — Wikipedia. It is a very popular game and xiaodao likes it very much. One day when playing the game she wondered whether there exists a platform that the doodler couldn't reach due to the limits of its jumping ability. Consider the following problem. There are *n* platforms. The height of the *x*-th (1<=≤<=*x*<=≤<=*n*) platform is *a*·*x* mod *p*, where *a* and *p* are positive co-prime integers. The maximum possible height of a Doodler's jump is *h*. That is, it can jump from height *h*1 to height *h*2 (*h*1<=&lt;<=*h*2) if *h*2<=-<=*h*1<=≤<=*h*. Initially, the Doodler is on the ground, the height of which is 0. The question is whether it can reach the highest platform or not. For example, when *a*<==<=7, *n*<==<=4, *p*<==<=12, *h*<==<=2, the heights of the platforms are 7, 2, 9, 4 as in the picture below. With the first jump the Doodler can jump to the platform at height 2, with the second one the Doodler can jump to the platform at height 4, but then it can't jump to any of the higher platforms. So, it can't reach the highest platform. User xiaodao thought about the problem for a long time but didn't solve it, so she asks you for help. Also, she has a lot of instances of the problem. Your task is solve all of these instances.
The first line contains an integer *t* (1<=≤<=*t*<=≤<=104) — the number of problem instances. Each of the next *t* lines contains four integers *a*, *n*, *p* and *h* (1<=≤<=*a*<=≤<=109, 1<=≤<=*n*<=&lt;<=*p*<=≤<=109, 0<=≤<=*h*<=≤<=109). It's guaranteed that *a* and *p* are co-prime.
For each problem instance, if the Doodler can reach the highest platform, output "YES", otherwise output "NO".
[ "3\n7 4 12 2\n7 1 9 4\n7 4 12 3\n" ]
[ "NO\nNO\nYES\n" ]
none
[]
343
0
0
47,919
815
Karen and Supermarket
[ "brute force", "dp", "trees" ]
null
null
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to *b* dollars. The supermarket sells *n* goods. The *i*-th good can be bought for *c**i* dollars. Of course, each good can only be bought once. Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given *n* coupons. If Karen purchases the *i*-th good, she can use the *i*-th coupon to decrease its price by *d**i*. Of course, a coupon cannot be used without buying the corresponding good. There is, however, a constraint with the coupons. For all *i*<=≥<=2, in order to use the *i*-th coupon, Karen must also use the *x**i*-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon). Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget *b*?
The first line of input contains two integers *n* and *b* (1<=≤<=*n*<=≤<=5000, 1<=≤<=*b*<=≤<=109), the number of goods in the store and the amount of money Karen has, respectively. The next *n* lines describe the items. Specifically: - The *i*-th line among these starts with two integers, *c**i* and *d**i* (1<=≤<=*d**i*<=&lt;<=*c**i*<=≤<=109), the price of the *i*-th good and the discount when using the coupon for the *i*-th good, respectively. - If *i*<=≥<=2, this is followed by another integer, *x**i* (1<=≤<=*x**i*<=&lt;<=*i*), denoting that the *x**i*-th coupon must also be used before this coupon can be used.
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
[ "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5\n", "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4\n" ]
[ "4\n", "5\n" ]
In the first test case, Karen can purchase the following 4 items: - Use the first coupon to buy the first item for 10 - 9 = 1 dollar. - Use the third coupon to buy the third item for 12 - 2 = 10 dollars. - Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars. - Buy the sixth item for 2 dollars. The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here. In the second test case, Karen has enough money to use all the coupons and purchase everything.
[ { "input": "6 16\n10 9\n10 5 1\n12 2 1\n20 18 3\n10 2 3\n2 1 5", "output": "4" }, { "input": "5 10\n3 1\n3 1 1\n3 1 2\n3 1 3\n3 1 4", "output": "5" }, { "input": "13 30\n6 4\n25 5 1\n7 1 2\n9 4 2\n10 2 1\n12 3 1\n5 2 3\n10 9 6\n2 1 1\n5 3 9\n10 2 10\n10 9 6\n3 2 11", "output": "9" }, { "input": "8 9\n4 3\n8 3 1\n2 1 1\n4 2 2\n7 2 2\n3 1 2\n7 3 5\n2 1 3", "output": "4" }, { "input": "9 15\n3 1\n6 2 1\n8 3 2\n4 1 2\n2 1 2\n3 2 3\n8 7 1\n6 5 5\n8 4 4", "output": "7" }, { "input": "15 1000\n449 257\n881 657 1\n182 101 1\n733 545 2\n277 13 2\n991 689 3\n360 302 3\n965 570 4\n502 178 4\n43 28 5\n446 406 5\n484 152 6\n451 335 6\n874 600 7\n602 34 7", "output": "7" }, { "input": "9 7\n3 1\n6 2 1\n8 3 2\n4 1 2\n2 1 2\n3 2 3\n8 7 1\n6 5 5\n8 4 4", "output": "3" }, { "input": "3 100\n100 48\n50 1 1\n50 1 1", "output": "2" }, { "input": "1 1\n2 1", "output": "1" }, { "input": "1 1\n1000000000 1", "output": "0" }, { "input": "1 1000000000\n1000000000 1", "output": "1" }, { "input": "2 1000000000\n500000001 1\n500000001 1 1", "output": "2" } ]
1,481
536,883,200
0
47,970
236
Easy Number Challenge
[ "implementation", "number theory" ]
null
null
Let's denote *d*(*n*) as the number of divisors of a positive integer *n*. You are given three integers *a*, *b* and *c*. Your task is to calculate the following sum: Find the sum modulo 1073741824 (230).
The first line contains three space-separated integers *a*, *b* and *c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=100).
Print a single integer — the required sum modulo 1073741824 (230).
[ "2 2 2\n", "5 6 7\n" ]
[ "20\n", "1520\n" ]
For the first example. - *d*(1·1·1) = *d*(1) = 1; - *d*(1·1·2) = *d*(2) = 2; - *d*(1·2·1) = *d*(2) = 2; - *d*(1·2·2) = *d*(4) = 3; - *d*(2·1·1) = *d*(2) = 2; - *d*(2·1·2) = *d*(4) = 3; - *d*(2·2·1) = *d*(4) = 3; - *d*(2·2·2) = *d*(8) = 4. So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20.
[ { "input": "2 2 2", "output": "20" }, { "input": "5 6 7", "output": "1520" }, { "input": "91 42 25", "output": "3076687" }, { "input": "38 47 5", "output": "160665" }, { "input": "82 29 45", "output": "3504808" }, { "input": "40 15 33", "output": "460153" }, { "input": "35 5 21", "output": "55282" }, { "input": "71 2 1", "output": "811" }, { "input": "22 44 41", "output": "1063829" }, { "input": "73 19 29", "output": "1047494" }, { "input": "76 12 17", "output": "330197" }, { "input": "16 10 49", "output": "146199" }, { "input": "59 99 33", "output": "7052988" }, { "input": "17 34 25", "output": "306673" }, { "input": "21 16 9", "output": "45449" }, { "input": "31 51 29", "output": "1255099" }, { "input": "26 41 17", "output": "402568" }, { "input": "85 19 5", "output": "139747" }, { "input": "36 61 45", "output": "3253358" }, { "input": "76 58 25", "output": "3635209" }, { "input": "71 48 13", "output": "1179722" }, { "input": "29 34 53", "output": "1461871" }, { "input": "72 16 41", "output": "1309118" }, { "input": "8 21 21", "output": "54740" }, { "input": "11 51 5", "output": "38092" }, { "input": "70 38 49", "output": "4467821" }, { "input": "13 31 33", "output": "274773" }, { "input": "53 29 17", "output": "621991" }, { "input": "56 18 53", "output": "1518698" }, { "input": "55 45 45", "output": "3751761" }, { "input": "58 35 29", "output": "1706344" }, { "input": "67 2 24", "output": "45108" }, { "input": "62 96 8", "output": "1257040" }, { "input": "21 22 100", "output": "1274891" }, { "input": "64 12 36", "output": "687986" }, { "input": "4 9 20", "output": "7302" }, { "input": "7 99 4", "output": "36791" }, { "input": "58 25 96", "output": "4812548" }, { "input": "9 19 32", "output": "91192" }, { "input": "45 16 12", "output": "167557" }, { "input": "40 6 100", "output": "558275" }, { "input": "46 93 44", "output": "6945002" }, { "input": "49 31 28", "output": "1158568" }, { "input": "89 28 8", "output": "441176" }, { "input": "84 17 96", "output": "4615400" }, { "input": "91 96 36", "output": "12931148" }, { "input": "86 90 24", "output": "6779764" }, { "input": "4 21 45", "output": "58045" }, { "input": "100 7 28", "output": "429933" }, { "input": "58 41 21", "output": "1405507" }, { "input": "53 31 5", "output": "144839" }, { "input": "41 28 36", "output": "1135934" }, { "input": "44 18 24", "output": "436880" }, { "input": "3 96 16", "output": "70613" }, { "input": "98 34 100", "output": "13589991" }, { "input": "82 31 32", "output": "2502213" }, { "input": "85 25 20", "output": "1142825" }, { "input": "35 12 8", "output": "50977" }, { "input": "39 94 48", "output": "6368273" }, { "input": "27 99 28", "output": "2276216" }, { "input": "22 28 16", "output": "198639" }, { "input": "80 15 4", "output": "76139" }, { "input": "23 9 44", "output": "170773" }, { "input": "33 16 36", "output": "441858" }, { "input": "36 6 24", "output": "88626" }, { "input": "98 92 12", "output": "3475151" }, { "input": "90 82 100", "output": "35482866" }, { "input": "77 79 31", "output": "6870344" }, { "input": "81 21 19", "output": "812886" }, { "input": "31 96 7", "output": "458123" }, { "input": "34 89 95", "output": "11308813" }, { "input": "18 86 27", "output": "1116623" }, { "input": "13 76 11", "output": "206844" }, { "input": "76 3 3", "output": "6118" }, { "input": "15 93 87", "output": "4007595" }, { "input": "63 90 23", "output": "4384553" }, { "input": "58 83 7", "output": "819473" }, { "input": "16 18 99", "output": "702678" }, { "input": "60 8 35", "output": "363723" }, { "input": "22 87 4", "output": "133986" }, { "input": "73 25 44", "output": "2478308" }, { "input": "36 3 32", "output": "50842" }, { "input": "27 93 20", "output": "1393947" }, { "input": "67 90 100", "output": "27880104" }, { "input": "18 84 36", "output": "1564297" }, { "input": "68 14 28", "output": "646819" }, { "input": "71 8 12", "output": "119311" }, { "input": "7 5 96", "output": "46328" }, { "input": "50 95 32", "output": "5324602" }, { "input": "13 22 24", "output": "124510" }, { "input": "4 12 8", "output": "3347" }, { "input": "100 9 88", "output": "2334910" }, { "input": "95 2 28", "output": "82723" }, { "input": "54 77 20", "output": "2573855" }, { "input": "49 19 4", "output": "55037" }, { "input": "58 86 99", "output": "21920084" }, { "input": "9 76 83", "output": "1554836" }, { "input": "64 2 27", "output": "49141" }, { "input": "63 96 11", "output": "1898531" }, { "input": "3 93 91", "output": "555583" }, { "input": "100 100 100", "output": "51103588" }, { "input": "1 5 1", "output": "10" } ]
2,000
0
0
47,988
599
Spongebob and Squares
[ "brute force", "math" ]
null
null
Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly *x* distinct squares in the table consisting of *n* rows and *m* columns. For example, in a 3<=×<=5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3<=×<=5 table is 15<=+<=8<=+<=3<==<=26.
The first line of the input contains a single integer *x* (1<=≤<=*x*<=≤<=1018) — the number of squares inside the tables Spongebob is interested in.
First print a single integer *k* — the number of tables with exactly *x* distinct squares inside. Then print *k* pairs of integers describing the tables. Print the pairs in the order of increasing *n*, and in case of equality — in the order of increasing *m*.
[ "26\n", "2\n", "8\n" ]
[ "6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1\n", "2\n1 2\n2 1\n", "4\n1 8\n2 3\n3 2\n8 1\n" ]
In a 1 × 2 table there are 2 1 × 1 squares. So, 2 distinct squares in total. In a 2 × 3 table there are 6 1 × 1 squares and 2 2 × 2 squares. That is equal to 8 squares in total.
[ { "input": "26", "output": "6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1" }, { "input": "2", "output": "2\n1 2\n2 1" }, { "input": "8", "output": "4\n1 8\n2 3\n3 2\n8 1" }, { "input": "1", "output": "1\n1 1" }, { "input": "5005", "output": "12\n1 5005\n5 335\n6 240\n10 94\n13 59\n14 52\n52 14\n59 13\n94 10\n240 6\n335 5\n5005 1" }, { "input": "17284", "output": "2\n1 17284\n17284 1" }, { "input": "151618", "output": "2\n1 151618\n151618 1" }, { "input": "360700", "output": "8\n1 360700\n4 36071\n5 24048\n24 1210\n1210 24\n24048 5\n36071 4\n360700 1" }, { "input": "500500500", "output": "8\n1 500500500\n4 50050051\n8 13902794\n9 11122236\n11122236 9\n13902794 8\n50050051 4\n500500500 1" }, { "input": "200200", "output": "26\n1 200200\n4 20021\n5 13348\n6 9535\n7 7152\n10 3643\n13 2204\n14 1911\n15 1673\n24 675\n25 624\n55 148\n77 92\n92 77\n148 55\n624 25\n675 24\n1673 15\n1911 14\n2204 13\n3643 10\n7152 7\n9535 6\n13348 5\n20021 4\n200200 1" }, { "input": "800800", "output": "32\n1 800800\n4 80081\n5 53388\n6 38135\n7 28602\n10 14563\n13 8804\n14 7631\n15 6678\n24 2677\n25 2472\n32 1527\n55 538\n64 406\n77 292\n104 181\n181 104\n292 77\n406 64\n538 55\n1527 32\n2472 25\n2677 24\n6678 15\n7631 14\n8804 13\n14563 10\n28602 7\n38135 6\n53388 5\n80081 4\n800800 1" }, { "input": "200000800200", "output": "4\n1 200000800200\n4 20000080021\n20000080021 4\n200000800200 1" }, { "input": "999999999999999999", "output": "6\n1 999999999999999999\n13 10989010989010993\n37 1422475106685645\n1422475106685645 37\n10989010989010993 13\n999999999999999999 1" }, { "input": "128593726482159", "output": "2\n1 128593726482159\n128593726482159 1" }, { "input": "50044422", "output": "2\n1 50044422\n50044422 1" }, { "input": "18", "output": "2\n1 18\n18 1" }, { "input": "30", "output": "3\n1 30\n4 4\n30 1" }, { "input": "20", "output": "6\n1 20\n2 7\n3 4\n4 3\n7 2\n20 1" }, { "input": "649708734844", "output": "2\n1 649708734844\n649708734844 1" }, { "input": "649030984", "output": "8\n1 649030984\n6 30906239\n7 23179680\n41 753824\n753824 41\n23179680 7\n30906239 6\n649030984 1" }, { "input": "333333333", "output": "2\n1 333333333\n333333333 1" }, { "input": "5050505060", "output": "8\n1 5050505060\n2 1683501687\n3 841750844\n4 505050507\n505050507 4\n841750844 3\n1683501687 2\n5050505060 1" }, { "input": "1000000000000000000", "output": "10\n1 1000000000000000000\n4 100000000000000001\n5 66666666666666668\n15 8333333333333338\n24 3333333333333341\n3333333333333341 24\n8333333333333338 15\n66666666666666668 5\n100000000000000001 4\n1000000000000000000 1" }, { "input": "18270000000000", "output": "10\n1 18270000000000\n4 1827000000001\n7 652500000002\n27 48333333342\n28 45000000009\n45000000009 28\n48333333342 27\n652500000002 7\n1827000000001 4\n18270000000000 1" }, { "input": "10102030405090000", "output": "16\n1 10102030405090000\n4 1010203040509001\n5 673468693672668\n15 84183586709088\n24 33673434683641\n25 31083170477208\n40 12319549274513\n499 80978199806\n80978199806 499\n12319549274513 40\n31083170477208 25\n33673434683641 24\n84183586709088 15\n673468693672668 5\n1010203040509001 4\n10102030405090000 1" }, { "input": "50004000222222228", "output": "10\n1 50004000222222228\n7 1785857150793653\n8 1389000006172842\n13 549494507936512\n117 7243807072648\n7243807072648 117\n549494507936512 13\n1389000006172842 8\n1785857150793653 7\n50004000222222228 1" }, { "input": "9000004000200000", "output": "8\n1 9000004000200000\n4 900000400020001\n8 250000111116669\n9 200000088893336\n200000088893336 9\n250000111116669 8\n900000400020001 4\n9000004000200000 1" }, { "input": "147456000000000", "output": "4\n1 147456000000000\n4 14745600000001\n14745600000001 4\n147456000000000 1" }, { "input": "80000010000020000", "output": "12\n1 80000010000020000\n2 26666670000006667\n3 13333335000003334\n4 8000001000002001\n10 1454545636364003\n11 1212121363636670\n1212121363636670 11\n1454545636364003 10\n8000001000002001 4\n13333335000003334 3\n26666670000006667 2\n80000010000020000 1" }, { "input": "8888", "output": "8\n1 8888\n2 2963\n3 1482\n11 138\n138 11\n1482 3\n2963 2\n8888 1" }, { "input": "22003000000000000", "output": "10\n1 22003000000000000\n4 2200300000000001\n5 1466866666666668\n15 183358333333338\n24 73343333333341\n73343333333341 24\n183358333333338 15\n1466866666666668 5\n2200300000000001 4\n22003000000000000 1" }, { "input": "40040", "output": "24\n1 40040\n2 13347\n3 6674\n4 4005\n7 1432\n10 731\n11 610\n12 517\n13 444\n20 197\n21 180\n39 64\n64 39\n180 21\n197 20\n444 13\n517 12\n610 11\n731 10\n1432 7\n4005 4\n6674 3\n13347 2\n40040 1" }, { "input": "1000000400", "output": "20\n1 1000000400\n2 333333467\n3 166666734\n4 100000041\n7 35714302\n19 5263166\n20 4761913\n56 626585\n75 350902\n399 12664\n12664 399\n350902 75\n626585 56\n4761913 20\n5263166 19\n35714302 7\n100000041 4\n166666734 3\n333333467 2\n1000000400 1" }, { "input": "5000004100", "output": "30\n1 5000004100\n4 500000411\n5 333333608\n6 238095435\n7 178571577\n13 54945104\n14 47619091\n24 16666688\n25 15384636\n49 4081652\n52 3628467\n104 915786\n105 898508\n195 261708\n636 24895\n24895 636\n261708 195\n898508 105\n915786 104\n3628467 52\n4081652 49\n15384636 25\n16666688 24\n47619091 14\n54945104 13\n178571577 7\n238095435 6\n333333608 5\n500000411 4\n5000004100 1" }, { "input": "700000000700", "output": "44\n1 700000000700\n2 233333333567\n3 116666666784\n4 70000000071\n7 25000000027\n10 12727272743\n11 10606060620\n12 8974358987\n13 7692307704\n19 3684210536\n20 3333333343\n21 3030303040\n25 2153846164\n38 944669379\n39 897435911\n49 571428588\n55 454545473\n56 438596510\n65 326340348\n75 245614060\n76 239234475\n209 31897996\n31897996 209\n239234475 76\n245614060 75\n326340348 65\n438596510 56\n454545473 55\n571428588 49\n897435911 39\n944669379 38\n2153846164 25\n3030303040 21\n3333333343 20\n3684210536..." }, { "input": "613400018000", "output": "44\n1 613400018000\n2 204466672667\n3 102233336334\n4 61340001801\n7 21907143502\n10 11152727603\n11 9293939670\n16 4510294255\n20 2920952473\n21 2655411340\n22 2424506007\n34 1030924411\n55 398311718\n84 171820761\n119 85910406\n160 47624278\n175 39831228\n183 36433894\n560 3905203\n4025 77048\n4675 57678\n7014 27271\n27271 7014\n57678 4675\n77048 4025\n3905203 560\n36433894 183\n39831228 175\n47624278 160\n85910406 119\n171820761 84\n398311718 55\n1030924411 34\n2424506007 22\n2655411340 21\n2920952473 2..." }, { "input": "85546414177840", "output": "38\n1 85546414177840\n4 8554641417785\n5 5703094278524\n6 4073638770375\n7 3055229077782\n10 1555389348691\n14 814727754079\n15 712886784820\n22 338128119287\n23 309950776014\n28 210705453649\n32 162019723832\n55 55549619614\n69 35422945852\n87 22347548142\n115 12825549390\n160 6641802396\n230 3220267879\n231 3192506950\n3192506950 231\n3220267879 230\n6641802396 160\n12825549390 115\n22347548142 87\n35422945852 69\n55549619614 55\n162019723832 32\n210705453649 28\n309950776014 23\n338128119287 22\n7128867..." }, { "input": "90006000426440", "output": "30\n1 90006000426440\n2 30002000142147\n3 15001000071074\n4 9000600042645\n7 3214500015232\n16 661808826670\n20 428600002037\n34 151270588963\n48 76535714664\n84 25211764853\n111 14479729835\n119 12605882452\n147 8274131362\n628 455712949\n784 292493438\n292493438 784\n455712949 628\n8274131362 147\n12605882452 119\n14479729835 111\n25211764853 84\n76535714664 48\n151270588963 34\n428600002037 20\n661808826670 16\n3214500015232 7\n9000600042645 4\n15001000071074 3\n30002000142147 2\n90006000426440 1" }, { "input": "90008000001920", "output": "30\n1 90008000001920\n2 30002666667307\n3 15001333333654\n4 9000800000193\n7 3214571428642\n10 1636509090947\n11 1363757575790\n12 1153948717977\n13 989098901124\n20 428609523825\n21 389645021660\n39 115394871810\n55 58446753266\n64 43273076945\n65 41961771584\n41961771584 65\n43273076945 64\n58446753266 55\n115394871810 39\n389645021660 21\n428609523825 20\n989098901124 13\n1153948717977 12\n1363757575790 11\n1636509090947 10\n3214571428642 7\n9000800000193 4\n15001333333654 3\n30002666667307 2\n900080000..." }, { "input": "6466460", "output": "46\n1 6466460\n2 2155487\n3 1077744\n4 646647\n7 230947\n10 117575\n11 97980\n12 82907\n13 71064\n19 34040\n20 30799\n21 28000\n34 10879\n38 8739\n39 8303\n55 4217\n56 4070\n65 3036\n76 2235\n84 1839\n119 945\n209 364\n220 339\n339 220\n364 209\n945 119\n1839 84\n2235 76\n3036 65\n4070 56\n4217 55\n8303 39\n8739 38\n10879 34\n28000 21\n30799 20\n34040 19\n71064 13\n82907 12\n97980 11\n117575 10\n230947 7\n646647 4\n1077744 3\n2155487 2\n6466460 1" }, { "input": "30009980", "output": "28\n1 30009980\n2 10003327\n3 5001664\n4 3000999\n7 1071787\n10 545639\n11 454700\n12 384747\n13 329784\n20 142911\n21 129920\n39 38487\n55 19505\n65 14012\n14012 65\n19505 55\n38487 39\n129920 21\n142911 20\n329784 13\n384747 12\n454700 11\n545639 10\n1071787 7\n3000999 4\n5001664 3\n10003327 2\n30009980 1" }, { "input": "800008300", "output": "28\n1 800008300\n4 80000831\n5 53333888\n6 38095635\n7 28571727\n13 8791304\n14 7619131\n19 4210576\n24 2666702\n25 2461572\n49 653084\n104 146556\n195 41928\n455 7863\n7863 455\n41928 195\n146556 104\n653084 49\n2461572 25\n2666702 24\n4210576 19\n7619131 14\n8791304 13\n28571727 7\n38095635 6\n53333888 5\n80000831 4\n800008300 1" }, { "input": "801021760", "output": "48\n1 801021760\n4 80102177\n5 53401452\n6 38143895\n7 28607922\n10 14564035\n14 7628783\n15 6675186\n19 4215910\n28 1972969\n32 1517097\n55 520162\n58 468179\n59 452574\n76 273785\n87 209282\n95 175694\n132 91297\n176 51485\n231 29970\n319 15800\n384 10964\n608 4529\n1120 1649\n1649 1120\n4529 608\n10964 384\n15800 319\n29970 231\n51485 176\n91297 132\n175694 95\n209282 87\n273785 76\n452574 59\n468179 58\n520162 55\n1517097 32\n1972969 28\n4215910 19\n6675186 15\n7628783 14\n14564035 10\n28607922 7\n3814..." }, { "input": "808007200", "output": "34\n1 808007200\n4 80800721\n5 53867148\n6 38476535\n7 28857402\n10 14691043\n13 8879204\n14 7695311\n15 6733398\n24 2693365\n25 2486184\n32 1530327\n55 524698\n64 388486\n77 269092\n104 148021\n175 52526\n52526 175\n148021 104\n269092 77\n388486 64\n524698 55\n1530327 32\n2486184 25\n2693365 24\n6733398 15\n7695311 14\n8879204 13\n14691043 10\n28857402 7\n38476535 6\n53867148 5\n80800721 4\n808007200 1" }, { "input": "900601520", "output": "34\n1 900601520\n2 300200507\n3 150100254\n4 90060153\n7 32164342\n12 11546177\n13 9896724\n16 6622075\n19 4740014\n20 4288585\n34 1513627\n38 1215399\n39 1154630\n56 564305\n84 252297\n119 126174\n272 24347\n24347 272\n126174 119\n252297 84\n564305 56\n1154630 39\n1215399 38\n1513627 34\n4288585 20\n4740014 19\n6622075 16\n9896724 13\n11546177 12\n32164342 7\n90060153 4\n150100254 3\n300200507 2\n900601520 1" }, { "input": "983282300", "output": "48\n1 983282300\n2 327760767\n3 163880384\n4 98328231\n7 35117227\n10 17877863\n11 14898220\n12 12606187\n13 10805304\n19 5175176\n20 4682303\n21 4256640\n25 3025492\n38 1326979\n39 1260631\n55 638513\n56 616110\n65 458428\n75 345036\n76 336075\n94 220251\n120 135478\n209 44876\n363 15004\n15004 363\n44876 209\n135478 120\n220251 94\n336075 76\n345036 75\n458428 65\n616110 56\n638513 55\n1260631 39\n1326979 38\n3025492 25\n4256640 21\n4682303 20\n5175176 19\n10805304 13\n12606187 12\n14898220 11\n17877863 ..." }, { "input": "10152342200", "output": "70\n1 10152342200\n2 3384114067\n3 1692057034\n4 1015234221\n7 362583652\n10 184588043\n11 153823370\n12 130158237\n13 111564204\n16 74649580\n19 53433386\n20 48344493\n21 43949540\n25 31237984\n34 17062771\n38 13700879\n39 13015836\n55 6592448\n56 6361135\n65 4733048\n75 3562250\n76 3469725\n84 2843821\n119 1421936\n156 829085\n175 659301\n208 467144\n209 462696\n220 417693\n272 273532\n399 127355\n475 89962\n560 64818\n714 40011\n1099 17162\n17162 1099\n40011 714\n64818 560\n89962 475\n127355 399\n273532..." }, { "input": "10203693500", "output": "74\n1 10203693500\n2 3401231167\n3 1700615584\n4 1020369351\n7 364417627\n10 185521703\n11 154601420\n12 130816587\n13 112128504\n19 53703656\n20 48589023\n21 44171840\n25 31395988\n28 25132259\n29 23456776\n37 14514512\n38 13770179\n39 13081671\n55 6625793\n56 6393310\n57 6172852\n65 4756988\n74 3677031\n75 3580268\n76 3487275\n110 1671403\n174 670251\n209 465036\n259 303136\n406 123635\n550 67523\n740 37463\n1000 20720\n1595 8548\n1624 8274\n1924 6151\n2145 5148\n5148 2145\n6151 1924\n8274 1624\n8548 159..." }, { "input": "20202582400", "output": "66\n1 20202582400\n4 2020258241\n5 1346838828\n6 962027735\n7 721520802\n10 367319683\n13 222006404\n14 192405551\n15 168354858\n16 148548405\n24 67341949\n25 62161800\n32 38262477\n33 36011744\n34 33953931\n49 16491920\n50 15845179\n51 15235750\n52 14660817\n55 13118578\n64 9712801\n77 6727492\n104 3700141\n105 3630328\n159 1588306\n175 1311914\n195 1057238\n220 831113\n384 273431\n424 224365\n636 99945\n832 58577\n1274 25299\n25299 1274\n58577 832\n99945 636\n224365 424\n273431 384\n831113 220\n1057238 1..." }, { "input": "30224994800", "output": "68\n1 30224994800\n2 10074998267\n3 5037499134\n4 3022499481\n7 1079464102\n10 549545363\n11 457954470\n12 387499937\n13 332142804\n19 159078926\n20 143928553\n21 130844140\n25 92999992\n28 74445809\n29 69482756\n38 40789479\n39 38750006\n55 19626638\n56 18937985\n57 18284952\n65 14090928\n75 10605286\n76 10329825\n174 1985279\n175 1962720\n208 1390619\n209 1377376\n273 808224\n274 802347\n399 378892\n550 199655\n1507 27102\n1595 24278\n2639 9556\n9556 2639\n24278 1595\n27102 1507\n199655 550\n378892 399\n..." }, { "input": "500600123456789", "output": "4\n1 500600123456789\n2 166866707818930\n166866707818930 2\n500600123456789 1" }, { "input": "600201602000", "output": "66\n1 600201602000\n2 200067200667\n3 100033600334\n4 60020160201\n7 21435771502\n10 10912756403\n11 9093963670\n12 7694892337\n13 6595622004\n19 3158955806\n20 2858102873\n21 2598275340\n25 1846774168\n30 1290756143\n31 1210083885\n38 809988679\n39 769489246\n55 389741318\n56 376066185\n65 279814288\n75 210597078\n76 205127025\n124 77445409\n154 50289251\n155 49644518\n175 38974188\n208 27613319\n209 27350336\n247 19596582\n399 7521456\n650 2837043\n1000 1199537\n5599 40151\n40151 5599\n1199537 1000\n2837..." }, { "input": "8080214542400", "output": "58\n1 8080214542400\n2 2693404847467\n3 1346702423734\n4 808021454241\n7 288579090802\n10 146912991683\n11 122427493070\n12 103592494137\n13 88793566404\n19 42527444966\n20 38477212113\n21 34979283740\n25 24862198600\n38 10904473079\n39 10359249426\n48 6870930749\n49 6596093520\n55 5246892578\n56 5062791085\n64 3884718551\n65 3766999808\n75 2835163022\n76 2761522425\n175 524689314\n208 371743469\n209 368203056\n399 101255954\n3135 1644808\n13376 94775\n94775 13376\n1644808 3135\n101255954 399\n368203056 20..." }, { "input": "99080001620600", "output": "68\n1 99080001620600\n2 33026667206867\n3 16513333603434\n4 9908000162061\n7 3538571486452\n10 1801454574923\n11 1501212145770\n12 1270256431037\n13 1088791226604\n19 521473692746\n20 471809531533\n21 428917755940\n25 304861543456\n38 133711203279\n39 127025643116\n43 104735731114\n48 84251702074\n49 80881633992\n55 64337663408\n56 62080201535\n65 46191142968\n75 34764912874\n76 33861928125\n120 13647383183\n129 11816338936\n175 6433766397\n208 4558336544\n209 4514923816\n300 2194463037\n363 1499712554\n39..." }, { "input": "83890006360160", "output": "58\n1 83890006360160\n2 27963335453387\n3 13981667726694\n4 8389000636017\n7 2996071655722\n10 1525272842915\n11 1271060702430\n12 1075512902057\n13 921868201764\n16 616838282065\n19 441526349270\n20 399476220769\n21 363160200700\n34 140991607339\n38 113211884439\n39 107551290218\n55 54474030122\n56 52562660645\n64 40331733848\n65 39109560096\n76 28670542185\n84 23498601249\n119 11749300650\n208 3859496129\n209 3822739024\n220 3450843609\n272 2259480977\n560 534059305\n714 328652003\n328652003 714\n5340593..." }, { "input": "1457770000887200", "output": "82\n1 1457770000887200\n2 485923333629067\n3 242961666814534\n4 145777000088721\n7 52063214317402\n10 26504909107043\n11 22087424255870\n12 18689358985737\n13 16019450559204\n19 7672473688886\n20 6941761908993\n21 6310692644540\n22 5761936762407\n25 4485446156584\n38 1967300945879\n39 1868935898586\n46 1348538391215\n47 1292349291582\n55 946603896698\n56 913389724885\n64 700850961986\n65 679613054048\n75 511498245950\n76 498212577225\n91 348248925230\n94 326488242111\n160 113180900743\n175 94660389726\n208..." }, { "input": "380060000009189500", "output": "68\n1 380060000009189500\n4 38006000000918951\n5 25337333333945968\n6 18098095238532835\n7 13573571428899627\n10 6910181818348903\n13 4176483516584504\n14 3619619047706571\n19 2000315789522056\n22 1502213438771507\n23 1377028985540549\n24 1266866666697306\n25 1169415384643668\n42 420885935779847\n43 401754756880764\n55 246792207798193\n69 157374741204656\n76 129890635683275\n77 126560106563192\n91 90793119925780\n104 69608058609776\n114 57980167812271\n132 43296878561127\n230 14306794655043\n275 1001475625..." }, { "input": "4729310003500000", "output": "10\n1 4729310003500000\n4 472931000350001\n5 315287333566668\n15 39410916695838\n24 15764366678341\n15764366678341 24\n39410916695838 15\n315287333566668 5\n472931000350001 4\n4729310003500000 1" }, { "input": "590084357100000000", "output": "20\n1 590084357100000000\n4 59008435710000001\n7 21074441325000002\n8 16391232141666669\n9 13112985713333336\n35 936641836666678\n49 481701516000016\n63 292700573958354\n224 23416045916741\n39374 761239791\n761239791 39374\n23416045916741 224\n292700573958354 63\n481701516000016 49\n936641836666678 35\n13112985713333336 9\n16391232141666669 8\n21074441325000002 7\n59008435710000001 4\n590084357100000000 1" }, { "input": "2937500926541895", "output": "2\n1 2937500926541895\n2937500926541895 1" }, { "input": "400000089000000000", "output": "10\n1 400000089000000000\n4 40000008900000001\n16 2941177125000005\n17 2614379666666672\n127 49212609375042\n49212609375042 127\n2614379666666672 17\n2941177125000005 16\n40000008900000001 4\n400000089000000000 1" }, { "input": "400000089006618100", "output": "54\n1 400000089006618100\n4 40000008900661811\n5 26666672600441208\n6 19047623286029435\n7 14285717464522077\n10 7272728891029423\n13 4395605373699104\n14 3809524657205891\n24 1333333630022068\n25 1230769504635756\n33 713012636375444\n34 672269057153991\n42 442967983396047\n43 422833075059864\n50 313725560005207\n51 301659192312700\n55 259740317536783\n59 225988750851216\n77 133200162839392\n85 109439148839048\n104 73260089561686\n118 56971953996139\n220 16454137762583\n429 4336749487936\n472 3583331144732..." }, { "input": "5", "output": "3\n1 5\n2 2\n5 1" }, { "input": "200800200", "output": "4\n1 200800200\n4 20080021\n20080021 4\n200800200 1" }, { "input": "999999853754584125", "output": "5\n1 999999853754584125\n45 966183433579323\n1442249 1442249\n966183433579323 45\n999999853754584125 1" }, { "input": "114335783345000", "output": "13\n1 114335783345000\n2 38111927781667\n3 19055963890834\n4 11433578334501\n7 4083420833752\n20 544456111173\n70000 70000\n544456111173 20\n4083420833752 7\n11433578334501 4\n19055963890834 3\n38111927781667 2\n114335783345000 1" }, { "input": "333343833443500385", "output": "7\n1 333343833443500385\n2 111114611147833462\n10 6060796971700010\n1000010 1000010\n6060796971700010 10\n111114611147833462 2\n333343833443500385 1" }, { "input": "333336333342000008", "output": "8\n1 333336333342000008\n2 111112111114000003\n3 55556055557000002\n1000002 1000003\n1000003 1000002\n55556055557000002 3\n111112111114000003 2\n333336333342000008 1" }, { "input": "41679167500", "output": "11\n1 41679167500\n4 4167916751\n5 2778611168\n24 138930566\n1095 69823\n5000 5000\n69823 1095\n138930566 24\n2778611168 5\n4167916751 4\n41679167500 1" }, { "input": "333357833933504900", "output": "15\n1 333357833933504900\n2 111119277977834967\n3 55559638988917484\n4 33335783393350491\n12 4273818383762887\n25 1025716412103100\n39 427381838376301\n1000024 1000024\n427381838376301 39\n1025716412103100 25\n4273818383762887 12\n33335783393350491 4\n55559638988917484 3\n111119277977834967 2\n333357833933504900 1" }, { "input": "2666686666700000", "output": "13\n1 2666686666700000\n2 888895555566667\n3 444447777783334\n4 266668666670001\n7 95238809525002\n20 12698507936673\n200000 200000\n12698507936673 20\n95238809525002 7\n266668666670001 4\n444447777783334 3\n888895555566667 2\n2666686666700000 1" }, { "input": "334344854787443885", "output": "15\n1 334344854787443885\n2 111448284929147962\n10 6078997359771710\n21 1447380323755175\n34 561924125693194\n101 64908727390335\n15554 2763842842\n1001010 1001010\n2763842842 15554\n64908727390335 101\n561924125693194 34\n1447380323755175 21\n6078997359771710 10\n111448284929147962 2\n334344854787443885 1" }, { "input": "979139840681508275", "output": "19\n1 979139840681508275\n2 326379946893836092\n10 17802542557845608\n21 4238700609010865\n29 2250896185474741\n109 163326078512381\n145 92502582964763\n174 64311319585050\n218 41017964923264\n1432150 1432150\n41017964923264 218\n64311319585050 174\n92502582964763 145\n163326078512381 109\n2250896185474741 29\n4238700609010865 21\n17802542557845608 10\n326379946893836092 2\n979139840681508275 1" }, { "input": "914669606669700001", "output": "3\n1 914669606669700001\n1400001 1400001\n914669606669700001 1" }, { "input": "333334833335500001", "output": "5\n1 333334833335500001\n2 111111611111833334\n1000001 1000001\n111111611111833334 2\n333334833335500001 1" }, { "input": "443667271666850000", "output": "33\n1 443667271666850000\n2 147889090555616667\n3 73944545277808334\n4 44366727166685001\n7 15845259702387502\n10 8066677666670003\n11 6722231388891670\n20 2112701293651673\n21 1920637539683340\n48 377268088152099\n49 362177364626016\n55 288095630952518\n146 41344448016715\n175 28809563095308\n219 18417072298406\n875 1157644544758\n1100000 1100000\n1157644544758 875\n18417072298406 219\n28809563095308 175\n41344448016715 146\n288095630952518 55\n362177364626016 49\n377268088152099 48\n1920637539683340 21\n..." }, { "input": "732334178333550000", "output": "11\n1 732334178333550000\n4 73233417833355001\n25 2253335933334008\n31 1476480198253135\n124 94494732688241\n1300000 1300000\n94494732688241 124\n1476480198253135 31\n2253335933334008 25\n73233417833355001 4\n732334178333550000 1" }, { "input": "14", "output": "5\n1 14\n2 5\n3 3\n5 2\n14 1" }, { "input": "576000720000200000", "output": "21\n1 576000720000200000\n2 192000240000066667\n3 96000120000033334\n4 57600072000020001\n10 10472740363640003\n11 8727283636366670\n43 608880253700014\n128 69767529069834\n472 5160002150157\n1375 608880254158\n1200000 1200000\n608880254158 1375\n5160002150157 472\n69767529069834 128\n608880253700014 43\n8727283636366670 11\n10472740363640003 10\n57600072000020001 4\n96000120000033334 3\n192000240000066667 2\n576000720000200000 1" }, { "input": "3456346346334634", "output": "2\n1 3456346346334634\n3456346346334634 1" } ]
140
0
0
48,013
177
Fibonacci Strings
[ "strings" ]
null
null
Fibonacci strings are defined as follows: - *f*1 = «a» - *f*2 = «b» - *f**n* = *f**n*<=-<=1 *f**n*<=-<=2, *n*<=&gt;<=2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". You are given a Fibonacci string and *m* strings *s**i*. For each string *s**i*, find the number of times it occurs in the given Fibonacci string as a substring.
The first line contains two space-separated integers *k* and *m* — the number of a Fibonacci string and the number of queries, correspondingly. Next *m* lines contain strings *s**i* that correspond to the queries. It is guaranteed that strings *s**i* aren't empty and consist only of characters "a" and "b". The input limitations for getting 30 points are: - 1<=≤<=*k*<=≤<=3000 - 1<=≤<=*m*<=≤<=3000 - The total length of strings *s**i* doesn't exceed 3000 The input limitations for getting 100 points are: - 1<=≤<=*k*<=≤<=1018 - 1<=≤<=*m*<=≤<=104 - The total length of strings *s**i* doesn't exceed 105 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.
For each string *s**i* print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109<=+<=7). Print the answers for the strings in the order in which they are given in the input.
[ "6 5\na\nb\nab\nba\naba\n" ]
[ "3\n5\n3\n3\n1\n" ]
none
[ { "input": "6 5\na\nb\nab\nba\naba", "output": "3\n5\n3\n3\n1" }, { "input": "10 10\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab", "output": "12\n21\n21\n0\n12\n21\n21\n0\n12\n21" }, { "input": "10 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb", "output": "0\n12\n21\n0\n12\n8\n0\n0\n0\n12" }, { "input": "1 10\nb\na\nb\na\nb\na\nb\na\nb\na", "output": "0\n1\n0\n1\n0\n1\n0\n1\n0\n1" }, { "input": "2 10\nb\na\nb\na\nb\na\nb\na\nb\na", "output": "1\n0\n1\n0\n1\n0\n1\n0\n1\n0" }, { "input": "3 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb", "output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "15 100\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab", "output": "0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0" }, { "input": "15 100\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na", "output": "377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233" }, { "input": "15 100\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa", "output": "144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0" }, { "input": "50 100\na\na\nb\na\na\nb\nb\nb\na\na\nb\nb\na\na\na\na\nb\nb\na\nb\nb\nb\na\na\na\na\na\na\nb\nb\na\nb\na\nb\na\nb\na\nb\nb\nb\na\na\nb\na\na\na\nb\na\nb\na\na\na\nb\na\nb\na\na\nb\na\na\nb\na\na\na\na\nb\na\nb\na\nb\nb\na\nb\nb\nb\na\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\na\na\na\nb\nb\nb\nb\na\na\na\nb\nb\nb\na", "output": "807526948\n807526948\n778742000\n807526948\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n778742000\n778742000\n807526948\n807526948\n807526948\n807526948\n778742000\n778742000\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n807526948\n807526948\n807526948\n807526948\n778742000\n778742000\n807526948\n778742000\n807526948\n778742000\n807526948\n778742000\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n778742000\n807526948\n807526948\n807526948\n77874..." }, { "input": "50 100\nb\naa\na\na\na\nb\na\nb\nbb\na\naa\naa\naa\nba\naa\na\na\naa\na\na\naa\nb\nbb\naa\naa\nbb\nb\nbb\na\naa\naa\naa\na\na\nb\nb\na\naa\nb\naa\nab\nb\nb\na\naa\nbb\nb\nb\na\nb\na\na\nb\na\nbb\nb\nab\nbb\na\naa\naa\nb\nb\na\nbb\nba\naa\nba\nbb\nba\nbb\na\nb\na\nba\na\nab\na\nbb\nab\na\nab\nbb\na\na\nb\na\nba\na\nbb\nb\nab\nab\naa\na\nab\nab\nbb\nab\nab", "output": "778742000\n0\n807526948\n807526948\n807526948\n778742000\n807526948\n778742000\n971215058\n807526948\n0\n0\n0\n807526948\n0\n807526948\n807526948\n0\n807526948\n807526948\n0\n778742000\n971215058\n0\n0\n971215058\n778742000\n971215058\n807526948\n0\n0\n0\n807526948\n807526948\n778742000\n778742000\n807526948\n0\n778742000\n0\n807526948\n778742000\n778742000\n807526948\n0\n971215058\n778742000\n778742000\n807526948\n778742000\n807526948\n807526948\n778742000\n807526948\n971215058\n778742000\n807526948\n9712..." }, { "input": "50 100\nb\naab\nab\naaa\na\naab\naaa\nbb\na\na\nb\naab\nbbb\naa\nbbb\nb\nab\nab\nbbb\nb\nbaa\na\nbab\nbbb\na\naba\nab\na\nba\nb\nbba\naba\nba\nbba\nb\nb\nb\nb\nab\na\nabb\nab\nbb\nbba\nb\nbbb\nbb\nb\naba\naab\nba\nbb\na\na\nbb\nba\nbaa\nba\nba\na\nb\nbb\nb\nbaa\nbab\nbba\nb\nbb\nbbb\nb\naba\nbba\nbba\naaa\nab\na\nbbb\nab\nb\nba\nbab\nb\naab\nbb\naba\nb\na\naa\nbaa\nbbb\naa\naba\nb\nbb\nba\nb\nb\naaa\nab\nba", "output": "778742000\n0\n807526948\n0\n807526948\n0\n0\n971215058\n807526948\n807526948\n778742000\n0\n0\n0\n0\n778742000\n807526948\n807526948\n0\n778742000\n0\n807526948\n807526948\n0\n807526948\n836311896\n807526948\n807526948\n807526948\n778742000\n971215058\n836311896\n807526948\n971215058\n778742000\n778742000\n778742000\n778742000\n807526948\n807526948\n971215058\n807526948\n971215058\n971215058\n778742000\n0\n971215058\n778742000\n836311896\n0\n807526948\n971215058\n807526948\n807526948\n971215058\n807526948\n..." }, { "input": "50 100\nbb\naa\nb\nbaa\nbbba\naa\nba\na\nabba\nbaa\naa\naab\nab\nbabb\naabb\nbaa\nbaaa\nbaa\naab\nbba\nbb\naba\naaba\nbab\naaba\naa\naaaa\nbabb\nbbb\naaba\naaa\nab\nbab\nb\nb\naa\naaab\naa\nbba\nbaa\nbabb\nbaba\nba\naaba\nbba\nba\nab\nabb\nb\nba\nbbb\nba\naaa\nbbb\nbaa\nbb\na\naaa\naaba\nab\nbba\nba\nb\nbbb\naaa\na\na\nb\nb\naba\nbb\nba\na\nb\nbaa\nb\naaaa\na\naaab\nbaba\nba\nbb\nbaba\nab\nbaaa\nbbbb\na\naabb\nab\nb\nb\naaa\nb\nb\nabab\nabb\nb\nb\nbb\nb", "output": "971215058\n0\n778742000\n0\n0\n0\n807526948\n807526948\n971215058\n0\n0\n0\n807526948\n971215058\n0\n0\n0\n0\n0\n971215058\n971215058\n836311896\n0\n807526948\n0\n0\n0\n971215058\n0\n0\n0\n807526948\n807526948\n778742000\n778742000\n0\n0\n0\n971215058\n0\n971215058\n836311896\n807526948\n0\n971215058\n807526948\n807526948\n971215058\n778742000\n807526948\n0\n807526948\n0\n0\n0\n971215058\n807526948\n0\n0\n807526948\n971215058\n807526948\n778742000\n0\n0\n807526948\n807526948\n778742000\n778742000\n83631189..." }, { "input": "1 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb", "output": "1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "2 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb", "output": "0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "3 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb", "output": "1\n1\n0\n0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0" }, { "input": "4 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb", "output": "1\n2\n0\n1\n1\n0\n0\n0\n0\n0\n0\n1\n0\n0" }, { "input": "5 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb", "output": "2\n3\n0\n1\n2\n1\n0\n0\n0\n1\n0\n1\n1\n0" } ]
62
0
0
48,026
452
Permutation
[ "data structures", "divide and conquer", "hashing" ]
null
null
You are given a permutation of numbers from 1 to *n*. Determine whether there's a pair of integers *a*,<=*b* (1<=≤<=*a*,<=*b*<=≤<=*n*; *a*<=≠<=*b*) such that the element (note, that it is usual division, not integer one) is between *a* and *b* in this permutation.
First line consists of a single integer *n* (1<=≤<=*n*<=≤<=300000) — the size of permutation. Second line contains *n* integers — the permutation itself.
Print "YES", if such a pair exists, "NO" otherwise (in both cases without quotes, the answer is case insensitive).
[ "4\n1 3 4 2\n", "5\n1 5 2 4 3\n" ]
[ "NO\n", "YES\n" ]
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5.
[ { "input": "4\n1 3 4 2", "output": "NO" }, { "input": "5\n1 5 2 4 3", "output": "YES" }, { "input": "100\n17 41 19 23 46 16 10 31 82 12 77 32 11 71 83 25 98 18 34 59 13 73 80 65 37 22 6 2 24 5 94 42 51 63 52 92 97 26 93 38 36 87 64 70 14 43 68 85 33 44 74 89 56 1 69 88 20 49 48 21 84 90 7 47 39 55 81 86 76 57 3 62 15 78 100 60 61 66 91 30 58 35 99 96 54 27 79 9 29 50 45 72 75 4 67 40 8 53 95 28", "output": "YES" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n22 78 19 70 81 33 15 72 40 51 18 62 36 24 37 20 97 65 89 75 14 55 23 53 1 67 50 99 54 76 41 16 44 60 2 90 7 28 79 43 47 64 71 27 25 8 46 92 95 80 31 100 42 96 86 66 52 63 98 4 56 91 34 83 85 3 84 17 94 11 73 29 6 58 61 68 57 88 10 32 82 93 26 13 87 39 30 5 12 59 9 48 69 35 77 21 45 38 49 74", "output": "YES" }, { "input": "100\n75 28 8 98 60 16 40 89 90 39 44 88 51 9 95 42 27 63 92 15 67 3 19 81 54 2 97 61 45 93 58 84 70 83 79 78 21 12 94 87 64 11 56 4 10 49 25 1 33 86 62 72 69 74 96 48 6 46 29 66 23 73 50 37 5 18 41 34 22 7 82 99 35 57 38 26 31 100 55 17 91 85 24 71 43 13 52 65 76 77 36 47 53 32 14 30 20 68 59 80", "output": "YES" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 2 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 95 63 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 31 2 95 63 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "NO" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 2 31 66 95 34 63 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "YES" }, { "input": "100\n1 65 33 97 17 81 49 9 73 41 25 89 57 5 69 37 21 85 53 13 77 45 29 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 15 79 47 2 31 95 66 34 63 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 4 68 36 100 20 84 52 12 76 44 28 92 60 8 72 40 24 88 56 16 80 48 32 96 64", "output": "YES" }, { "input": "4\n3 4 1 2", "output": "NO" }, { "input": "5\n3 5 4 1 2", "output": "NO" }, { "input": "14\n7 11 9 13 10 3 1 14 12 5 2 6 4 8", "output": "NO" }, { "input": "6\n6 2 4 1 3 5", "output": "YES" } ]
30
0
0
48,048
420
Online Meeting
[ "implementation" ]
null
null
Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat. One day the director of the F company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting. You are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting).
The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of team participants and the number of messages. Each of the next *m* lines contains a message in the format: - '+ *id*': the record means that the person with number *id* (1<=≤<=*id*<=≤<=*n*) has logged on to the meeting. - '- *id*': the record means that the person with number *id* (1<=≤<=*id*<=≤<=*n*) has logged off from the meeting. Assume that all the people of the team are numbered from 1 to *n* and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously.
In the first line print integer *k* (0<=≤<=*k*<=≤<=*n*) — how many people can be leaders. In the next line, print *k* integers in the increasing order — the numbers of the people who can be leaders. If the data is such that no member of the team can be a leader, print a single number 0.
[ "5 4\n+ 1\n+ 2\n- 2\n- 1\n", "3 2\n+ 1\n- 2\n", "2 4\n+ 1\n- 1\n+ 2\n- 2\n", "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4\n", "2 4\n+ 1\n- 2\n+ 2\n- 1\n" ]
[ "4\n1 3 4 5 ", "1\n3 ", "0\n", "3\n2 3 5 ", "0\n" ]
none
[ { "input": "5 4\n+ 1\n+ 2\n- 2\n- 1", "output": "4\n1 3 4 5 " }, { "input": "3 2\n+ 1\n- 2", "output": "1\n3 " }, { "input": "2 4\n+ 1\n- 1\n+ 2\n- 2", "output": "0" }, { "input": "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4", "output": "3\n2 3 5 " }, { "input": "2 4\n+ 1\n- 2\n+ 2\n- 1", "output": "0" }, { "input": "1 1\n+ 1", "output": "1\n1 " }, { "input": "2 1\n- 2", "output": "2\n1 2 " }, { "input": "3 5\n- 1\n+ 1\n+ 2\n- 2\n+ 3", "output": "1\n1 " }, { "input": "10 8\n+ 1\n- 1\n- 2\n- 3\n+ 3\n+ 7\n- 7\n+ 9", "output": "6\n3 4 5 6 8 10 " }, { "input": "5 5\n+ 5\n+ 2\n+ 3\n+ 4\n+ 1", "output": "1\n5 " }, { "input": "5 4\n+ 1\n- 1\n+ 1\n+ 2", "output": "4\n1 3 4 5 " }, { "input": "10 3\n+ 1\n+ 2\n- 7", "output": "7\n3 4 5 6 8 9 10 " }, { "input": "1 20\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1\n- 1\n+ 1", "output": "1\n1 " }, { "input": "20 1\n- 16", "output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 " }, { "input": "50 20\n- 6\n+ 40\n- 3\n- 23\n+ 31\n- 27\n- 40\n+ 25\n+ 29\n- 41\n- 16\n+ 23\n+ 20\n+ 13\n- 45\n+ 40\n+ 24\n+ 22\n- 23\n+ 17", "output": "34\n1 2 4 5 7 8 9 10 11 12 14 15 18 19 21 26 28 30 32 33 34 35 36 37 38 39 42 43 44 46 47 48 49 50 " }, { "input": "20 50\n+ 5\n+ 11\n- 5\n+ 6\n- 16\n- 13\n+ 5\n+ 7\n- 8\n- 7\n- 10\n+ 10\n- 20\n- 19\n+ 17\n- 2\n+ 2\n+ 19\n+ 18\n- 2\n- 6\n- 5\n+ 6\n+ 4\n- 14\n+ 14\n- 9\n+ 15\n- 17\n- 15\n+ 2\n+ 5\n- 2\n+ 9\n- 11\n+ 2\n- 19\n+ 7\n+ 12\n+ 16\n+ 19\n- 18\n- 2\n+ 18\n- 9\n- 10\n+ 9\n+ 13\n- 14\n- 16", "output": "2\n1 3 " }, { "input": "100 5\n- 60\n- 58\n+ 25\n- 32\n+ 86", "output": "95\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 59 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 87 88 89 90 91 92 93 94 95 96 97 98 99 100 " }, { "input": "4 4\n+ 2\n- 1\n- 3\n- 2", "output": "1\n4 " }, { "input": "3 3\n- 2\n+ 1\n+ 2", "output": "1\n3 " }, { "input": "5 4\n- 1\n- 2\n+ 3\n+ 4", "output": "1\n5 " }, { "input": "6 6\n- 5\n- 6\n- 3\n- 1\n- 2\n- 4", "output": "1\n4 " }, { "input": "10 7\n- 8\n+ 1\n+ 2\n+ 3\n- 2\n- 3\n- 1", "output": "6\n4 5 6 7 9 10 " }, { "input": "10 7\n- 8\n+ 1\n+ 2\n+ 3\n- 2\n- 3\n- 1", "output": "6\n4 5 6 7 9 10 " }, { "input": "4 10\n+ 2\n- 1\n- 2\n- 3\n+ 3\n+ 2\n+ 4\n- 2\n+ 2\n+ 1", "output": "1\n3 " }, { "input": "4 9\n+ 2\n- 1\n- 2\n- 3\n+ 3\n+ 2\n+ 4\n- 2\n+ 2", "output": "1\n3 " }, { "input": "10 8\n+ 1\n- 1\n- 4\n+ 4\n+ 3\n+ 7\n- 7\n+ 9", "output": "6\n2 4 5 6 8 10 " }, { "input": "10 6\n+ 2\n- 2\n+ 2\n- 2\n+ 2\n- 3", "output": "8\n1 4 5 6 7 8 9 10 " }, { "input": "10 5\n+ 2\n- 2\n+ 2\n- 2\n- 3", "output": "9\n1 3 4 5 6 7 8 9 10 " }, { "input": "10 11\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6\n+ 7", "output": "4\n6 8 9 10 " }, { "input": "10 10\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6", "output": "5\n6 7 8 9 10 " }, { "input": "10 9\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6", "output": "5\n6 7 8 9 10 " }, { "input": "10 12\n+ 1\n- 1\n- 2\n+ 3\n- 3\n- 4\n+ 5\n- 5\n- 6\n+ 6\n+ 7\n- 7", "output": "4\n6 8 9 10 " }, { "input": "2 2\n- 1\n+ 1", "output": "2\n1 2 " }, { "input": "7 4\n- 2\n- 3\n+ 3\n- 6", "output": "4\n1 4 5 7 " }, { "input": "2 3\n+ 1\n+ 2\n- 1", "output": "0" }, { "input": "5 5\n- 2\n+ 1\n+ 2\n- 2\n+ 4", "output": "2\n3 5 " }, { "input": "5 3\n+ 1\n- 1\n+ 2", "output": "3\n3 4 5 " }, { "input": "4 4\n- 1\n+ 1\n- 1\n+ 2", "output": "2\n3 4 " } ]
15
0
0
48,060
730
Car Repair Shop
[ "implementation" ]
null
null
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected *n* requests from clients. The requests are numbered from 1 to *n* in order they came. The *i*-th request is characterized by two values: *s**i* — the day when a client wants to start the repair of his car, *d**i* — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on. Polycarp is making schedule by processing requests in the order from the first to the *n*-th request. He schedules the *i*-th request as follows: - If the car repair shop is idle for *d**i* days starting from *s**i* (*s**i*,<=*s**i*<=+<=1,<=...,<=*s**i*<=+<=*d**i*<=-<=1), then these days are used to repair a car of the *i*-th client. - Otherwise, Polycarp finds the first day *x* (from 1 and further) that there are *d**i* subsequent days when no repair is scheduled starting from *x*. In other words he chooses the smallest positive *x* that all days *x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*d**i*<=-<=1 are not scheduled for repair of any car. So, the car of the *i*-th client will be repaired in the range [*x*,<=*x*<=+<=*d**i*<=-<=1]. It is possible that the day *x* when repair is scheduled to start will be less than *s**i*. Given *n* requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer *n* (1<=≤<=*n*<=≤<=200) — the number of requests from clients. The following *n* lines contain requests, one request per line. The *i*-th request is given as the pair of integers *s**i*,<=*d**i* (1<=≤<=*s**i*<=≤<=109, 1<=≤<=*d**i*<=≤<=5·106), where *s**i* is the preferred time to start repairing the *i*-th car, *d**i* is the number of days to repair the *i*-th car. The requests should be processed in the order they are given in the input.
Print *n* lines. The *i*-th line should contain two integers — the start day to repair the *i*-th car and the finish day to repair the *i*-th car.
[ "3\n9 2\n7 3\n2 4\n", "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n" ]
[ "9 10\n1 3\n4 7\n", "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000\n" ]
none
[ { "input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7" }, { "input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000" }, { "input": "1\n1 1", "output": "1 1" }, { "input": "1\n1000000000 1", "output": "1000000000 1000000000" }, { "input": "1\n1000000000 5000000", "output": "1000000000 1004999999" }, { "input": "5\n6 2\n10 1\n10 2\n9 2\n5 1", "output": "6 7\n10 10\n1 2\n3 4\n5 5" }, { "input": "10\n1 3\n77 8\n46 5\n83 4\n61 7\n8 4\n54 7\n80 7\n33 7\n13 4", "output": "1 3\n77 84\n46 50\n4 7\n61 67\n8 11\n54 60\n12 18\n33 39\n19 22" }, { "input": "10\n588 12\n560 10\n593 14\n438 15\n761 11\n984 6\n503 2\n855 19\n538 2\n650 7", "output": "588 599\n560 569\n1 14\n438 452\n761 771\n984 989\n503 504\n855 873\n538 539\n650 656" }, { "input": "20\n360 26\n475 17\n826 12\n815 23\n567 28\n897 26\n707 20\n1000 9\n576 5\n16 5\n714 16\n630 17\n426 26\n406 23\n899 25\n102 22\n896 8\n320 27\n964 25\n932 18", "output": "360 385\n475 491\n826 837\n1 23\n567 594\n897 922\n707 726\n1000 1008\n24 28\n29 33\n34 49\n630 646\n426 451\n50 72\n73 97\n102 123\n124 131\n320 346\n964 988\n932 949" }, { "input": "30\n522692116 84\n589719489 488\n662495181 961\n915956552 470\n683572975 271\n498400137 480\n327010963 181\n200704287 367\n810826488 54\n978100746 208\n345455616 986\n106372142 876\n446972337 42\n309349333 200\n93462198 543\n167946793 318\n325598940 427\n121873339 459\n174934933 598\n279521023 655\n739750520 3\n870850765 192\n622303167 400\n471234786 63\n805952711 18\n349834333 857\n804873364 302\n512746562 39\n533285962 561\n996718586 494", "output": "522692116 522692199\n589719489 589719976\n662495181 662496141\n915956552 915957021\n683572975 683573245\n498400137 498400616\n327010963 327011143\n200704287 200704653\n810826488 810826541\n978100746 978100953\n345455616 345456601\n106372142 106373017\n446972337 446972378\n309349333 309349532\n93462198 93462740\n167946793 167947110\n325598940 325599366\n121873339 121873797\n174934933 174935530\n279521023 279521677\n739750520 739750522\n870850765 870850956\n622303167 622303566\n471234786 471234848\n805952711..." }, { "input": "2\n10 3\n9 2", "output": "10 12\n1 2" }, { "input": "1\n1 5000000", "output": "1 5000000" } ]
31
0
0
48,163
391
Word Folding
[ "brute force" ]
null
null
You will receive 5 points for solving this problem. Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity. We will denote the positions of folds with '|' characters. For example, the word "ABRACADABRA" written as "AB|RACA|DAB|RA" indicates that it has been folded three times: first, between the leftmost pair of 'B' and 'R' letters; second, between 'A' and 'D'; and third, between the rightmost pair of 'B' and 'R' letters. Here are several examples of folded strings: One last example for "ABCD|EFGH|IJ|K": Manao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as "AHI", "BGJK", "CF", and "DE" from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both.
The input will consist of one line containing a single string of *n* characters with 1<=≤<=*n*<=≤<=1000 and no spaces. All characters of the string will be uppercase letters. This problem doesn't have subproblems. You will get 5 points for the correct submission.
Print a single integer — the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string.
[ "ABRACADABRA\n", "ABBBCBDB\n", "AB\n" ]
[ "3\n", "3\n", "1\n" ]
Consider the first example. Manao can create a pile of three 'A's using the folding "AB|RACAD|ABRA", which results in the following structure: In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB". Another way for Manao to create a pile of three 'B's with "ABBBCBDB" is the following folding: "AB|B|BCBDB". In the third example, there are no folds performed and the string is just written in one line.
[ { "input": "ABRACADABRA", "output": "3" }, { "input": "ABBBCBDB", "output": "3" }, { "input": "AB", "output": "1" }, { "input": "ABBCDEFB", "output": "3" }, { "input": "THISISATEST", "output": "3" }, { "input": "Z", "output": "1" }, { "input": "ZZ", "output": "2" }, { "input": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "output": "1" }, { "input": "ABACBCABACACB", "output": "4" }, { "input": "LEHLLLLLLHAFGEGLLHAFDLHHLLLLLDGGEHGGHLLLLLLLLDFLCBLLEFLLCBLLCGLEDLGGLECLDGLEHLLLGELLLEGLLLLGDLLLDALD", "output": "49" }, { "input": "THISISTHELASTTEST", "output": "3" } ]
62
0
3
48,204
139
Wallpaper
[ "implementation", "math" ]
null
null
Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has *n* rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height). Boris chose *m* types of wallpaper to paper the walls of the rooms with (but it is not necessary to use all the types). Each type of wallpaper is sold in rolls of a fixed length and width (the length, naturally, shows how long the unfolded roll will be). In addition, for each type we know the price of one roll of this type. The wallpaper of each type contains strips running along the length of the roll. When gluing the strips must be located strictly vertically (so the roll cannot be rotated, even if the length is less than the width). Besides, a roll can be cut in an arbitrary manner, but the joints of glued pieces should also be vertical. In addition, each room should be papered by only one type of wallpaper. And pieces of the same roll cannot be used to paper different rooms. That is, for each room the rolls are purchased separately. Also, some rolls can be used not completely. After buying an apartment Boris is short of cash, so he wants to spend the minimum money on wallpaper. Help him.
The first line contains a positive integer *n* (1<=≤<=*n*<=≤<=500) — the number of rooms in Boris's apartment. Each of the next *n* lines contains three space-separated positive integers — the length, width and height of the walls in a given room in meters, respectively. The next line contains a positive integer *m* (1<=≤<=*m*<=≤<=500) — the number of available wallpaper types. Each of the following *m* lines contains three space-separated positive integers — the length and width in meters of a given wallpaper and the price of one roll, respectively. All numbers in the input data do not exceed 500. It is guaranteed that each room can be papered using these types of wallpaper.
Print a single number — the minimum total cost of the rolls.
[ "1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500\n" ]
[ "640\n" ]
Note to the sample: The total length of the walls (the perimeter) of the room is 20 m. One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700. A roll of the second type can be cut into pieces to get five 2 meter wide strips, we need 2 rolls, the price is 640. One roll of the third type can immediately paper 19 meters out of 20, but we cannot use other types and we have to buy a second roll, the price is 1000.
[ { "input": "1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500", "output": "640" }, { "input": "1\n9 10 7\n1\n7 1 3", "output": "114" }, { "input": "1\n6 9 5\n3\n8 5 10\n8 5 2\n6 3 7", "output": "12" }, { "input": "1\n3 3 10\n3\n5 5 1\n9 9 2\n10 1 500", "output": "6000" }, { "input": "3\n29 30 29\n30 15 28\n27 30 23\n3\n30 27 21\n11 24 30\n25 20 12", "output": "261" }, { "input": "5\n4 4 1\n3 1 4\n4 1 3\n5 1 1\n1 1 1\n5\n5 3 3\n4 3 4\n5 2 3\n2 1 5\n3 1 3", "output": "33" }, { "input": "10\n57 66 71\n14 30 28\n100 56 45\n22 24 76\n64 7 65\n26 80 9\n15 62 23\n63 46 34\n86 20 58\n10 33 2\n1\n86 9 99", "output": "13959" }, { "input": "5\n98 97 79\n88 82 82\n93 90 86\n70 76 75\n72 88 89\n10\n13 10 97\n100 8 77\n39 5 87\n50 10 71\n17 9 74\n65 8 100\n74 8 94\n60 6 70\n67 6 90\n97 7 71", "output": "16555" }, { "input": "10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10 10 10\n10\n4 10 10\n10 10 10\n7 10 10\n6 10 10\n7 10 10\n4 10 10\n4 10 10\n2 10 10\n7 10 10\n7 10 10", "output": "400" }, { "input": "1\n95 83 81\n10\n41 85 94\n95 97 95\n84 89 99\n23 85 86\n93 90 98\n55 84 82\n80 88 94\n32 83 98\n5 85 85\n48 98 100", "output": "380" }, { "input": "20\n110 466 472\n112 153 152\n424 492 490\n348 366 113\n208 337 415\n491 448 139\n287 457 403\n444 382 160\n325 486 284\n447 454 136\n216 412 418\n217 208 228\n109 436 291\n293 382 421\n483 339 174\n213 327 183\n278 268 147\n181 424 457\n388 289 430\n174 269 305\n20\n221 4 334\n257 4 331\n444 3 127\n492 3 391\n154 4 389\n362 4 346\n264 3 448\n347 5 296\n416 5 496\n407 3 400\n133 3 140\n189 3 111\n272 3 262\n115 5 274\n128 3 230\n488 5 214\n458 3 428\n54 4 440\n191 5 160\n228 5 139", "output": "959915" }, { "input": "1\n500 500 500\n1\n500 500 500", "output": "2000" }, { "input": "1\n1 1 1\n1\n1 1 1", "output": "4" }, { "input": "1\n500 500 1\n1\n1 1 500", "output": "1000000" } ]
92
0
-1
48,427
0
none
[ "none" ]
null
null
Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are *n* bamboos in a row, and the *i*-th from the left is *a**i* meters high. Vladimir has just planted *n* bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing. Vladimir wants to check the bamboos each *d* days (i.e. *d* days after he planted, then after 2*d* days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than *k* meters. What is the maximum value *d* he can choose so that he can achieve what he wants without cutting off more than *k* meters of bamboo?
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1011) — the number of bamboos and the maximum total length of cut parts, in meters. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the required heights of bamboos, in meters.
Print a single integer — the maximum value of *d* such that Vladimir can reach his goal.
[ "3 4\n1 3 5\n", "3 40\n10 30 50\n" ]
[ "3\n", "32\n" ]
In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.
[]
46
0
0
48,514
601
Lipshitz Sequence
[ "data structures", "math" ]
null
null
A function is called Lipschitz continuous if there is a real constant *K* such that the inequality |*f*(*x*)<=-<=*f*(*y*)|<=≤<=*K*·|*x*<=-<=*y*| holds for all . We'll deal with a more... discrete version of this term. For an array , we define it's Lipschitz constant as follows: - if *n*<=&lt;<=2, - if *n*<=≥<=2, over all 1<=≤<=*i*<=&lt;<=*j*<=≤<=*n* In other words, is the smallest non-negative integer such that |*h*[*i*]<=-<=*h*[*j*]|<=≤<=*L*·|*i*<=-<=*j*| holds for all 1<=≤<=*i*,<=*j*<=≤<=*n*. You are given an array of size *n* and *q* queries of the form [*l*,<=*r*]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .
The first line of the input contains two space-separated integers *n* and *q* (2<=≤<=*n*<=≤<=100<=000 and 1<=≤<=*q*<=≤<=100) — the number of elements in array and the number of queries respectively. The second line contains *n* space-separated integers (). The following *q* lines describe queries. The *i*-th of those lines contains two space-separated integers *l**i* and *r**i* (1<=≤<=*l**i*<=&lt;<=*r**i*<=≤<=*n*).
Print the answers to all queries in the order in which they are given in the input. For the *i*-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .
[ "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9\n", "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5\n" ]
[ "17\n82\n23\n210\n", "2\n0\n22\n59\n16\n8\n" ]
In the first query of the first sample, the Lipschitz constants of subarrays of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ddadffaf9e222576b1abb771dcd83dbad588d7fe.png" style="max-width: 100.0%;max-height: 100.0%;"/> with length at least 2 are: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/887651f76ac57b8f2b2dbd46c6ac2335ac2a270a.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e9eed4a49028e9bd234162d9da74fd4f86481378.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16eff35d7bd22d3e34c55fc95cc6c59212d5b1c8.png" style="max-width: 100.0%;max-height: 100.0%;"/> The answer to the query is their sum.
[ { "input": "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9", "output": "17\n82\n23\n210" }, { "input": "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5", "output": "2\n0\n22\n59\n16\n8" }, { "input": "2 2\n0 0\n1 2\n1 2", "output": "0\n0" }, { "input": "2 2\n0 100000000\n1 2\n1 2", "output": "100000000\n100000000" }, { "input": "4 6\n1 2 3 2\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "1\n3\n6\n1\n3\n1" }, { "input": "3 6\n10 20 30\n1 2\n1 3\n2 3\n1 2\n2 3\n1 3", "output": "10\n30\n10\n10\n10\n30" }, { "input": "3 6\n48261735 26888803 75904937\n1 2\n1 3\n2 3\n1 2\n2 3\n1 3", "output": "21372932\n119405200\n49016134\n21372932\n49016134\n119405200" }, { "input": "3 6\n100000000 99999999 0\n1 2\n1 3\n2 3\n1 2\n2 3\n1 3", "output": "1\n199999999\n99999999\n1\n99999999\n199999999" }, { "input": "2 2\n100000000 0\n1 2\n1 2", "output": "100000000\n100000000" } ]
0
0
-1
48,542
56
Changing a String
[ "dp" ]
D. Changing a String
2
256
There is a string *s*, consisting of capital Latin letters. Let's denote its current length as |*s*|. During one move it is allowed to apply one of the following operations to it: - INSERT *pos* *ch* — insert a letter *ch* in the string *s* in the position *pos* (1<=≤<=*pos*<=≤<=|*s*|<=+<=1,<=*A*<=≤<=*ch*<=≤<=*Z*). The letter *ch* becomes the *pos*-th symbol of the string *s*, at that the letters shift aside and the length of the string increases by 1. - DELETE *pos* — delete a character number *pos* (1<=≤<=*pos*<=≤<=|*s*|) from the string *s*. At that the letters shift together and the length of the string decreases by 1. - REPLACE *pos* *ch* — the letter in the position *pos* of the line *s* is replaced by *ch* (1<=≤<=*pos*<=≤<=|*s*|,<=*A*<=≤<=*ch*<=≤<=*Z*). At that the length of the string does not change. Your task is to find in which minimal number of moves one can get a *t* string from an *s* string. You should also find the sequence of actions leading to the required results.
The first line contains *s*, the second line contains *t*. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000.
In the first line print the number of moves *k* in the given sequence of operations. The number should be the minimal possible one. Then print *k* lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them.
[ "ABA\nABBBA\n", "ACCEPTED\nWRONGANSWER\n" ]
[ "2\nINSERT 3 B\nINSERT 4 B\n", "10\nREPLACE 1 W\nREPLACE 2 R\nREPLACE 3 O\nREPLACE 4 N\nREPLACE 5 G\nREPLACE 6 A\nINSERT 7 N\nINSERT 8 S\nINSERT 9 W\nREPLACE 11 R\n" ]
none
[ { "input": "ABA\nABBBA", "output": "2\nINSERT 3 B\nINSERT 4 B" }, { "input": "ACCEPTED\nWRONGANSWER", "output": "10\nREPLACE 1 W\nREPLACE 2 R\nREPLACE 3 O\nREPLACE 4 N\nREPLACE 5 G\nREPLACE 6 A\nINSERT 7 N\nINSERT 8 S\nINSERT 9 W\nREPLACE 11 R" }, { "input": "V\nBNBNE", "output": "5\nREPLACE 1 B\nINSERT 2 N\nINSERT 3 B\nINSERT 4 N\nINSERT 5 E" }, { "input": "UB\nPWL", "output": "3\nREPLACE 1 P\nREPLACE 2 W\nINSERT 3 L" }, { "input": "JOYXNKYPF\nGDV", "output": "9\nREPLACE 1 G\nREPLACE 2 D\nREPLACE 3 V\nDELETE 4\nDELETE 4\nDELETE 4\nDELETE 4\nDELETE 4\nDELETE 4" }, { "input": "SZDAWSVGK\nUM", "output": "9\nREPLACE 1 U\nREPLACE 2 M\nDELETE 3\nDELETE 3\nDELETE 3\nDELETE 3\nDELETE 3\nDELETE 3\nDELETE 3" }, { "input": "TJHGFKKCDOHRNAXZROCWIYFF\nZBWUEHEVEOUATECAGLZIQMUDXEMHRSOZMAUJRWLQMPPZOUMXHAMWUGEDIKVKBLVMXWUOFMPAFDPRBCFTEWOULCZWRQHCTBTBXRHHODWBCXWIMNCNEXOSKSUJLISGCLLLXOKRSBNOZTHAJNNLILYFFMSYKOFPTXRNEFBSOUHFOLTIQAINRPXWRQ", "output": "164\nINSERT 1 Z\nINSERT 2 B\nINSERT 3 W\nINSERT 4 U\nINSERT 5 E\nINSERT 6 H\nINSERT 7 E\nINSERT 8 V\nINSERT 9 E\nINSERT 10 O\nINSERT 11 U\nINSERT 12 A\nINSERT 14 E\nINSERT 15 C\nINSERT 16 A\nINSERT 17 G\nINSERT 18 L\nINSERT 19 Z\nINSERT 20 I\nINSERT 21 Q\nINSERT 22 M\nINSERT 23 U\nINSERT 24 D\nINSERT 25 X\nINSERT 26 E\nINSERT 27 M\nINSERT 28 H\nINSERT 29 R\nINSERT 30 S\nINSERT 31 O\nINSERT 32 Z\nINSERT 33 M\nINSERT 34 A\nINSERT 35 U\nINSERT 37 R\nINSERT 38 W\nINSERT 39 L\nINSERT 40 Q\nINSERT 41 M\nINSERT 4..." }, { "input": "GXPLMUNZIRBHFJOOJHOMQNIKHVQSGFYSJLSWJQBUWYWHLQHLZYLPZWBOMPOLOLUGSBMHHLYGEIOUWDKPFIAAIRKYRLXTIFAZOPOLLPSNZHCIZDRTJPCYSCDTXBTMFSGEPRNOHJHNXZFYJPAMSHNOVZZYWCVEXLLOVHGAJBANAXVNTWCYTCUMQPEUUESQZTSDANIMUVJGDJCDLIAZKIAYAUQKPXRYKFRBVQJDRSUQZQZTIHWZDXQND\nYTJUCSBGESVMVRIDTBJTMPVBCWWDWKBPEBVMGDXGIVLWQXVEETNSDXKTVJPXOPERWSGDPPMKNMWDIGEHFXNUQADIS", "output": "209\nREPLACE 1 Y\nREPLACE 2 T\nREPLACE 3 J\nDELETE 4\nDELETE 4\nREPLACE 5 C\nREPLACE 6 S\nDELETE 7\nDELETE 7\nREPLACE 8 G\nREPLACE 9 E\nREPLACE 10 S\nREPLACE 11 V\nDELETE 12\nDELETE 12\nDELETE 12\nDELETE 12\nREPLACE 13 V\nREPLACE 14 R\nREPLACE 16 D\nREPLACE 17 T\nREPLACE 18 B\nDELETE 19\nDELETE 19\nDELETE 19\nDELETE 19\nDELETE 19\nDELETE 19\nREPLACE 20 T\nREPLACE 21 M\nREPLACE 22 P\nREPLACE 23 V\nDELETE 24\nREPLACE 25 C\nDELETE 27\nREPLACE 28 D\nREPLACE 29 W\nREPLACE 30 K\nREPLACE 31 B\nDELETE 32\nDELETE 3..." }, { "input": "BPYEOOTCVXAZPTHUEAIUZURZPHBWOEHGVHSDBYNXLHGOPBPCLPWQWVRYJGILKOOLASFSFYFGIMPVFGRRINJOXENOIMETWXUWCKXYBPBPPYRTMOZSBMBVJENPUSAEUPMQDGAQXUDIFUPOHYXPUWZLJP\nYRWPQARABPMMBLUZJHDVOUODHRNLMOOVGVJIZDDLXEWCPUFYYPKCDDYGYOLTNHAVURLLEKNOLC", "output": "123\nDELETE 1\nDELETE 1\nREPLACE 2 R\nREPLACE 3 W\nREPLACE 4 P\nREPLACE 5 Q\nREPLACE 6 A\nREPLACE 7 R\nDELETE 8\nREPLACE 9 B\nREPLACE 11 M\nREPLACE 12 M\nREPLACE 13 B\nREPLACE 14 L\nDELETE 15\nDELETE 15\nREPLACE 17 J\nDELETE 18\nDELETE 18\nDELETE 18\nREPLACE 19 D\nREPLACE 20 V\nREPLACE 22 U\nREPLACE 23 O\nDELETE 24\nDELETE 24\nDELETE 24\nDELETE 24\nDELETE 25\nDELETE 25\nDELETE 25\nDELETE 25\nDELETE 25\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE 26\nDELETE..." }, { "input": "YWJJBIRAPVSKOZUBVXOEMSCFWLSGNDUUBAOGTFAIOWJIZLVJCUKUYCYSAAHVLGCUAXDGYKJQMNOIFUDWFOSAVKRMAKLEKMPPISHARHVGEWRKUADEQZCJUXDVRQIIGO\nYWJJBIRAPVSKOZUBVXOEMSCFWLSGNDUUBAOGTFAIOWJIZLVJCUKUYCYSAAHVLGCUAXDGYKJQMNOIFUDWFOSAVKRMAKLEKMPISHARHVGEWRKUADEQZCJUXDVRQIIGO", "output": "1\nDELETE 96" }, { "input": "A\nC", "output": "1\nREPLACE 1 C" }, { "input": "Z\nZ", "output": "0" } ]
60
102,400
0
48,552
0
none
[ "none" ]
null
null
Dreamoon has a string *s* and a pattern string *p*. He first removes exactly *x* characters from *s* obtaining string *s*' as a result. Then he calculates that is defined as the maximal number of non-overlapping substrings equal to *p* that can be found in *s*'. He wants to make this number as big as possible. More formally, let's define as maximum value of over all *s*' that can be obtained by removing exactly *x* characters from *s*. Dreamoon wants to know for all *x* from 0 to |*s*| where |*s*| denotes the length of string *s*.
The first line of the input contains the string *s* (1<=≤<=|*s*|<=≤<=2<=000). The second line of the input contains the string *p* (1<=≤<=|*p*|<=≤<=500). Both strings will only consist of lower case English letters.
Print |*s*|<=+<=1 space-separated integers in a single line representing the for all *x* from 0 to |*s*|.
[ "aaaaa\naa\n", "axbaxxb\nab\n" ]
[ "2 2 1 1 0 0\n", "0 1 1 2 1 1 0 0\n" ]
For the first sample, the corresponding optimal values of *s*' after removal 0 through |*s*| = 5 characters from *s* are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}. For the second sample, possible corresponding optimal values of *s*' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}.
[]
46
307,200
-1
48,559
50
Bombing
[ "binary search", "dp", "probabilities" ]
D. Bombing
2
256
The commanding officers decided to drop a nuclear bomb on the enemy's forces. You are ordered to determine the power of the warhead that needs to be used. The enemy has *N* strategically important objects. Their positions are known due to the intelligence service. The aim of the strike is to deactivate at least *K* important objects of the enemy. The bombing impact point is already determined and has coordinates of [*X*0; *Y*0]. The nuclear warhead is marked by the estimated impact radius *R*<=≥<=0. All the buildings that are located closer than *R* to the bombing epicentre will be destroyed. All the buildings that are located further than *R* from the epicentre, can also be deactivated with some degree of probability. Let's assume that *D* is the distance between a building and the epicentre. This building's deactivation probability *P*(*D*,<=*R*) is calculated according to the following formula: If the estimated impact radius of the warhead is equal to zero, then all the buildings located in the impact point will be completely demolished and all the rest of important objects will not be damaged. The commanding officers want the probability of failing the task to be no more than ε. Nuclear warheads are too expensive a luxury, that's why you have to minimise the estimated impact radius of the warhead.
The first line contains an integer *N* which represents the number of the enemy's objects (1<=≤<=*N*<=≤<=100). The second line contains two integers: *K* is the required number of deactivated objects, and ε is the maximally permitted probability of not completing the task, given in per mils (1<=≤<=*K*<=≤<=*N*, 1<=≤<=ε<=≤<=999). The third line contains *X*0 and *Y*0 which are the coordinates of the strike impact point. The next *N* lines contain two numbers *X**i* and *Y**i* each which are the coordinates of every strategically important object. All the coordinates are integer, their absolute values do not exceed 1000. Let us remind you that there are a thousand per mils in unity (number one). There can be several objects in one point.
Print the sought estimated impact radius of the warhead. The absolute or relative measure of the inaccuracy of your answer should not exceed 10<=-<=6.
[ "1\n1 500\n5 5\n1 2\n", "5\n3 100\n0 0\n3 4\n60 70\n100 100\n10 10\n5 12\n" ]
[ "3.84257761518762740\n", "13.45126176453737600\n" ]
none
[]
62
0
0
48,613
0
none
[ "none" ]
null
null
Drazil is a monkey. He lives in a circular park. There are *n* trees around the park. The distance between the *i*-th tree and (*i*<=+<=1)-st trees is *d**i*, the distance between the *n*-th tree and the first tree is *d**n*. The height of the *i*-th tree is *h**i*. Drazil starts each day with the morning run. The morning run consists of the following steps: - Drazil chooses two different trees - He starts with climbing up the first tree - Then he climbs down the first tree, runs around the park (in one of two possible directions) to the second tree, and climbs on it - Then he finally climbs down the second tree. But there are always children playing around some consecutive trees. Drazil can't stand children, so he can't choose the trees close to children. He even can't stay close to those trees. If the two trees Drazil chooses are *x*-th and *y*-th, we can estimate the energy the morning run takes to him as 2(*h**x*<=+<=*h**y*)<=+<=*dist*(*x*,<=*y*). Since there are children on exactly one of two arcs connecting *x* and *y*, the distance *dist*(*x*,<=*y*) between trees *x* and *y* is uniquely defined. Now, you know that on the *i*-th day children play between *a**i*-th tree and *b**i*-th tree. More formally, if *a**i*<=≤<=*b**i*, children play around the trees with indices from range [*a**i*,<=*b**i*], otherwise they play around the trees with indices from . Please help Drazil to determine which two trees he should choose in order to consume the most energy (since he wants to become fit and cool-looking monkey) and report the resulting amount of energy for each day.
The first line contains two integer *n* and *m* (3<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=105), denoting number of trees and number of days, respectively. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=≤<=*d**i*<=≤<=109), the distances between consecutive trees. The third line contains *n* integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≤<=*h**i*<=≤<=109), the heights of trees. Each of following *m* lines contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*) describing each new day. There are always at least two different trees Drazil can choose that are not affected by children.
For each day print the answer in a separate line.
[ "5 3\n2 2 2 2 2\n3 5 2 1 4\n1 3\n2 2\n4 5\n", "3 3\n5 1 4\n5 1 4\n3 3\n2 2\n1 1\n" ]
[ "12\n16\n18\n", "17\n22\n11\n" ]
none
[ { "input": "5 3\n2 2 2 2 2\n3 5 2 1 4\n1 3\n2 2\n4 5", "output": "12\n16\n18" }, { "input": "3 3\n5 1 4\n5 1 4\n3 3\n2 2\n1 1", "output": "17\n22\n11" }, { "input": "10 10\n8477 33103 38654 6582 27496 1106 15985 3644 29818 8849\n88745 72099 87767 85285 73517 94562 87214 63194 83791 77619\n2 8\n1 5\n9 5\n7 8\n6 9\n8 1\n6 1\n4 9\n8 10\n5 10", "output": "383739\n394915\n364658\n509685\n428294\n439157\n386525\n394604\n480926\n428294" }, { "input": "9 9\n1 1 1 1 1 1 1 1 1\n1 1 1 1 1000000000 1 1 1 1\n9 1\n9 9\n1 1\n8 9\n7 9\n9 2\n8 2\n1 1\n1 2", "output": "2000000005\n2000000006\n2000000006\n2000000006\n2000000006\n2000000005\n2000000004\n2000000006\n2000000006" }, { "input": "10 10\n91616899 35356500 87449167 31557462 21778951 474730484 302870359 398428048 174667839 183336304\n955685310 810816265 348361987 966143351 883722429 699134978 928163574 775129554 873615248 931808862\n4 4\n7 1\n2 7\n8 9\n6 2\n8 4\n9 10\n8 8\n3 10\n3 6", "output": "5234627463\n3731289022\n4218061919\n4645770639\n3731289022\n4120281441\n4510187231\n4704051250\n3624620049\n4827000318" } ]
30
0
0
48,755
119
Alternative Reality
[ "geometry" ]
null
null
In the year of 3000 travelling around parallel realities became a routine thing. However one has to take into consideration that travelling like that is highly dangerous as you never know beforehand where you're gonna get... Little Vasya, for instance, found himself in a gaming reality and now he has to successfully complete all levels of a very weird game to get back. The gaming reality is a three-dimensional space where *n* points are given. The game has *m* levels and at the beginning of the *i*-th level the player is positioned at some plane *Q**i* that passes through the origin. On each level Vasya has to use special robots to construct and activate *n* powerful energy spheres of the equal radius with centers at the given points. The player chooses the radius of the spheres himself. The player has to spend *R* units of money to construct spheres whose radius equals *R* (consequently, one can construct spheres whose radius equals zero for free). Besides, once for each level a player can choose any point in space and release a laser ray from there, perpendicular to plane *Q**i* (this action costs nothing). The ray can either be directed towards the plane or from the plane. The spheres that share at least one point with the ray will be immediately activated. The level is considered completed if the player has managed to activate all spheres. Note that the centers of the spheres are the same for all *m* levels but the spheres do not remain: the player should construct them anew on each new level. Help Vasya find out what minimum sum of money will be enough to complete each level.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=900,<=1<=≤<=*m*<=≤<=100) — the number of energetic spheres and the number of levels in the game correspondingly. Each of the following *n* lines contains three integers *x**i*, *y**i*, *z**i* (0<=≤<=*x**i*,<=*y**i*,<=*z**i*<=≤<=104) — the coordinates of the center of the *i*-th sphere. Assume that these points do not change their positions throughout the game. Then follow *m* lines, each containing three integers *a**i*, *b**i*, *c**i* (0<=≤<=*a**i*,<=*b**i*,<=*c**i*<=≤<=100, *a**i*2<=+<=*b**i*2<=+<=*c**i*2<=&gt;<=0). These numbers are the coefficients in the equation of plane *Q**i* (*a**i**x*<=+<=*b**i**y*<=+<=*c**i**z*<==<=0), where the player is positioned at the beginning of the *i*-th level.
Print *m* numbers, one per line: the *i*-th line should contain the minimum sum of money needed to complete the *i*-th level. The absolute or relative error should not exceed 10<=-<=6.
[ "4 1\n0 0 0\n0 1 0\n1 0 0\n1 1 0\n0 0 1\n", "5 3\n0 1 0\n1 0 1\n1 2 1\n2 0 1\n1 3 0\n1 1 1\n1 2 3\n3 0 3\n", "2 1\n0 20 0\n0 0 0\n0 10 0\n" ]
[ "0.7071067812\n", "1.6329931619\n1.6366341768\n1.5411035007\n", "0.0000000000\n" ]
none
[]
60
0
0
48,758
690
Recover Polygon (easy)
[]
null
null
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good. Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle. As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides).
The first line of each test case contains one integer *N*, the size of the lattice grid (5<=≤<=*N*<=≤<=50). The next *N* lines each contain *N* characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of *y* coordinate, and in one row cells go in the order of increasing *x* coordinate. This means that the first row corresponds to cells with coordinates (1,<=*N*),<=...,<=(*N*,<=*N*) and the last row corresponds to cells with coordinates (1,<=1),<=...,<=(*N*,<=1).
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
[ "6\n000000\n000000\n012100\n024200\n012100\n000000\n" ]
[ "Yes\n" ]
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (*x*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">1</sub>), (*x*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">2</sub>), (*x*<sub class="lower-index">2</sub>, *y*<sub class="lower-index">1</sub>), (*x*<sub class="lower-index">2</sub>, *y*<sub class="lower-index">2</sub>)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ *x*<sub class="lower-index">1</sub> &lt; *x*<sub class="lower-index">2</sub> ≤ *N*, 0 ≤ *y*<sub class="lower-index">1</sub> &lt; *y*<sub class="lower-index">2</sub> ≤ *N*), and result in the levels of Zombie Contamination as reported in the input.
[ { "input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes" }, { "input": "6\n000000\n012210\n024420\n012210\n000000\n000000", "output": "Yes" }, { "input": "6\n000100\n001210\n002420\n001210\n000000\n000000", "output": "No" }, { "input": "10\n0000000000\n0122210000\n0244420100\n0122210000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000", "output": "No" }, { "input": "10\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0012100000\n0024200000\n0012100000\n0000000000", "output": "Yes" }, { "input": "9\n000000000\n000000000\n012221000\n024442000\n012221000\n000000000\n000000000\n000000010\n000000000", "output": "No" }, { "input": "9\n000000000\n012222100\n024444200\n024444200\n024444200\n024444200\n024444200\n012222100\n000000000", "output": "Yes" }, { "input": "8\n00000000\n00001210\n00002420\n00002020\n00001210\n00000000\n00000000\n00000000", "output": "No" }, { "input": "8\n00000000\n00000000\n01210000\n02420000\n01210000\n00000000\n00000000\n00000000", "output": "Yes" }, { "input": "7\n0000000\n0000000\n0000000\n1122210\n0244420\n0122210\n0000000", "output": "No" }, { "input": "7\n0000000\n0012210\n0024420\n0012210\n0000000\n0000000\n0000000", "output": "Yes" }, { "input": "6\n000000\n000000\n001100\n001200\n000000\n000000", "output": "No" }, { "input": "6\n000000\n000000\n002200\n002200\n000000\n000000", "output": "No" }, { "input": "6\n000000\n000000\n003300\n003300\n000000\n000000", "output": "No" }, { "input": "6\n000000\n001100\n013310\n013310\n001100\n000000", "output": "No" } ]
93
7,065,600
0
48,783
280
Rectangle Puzzle
[ "geometry" ]
null
null
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the *Ox* axis, equals *w*, the length of the side that is parallel to the *Oy* axis, equals *h*. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle α. Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
The first line contains three integers *w*,<=*h*,<=α (1<=≤<=*w*,<=*h*<=≤<=106; 0<=≤<=α<=≤<=180). Angle α is given in degrees.
In a single line print a real number — the area of the region which belongs to both given rectangles. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "1 1 45\n", "6 4 30\n" ]
[ "0.828427125\n", "19.668384925\n" ]
The second sample has been drawn on the picture above.
[ { "input": "1 1 45", "output": "0.828427125" }, { "input": "6 4 30", "output": "19.668384925" }, { "input": "100 100 0", "output": "10000.000000000" }, { "input": "100 100 30", "output": "8452.994616207" }, { "input": "303304 904227 3", "output": "262706079399.496890000" }, { "input": "217708 823289 162", "output": "128074702873.298310000" }, { "input": "872657 1807 27", "output": "7192328.918497734" }, { "input": "787062 371814 73", "output": "144562337198.439790000" }, { "input": "925659 774524 134", "output": "587010971679.470460000" }, { "input": "852428 738707 49", "output": "517909750353.868960000" }, { "input": "991024 917226 95", "output": "843996470740.052250000" }, { "input": "938133 287232 156", "output": "182978083107.739690000" }, { "input": "76730 689942 119", "output": "6731488956.790288000" }, { "input": "507487 609004 180", "output": "309061612948.000000000" }, { "input": "646084 979010 45", "output": "491534756284.375060000" }, { "input": "560489 381720 91", "output": "145732354143.406560000" }, { "input": "991245 527535 69", "output": "298092342476.756290000" }, { "input": "129842 930245 115", "output": "18601787610.281502000" }, { "input": "44247 849307 176", "output": "25011463322.593517000" }, { "input": "89781 351632 35", "output": "14053275989.299274000" }, { "input": "36890 754342 82", "output": "1374246169.251312700" }, { "input": "175486 640701 60", "output": "35559391285.091263000" }, { "input": "606243 819219 106", "output": "382341849885.364870000" }, { "input": "520648 189225 167", "output": "83168927181.776108000" }, { "input": "659245 591935 32", "output": "327438873731.782960000" }, { "input": "90002 994645 176", "output": "72280791543.454956000" }, { "input": "228598 881004 56", "output": "63033386343.331917000" }, { "input": "143003 283714 102", "output": "20906720001.826447000" }, { "input": "314304 429528 163", "output": "119035307824.125410000" }, { "input": "135646 480909 0", "output": "65233382214.000000000" }, { "input": "34989 23482 180", "output": "821611698.000000000" }, { "input": "100 10 80", "output": "101.542661189" }, { "input": "2 100 90", "output": "4.000000000" }, { "input": "23141 2132 180", "output": "49336612.000000000" } ]
154
0
3
48,977
908
New Year and Entity Enumeration
[ "bitmasks", "combinatorics", "dp", "math" ]
null
null
You are given an integer *m*. Let *M*<==<=2*m*<=-<=1. You are also given a set of *n* integers denoted as the set *T*. The integers will be provided in base 2 as *n* binary strings of length *m*. A set of integers *S* is called "good" if the following hold. 1. If , then . 1. If , then 1. 1. All elements of *S* are less than or equal to *M*. Here, and refer to the bitwise XOR and bitwise AND operators, respectively. Count the number of good sets *S*, modulo 109<=+<=7.
The first line will contain two integers *m* and *n* (1<=≤<=*m*<=≤<=1<=000, 1<=≤<=*n*<=≤<=*min*(2*m*,<=50)). The next *n* lines will contain the elements of *T*. Each line will contain exactly *m* zeros and ones. Elements of *T* will be distinct.
Print a single integer, the number of good sets modulo 109<=+<=7.
[ "5 3\n11010\n00101\n11000\n", "30 2\n010101010101010010101010101010\n110110110110110011011011011011\n" ]
[ "4\n", "860616440\n" ]
An example of a valid set *S* is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}.
[ { "input": "5 3\n11010\n00101\n11000", "output": "4" }, { "input": "30 2\n010101010101010010101010101010\n110110110110110011011011011011", "output": "860616440" }, { "input": "30 10\n001000000011000111000010010000\n000001100001010000000000000100\n000110100010100000000000101000\n110000010000000001000000000000\n100001000000000010010101000101\n001001000000000100000000110000\n000000010000100000001000000000\n001000010001000000001000000010\n000000110000000001001010000000\n000011001000000000010001000000", "output": "80" } ]
46
5,529,600
0
48,980
354
Transferring Pyramid
[ "dp" ]
null
null
Vasya and Petya are using an interesting data storing structure: a pyramid. The pyramid consists of *n* rows, the *i*-th row contains *i* cells. Each row is shifted half a cell to the left relative to the previous row. The cells are numbered by integers from 1 to as shown on the picture below. An example of a pyramid at *n*<==<=5 is: This data structure can perform operations of two types: 1. Change the value of a specific cell. It is described by three integers: "*t* *i* *v*", where *t*<==<=1 (the type of operation), *i* — the number of the cell to change and *v* the value to assign to the cell. 1. Change the value of some subpyramid. The picture shows a highlighted subpyramid with the top in cell 5. It is described by *s*<=+<=2 numbers: "*t* *i* *v*1 *v*2 ... *v**s*", where *t*<==<=2, *i* — the number of the top cell of the pyramid, *s* — the size of the subpyramid (the number of cells it has), *v**j* — the value you should assign to the *j*-th cell of the subpyramid. Formally: a subpyramid with top at the *i*-th cell of the *k*-th row (the 5-th cell is the second cell of the third row) will contain cells from rows from *k* to *n*, the (*k*<=+<=*p*)-th row contains cells from the *i*-th to the (*i*<=+<=*p*)-th (0<=≤<=*p*<=≤<=*n*<=-<=*k*). Vasya and Petya had two identical pyramids. Vasya changed some cells in his pyramid and he now wants to send his changes to Petya. For that, he wants to find a sequence of operations at which Petya can repeat all Vasya's changes. Among all possible sequences, Vasya has to pick the minimum one (the one that contains the fewest numbers). You have a pyramid of *n* rows with *k* changed cells. Find the sequence of operations which result in each of the *k* changed cells being changed by at least one operation. Among all the possible sequences pick the one that contains the fewest numbers.
The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=105). The next *k* lines contain the coordinates of the modified cells *r**i* and *c**i* (1<=≤<=*c**i*<=≤<=*r**i*<=≤<=*n*) — the row and the cell's number in the row. All cells are distinct.
Print a single number showing how many numbers the final sequence has.
[ "4 5\n3 1\n3 3\n4 1\n4 3\n4 4\n", "7 11\n2 2\n3 1\n4 3\n5 1\n5 2\n5 5\n6 4\n7 2\n7 3\n7 4\n7 5\n" ]
[ "10\n", "26\n" ]
One of the possible solutions of the first sample consists of two operations: 2 4 *v*<sub class="lower-index">4</sub> *v*<sub class="lower-index">7</sub> *v*<sub class="lower-index">8</sub> 2 6 *v*<sub class="lower-index">6</sub> *v*<sub class="lower-index">9</sub> *v*<sub class="lower-index">10</sub> The picture shows the changed cells color-highlighted. The subpyramid used by the first operation is highlighted blue and the subpyramid used by the first operation is highlighted yellow:
[ { "input": "4 5\n3 1\n3 3\n4 1\n4 3\n4 4", "output": "10" }, { "input": "7 11\n2 2\n3 1\n4 3\n5 1\n5 2\n5 5\n6 4\n7 2\n7 3\n7 4\n7 5", "output": "26" }, { "input": "2 3\n1 1\n2 1\n2 2", "output": "5" }, { "input": "10 15\n2 1\n3 2\n4 1\n4 2\n4 3\n5 5\n7 6\n7 7\n8 2\n8 6\n9 7\n10 4\n10 7\n10 9\n10 10", "output": "41" }, { "input": "10 18\n4 3\n5 4\n6 1\n6 3\n7 2\n7 6\n8 2\n9 1\n9 4\n9 6\n9 7\n9 8\n9 9\n10 3\n10 4\n10 7\n10 9\n10 10", "output": "43" }, { "input": "10 11\n4 2\n4 4\n5 2\n6 1\n7 1\n7 4\n7 6\n9 1\n9 4\n9 6\n10 4", "output": "32" }, { "input": "1 1\n1 1", "output": "3" }, { "input": "2 1\n1 1", "output": "3" }, { "input": "2 2\n1 1\n2 2", "output": "5" } ]
1,418
11,468,800
3
49,179
58
Trees
[ "brute force" ]
C. Trees
2
256
On Bertown's main street *n* trees are growing, the tree number *i* has the height of *a**i* meters (1<=≤<=*i*<=≤<=*n*). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the *n*-th one) should be equal to each other, the heights of the 2-nd and the (*n*<=-<=1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful.
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) which is the number of trees. The second line contains integers *a**i* (1<=≤<=*a**i*<=≤<=105) which are the heights of the trees.
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
[ "3\n2 2 2\n", "4\n1 2 2 1\n" ]
[ "1\n", "0\n" ]
none
[ { "input": "3\n2 2 2", "output": "1" }, { "input": "4\n1 2 2 1", "output": "0" }, { "input": "3\n61452 50974 73849", "output": "2" }, { "input": "4\n86002 1199 86003 86002", "output": "1" }, { "input": "5\n92605 92606 41969 98774 92605", "output": "2" }, { "input": "10\n1 1 2 3 4 4 3 2 1 10", "output": "9" }, { "input": "10\n73905 73906 73907 85732 73909 73909 73908 73907 73906 73905", "output": "1" }, { "input": "10\n60718 99414 65042 65043 65044 38495 95782 65042 65041 65040", "output": "4" }, { "input": "7\n1 2 3 7 6 5 4", "output": "3" }, { "input": "5\n4 5 6 5 1", "output": "1" }, { "input": "1\n100000", "output": "0" }, { "input": "5\n1 1 1 1 1", "output": "3" }, { "input": "7\n1 2 3 7 6 5 4", "output": "3" }, { "input": "1\n2727", "output": "0" }, { "input": "5\n1 2 3 2 4", "output": "1" }, { "input": "9\n100 12 13 14 15 14 13 12 11", "output": "1" }, { "input": "5\n2 4 5 4 2", "output": "2" }, { "input": "5\n3 3 4 3 2", "output": "1" } ]
92
0
0
49,221
827
Rusty String
[ "fft", "math", "strings" ]
null
null
Grigory loves strings. Recently he found a metal strip on a loft. The strip had length *n* and consisted of letters "V" and "K". Unfortunately, rust has eaten some of the letters so that it's now impossible to understand which letter was written. Grigory couldn't understand for a long time what these letters remind him of, so he became interested in the following question: if we put a letter "V" or "K" on each unreadable position, which values can the period of the resulting string be equal to? A period of a string is such an integer *d* from 1 to the length of the string that if we put the string shifted by *d* positions to the right on itself, then all overlapping letters coincide. For example, 3 and 5 are periods of "VKKVK".
There are several (at least one) test cases in the input. The first line contains single integer — the number of test cases. There is an empty line before each test case. Each test case is described in two lines: the first line contains single integer *n* (1<=≤<=*n*<=≤<=5·105) — the length of the string, the second line contains the string of length *n*, consisting of letters "V", "K" and characters "?". The latter means the letter on its position is unreadable. It is guaranteed that the sum of lengths among all test cases doesn't exceed 5·105. For hacks you can only use tests with one test case.
For each test case print two lines. In the first line print the number of possible periods after we replace each unreadable letter with "V" or "K". In the next line print all these values in increasing order.
[ "3\n \n5\nV??VK\n \n6\n??????\n \n4\n?VK?\n" ]
[ "2\n3 5\n6\n1 2 3 4 5 6\n3\n2 3 4\n" ]
In the first test case from example we can obtain, for example, "VKKVK", which has periods 3 and 5. In the second test case we can obtain "VVVVVV" which has all periods from 1 to 6. In the third test case string "KVKV" has periods 2 and 4, and string "KVKK" has periods 3 and 4.
[]
3,000
139,571,200
0
49,290
78
Archer's Shot
[ "binary search", "geometry", "math", "two pointers" ]
D. Archer's Shot
2
256
A breakthrough among computer games, "Civilization XIII", is striking in its scale and elaborate details. Let's take a closer look at one of them. The playing area in the game is split into congruent cells that are regular hexagons. The side of each cell is equal to 1. Each unit occupies exactly one cell of the playing field. The field can be considered infinite. Let's take a look at the battle unit called an "Archer". Each archer has a parameter "shot range". It's a positive integer that determines the radius of the circle in which the archer can hit a target. The center of the circle coincides with the center of the cell in which the archer stays. A cell is considered to be under the archer’s fire if and only if all points of this cell, including border points are located inside the circle or on its border. The picture below shows the borders for shot ranges equal to 3, 4 and 5. The archer is depicted as *A*. Find the number of cells that are under fire for some archer.
The first and only line of input contains a single positive integer *k* — the archer's shot range (1<=≤<=*k*<=≤<=106).
Print the single number, the number of cells that are under fire. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
[ "3\n", "4\n", "5\n" ]
[ "7", "13", "19" ]
none
[ { "input": "3", "output": "7" }, { "input": "4", "output": "13" }, { "input": "5", "output": "19" }, { "input": "9", "output": "85" }, { "input": "11", "output": "121" }, { "input": "51", "output": "3037" }, { "input": "101", "output": "12109" }, { "input": "911", "output": "1001425" }, { "input": "1", "output": "1" }, { "input": "2", "output": "1" }, { "input": "8", "output": "61" }, { "input": "6", "output": "31" }, { "input": "7", "output": "43" }, { "input": "10", "output": "97" }, { "input": "13", "output": "175" }, { "input": "43", "output": "2137" }, { "input": "88", "output": "9151" }, { "input": "217", "output": "56425" }, { "input": "301", "output": "108847" }, { "input": "403", "output": "195457" }, { "input": "1034", "output": "1290385" }, { "input": "1249", "output": "1883353" }, { "input": "2749", "output": "9131485" }, { "input": "5832", "output": "41114119" }, { "input": "6486", "output": "50854039" }, { "input": "6726", "output": "54687523" }, { "input": "8910", "output": "95975509" }, { "input": "9654", "output": "112674937" }, { "input": "9991", "output": "120678955" }, { "input": "10000", "output": "120896557" }, { "input": "10264", "output": "127364821" }, { "input": "21352", "output": "551234029" }, { "input": "40094", "output": "1943730367" }, { "input": "47248", "output": "2699275123" }, { "input": "100000", "output": "12091764163" }, { "input": "225481", "output": "61477218007" }, { "input": "498598", "output": "300605820001" }, { "input": "777777", "output": "731487842995" }, { "input": "999811", "output": "1208740231393" }, { "input": "999998", "output": "1209192430093" }, { "input": "999999", "output": "1209194850235" }, { "input": "1000000", "output": "1209197264797" } ]
109
0
-1
49,374
147
Smile House
[ "binary search", "graphs", "matrices" ]
null
null
A smile house is created to raise the mood. It has *n* rooms. Some of the rooms are connected by doors. For each two rooms (number *i* and *j*), which are connected by a door, Petya knows their value *c**ij* — the value which is being added to his mood when he moves from room *i* to room *j*. Petya wondered whether he can raise his mood infinitely, moving along some cycle? And if he can, then what minimum number of rooms he will need to visit during one period of a cycle?
The first line contains two positive integers *n* and *m* (), where *n* is the number of rooms, and *m* is the number of doors in the Smile House. Then follows the description of the doors: *m* lines each containing four integers *i*, *j*, *c**ij* и *c**ji* (1<=≤<=*i*,<=*j*<=≤<=*n*,<=*i*<=≠<=*j*,<=<=-<=104<=≤<=*c**ij*,<=*c**ji*<=≤<=104). It is guaranteed that no more than one door connects any two rooms. No door connects the room with itself.
Print the minimum number of rooms that one needs to visit during one traverse of the cycle that can raise mood infinitely. If such cycle does not exist, print number 0.
[ "4 4\n1 2 -10 3\n1 3 1 -10\n2 4 -10 -1\n3 4 0 -3\n" ]
[ "4\n" ]
Cycle is such a sequence of rooms *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*k*</sub>, that *a*<sub class="lower-index">1</sub> is connected with *a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">2</sub> is connected with *a*<sub class="lower-index">3</sub>, ..., *a*<sub class="lower-index">*k* - 1</sub> is connected with *a*<sub class="lower-index">*k*</sub>, *a*<sub class="lower-index">*k*</sub> is connected with *a*<sub class="lower-index">1</sub>. Some elements of the sequence can coincide, that is, the cycle should not necessarily be simple. The number of rooms in the cycle is considered as *k*, the sequence's length. Note that the minimum possible length equals two.
[]
3,000
307,200
0
49,395
986
Oppa Funcan Style Remastered
[ "graphs", "math", "number theory", "shortest paths" ]
null
null
Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description [here](http://acm.timus.ru/problem.aspx?space=1&amp;num=2107&amp;locale=en)): On the ground there are $n$ platforms, which are numbered with integers from $1$ to $n$, on $i$-th platform there is a dancer with number $i$. Further, every second all the dancers standing on the platform with number $i$ jump to the platform with the number $f(i)$. The moving rule $f$ is selected in advance and is not changed throughout the clip. The duration of the clip was $k$ seconds and the rule $f$ was chosen in such a way that after $k$ seconds all dancers were in their initial positions (i.e. the $i$-th dancer stood on the platform with the number $i$). That allowed to loop the clip and collect even more likes. PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video. In his case "enhanced version" means even more insanity, so the number of platforms can be up to $10^{18}$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $x$ from $1$ to $n$ $f(x) \neq x$ must hold. Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well. PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $f$ for different options.
In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$) — the number of options for $n$ and $k$ to check. In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$) — the number of dancers and the duration in seconds. It is guaranteed that the number of different values of $k$ in one test is not greater than $50$.
Print $t$ lines. If the $i$-th option of the video is feasible, print "YES" (without quotes) in $i$-th line, otherwise print "NO" (without quotes).
[ "3\n7 7\n3 8\n5 6\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
0
0
-1
49,449
394
Dominoes
[ "constructive algorithms", "greedy" ]
null
null
During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes. The teacher responded instantly at our request. He put *nm* dominoes on the table as an *n*<=×<=2*m* rectangle so that each of the *n* rows contained *m* dominoes arranged horizontally. Each half of each domino contained number (0 or 1). We were taken aback, and the teacher smiled and said: "Consider some arrangement of dominoes in an *n*<=×<=2*m* matrix. Let's count for each column of the matrix the sum of numbers in this column. Then among all such sums find the maximum one. Can you rearrange the dominoes in the matrix in such a way that the maximum sum will be minimum possible? Note that it is prohibited to change the orientation of the dominoes, they all need to stay horizontal, nevertheless dominoes are allowed to rotate by 180 degrees. As a reward I will give you all my dominoes". We got even more taken aback. And while we are wondering what was going on, help us make an optimal matrix of dominoes.
The first line contains integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=103). In the next lines there is a description of the teachers' matrix. Each of next *n* lines contains *m* dominoes. The description of one domino is two integers (0 or 1), written without a space — the digits on the left and right half of the domino.
Print the resulting matrix of dominoes in the format: *n* lines, each of them contains *m* space-separated dominoes. If there are multiple optimal solutions, print any of them.
[ "2 3\n01 11 00\n00 01 11\n", "4 1\n11\n10\n01\n00\n" ]
[ "11 11 10\n00 00 01\n", "11\n10\n01\n00\n" ]
Consider the answer for the first sample. There, the maximum sum among all columns equals 1 (the number of columns is 6, and not 3). Obviously, this maximum can't be less than 1, then such matrix is optimal. Note that the dominoes can be rotated by 180 degrees.
[ { "input": "2 3\n01 11 00\n00 01 11", "output": "11 11 10\n00 00 01" }, { "input": "4 1\n11\n10\n01\n00", "output": "11\n10\n01\n00" }, { "input": "1 1\n00", "output": "00" }, { "input": "1 1\n01", "output": "10" }, { "input": "1 1\n11", "output": "11" }, { "input": "9 9\n01 00 00 01 00 01 11 11 11\n10 10 10 01 10 01 11 01 10\n10 00 10 00 11 01 00 10 00\n01 00 01 01 11 00 00 11 11\n11 00 10 11 01 01 11 00 01\n01 10 00 00 11 10 01 01 10\n11 10 11 00 11 11 01 10 10\n10 00 01 00 00 00 11 01 01\n00 11 01 00 10 01 10 00 01", "output": "11 11 11 11 11 11 11 11 11\n11 11 11 11 11 11 11 11 11\n10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10\n10 10 10 01 01 01 01 01 01\n01 01 01 01 01 01 01 01 01\n01 01 01 00 00 00 00 01 01\n00 00 00 00 00 00 00 00 00\n00 00 00 00 00 00 00 00 00" }, { "input": "9 9\n10 10 10 01 10 11 11 01 10\n11 00 10 10 11 10 01 00 00\n10 00 11 01 00 01 01 11 10\n10 11 10 00 01 11 11 10 11\n01 11 11 01 11 00 10 00 01\n01 00 00 10 01 01 10 00 01\n11 10 11 10 01 00 00 11 00\n10 11 10 10 01 10 10 10 01\n10 10 10 10 11 11 01 00 11", "output": "11 11 11 11 11 11 11 11 11\n11 11 11 11 11 11 11 11 11\n11 11 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 01 01\n01 01 01 01 01 01 01 01 01\n01 01 01 01 01 01 01 01 01\n00 00 00 00 01 01 01 00 00\n00 00 00 00 00 00 00 00 00" }, { "input": "9 1\n01\n00\n01\n01\n00\n00\n00\n01\n11", "output": "11\n10\n10\n01\n01\n00\n00\n00\n00" }, { "input": "2 9\n11 10 11 10 10 11 00 10 00\n10 00 00 10 10 00 11 01 01", "output": "11 11 11 11 10 10 10 10 10\n00 00 00 00 00 01 01 01 01" }, { "input": "2 8\n10 01 01 11 10 10 01 10\n01 11 01 01 11 10 01 01", "output": "11 11 11 10 10 10 10 10\n10 10 01 01 01 01 01 01" }, { "input": "3 5\n00 10 10 11 01\n11 01 11 11 10\n10 11 00 00 00", "output": "11 11 11 11 11\n10 10 10 01 01\n00 00 01 00 00" }, { "input": "2 3\n00 10 01\n01 01 00", "output": "10 10 01\n00 01 00" }, { "input": "2 5\n01 00 01 01 00\n11 01 11 11 10", "output": "11 11 11 10 10\n10 00 00 01 01" } ]
358
614,400
3
49,560
380
Sereja and Prefixes
[ "binary search", "brute force" ]
null
null
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in *m* stages. Each time he either adds a new number to the end of the sequence or takes *l* first elements of the current sequence and adds them *c* times to the end. More formally, if we represent the current sequence as *a*1,<=*a*2,<=...,<=*a**n*, then after we apply the described operation, the sequence transforms into *a*1,<=*a*2,<=...,<=*a**n*[,<=*a*1,<=*a*2,<=...,<=*a**l*] (the block in the square brackets must be repeated *c* times). A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
The first line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of stages to build a sequence. Next *m* lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer *x**i* (1<=≤<=*x**i*<=≤<=105) — the number to add. Type 2 means copying a prefix of length *l**i* to the end *c**i* times, in this case the line further contains two integers *l**i*,<=*c**i* (1<=≤<=*l**i*<=≤<=105,<=1<=≤<=*c**i*<=≤<=104), *l**i* is the length of the prefix, *c**i* is the number of copyings. It is guaranteed that the length of prefix *l**i* is never larger than the current length of the sequence. The next line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
[ "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n" ]
[ "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4\n" ]
none
[ { "input": "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "output": "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4" }, { "input": "2\n1 33085\n1 44638\n2\n1 2", "output": "33085 44638" }, { "input": "10\n1 57757\n1 45234\n1 80807\n1 38496\n1 27469\n1 42645\n1 72643\n1 33235\n1 10843\n1 80598\n10\n1 2 3 4 5 6 7 8 9 10", "output": "57757 45234 80807 38496 27469 42645 72643 33235 10843 80598" }, { "input": "3\n1 97601\n1 32580\n1 70519\n3\n1 2 3", "output": "97601 32580 70519" }, { "input": "7\n1 53989\n1 47249\n1 71935\n2 1 3\n1 84520\n1 84185\n2 6 1\n14\n1 2 3 4 5 6 7 8 9 10 11 12 13 14", "output": "53989 47249 71935 53989 53989 53989 84520 84185 53989 47249 71935 53989 53989 53989" }, { "input": "1\n1 1\n1\n1", "output": "1" } ]
77
307,200
0
49,563
938
Max History
[ "combinatorics", "math" ]
null
null
You are given an array *a* of length *n*. We define *f**a* the following way: - Initially *f**a*<==<=0, *M*<==<=1; - for every 2<=≤<=*i*<=≤<=*n* if *a**M*<=&lt;<=*a**i* then we set *f**a*<==<=*f**a*<=+<=*a**M* and then set *M*<==<=*i*. Calculate the sum of *f**a* over all *n*! permutations of the array *a* modulo 109<=+<=7. Note: two elements are considered different if their indices differ, so for every array *a* there are exactly *n*! permutations.
The first line contains integer *n* (1<=≤<=*n*<=≤<=<=1 000 000) — the size of array *a*. Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=<=*a**i*<=≤<=<=109).
Print the only integer, the sum of *f**a* over all *n*! permutations of the array *a* modulo 109<=+<=7.
[ "2\n1 3\n", "3\n1 1 2\n" ]
[ "1", "4" ]
For the second example all the permutations are: - *p* = [1, 2, 3] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [1, 3, 2] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [2, 1, 3] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [2, 3, 1] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [3, 1, 2] : *f*<sub class="lower-index">*a*</sub> is equal to 0; - *p* = [3, 2, 1] : *f*<sub class="lower-index">*a*</sub> is equal to 0. Where *p* is the array of the indices of initial array *a*. The sum of *f*<sub class="lower-index">*a*</sub> is equal to 4.
[ { "input": "2\n1 3", "output": "1" }, { "input": "3\n1 1 2", "output": "4" }, { "input": "6\n1 4 5 2 3 3", "output": "2928" }, { "input": "8\n8 7 5 4 6 6 6 6", "output": "351360" }, { "input": "8\n1 2 3 9 100 100 100 100", "output": "109296" }, { "input": "1\n364489807", "output": "0" }, { "input": "1\n194945396", "output": "0" }, { "input": "9\n25401015 88843847 702650194 306965770 57623156 571088345 835502151 56113403 116176210", "output": "168126961" }, { "input": "3\n855856619 518546431 920370158", "output": "604662698" }, { "input": "7\n686312223 948248999 138090108 566544521 711825575 414057105 925454439", "output": "75238511" }, { "input": "5\n516767827 377951584 355810087 196333905 38926793", "output": "733758401" }, { "input": "9\n347223417 807654168 573530036 826123287 366028010 257025851 15406743 784063803 132844347", "output": "932879531" }, { "input": "7\n177679021 237356752 791250000 455912656 693129227 678510224 60382864", "output": "14393405" }, { "input": "7\n8134640 667059306 8969950 85702040 20230430 99994612 105359016", "output": "264207095" }, { "input": "1\n911130621", "output": "0" }, { "input": "4\n1 1 8 10", "output": "108" } ]
0
0
-1
49,874