question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
You are given a sequence of integers a_1, a_2, ..., a_n. You need to paint elements in colors, so that: If we consider any color, all elements of this color must be divisible by the minimal element of this color. The number of used colors must be minimized. For example, it's fine to paint elements [40, 10, 60] in a single color, because they are all divisible by 10. You can use any color an arbitrary amount of times (in particular, it is allowed to use a color only once). The elements painted in one color do not need to be consecutive.For example, if a=[6, 2, 3, 4, 12] then two colors are required: let's paint 6, 3 and 12 in the first color (6, 3 and 12 are divisible by 3) and paint 2 and 4 in the second color (2 and 4 are divisible by 2). For example, if a=[10, 7, 15] then 3 colors are required (we can simply paint each element in an unique color). | Input: ['6', '10 2 3 5 4 2', ''] Output:['3', ''] | [
2,
3
] |
You are given two integers n and k.You need to construct k regular polygons having same circumcircle, with distinct number of sides l between 3 and n. Illustration for the first example. You can rotate them to minimize the total number of distinct points on the circle. Find the minimum number of such points. | Input: ['6 2', ''] Output:['6', ''] | [
2,
3
] |
You are given an array a of n integers.You need to find the maximum value of a_{i} | ( a_{j} \& a_{k} ) over all triplets (i,j,k) such that i < j < k.Here \& denotes the bitwise AND operation, and | denotes the bitwise OR operation. | Input: ['3', '2 4 6', ''] Output:['6', ''] | [
2
] |
An array of integers p_{1},p_{2}, ...,p_{n} is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3,1,2], [1], [1,2,3,4,5] and [4,3,1,2]. The following arrays are not permutations: [2], [1,1], [2,3,4].There is a hidden permutation of length n.For each index i, you are given s_{i}, which equals to the sum of all p_{j} such that j < i and p_{j} < p_{i}. In other words, s_i is the sum of elements before the i-th element that are smaller than the i-th element.Your task is to restore the permutation. | Input: ['3', '0 0 0', ''] Output:['3 2 1', ''] | [
2,
4
] |
You are given an array a_{1}, a_{2}, ..., a_{n}. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers l and r (1 <=q l <=q r <=q n) and delete integers a_l, a_{l+1}, ..., a_r from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. | Input: ['3', '1 2 3', ''] Output:['0', ''] | [
0,
4
] |
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: f(0) = a; f(1) = b; f(n) = f(n-1) \oplus f(n-2) when n > 1, where \oplus denotes the bitwise XOR operation. You are given three integers a, b, and n, calculate f(n).You have to answer for T independent test cases. | Input: ['3', '3 4 2', '4 5 0', '325 265 1231232', ''] Output:['7', '4', '76', ''] | [
3
] |
You are given an array a consisting of 500000 integers (numbered from 1 to 500000). Initially all elements of a are zero.You have to process two types of queries to this array: 1 x y β increase a_x by y; 2 x y β compute \sum\limits_{i \in R(x, y)} a_i, where R(x, y) is the set of all integers from 1 to 500000 which have remainder y modulo x. Can you process all the queries? | Input: ['5', '1 3 4', '2 3 0', '2 4 3', '1 4 -4', '2 1 0', ''] Output:['4', '4', '0', ''] | [
0
] |
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury picked an integer x not less than 0 and not greater than 2^{14} - 1. You have to guess this integer.To do so, you may ask no more than 2 queries. Each query should consist of 100 integer numbers a_1, a_2, ..., a_{100} (each integer should be not less than 0 and not greater than 2^{14} - 1). In response to your query, the jury will pick one integer i (1 <= i <= 100) and tell you the value of a_i \oplus x (the bitwise XOR of a_i and x). There is an additional constraint on the queries: all 200 integers you use in the queries should be distinct.It is guaranteed that the value of x is fixed beforehand in each test, but the choice of i in every query may depend on the integers you send. | Input: ['0', '32'] Output:['? 3 5 6', '? 32 24 37', '! 5'] | [
3
] |
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment [0, n] on OX axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval (x, x + 1) with integer x. So we can represent the road as a binary string consisting of n characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad.Usually, we can install the pipeline along the road on height of 1 unit with supporting pillars in each integer point (so, if we are responsible for [0, n] road, we must install n + 1 pillars). But on crossroads we should lift the pipeline up to the height 2, so the pipeline won't obstruct the way for cars.We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment [x, x + 1] with integer x consisting of three parts: 0.5 units of horizontal pipe + 1 unit of vertical pipe + 0.5 of horizontal. Note that if pipeline is currently on height 2, the pillars that support it should also have length equal to 2 units. Each unit of gas pipeline costs us a bourles, and each unit of pillar β b bourles. So, it's not always optimal to make the whole pipeline on the height 2. Find the shape of the pipeline with minimum possible cost and calculate that cost.Note that you must start and finish the pipeline on height 1 and, also, it's guaranteed that the first and last characters of the input string are equal to 0. | Input: ['4', '8 2 5', '00110010', '8 1 1', '00110010', '9 100000000 100000000', '010101010', '2 5 1', '00', ''] Output:['94', '25', '2900000000', '13', ''] | [
2
] |
You are given two matrices A and B. Each matrix contains exactly n rows and m columns. Each element of A is either 0 or 1; each element of B is initially 0.You may perform some operations with matrix B. During each operation, you choose any submatrix of B having size 2 * 2, and replace every element in the chosen submatrix with 1. In other words, you choose two integers x and y such that 1 <= x < n and 1 <= y < m, and then set B_{x, y}, B_{x, y + 1}, B_{x + 1, y} and B_{x + 1, y + 1} to 1.Your goal is to make matrix B equal to matrix A. Two matrices A and B are equal if and only if every element of matrix A is equal to the corresponding element of matrix B.Is it possible to make these matrices equal? If it is, you have to come up with a sequence of operations that makes B equal to A. Note that you don't have to minimize the number of operations. | Input: ['3 3', '1 1 1', '1 1 1', '0 1 1', ''] Output:['3', '1 1', '1 2', '2 2', ''] | [
2
] |
There are two types of burgers in your restaurant β hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have b buns, p beef patties and f chicken cutlets in your restaurant. You can sell one hamburger for h dollars and one chicken burger for c dollars. Calculate the maximum profit you can achieve.You have to answer t independent queries. | Input: ['3', '15 2 3', '5 10', '7 5 2', '10 12', '1 100 100', '100 100', ''] Output:['40', '34', '0', ''] | [
0,
2,
3
] |
You are given an array A, consisting of n positive integers a_1, a_2, ..., a_n, and an array B, consisting of m positive integers b_1, b_2, ..., b_m. Choose some element a of A and some element b of B such that a+b doesn't belong to A and doesn't belong to B. For example, if A = [2, 1, 7] and B = [1, 3, 4], we can choose 1 from A and 4 from B, as number 5 = 1 + 4 doesn't belong to A and doesn't belong to B. However, we can't choose 2 from A and 1 from B, as 3 = 2 + 1 belongs to B.It can be shown that such a pair exists. If there are multiple answers, print any.Choose and print any such two numbers. | Input: ['1', '20', '2', '10 20', ''] Output:['20 20'] | [
3
] |
Define the beauty of a permutation of numbers from 1 to n (p_1, p_2, ..., p_n) as number of pairs (L, R) such that 1 <= L <= R <= n and numbers p_L, p_{L+1}, ..., p_R are consecutive R-L+1 numbers in some order. For example, the beauty of the permutation (1, 2, 5, 3, 4) equals 9, and segments, corresponding to pairs, are [1], [2], [5], [4], [3], [1, 2], [3, 4], [5, 3, 4], [1, 2, 5, 3, 4].Answer q independent queries. In each query, you will be given integers n and k. Determine if there exists a permutation of numbers from 1 to n with beauty equal to k, and if there exists, output one of them. | Input: ['4', '1 1', '5 6', '5 8', '5 10', ''] Output:['YES', '1 ', 'YES', '2 4 1 5 3 ', 'NO', 'YES', '2 3 1 4 5 ', ''] | [
3
] |
You are given n integer numbers a_1, a_2, ..., a_n. Consider graph on n nodes, in which nodes i, j (i\neq j) are connected if and only if, a_i AND a_j\neq 0, where AND denotes the bitwise AND operation.Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all. | Input: ['4', '3 6 28 9', ''] Output:['4'] | [
0
] |
You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied:For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1.For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. | Input: ['3', ''] Output:['YES', '1 4 5 2 3 6 '] | [
2,
3
] |
Natasha's favourite numbers are n and 1, and Sasha's favourite numbers are m and -1. One day Natasha and Sasha met and wrote down every possible array of length n+m such that some n of its elements are equal to 1 and another m elements are equal to -1. For each such array they counted its maximal prefix sum, probably an empty one which is equal to 0 (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as f(a) the maximal prefix sum of an array a_{1, ... ,l} of length l >=q 0. Then: f(a) = \max (0, \smash{\displaystyle\max_{1 <=q i <=q l}} \sum_{j=1}^{i} a_j )Now they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo 998\: 244\: 853. | Input: ['0 2'] Output:['0'] | [
3
] |
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.Kirk has a binary string s (a string which consists of zeroes and ones) of length n and he is asking you to find a binary string t of the same length which satisfies the following conditions: For any l and r (1 <=q l <=q r <=q n) the length of the longest non-decreasing subsequence of the substring s_{l}s_{l+1} ... s_{r} is equal to the length of the longest non-decreasing subsequence of the substring t_{l}t_{l+1} ... t_{r}; The number of zeroes in t is the maximum possible.A non-decreasing subsequence of a string p is a sequence of indices i_1, i_2, ..., i_k such that i_1 < i_2 < ... < i_k and p_{i_1} <=q p_{i_2} <=q ... <=q p_{i_k}. The length of the subsequence is k.If there are multiple substrings which satisfy the conditions, output any. | Input: ['110', ''] Output:['010', ''] | [
2,
3
] |
The only difference between easy and hard versions is the length of the string. You can hack this problem only if you solve both problems.Kirk has a binary string s (a string which consists of zeroes and ones) of length n and he is asking you to find a binary string t of the same length which satisfies the following conditions: For any l and r (1 <=q l <=q r <=q n) the length of the longest non-decreasing subsequence of the substring s_{l}s_{l+1} ... s_{r} is equal to the length of the longest non-decreasing subsequence of the substring t_{l}t_{l+1} ... t_{r}; The number of zeroes in t is the maximum possible.A non-decreasing subsequence of a string p is a sequence of indices i_1, i_2, ..., i_k such that i_1 < i_2 < ... < i_k and p_{i_1} <=q p_{i_2} <=q ... <=q p_{i_k}. The length of the subsequence is k.If there are multiple substrings which satisfy the conditions, output any. | Input: ['110', ''] Output:['010', ''] | [
0,
2
] |
The main characters have been omitted to be short.You are given a directed unweighted graph without loops with n vertexes and a path in it (that path is not necessary simple) given by a sequence p_1, p_2, ..., p_m of m vertexes; for each 1 <=q i < m there is an arc from p_i to p_{i+1}.Define the sequence v_1, v_2, ..., v_k of k vertexes as good, if v is a subsequence of p, v_1 = p_1, v_k = p_m, and p is one of the shortest paths passing through the vertexes v_1, ..., v_k in that order.A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements. It is obvious that the sequence p is good but your task is to find the shortest good subsequence.If there are multiple shortest good subsequences, output any of them. | Input: ['4', '0110', '0010', '0001', '1000', '4', '1 2 3 4', ''] Output:['3', '1 2 4 '] | [
2
] |
Mislove had an array a_1, a_2, \cdots, a_n of n positive integers, but he has lost it. He only remembers the following facts about it: The number of different numbers in the array is not less than l and is not greater than r; For each array's element a_i either a_i = 1 or a_i is even and there is a number \dfrac{a_i}{2} in the array.For example, if n=5, l=2, r=3 then an array could be [1,2,2,4,4] or [1,1,1,1,2]; but it couldn't be [1,2,2,4,8] because this array contains 4 different numbers; it couldn't be [1,2,2,3,3] because 3 is odd and isn't equal to 1; and it couldn't be [1,1,2,2,16] because there is a number 16 in the array but there isn't a number \frac{16}{2} = 8.According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array. | Input: ['4 2 2', ''] Output:['5 7', ''] | [
2,
3
] |
In the city of Saint Petersburg, a day lasts for 2^{100} minutes. From the main station of Saint Petersburg, a train departs after 1 minute, 4 minutes, 16 minutes, and so on; in other words, the train departs at time 4^k for each integer k >=q 0. Team BowWow has arrived at the station at the time s and it is trying to count how many trains have they missed; in other words, the number of trains that have departed strictly before time s. For example if s = 20, then they missed trains which have departed at 1, 4 and 16. As you are the only one who knows the time, help them!Note that the number s will be given you in a binary representation without leading zeroes. | Input: ['100000000', ''] Output:['4', ''] | [
3
] |
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.Polycarp is a very famous freelancer. His current rating is r units.Some very rich customers asked him to complete some projects for their companies. To complete the i-th project, Polycarp needs to have at least a_i units of rating; after he completes this project, his rating will change by b_i (his rating will increase or decrease by b_i) (b_i can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether.To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.Your task is to calculate the maximum possible size of such subset of projects. | Input: ['3 4', '4 6', '10 -2', '8 -1', ''] Output:['3', ''] | [
2
] |
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.Polycarp is a very famous freelancer. His current rating is r units.Some very rich customers asked him to complete some projects for their companies. To complete the i-th project, Polycarp needs to have at least a_i units of rating; after he completes this project, his rating will change by b_i (his rating will increase or decrease by b_i) (b_i can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. | Input: ['3 4', '4 6', '10 -2', '8 -1', ''] Output:['YES', ''] | [
2
] |
There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).Write a program that for given current values βa_i will find the maximum possible number of boxers in a team.It is possible that after some change the weight of some boxer is 150001 (but no more). | Input: ['43 2 4 1'] Output:['4'] | [
2
] |
The only difference between easy and hard versions is the length of the string.You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s. | Input: ['bbaba', 'bb', ''] Output:['3', ''] | [
2,
4
] |
The only difference between easy and hard versions is the length of the string.You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.If you want to remove the substring s[l;r] then the string s will be transformed to s_1 s_2 ... s_{l-1} s_{r+1} s_{r+2} ... s_{|s|-1} s_{|s|} (where |s| is the length of s).Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s. | Input: ['bbaba', 'bb', ''] Output:['3', ''] | [
2
] |
You are given an array a consisting of n integers.Your task is to say the number of such positive integers x such that x divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.For example, if the array a will be [2, 4, 6, 2, 10], then 1 and 2 divide each number from the array (so the answer for this test is 2). | Input: ['5', '1 2 3 4 5', ''] Output:['1', ''] | [
3
] |
You are given 4n sticks, the length of the i-th stick is a_i.You have to create n rectangles, each rectangle will consist of exactly 4 sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.You want to all rectangles to have equal area. The area of the rectangle with sides a and b is a \cdot b.Your task is to say if it is possible to create exactly n rectangles of equal area or not.You have to answer q independent queries. | Input: ['5', '1', '1 1 10 10', '2', '10 5 2 10 1 1 2 5', '2', '10 5 1 10 5 1 1 1', '2', '1 1 1 1 1 1 1 1', '1', '10000 10000 10000 10000', ''] Output:['YES', 'YES', 'NO', 'YES', 'YES', ''] | [
2,
3
] |
You are given a uppercase Latin letters 'A' and b letters 'B'.The period of the string is the smallest such positive integer k that s_i = s_{i~mod~k} (0-indexed) for each i. Note that this implies that k won't always divide a+b = |s|.For example, the period of string "ABAABAA" is 3, the period of "AAAA" is 1, and the period of "AABBB" is 5.Find the number of different periods over all possible strings with a letters 'A' and b letters 'B'. | Input: ['2 4', ''] Output:['4', ''] | [
3,
4
] |
You are given a string t and n strings s_1, s_2, ..., s_n. All strings consist of lowercase Latin letters.Let f(t, s) be the number of occurences of string s in string t. For example, f('\text{aaabacaa}', '\text{aa}') = 3, and f('\text{ababa}', '\text{aba}') = 2.Calculate the value of \sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j), where s + t is the concatenation of strings s and t. Note that if there are two pairs i_1, j_1 and i_2, j_2 such that s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}, you should include both f(t, s_{i_1} + s_{j_1}) and f(t, s_{i_2} + s_{j_2}) in answer. | Input: ['aaabacaa', '2', 'a', 'aa', ''] Output:['5', ''] | [
0
] |
The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer n. You have to find a sequence s consisting of digits \{1, 3, 7\} such that it has exactly n subsequences equal to 1337.For example, sequence 337133377 has 6 subsequences equal to 1337: 337\underline{1}3\underline{3}\underline{3}7\underline{7} (you can remove the second and fifth characters); 337\underline{1}\underline{3}3\underline{3}7\underline{7} (you can remove the third and fifth characters); 337\underline{1}\underline{3}\underline{3}37\underline{7} (you can remove the fourth and fifth characters); 337\underline{1}3\underline{3}\underline{3}\underline{7}7 (you can remove the second and sixth characters); 337\underline{1}\underline{3}3\underline{3}\underline{7}7 (you can remove the third and sixth characters); 337\underline{1}\underline{3}\underline{3}3\underline{7}7 (you can remove the fourth and sixth characters). Note that the length of the sequence s must not exceed 10^5.You have to answer t independent queries. | Input: ['2', '6', '1', ''] Output:['113337', '1337'] | [
3
] |
You have a string s β a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' β move one cell up; 'S' β move one cell down; 'A' β move one cell left; 'D' β move one cell right. Let Grid(s) be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands s. For example, if s = \text{DSAWWAW} then Grid(s) is the 4 * 3 grid: you can place the robot in the cell (3, 2); the robot performs the command 'D' and moves to (3, 3); the robot performs the command 'S' and moves to (4, 3); the robot performs the command 'A' and moves to (4, 2); the robot performs the command 'W' and moves to (3, 2); the robot performs the command 'W' and moves to (2, 2); the robot performs the command 'A' and moves to (2, 1); the robot performs the command 'W' and moves to (1, 1). You have 4 extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence s to minimize the area of Grid(s).What is the minimum area of Grid(s) you can achieve? | Input: ['3', 'DSAWWAW', 'D', 'WA', ''] Output:['8', '2', '4', ''] | [
0,
2,
3
] |
Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows: it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0; it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04; it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048; it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482; it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824. This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string s β the remaining data of the sequence. For all 0 <= x, y < 10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can't change the order of digits in string s or erase any of them; only insertions are allowed. | Input: ['0840', ''] Output:['-1 17 7 7 7 -1 2 17 2 7 ', '17 17 7 5 5 5 2 7 2 7 ', '7 7 7 4 3 7 1 7 2 5 ', '7 5 4 7 3 3 2 5 2 3 ', '7 5 3 3 7 7 1 7 2 7 ', '-1 5 7 3 7 -1 2 9 2 7 ', '2 2 1 2 1 2 2 2 0 1 ', '17 7 7 5 7 9 2 17 2 3 ', '2 2 2 2 2 2 0 2 2 2 ', '7 7 5 3 7 7 1 3 2 7 ', ''] | [
0
] |
You are given two binary strings x and y, which are binary representations of some two integers (let's denote these integers as f(x) and f(y)). You can choose any integer k >= 0, calculate the expression s_k = f(x) + f(y) \cdot 2^k and write the binary representation of s_k in reverse order (let's denote it as rev_k). For example, let x = 1010 and y = 11; you've chosen k = 1 and, since 2^1 = 10_2, so s_k = 1010_2 + 11_2 \cdot 10_2 = 10000_2 and rev_k = 00001.For given x and y, you need to choose such k that rev_k is lexicographically minimal (read notes if you don't know what does "lexicographically" means).It's guaranteed that, with given constraints, k exists and is finite. | Input: ['4', '1010', '11', '10001', '110', '1', '1', '1010101010101', '11110000', ''] Output:['1', '3', '0', '0', ''] | [
2
] |
You are on the island which can be represented as a n * m table. The rows are numbered from 1 to n and the columns are numbered from 1 to m. There are k treasures on the island, the i-th of them is located at the position (r_i, c_i).Initially you stand at the lower left corner of the island, at the position (1, 1). If at any moment you are at the cell with a treasure, you can pick it up without any extra time. In one move you can move up (from (r, c) to (r+1, c)), left (from (r, c) to (r, c-1)), or right (from position (r, c) to (r, c+1)). Because of the traps, you can't move down.However, moving up is also risky. You can move up only if you are in a safe column. There are q safe columns: b_1, b_2, ..., b_q. You want to collect all the treasures as fast as possible. Count the minimum number of moves required to collect all the treasures. | Input: ['3 3 3 2', '1 1', '2 1', '3 1', '2 3', ''] Output:['6'] | [
2,
4
] |
You are given an array a of n integers, where n is odd. You can make the following operation with it: Choose one of the elements of the array (for example a_i) and increase it by 1 (that is, replace it with a_i + 1). You want to make the median of the array the largest possible using at most k operations.The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1, 5, 2, 3, 5] is 3. | Input: ['3 2', '1 3 5', ''] Output:['5'] | [
2,
3,
4
] |
You are given an array a_1, a_2, ..., a_n.In one operation you can choose two elements a_i and a_j (i!=j) and decrease each of them by one.You need to check whether it is possible to make all the elements equal to zero or not. | Input: ['4', '1 1 2 2', ''] Output:['YES'] | [
2,
3
] |
Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], ..., e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself.A travel on this graph works as follows. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. The next vertex is e_i[x] where x is an integer 0 <= x <= m_i-1 satisfying x \equiv c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly.For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 <= x <= 1) where x \equiv c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on.Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. | Input: ['4', '0 0 0 0', '2', '2 3', '1', '2', '3', '2 4 1', '4', '3 1 2 1', '6', '1 0', '2 0', '3 -1', '4 -2', '1 1', '1 5', ''] Output:['1', '1', '2', '1', '3', '2', ''] | [
0,
3
] |
Amugae has a sentence consisting of n words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges "sample" and "please" into "samplease".Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends. | Input: ['5', 'I want to order pizza', ''] Output:['Iwantorderpizza'] | [
0
] |
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of n rows and n columns of square cells. The rows are numbered from 1 to n, from top to bottom, and the columns are numbered from 1 to n, from left to right. The position of a cell at row r and column c is represented as (r, c). There are only two colors for the cells in cfpaint β black and white.There is a tool named eraser in cfpaint. The eraser has an integer size k (1 <= k <= n). To use the eraser, Gildong needs to click on a cell (i, j) where 1 <= i, j <= n - k + 1. When a cell (i, j) is clicked, all of the cells (i', j') where i <= i' <= i + k - 1 and j <= j' <= j + k - 1 become white. In other words, a square with side equal to k cells and top left corner at (i, j) is colored white.A white line is a row or a column without any black cells.Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question. | Input: ['4 2', 'BWWW', 'WBBW', 'WBBW', 'WWWB', ''] Output:['4', ''] | [
0
] |
Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wall always exists at the 12 o'clock position. The inner area's sectors are denoted as (1,1), (1,2), ..., (1,n) in clockwise direction. The outer area's sectors are denoted as (2,1), (2,2), ..., (2,m) in the same manner. For a clear understanding, see the example image above.Amugae wants to know if he can move from one sector to another sector. He has q questions.For each question, check if he can move between two given sectors. | Input: ['4 6 3', '1 1 2 3', '2 6 1 2', '2 6 2 4', ''] Output:['YES', 'NO', 'YES', ''] | [
3
] |
Gildong is playing a video game called Block Adventure. In Block Adventure, there are n columns of blocks in a row, and the columns are numbered from 1 to n. All blocks have equal heights. The height of the i-th column is represented as h_i, which is the number of blocks stacked in the i-th column.Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the 1-st column. The goal of the game is to move the character to the top of the n-th column.The character also has a bag that can hold infinitely many blocks. When the character is on the top of the i-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the i-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the i-th column; if i < n and |h_i - h_{i+1}| <= k, move the character to the top of the i+1-st column. k is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the i-th column, and the value h_i changes.The character initially has m blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question. | Input: ['53 0 14 3 53 1 21 4 74 10 010 20 10 202 5 50 111 9 999'] Output:['YESNOYESNOYES'] | [
2
] |
Amugae has a hotel consisting of 10 rooms. The rooms are numbered from 0 to 9 from left to right.The hotel has two entrances β one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.One day, Amugae lost the room assignment list. Thankfully Amugae's memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae's memory. | Input: ['8', 'LLRL1RL1', ''] Output:['1010000011'] | [
0
] |
While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly H centimeters above the water surface. Inessa grabbed the flower and sailed the distance of L centimeters. Exactly at this point the flower touched the water surface. Suppose that the lily grows at some point A on the lake bottom, and its stem is always a straight segment with one endpoint at point A. Also suppose that initially the flower was exactly above the point A, i.e. its stem was vertical. Can you determine the depth of the lake at point A? | Input: ['1 2', ''] Output:['1.5000000000000', ''] | [
3
] |
You are given an array of n integers. You need to split all integers into two groups so that the GCD of all integers in the first group is equal to one and the GCD of all integers in the second group is equal to one.The GCD of a group of integers is the largest non-negative integer that divides all the integers in the group.Both groups have to be non-empty. | Input: ['4', '2 3 6 7', ''] Output:['YES', '2 2 1 1 ', ''] | [
2
] |
You are given a graph with 3 \cdot n vertices and m edges. You are to find a matching of n edges, or an independent set of n vertices.A set of edges is called a matching if no two edges share an endpoint.A set of vertices is called an independent set if no two vertices are connected with an edge. | Input: ['4', '1 2', '1 3', '1 2', '1 2', '1 3', '1 2', '2 5', '1 2', '3 1', '1 4', '5 1', '1 6', '2 15', '1 2', '1 3', '1 4', '1 5', '1 6', '2 3', '2 4', '2 5', '2 6', '3 4', '3 5', '3 6', '4 5', '4 6', '5 6', ''] Output:['Matching', '2', 'IndSet', '1', 'IndSet', '2 4', 'Matching', '1 15', ''] | [
2
] |
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events. | Input: ['4', '1 2 3 4', '3', '2 3', '1 2 2', '2 1', ''] Output:['3 2 3 4 ', ''] | [
0,
4
] |
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has n different matryoshkas. Any matryoshka is a figure of volume out_i with an empty space inside of volume in_i (of course, out_i > in_i).You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll β inside the third one and so on. Matryoshka i can be nested inside matryoshka j if out_i <= in_j. So only the last doll will take space inside your bag.Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + ... + (in_{i_k} - out_{i_{k-1}}), where i_1, i_2, ..., i_k are the indices of the chosen dolls in the order they are nested in each other.Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index i such that one of the subsets contains the i-th doll, and another subset doesn't.Since the answer can be large, print it modulo 10^9 + 7. | Input: ['7', '4 1', '4 2', '4 2', '2 1', '5 4', '6 4', '3 2', ''] Output:['6', ''] | [
4
] |
You are given an array a_1, a_2, ... , a_n and two integers m and k.You can choose some subarray a_l, a_{l+1}, ..., a_{r-1}, a_r. The cost of subarray a_l, a_{l+1}, ..., a_{r-1}, a_r is equal to \sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil, where \lceil x \rceil is the least integer greater than or equal to x. The cost of empty subarray is equal to zero.For example, if m = 3, k = 10 and a = [2, -4, 15, -3, 4, 8, 3], then the cost of some subarrays are: a_3 ... a_3: 15 - k \lceil \frac{1}{3} \rceil = 15 - 10 = 5; a_3 ... a_4: (15 - 3) - k \lceil \frac{2}{3} \rceil = 12 - 10 = 2; a_3 ... a_5: (15 - 3 + 4) - k \lceil \frac{3}{3} \rceil = 16 - 10 = 6; a_3 ... a_6: (15 - 3 + 4 + 8) - k \lceil \frac{4}{3} \rceil = 24 - 20 = 4; a_3 ... a_7: (15 - 3 + 4 + 8 + 3) - k \lceil \frac{5}{3} \rceil = 27 - 20 = 7. Your task is to find the maximum cost of some subarray (possibly empty) of array a. | Input: ['7 3 10', '2 -4 15 -3 4 8 3', ''] Output:['7', ''] | [
2,
3
] |
You are given a sorted array a_1, a_2, ..., a_n (for each index i > 1 condition a_i >= a_{i-1} holds) and an integer k.You are asked to divide this array into k non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let max(i) be equal to the maximum in the i-th subarray, and min(i) be equal to the minimum in the i-th subarray. The cost of division is equal to \sum\limits_{i=1}^{k} (max(i) - min(i)). For example, if a = [2, 4, 5, 5, 8, 11, 19] and we divide it into 3 subarrays in the following way: [2, 4], [5, 5], [8, 11, 19], then the cost of division is equal to (4 - 2) + (5 - 5) + (19 - 8) = 13.Calculate the minimum cost you can obtain by dividing the array a into k non-empty consecutive subarrays. | Input: ['6 3', '4 8 15 16 23 42', ''] Output:['12', ''] | [
2
] |
There are n pillars aligned in a row and numbered from 1 to n.Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius a_i.You can move these disks from one pillar to another. You can take a disk from pillar i and place it on top of pillar j if all these conditions are met: there is no other pillar between pillars i and j. Formally, it means that |i - j| = 1; pillar i contains exactly one disk; either pillar j contains no disks, or the topmost disk on pillar j has radius strictly greater than the radius of the disk you move. When you place a disk on a pillar that already has some disks on it, you put the new disk on top of previously placed disks, so the new disk will be used to check the third condition if you try to place another disk on the same pillar.You may take any disk and place it on other pillar any number of times, provided that every time you do it, all three aforementioned conditions are met. Now you wonder, is it possible to place all n disks on the same pillar simultaneously? | Input: ['4', '1 3 4 2', ''] Output:['YES', ''] | [
2
] |
Let's denote a k-step ladder as the following structure: exactly k + 2 wooden planks, of which two planks of length at least k+1 β the base of the ladder; k planks of length at least 1 β the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal.For example, ladders 1 and 3 are correct 2-step ladders and ladder 2 is a correct 1-step ladder. On the first picture the lengths of planks are [3, 3] for the base and [1] for the step. On the second picture lengths are [3, 3] for the base and [2] for the step. On the third picture lengths are [3, 4] for the base and [2, 3] for the steps. You have n planks. The length of the i-th planks is a_i. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised "ladder" from the planks.The question is: what is the maximum number k such that you can choose some subset of the given planks and assemble a k-step ladder using them? | Input: ['4', '4', '1 3 1 3', '3', '3 3 2', '5', '2 3 3 4 2', '3', '1 1 2', ''] Output:['2', '1', '2', '0', ''] | [
2,
3
] |
You are given a connected undirected weighted graph consisting of n vertices and m edges.You need to print the k-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from i to j and from j to i are counted as one).More formally, if d is the matrix of shortest paths, where d_{i, j} is the length of the shortest path between vertices i and j (1 <= i < j <= n), then you need to print the k-th element in the sorted array consisting of all d_{i, j}, where 1 <= i < j <= n. | Input: ['6 10 5', '2 5 1', '5 3 9', '6 2 2', '1 3 1', '5 1 8', '6 5 10', '1 6 5', '6 4 6', '3 6 2', '3 4 5', ''] Output:['3', ''] | [
0
] |
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i.βe. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k subsegments.Let's see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1, 2, 3, 4, 5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below: [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]. Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation.You have to answer q independent queries. | Input: ['3', '5 3', '7 18 3 14 1', '5 4', '1 2 3 4 5', '6 2', '1 2 8 4 10 2', ''] Output:['YES', '1 3 5', 'NO', 'NO', ''] | [
3
] |
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer q independent queries.Let's see the following example: [1, 3, 4]. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob β then Alice has 4 candies, and Bob has 4 candies.Another example is [1, 10, 100]. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes 54 candies, and Alice takes 46 candies. Now Bob has 55 candies, and Alice has 56 candies, so she has to discard one candy β and after that, she has 55 candies too. | Input: ['4', '1 3 4', '1 10 100', '10000000000000000 10000000000000000 10000000000000000', '23 34 45', ''] Output:['4', '55', '15000000000000000', '51', ''] | [
0,
3
] |
Denis holds a Geometers Anonymous Club meeting in SIS. He has prepared n convex polygons numbered from 1 to n for the club. He plans to offer members of the club to calculate Minkowski sums of these polygons. More precisely, he plans to give q tasks, the i-th of them asks to calculate the sum of Minkowski of polygons with indices from l_i to r_i inclusive.The sum of Minkowski of two sets A and B is the set C = \{a + b : a \in A, b \in B\}. It can be proven that if A and B are convex polygons then C will also be a convex polygon. Sum of two convex polygons To calculate the sum of Minkowski of p polygons (p > 2), you need to calculate the sum of Minkowski of the first p - 1 polygons, and then calculate the sum of Minkowski of the resulting polygon and the p-th polygon.For the convenience of checking answers, Denis has decided to prepare and calculate the number of vertices in the sum of Minkowski for each task he prepared. Help him to do it. | Input: ['3', '3', '0 0', '1 0', '0 1', '4', '1 1', '1 2', '0 2', '0 1', '3', '2 2', '1 2', '2 1', '3', '1 2', '2 3', '1 3', ''] Output:['5', '5', '6', ''] | [
3
] |
This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers a_1, a_2, ..., a_n.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers f(a_1 a_2 ... a_{p - 1} a_p, b_1 b_2 ... b_{q - 1} b_q), where a_1 ... a_p and b_1 ... b_q are digits of two integers written in the decimal notation without leading zeros.In other words, the function f(x, y) alternately shuffles the digits of the numbers x and y by writing them from the lowest digits to the older ones, starting with the number y. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: f(1111, 2222) = 12121212 f(7777, 888) = 7787878 f(33, 44444) = 4443434 f(555, 6) = 5556 f(111, 2222) = 2121212Formally, if p >= q then f(a_1 ... a_p, b_1 ... b_q) = a_1 a_2 ... a_{p - q + 1} b_1 a_{p - q + 2} b_2 ... a_{p - 1} b_{q - 1} a_p b_q; if p < q then f(a_1 ... a_p, b_1 ... b_q) = b_1 b_2 ... b_{q - p} a_1 b_{q - p + 1} a_2 ... a_{p - 1} b_{q - 1} a_p b_q. Mishanya gives you an array consisting of n integers a_i, your task is to help students to calculate \sum_{i = 1}^{n}\sum_{j = 1}^{n} f(a_i, a_j) modulo 998\,244\,353. | Input: ['3', '12 3 45', ''] Output:['12330'] | [
3
] |
This problem differs from the next one only in the presence of the constraint on the equal length of all numbers a_1, a_2, ..., a_n. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers f(a_1 a_2 ... a_{p - 1} a_p, b_1 b_2 ... b_{q - 1} b_q), where a_1 ... a_p and b_1 ... b_q are digits of two integers written in the decimal notation without leading zeros.In other words, the function f(x, y) alternately shuffles the digits of the numbers x and y by writing them from the lowest digits to the older ones, starting with the number y. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: f(1111, 2222) = 12121212 f(7777, 888) = 7787878 f(33, 44444) = 4443434 f(555, 6) = 5556 f(111, 2222) = 2121212Formally, if p >= q then f(a_1 ... a_p, b_1 ... b_q) = a_1 a_2 ... a_{p - q + 1} b_1 a_{p - q + 2} b_2 ... a_{p - 1} b_{q - 1} a_p b_q; if p < q then f(a_1 ... a_p, b_1 ... b_q) = b_1 b_2 ... b_{q - p} a_1 b_{q - p + 1} a_2 ... a_{p - 1} b_{q - 1} a_p b_q. Mishanya gives you an array consisting of n integers a_i. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate \sum_{i = 1}^{n}\sum_{j = 1}^{n} f(a_i, a_j) modulo 998\,244\,353. | Input: ['3', '12 33 45', ''] Output:['26730'] | [
3
] |
Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia. For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs n actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options: the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by 1; the second option is to put candies in the box. In this case, Alya will put 1 more candy, than she put in the previous time. Thus, if the box is empty, then it can only use the second option.For example, one possible sequence of Alya's actions look as follows: put one candy into the box; put two candies into the box; eat one candy from the box; eat one candy from the box; put three candies into the box; eat one candy from the box; put four candies into the box; eat one candy from the box; put five candies into the box; This way she will perform 9 actions, the number of candies at the end will be 11, while Alya will eat 4 candies in total.You know the total number of actions n and the number of candies at the end k. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given n and k the answer always exists.Please note, that during an action of the first option, Alya takes out and eats exactly one candy. | Input: ['1 1', ''] Output:['0'] | [
0,
3,
4
] |
Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?There are n students living in a building, and for each of them the favorite drink a_i is known. So you know n integers a_1, a_2, ..., a_n, where a_i (1 <= a_i <= k) is the type of the favorite drink of the i-th student. The drink types are numbered from 1 to k.There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are k types of drink sets, the j-th type contains two portions of the drink j. The available number of sets of each of the k types is infinite.You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly \lceil \frac{n}{2} \rceil, where \lceil x \rceil is x rounded up.After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if n is odd then one portion will remain unused and the students' teacher will drink it.What is the maximum number of students that can get their favorite drink if \lceil \frac{n}{2} \rceil sets will be chosen optimally and students will distribute portions between themselves optimally? | Input: ['5 3', '1', '3', '1', '1', '2', ''] Output:['4', ''] | [
2,
3
] |
There are n segments drawn on a plane; the i-th segment connects two points (x_{i, 1}, y_{i, 1}) and (x_{i, 2}, y_{i, 2}). Each segment is non-degenerate, and is either horizontal or vertical β formally, for every i \in [1, n] either x_{i, 1} = x_{i, 2} or y_{i, 1} = y_{i, 2} (but only one of these conditions holds). Only segments of different types may intersect: no pair of horizontal segments shares any common points, and no pair of vertical segments shares any common points.We say that four segments having indices h_1, h_2, v_1 and v_2 such that h_1 < h_2 and v_1 < v_2 form a rectangle if the following conditions hold: segments h_1 and h_2 are horizontal; segments v_1 and v_2 are vertical; segment h_1 intersects with segment v_1; segment h_2 intersects with segment v_1; segment h_1 intersects with segment v_2; segment h_2 intersects with segment v_2. Please calculate the number of ways to choose four segments so they form a rectangle. Note that the conditions h_1 < h_2 and v_1 < v_2 should hold. | Input: ['7', '-1 4 -1 -2', '6 -1 -2 -1', '-2 3 6 3', '2 -2 2 4', '4 -1 4 3', '5 3 5 1', '5 2 1 2', ''] Output:['7', ''] | [
0
] |
Alice and Bob play a game. There is a paper strip which is divided into nβ+β1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell iβ-β1, iβ-β2 or iβ-βk). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number iβ<βk. The player who can't make a move loses the game.Who wins if both participants play optimally?Alice and Bob would like to play several games, so you should determine the winner in each game. | Input: ['40 33 33 44 4'] Output:['BobAliceBobAlice'] | [
3
] |
You have a list of numbers from 1 to n written from left to right on the blackboard.You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? | Input: ['3', '3 1', '4 2', '69 6', ''] Output:['2', '4', '12', ''] | [
3
] |
You have been hired to supervise the project of a new amusement park. The park will have a special gimmick: directed slides that can get customers from one attraction to another quickly and in an entertaining way.The park owner has given you the current project: a list of planned attractions and a list of slides that should be built between them. However, him being a businessman, he casually envisioned the impossible: among other things, he projected a slide coming from the Haunted Castle to the Roller Coaster, another from the Roller Coaster to the Drop Tower, and a third from the Drop Tower to the Haunted Castle. As the slides can only go downhill, it is evident why this is a problem. You don't have the luxury of ignoring the laws of physics when building the park, so you have to request changes in the project. Maybe he would accept reversing the slide between the Drop Tower and the Haunted Castle?Formally: The project is a list of attractions and a list of directed slides. Each slide starts at one attraction and ends at another attraction. A proposal is obtained from the project by reversing the directions of some slides (possibly none or all of them). A proposal is legal if there is a way to assign an elevation to each attraction in such a way that every slide goes downhill. The cost of a proposal is the number of slides whose directions were reversed. For a given project, find and report the sum of costs all legal proposals. Since this number may be large, output it modulo 998,244,353. | Input: ['2 1', '1 2', ''] Output:['1', ''] | [
3
] |
A cubeword is a special type of a crossword. When building a cubeword, you start by choosing a positive integer a: the side length of the cube. Then, you build a big cube consisting of a * a * a unit cubes. This big cube has 12 edges. Then, you discard all unit cubes that do not touch the edges of the big cube. The figure below shows the object you will get for a=6. Finally, you assign a letter to each of the unit cubes in the object. You must get a meaningful word along each edge of the big cube. Each edge can be read in either direction, and it is sufficient if one of the two directions of reading gives a meaningful word.The figure below shows the object for a=6 in which some unit cubes already have assigned letters. You can already read the words 'SUBMIT', 'ACCEPT' and 'TURING' along three edges of the big cube. You are given a list of valid words. Each word from the wordlist may appear on arbitrarily many edges of a valid cubeword. Find and report the number of different cubewords that can be constructed, modulo 998,244,353.If one cubeword can be obtained from another by rotation or mirroring, they are considered distinct. | Input: ['1', 'radar', ''] Output:['1', ''] | [
0
] |
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, ..., 9m, 1p, 2p, ..., 9p, 1s, 2s, ..., 9s.In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.Do you know the minimum number of extra suited tiles she needs to draw so that she can win?Here are some useful definitions in this game: A mentsu, also known as meld, is formed by a koutsu or a shuntsu; A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: [2m, 3p, 2s, 4m, 1s, 2s, 4s] β it contains no koutsu or shuntsu, so it includes no mentsu; [4s, 3m, 3p, 4s, 5p, 4s, 5p] β it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. | Input: ['1s 2s 3s', ''] Output:['0', ''] | [
0
] |
Tokitsukaze is one of the characters in the game "Kantai Collection". In this game, every character has a common attribute β health points, shortened to HP.In general, different values of HP are grouped into 4 categories: Category A if HP is in the form of (4 n + 1), that is, when divided by 4, the remainder is 1; Category B if HP is in the form of (4 n + 3), that is, when divided by 4, the remainder is 3; Category C if HP is in the form of (4 n + 2), that is, when divided by 4, the remainder is 2; Category D if HP is in the form of 4 n, that is, when divided by 4, the remainder is 0. The above-mentioned n can be any integer.These 4 categories ordered from highest to lowest as A > B > C > D, which means category A is the highest and category D is the lowest.While playing the game, players can increase the HP of the character. Now, Tokitsukaze wants you to increase her HP by at most 2 (that is, either by 0, 1 or 2). How much should she increase her HP so that it has the highest possible category? | Input: ['33', ''] Output:['0 A', ''] | [
0
] |
Tokitsukaze and her friends are trying to infiltrate a secret base built by Claris. However, Claris has been aware of that and set a bomb which is going to explode in a minute. Although they try to escape, they have no place to go after they find that the door has been locked.At this very moment, CJB, Father of Tokitsukaze comes. With his magical power given by Ereshkigal, the goddess of the underworld, CJB is able to set m barriers to protect them from the explosion. Formally, let's build a Cartesian coordinate system on the plane and assume the bomb is at O(0, 0). There are n persons in Tokitsukaze's crew, the i-th one of whom is at P_i(X_i, Y_i). Every barrier can be considered as a line with infinity length and they can intersect each other. For every person from Tokitsukaze's crew, there must be at least one barrier separating the bomb and him, which means the line between the bomb and him intersects with at least one barrier. In this definition, if there exists a person standing at the position of the bomb, any line through O(0, 0) will satisfy the requirement.Although CJB is very powerful, he still wants his barriers to be as far from the bomb as possible, in order to conserve his energy. Please help him calculate the maximum distance between the bomb and the closest barrier while all of Tokitsukaze's crew are safe. | Input: ['3 1', '2 0', '0 2', '-1 0', ''] Output:['0.0000000000', ''] | [
2,
4
] |
"Duel!"Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started.There are n cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly k consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these n cards face the same direction after one's move, the one who takes this move will win.Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. | Input: ['4 2', '0101', ''] Output:['quailty', ''] | [
0,
2
] |
Consider a sequence of digits of length 2^k [a_1, a_2, ..., a_{2^k}]. We perform the following operation with it: replace pairs (a_{2i+1}, a_{2i+2}) with (a_{2i+1} + a_{2i+2})\bmod 10 for 0<= i<2^{k-1}. For every i where a_{2i+1} + a_{2i+2}>= 10 we get a candy! As a result, we will get a sequence of length 2^{k-1}.Less formally, we partition sequence of length 2^k into 2^{k-1} pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth ..., the last pair consists of the (2^k-1)-th and (2^k)-th numbers. For every pair such that sum of numbers in it is at least 10, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 10 (and don't change the order of the numbers).Perform this operation with a resulting array until it becomes of length 1. Let f([a_1, a_2, ..., a_{2^k}]) denote the number of candies we get in this process. For example: if the starting sequence is [8, 7, 3, 1, 7, 0, 9, 4] then:After the first operation the sequence becomes [(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10] = [5, 4, 7, 3], and we get 2 candies as 8 + 7 >= 10 and 9 + 4 >= 10.After the second operation the sequence becomes [(5 + 4)\bmod 10, (7 + 3)\bmod 10] = [9, 0], and we get one more candy as 7 + 3 >= 10. After the final operation sequence becomes [(9 + 0) \bmod 10] = [9]. Therefore, f([8, 7, 3, 1, 7, 0, 9, 4]) = 3 as we got 3 candies in total.You are given a sequence of digits of length n s_1, s_2, ... s_n. You have to answer q queries of the form (l_i, r_i), where for i-th query you have to output f([s_{l_i}, s_{l_i+1}, ..., s_{r_i}]). It is guaranteed that r_i-l_i+1 is of form 2^k for some nonnegative integer k. | Input: ['8', '8 7 3 1 7 0 9 4', '3', '1 8', '2 5', '7 7', ''] Output:['3', '1', '0', ''] | [
3
] |
You are given n numbers a_1, a_2, ..., a_n. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array [1, 4, 5, 6, 7, 8], the arrangement on the left is valid, while arrangement on the right is not, as 5>= 4 + 1 and 8> 1 + 6. | Input: ['3', '2 4 3', ''] Output:['YES', '4 2 3 '] | [
2,
3
] |
You are given a prime number p, n integers a_1, a_2, ..., a_n, and an integer k. Find the number of pairs of indexes (i, j) (1 <= i < j <= n) for which (a_i + a_j)(a_i^2 + a_j^2) \equiv k \bmod p. | Input: ['3 3 0', '0 1 2', ''] Output:['1'] | [
3
] |
Let x be an array of integers x = [x_1, x_2, ..., x_n]. Let's define B(x) as a minimal size of a partition of x into subsegments such that all elements in each subsegment are equal. For example, B([3, 3, 6, 1, 6, 6, 6]) = 4 using next partition: [3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6].Now you don't have any exact values of x, but you know that x_i can be any integer value from [l_i, r_i] (l_i <= r_i) uniformly at random. All x_i are independent.Calculate expected value of (B(x))^2, or E((B(x))^2). It's guaranteed that the expected value can be represented as rational fraction \frac{P}{Q} where (P, Q) = 1, so print the value P \cdot Q^{-1} \mod 10^9 + 7. | Input: ['3', '1 1 1', '1 2 3', ''] Output:['166666673', ''] | [
3
] |
Vasya has an array a_1, a_2, ..., a_n.You don't know this array, but he told you m facts about this array. The i-th fact is a triple of numbers t_i, l_i and r_i (0 <= t_i <= 1, 1 <= l_i < r_i <= n) and it means: if t_i=1 then subbarray a_{l_i}, a_{l_i + 1}, ..., a_{r_i} is sorted in non-decreasing order; if t_i=0 then subbarray a_{l_i}, a_{l_i + 1}, ..., a_{r_i} is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if a = [2, 1, 1, 3, 2] then he could give you three facts: t_1=1, l_1=2, r_1=4 (the subarray [a_2, a_3, a_4] = [1, 1, 3] is sorted), t_2=0, l_2=4, r_2=5 (the subarray [a_4, a_5] = [3, 2] is not sorted), and t_3=0, l_3=3, r_3=5 (the subarray [a_3, a_5] = [1, 3, 2] is not sorted).You don't know the array a. Find any array which satisfies all the given facts. | Input: ['7 4', '1 1 3', '1 2 5', '0 5 6', '1 6 7', ''] Output:['YES', '1 2 2 3 5 4 4', ''] | [
2
] |
The letters shop showcase is a string s, consisting of n lowercase Latin letters. As the name tells, letters are sold in the shop.Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string s.There are m friends, the i-th of them is named t_i. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount. For example, for s="arrayhead" and t_i="arya" 5 letters have to be bought ("arrayhead"). For example, for s="arrayhead" and t_i="harry" 6 letters have to be bought ("arrayhead"). For example, for s="arrayhead" and t_i="ray" 5 letters have to be bought ("arrayhead"). For example, for s="arrayhead" and t_i="r" 2 letters have to be bought ("arrayhead"). For example, for s="arrayhead" and t_i="areahydra" all 9 letters have to be bought ("arrayhead"). It is guaranteed that every friend can construct her/his name using the letters from the string s.Note that the values for friends are independent, friends are only estimating them but not actually buying the letters. | Input: ['9', 'arrayhead', '5', 'arya', 'harry', 'ray', 'r', 'areahydra', ''] Output:['5', '6', '5', '2', '9', ''] | [
4
] |
Your favorite shop sells n Kinder Surprise chocolate eggs. You know that exactly s stickers and exactly t toys are placed in n eggs in total.Each Kinder Surprise can be one of three types: it can contain a single sticker and no toy; it can contain a single toy and no sticker; it can contain both a single sticker and a single toy. But you don't know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other.What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and at least one toy?Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It's guaranteed that the answer always exists. | Input: ['3', '10 5 7', '10 10 10', '2 1 1', ''] Output:['6', '1', '2', ''] | [
3
] |
Vus the Cossack has a simple graph with n vertices and m edges. Let d_i be a degree of the i-th vertex. Recall that a degree of the i-th vertex is the number of conected edges to the i-th vertex.He needs to remain not more than \lceil \frac{n+m}{2} \rceil edges. Let f_i be the degree of the i-th vertex after removing. He needs to delete them in such way so that \lceil \frac{d_i}{2} \rceil <=q f_i for each i. In other words, the degree of each vertex should not be reduced more than twice. Help Vus to remain the needed edges! | Input: ['6 6', '1 2', '2 3', '3 4', '4 5', '5 3', '6 5', ''] Output:['5', '2 1', '3 2', '5 3', '5 4', '6 5', ''] | [
2
] |
Vus the Cossack has a field with dimensions n * m, which consists of "0" and "1". He is building an infinite field from this field. He is doing this in this way: He takes the current field and finds a new inverted field. In other words, the new field will contain "1" only there, where "0" was in the current field, and "0" there, where "1" was. To the current field, he adds the inverted field to the right. To the current field, he adds the inverted field to the bottom. To the current field, he adds the current field to the bottom right. He repeats it.For example, if the initial field was: \begin{matrix} 1 & 0 & \\ 1 & 1 & \\ \end{matrix} After the first iteration, the field will be like this: \begin{matrix} 1 & 0 & 0 & 1 \\ 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 \\ \end{matrix} After the second iteration, the field will be like this: \begin{matrix} 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ 1 & 1 & 0& 0 & 0 & 0 & 1 & 1 \\ \end{matrix} And so on...Let's numerate lines from top to bottom from 1 to infinity, and columns from left to right from 1 to infinity. We call the submatrix (x_1, y_1, x_2, y_2) all numbers that have coordinates (x, y) such that x_1 <=q x <=q x_2 and y_1 <=q y <=q y_2.The Cossack needs sometimes to find the sum of all the numbers in submatrices. Since he is pretty busy right now, he is asking you to find the answers! | Input: ['2 2 5', '10', '11', '1 1 8 8', '2 4 5 6', '1 2 7 8', '3 3 6 8', '5 6 7 8', ''] Output:['32', '5', '25', '14', '4', ''] | [
3
] |
Vus the Cossack has n real numbers a_i. It is known that the sum of all numbers is equal to 0. He wants to choose a sequence b the size of which is n such that the sum of all numbers is 0 and each b_i is either \lfloor a_i \rfloor or \lceil a_i \rceil. In other words, b_i equals a_i rounded up or down. It is not necessary to round to the nearest integer.For example, if a = [4.58413, 1.22491, -2.10517, -3.70387], then b can be equal, for example, to [4, 2, -2, -4]. Note that if a_i is an integer, then there is no difference between \lfloor a_i \rfloor and \lceil a_i \rceil, b_i will always be equal to a_i.Help Vus the Cossack find such sequence! | Input: ['4', '4.58413', '1.22491', '-2.10517', '-3.70387', ''] Output:['4', '2', '-2', '-4', ''] | [
2,
3
] |
Vus the Cossack has two binary strings, that is, strings that consist only of "0" and "1". We call these strings a and b. It is known that |b| <=q |a|, that is, the length of b is at most the length of a.The Cossack considers every substring of length |b| in string a. Let's call this substring c. He matches the corresponding characters in b and c, after which he counts the number of positions where the two strings are different. We call this function f(b, c).For example, let b = 00110, and c = 11000. In these strings, the first, second, third and fourth positions are different.Vus the Cossack counts the number of such substrings c such that f(b, c) is even.For example, let a = 01100010 and b = 00110. a has four substrings of the length |b|: 01100, 11000, 10001, 00010. f(00110, 01100) = 2; f(00110, 11000) = 4; f(00110, 10001) = 4; f(00110, 00010) = 1. Since in three substrings, f(b, c) is even, the answer is 3.Vus can not find the answer for big strings. That is why he is asking you to help him. | Input: ['01100010', '00110', ''] Output:['3', ''] | [
3
] |
A company of n friends wants to order exactly two pizzas. It is known that in total there are 9 pizza ingredients in nature, which are denoted by integers from 1 to 9.Each of the n friends has one or more favorite ingredients: the i-th of friends has the number of favorite ingredients equal to f_i (1 <= f_i <= 9) and your favorite ingredients form the sequence b_{i1}, b_{i2}, ..., b_{if_i} (1 <= b_{it} <= 9).The website of CodePizza restaurant has exactly m (m >= 2) pizzas. Each pizza is characterized by a set of r_j ingredients a_{j1}, a_{j2}, ..., a_{jr_j} (1 <= r_j <= 9, 1 <= a_{jt} <= 9) , which are included in it, and its price is c_j.Help your friends choose exactly two pizzas in such a way as to please the maximum number of people in the company. It is known that a person is pleased with the choice if each of his/her favorite ingredients is in at least one ordered pizza. If there are several ways to choose two pizzas so as to please the maximum number of friends, then choose the one that minimizes the total price of two pizzas. | Input: ['3 4', '2 6 7', '4 2 3 9 5', '3 2 3 9', '100 1 7', '400 3 3 2 5', '100 2 9 2', '500 3 2 9 5', ''] Output:['2 3', ''] | [
0
] |
After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size n * m (where n is the number of rows, m is the number of columns) and starts to draw snakes in cells.Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only 26 letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed 26.Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals 1, i.e. each snake has size either 1 * l or l * 1, where l is snake's length. Note that snakes can't bend.When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake "over the top" and overwrites the previous value in the cell.Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes. | Input: ['1', '5 6', '...a..', '..bbb.', '...a..', '.cccc.', '...a..', ''] Output:['YES', '3', '1 4 5 4', '2 3 2 5', '4 2 4 5', ''] | [
0
] |
A sequence a_1, a_2, ..., a_k is called an arithmetic progression if for each i from 1 to k elements satisfy the condition a_i = a_1 + c \cdot (i - 1) for some fixed c.For example, these five sequences are arithmetic progressions: [5, 7, 9, 11], [101], [101, 100, 99], [13, 97] and [5, 5, 5, 5, 5]. And these four sequences aren't arithmetic progressions: [3, 1, 2], [1, 2, 4, 8], [1, -1, 1, -1] and [1, 2, 3, 3, 3].You are given a sequence of integers b_1, b_2, ..., b_n. Find any index j (1 <= j <= n), such that if you delete b_j from the sequence, you can reorder the remaining n-1 elements, so that you will get an arithmetic progression. If there is no such index, output the number -1. | Input: ['5', '2 6 8 7 4', ''] Output:['4'] | [
3
] |
The only difference between easy and hard versions is constraints.If you write a solution in Python, then prefer to send it in PyPy to speed up execution time.A session has begun at Beland State University. Many students are taking exams.Polygraph Poligrafovich is going to examine a group of n students. Students will take the exam one-by-one in order from 1-th to n-th. Rules of the exam are following: The i-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly t_i minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.The duration of the whole exam for all students is M minutes (\max t_i <= M), so students at the end of the list have a greater possibility to run out of time to pass the exam.For each student i, you should count the minimum possible number of students who need to fail the exam so the i-th student has enough time to pass the exam.For each student i, find the answer independently. That is, if when finding the answer for the student i_1 some student j should leave, then while finding the answer for i_2 (i_2>i_1) the student j student does not have to go home. | Input: ['7 15', '1 2 3 4 5 6 7', ''] Output:['0 0 0 0 0 2 3 '] | [
0,
2,
3
] |
The only difference between easy and hard versions is constraints.A session has begun at Beland State University. Many students are taking exams.Polygraph Poligrafovich is going to examine a group of n students. Students will take the exam one-by-one in order from 1-th to n-th. Rules of the exam are following: The i-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly t_i minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student.The duration of the whole exam for all students is M minutes (\max t_i <= M), so students at the end of the list have a greater possibility to run out of time to pass the exam.For each student i, you should count the minimum possible number of students who need to fail the exam so the i-th student has enough time to pass the exam.For each student i, find the answer independently. That is, if when finding the answer for the student i_1 some student j should leave, then while finding the answer for i_2 (i_2>i_1) the student j student does not have to go home. | Input: ['7 15', '1 2 3 4 5 6 7', ''] Output:['0 0 0 0 0 2 3 '] | [
2
] |
Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions a, b and c respectively. At the end of the performance, the distance between each pair of ropewalkers was at least d.Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by 1 (i. e. shift by 1 to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to d.Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides. | Input: ['5 2 6 3', ''] Output:['2', ''] | [
3
] |
Heidi enjoyed performing the simulation because she knew exactly when a new universe would be formed and where, and when a non-existent link would be broken and where.However, the multiverse itself works in mysterious ways. Well, it works using probabilities, which to some people is mysterious.At each unit time, when a decision is made, one of the two events will happen randomly. Let's denote l as the current length of the multiverse. With a probability of p_{create} = 1 - \frac{l}{m}, a universe will be created. With a probability of p_{break}=\frac{l}{m}, a non-existent link will be broken at some position.More specifically, When a universe is created, it will manifest itself between any two adjacent universes or at one of the ends. Each position occurs with a probability of \frac{1}{l + 1}. When a link is broken, it could be cut between any two adjacent universes, each with a probability of \frac{1}{l-1}. After separating the multiverse into two segments, the segment NOT containing the Doctor will cease to exist. As earlier, the Doctor remains in the same universe. However, if at some point the multiverse breaks in such a way that the Doctor finds himself at the leftmost or rightmost end of it, the TARDIS stops functioning.In such a case, the Doctor must actually walk across the multiverse to find the tools to fix it.We are interested in the expected value of the length of the multiverse when such an event occurs. | Input: ['2 1 2', ''] Output:['2', ''] | [
3
] |
Heidi and Doctor Who hopped out of the TARDIS and found themselves at EPFL in 2018. They were surrounded by stormtroopers and Darth Vader was approaching. Miraculously, they managed to escape to a nearby rebel base but the Doctor was very confused. Heidi reminded him that last year's HC2 theme was Star Wars. Now he understood, and he's ready to face the evils of the Empire!The rebels have s spaceships, each with a certain attacking power a.They want to send their spaceships to destroy the empire bases and steal enough gold and supplies in order to keep the rebellion alive.The empire has b bases, each with a certain defensive power d, and a certain amount of gold g.A spaceship can attack all the bases which have a defensive power less than or equal to its attacking power.If a spaceship attacks a base, it steals all the gold in that base.The rebels are still undecided which spaceship to send out first, so they asked for the Doctor's help. They would like to know, for each spaceship, the maximum amount of gold it can steal. | Input: ['5 4', '1 3 5 2 4', '0 1', '4 2', '2 8', '9 4', ''] Output:['1 9 11 9 11', ''] | [
4
] |
Now Heidi is ready to crack Madame Kovarian's hashing function.Madame Kovarian has a very strict set of rules for name changes. Two names can be interchanged only if using the following hashing function on them results in a collision. However, the hashing function is parametrized, so one can always find a set of parameters that causes such a collision. Heidi decided to exploit this to her advantage.Given two strings w_1, w_2 of equal length n consisting of lowercase English letters and an integer m.Consider the standard polynomial hashing function:H_p(w) := <=ft( \sum_{i=0}^{|w|-1} w_i r^i \right) \mbox{mod}(p)where p is some prime, and r is some number such that 2<=q r <=q p-2.The goal is to find r and a prime p (m <=q p <=q 10^9) such that H_p(w_1) = H_p(w_2).Strings w_1 and w_2 are sampled independently at random from all strings of length n over lowercase English letters. | Input: ['10 5', 'bgcbaaaaaa', 'cccaaaaaaa', ''] Output:['5 2'] | [
3
] |
After learning about polynomial hashing, Heidi decided to learn about shift-xor hashing. In particular, she came across this interesting problem.Given a bitstring y \in \{0,1\}^n find out the number of different k (0 <=q k < n) such that there exists x \in \{0,1\}^n for which y = x \oplus \mbox{shift}^k(x).In the above, \oplus is the xor operation and \mbox{shift}^k is the operation of shifting a bitstring cyclically to the right k times. For example, 001 \oplus 111 = 110 and \mbox{shift}^3(00010010111000) = 00000010010111. | Input: ['4', '1010', ''] Output:['3', ''] | [
0
] |
Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.The first hashing function she designed is as follows.Given two positive integers (x, y) she defines H(x,y):=x^2+2xy+x+1.Now, Heidi wonders if the function is reversible. That is, given a positive integer r, can you find a pair (x, y) (of positive integers) such that H(x, y) = r?If multiple such pairs exist, output the one with smallest possible x. If there is no such pair, output "NO". | Input: ['19', ''] Output:['1 8', ''] | [
0,
3
] |
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are n candies in a candy box. The type of the i-th candy is a_i (1 <= a_i <= n). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i.βe. for example, a gift having two candies of type 1 and two candies of type 2 is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number f_i is given, which is equal to 0 if you really want to keep i-th candy for yourself, or 1 if you don't mind including it into your gift. It is possible that two candies of the same type have different values of f_i.You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having f_i = 1 in your gift.You have to answer q independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code. | Input: ['3', '8', '1 0', '4 1', '2 0', '4 1', '5 1', '6 1', '3 0', '2 0', '4', '1 1', '1 1', '2 1', '2 1', '9', '2 0', '2 0', '4 1', '4 1', '4 1', '7 0', '7 1', '7 0', '7 1', ''] Output:['3 3', '3 3', '9 5', ''] | [
2
] |
One important contest will take place on the most famous programming platform (Topforces) very soon!The authors have a pool of n problems and should choose at most three of them into this contest. The prettiness of the i-th problem is a_i. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x, y, z, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x. Any contest composed from one problem is considered good.Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.You have to answer q independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code. | Input: ['3', '4', '5 6 15 30', '4', '10 6 30 15', '3', '3 4 6', ''] Output:['30', '31', '10', ''] | [
0,
3
] |
This problem is actually a subproblem of problem G from the same contest.There are n candies in a candy box. The type of the i-th candy is a_i (1 <= a_i <= n).You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i.βe. for example, a gift having two candies of type 1 and two candies of type 2 is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.You have to answer q independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code. | Input: ['3', '8', '1 4 8 4 5 6 3 8', '16', '2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1', '9', '2 2 4 4 4 7 7 7 7', ''] Output:['3', '10', '9', ''] | [
2
] |
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a; if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b; if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game. Regardless of Vova's turns the charge of the laptop battery is always decreases.Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.You have to answer q independent queries. | Input: ['6', '15 5 3 2', '15 5 4 3', '15 5 2 1', '15 5 5 1', '16 7 5 2', '20 5 7 3', ''] Output:['4', '-1', '5', '2', '0', '1', ''] | [
3,
4
] |
There are n products in the shop. The price of the i-th product is a_i. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product i in such a way that the difference between the old price of this product a_i and the new price b_i is at most k. In other words, the condition |a_i - b_i| <= k should be satisfied (|x| is the absolute value of x).He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price b_i of each product i should be positive (i.e. b_i > 0 should be satisfied for all i from 1 to n).Your task is to find out the maximum possible equal price B of all productts with the restriction that for all products the condiion |a_i - B| <= k should be satisfied (where a_i is the old price of the product and B is the same new price of all products) or report that it is impossible to find such price B.Note that the chosen price B should be integer.You should answer q independent queries. | Input: ['4', '5 1', '1 1 2 3 1', '4 2', '6 4 8 5', '2 2', '1 6', '3 5', '5 2 5', ''] Output:['2', '6', '-1', '7', ''] | [
3
] |
You have given integers a, b, p, and q. Let f(x) = \text{abs}(\text{sin}(\frac{p}{q} \pi x)).Find minimum possible integer x that maximizes f(x) where a <= x <= b. | Input: ['2', '0 3 1 3', '17 86 389 995', ''] Output:['1', '55', ''] | [
4
] |
Let f_{x} = c^{2x-6} \cdot f_{x-1} \cdot f_{x-2} \cdot f_{x-3} for x >= 4.You have given integers n, f_{1}, f_{2}, f_{3}, and c. Find f_{n} \bmod (10^{9}+7). | Input: ['5 1 2 5 3', ''] Output:['72900', ''] | [
3
] |
You are given n words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible.Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel.For example of a beautiful lyric, "hello hellooowww" "whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o".For example of a not beautiful lyric, "hey man""iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second).How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. | Input: ['14', 'wow', 'this', 'is', 'the', 'first', 'mcdics', 'codeforces', 'round', 'hooray', 'i', 'am', 'proud', 'about', 'that', ''] Output:['3', 'about proud', 'hooray round', 'wow first', 'this is', 'i that', 'mcdics am', ''] | [
2
] |
You have a given integer n. Find the number of ways to fill all 3 * n tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap. This picture describes when n = 4. The left one is the shape and the right one is 3 * n tiles. | Input: ['4', ''] Output:['4'] | [
3
] |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.