question_text
stringlengths 2
3.82k
| input_outputs
stringlengths 23
941
| algo_tags
sequence |
---|---|---|
Your working week consists of n days numbered from 1 to n, after day n goes day 1 again. And 3 of them are days off. One of the days off is the last day, day n. You have to decide when the other two are.Choosing days off, you pursue two goals: No two days should go one after the other. Note that you can't make day 1 a day off because it follows day n. Working segments framed by days off should be as dissimilar as possible in duration. More specifically, if the segments are of size l_1, l_2, and l_3 days long, you want to maximize \min(|l_1 - l_2|, |l_2 - l_3|, |l_3 - l_1|). Output the maximum value of \min(|l_1 - l_2|, |l_2 - l_3|, |l_3 - l_1|) that can be obtained. | Input: ['36101033'] Output:['0', '1', '342', ''] | [
2,
3
] |
Let S be the Thue-Morse sequence. In other words, S is the 0-indexed binary string with infinite length that can be constructed as follows: Initially, let S be "0". Then, we perform the following operation infinitely many times: concatenate S with a copy of itself with flipped bits.For example, here are the first four iterations: IterationS before iterationS before iteration with flipped bitsConcatenated S1010120110011030110100101101001401101001100101100110100110010110............ You are given two positive integers n and m. Find the number of positions where the strings S_0 S_1 ... S_{m-1} and S_n S_{n + 1} ... S_{n + m - 1} are different. | Input: ['61 15 1034 21173 3419124639 5634877212073412269 96221437021'] Output:['1', '6', '95', '20', '28208137', '48102976088', ''] | [
3
] |
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are n slimes. For all positive integers i where 1 <= i <= n, the i-th slime is located at position i and has health a_i. You are controlling the slime at position k. There are two escapes located at positions 0 and n+1. Your goal is to reach any one of the two escapes by performing any number of game moves.In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game.Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game.Can you reach one of two escapes by performing any number of game moves, without ever losing the game? | Input: ['67 4-1 -2 -3 6 -2 -3 -13 1232 -500 -7007 4-1 -2 -4 6 -2 -4 -18 4-100 10 -7 6 -2 -3 6 -108 2-999 0 -2 3 4 5 6 77 37 3 3 4 2 1 1'] Output:['YES', 'YES', 'NO', 'YES', 'NO', 'YES', ''] | [
2
] |
You are given a set S, which contains the first n positive integers: 1, 2, ..., n.You can perform the following operation on S any number of times (possibly zero): Choose a positive integer k where 1 <= k <= n, such that there exists a multiple of k in S. Then, delete the smallest multiple of k from S. This operation requires a cost of k. You are given a set T, which is a subset of S. Find the minimum possible total cost of operations such that S would be transformed into T. We can show that such a transformation is always possible. | Input: ['6611111171101001400004001081001010115110011100101100'] Output:['0', '11', '4', '4', '17', '60', ''] | [
2,
3
] |
You are given n sticks with positive integral length a_1, a_2, ..., a_n.You can perform the following operation any number of times (possibly zero): choose one stick, then either increase or decrease its length by 1. After each operation, all sticks should have positive lengths. What is the minimum number of operations that you have to perform such that it is possible to select three of the n sticks and use them without breaking to form an equilateral triangle?An equilateral triangle is a triangle where all of its three sides have the same length. | Input: ['431 2 347 3 7 353 4 2 1 183 1 4 1 5 9 2 6'] Output:['2', '4', '1', '1', ''] | [
0,
2
] |
There is a conveyor with 120 rows and 120 columns. Each row and column is numbered from 0 to 119, and the cell in i-th row and j-th column is denoted as (i, j). The top leftmost cell is (0, 0). Each cell has a belt, and all belts are initially facing to the right.Initially, a slime ball is on the belt of (0, 0), and other belts are empty. Every second, the state of the conveyor changes as follows: All slime balls on the conveyor move one cell in the direction of the belt at the same time. If there is no cell in the moved position, the slime gets out of the conveyor, and if two slime balls move to the same cell, they merge into one. All belts with slime ball in the previous second change direction at the same time: belts facing to the right become facing to the down, and vice versa. A new slime ball is placed on cell (0, 0). There are q queries, each being three integers t, x, and y. You have to find out if there is a slime at the cell (x, y) after t seconds from the start. Can you do it? | Input: ['61 1 05 1 30 0 02 4 52 0 21547748756 100 111'] Output:['NO', 'YES', 'YES', 'NO', 'YES', 'YES', ''] | [
3
] |
This is the hard version of this problem. In this version, n <= 5000 holds, and this version has no restriction between x and y. You can make hacks only if both versions of the problem are solved.You are given two binary strings a and b, both of length n. You can do the following operation any number of times (possibly zero). Select two indices l and r (l < r). Change a_l to (1 - a_l), and a_r to (1 - a_r). If l + 1 = r, the cost of the operation is x. Otherwise, the cost is y. You have to find the minimum cost needed to make a equal to b or say there is no way to do so. | Input: ['65 8 901001001016 2 110000011000005 7 201000110117 8 3011100101000016 3 40100011010005 10 10110001100'] Output:['8', '10', '-1', '6', '7', '0', ''] | [
2
] |
This is the easy version of the problem. In this version, n <= 3000, x >= y holds. You can make hacks only if both versions of the problem are solved.You are given two binary strings a and b, both of length n. You can do the following operation any number of times (possibly zero). Select two indices l and r (l < r). Change a_l to (1 - a_l), and a_r to (1 - a_r). If l + 1 = r, the cost of the operation is x. Otherwise, the cost is y. You have to find the minimum cost needed to make a equal to b or say there is no way to do so. | Input: ['45 8 701001001015 7 201000110117 8 3011100101000015 10 10110001100'] Output:['8', '-1', '6', '0', ''] | [
2,
3
] |
There is a badminton championship in which n players take part. The players are numbered from 1 to n. The championship proceeds as follows: player 1 and player 2 play a game, then the winner and player 3 play a game, and then the winner and player 4 play a game, and so on. So, n-1 games are played, and the winner of the last game becomes the champion. There are no draws in the games.You want to find out the result of championship. Currently, you only know the following information: Each player has either won x games or y games in the championship. Given n, x, and y, find out if there is a result that matches this information. | Input: ['55 2 08 1 23 0 02 0 16 3 0'] Output:['1 1 4 4', '-1', '-1', '2 ', '-1', ''] | [
3
] |
You are given an array a with n integers. You can perform the following operation at most k times: Choose two indices i and j, in which i \,\bmod\, k = j \,\bmod\, k (1 <= i < j <= n). Swap a_i and a_j. After performing all operations, you have to select k consecutive elements, and the sum of the k elements becomes your score. Find the maximum score you can get.Here x \bmod y denotes the remainder from dividing x by y. | Input: ['53 25 6 01 175 37 0 4 0 44 22 7 3 43 31000000000 1000000000 999999997'] Output:['11', '7', '15', '10', '2999999997', ''] | [
2
] |
You are given two arrays of integers a_1, a_2, ..., a_n and b_1, b_2, ..., b_n. You need to handle q queries of the following two types: 1 l r x: assign a_i := x for all l <=q i <=q r; 2 l r: find the minimum value of the following expression among all l <=q i <=q r: \frac{\operatorname{lcm}(a_i, b_i)}{\gcd(a_i, b_i)}.In this problem \gcd(x, y) denotes the greatest common divisor of x and y, and \operatorname{lcm}(x, y) denotes the least common multiple of x and y. | Input: ['10 106 10 15 4 9 25 2 3 5 301 2 3 4 6 9 12 15 18 302 1 101 7 10 92 5 101 1 6 142 4 72 3 91 2 9 302 1 42 3 72 5 10'] Output:['1', '2', '12', '2', '10', '5', '2', ''] | [
3
] |
This is the hard version of the problem. The only difference is that in this version there are remove queries.Initially you have a set containing one element β 0. You need to handle q queries of the following types:+ x β add the integer x to the set. It is guaranteed that this integer is not contained in the set; - x β remove the integer x from the set. It is guaranteed that this integer is contained in the set; ? k β find the k\text{-mex} of the set. In our problem, we define the k\text{-mex} of a set of integers as the smallest non-negative integer x that is divisible by k and which is not contained in the set. | Input: ['18+ 1+ 2? 1+ 4? 2+ 6? 3+ 7+ 8? 1? 2+ 5? 1+ 1000000000000000000? 1000000000000000000- 4? 1? 2'] Output:['3', '6', '3', '3', '10', '3', '2000000000000000000', '3', '4', ''] | [
0
] |
This is the easy version of the problem. The only difference is that in this version there are no "remove" queries.Initially you have a set containing one element β 0. You need to handle q queries of the following types:+ x β add the integer x to the set. It is guaranteed that this integer is not contained in the set; ? k β find the k\text{-mex} of the set. In our problem, we define the k\text{-mex} of a set of integers as the smallest non-negative integer x that is divisible by k and which is not contained in the set. | Input: ['15+ 1+ 2? 1+ 4? 2+ 6? 3+ 7+ 8? 1? 2+ 5? 1+ 1000000000000000000? 1000000000000000000'] Output:['3', '6', '3', '3', '10', '3', '2000000000000000000', ''] | [
0
] |
This is the hard version of the problem. The only difference is that in this version q = n.You are given an array of integers a_1, a_2, ..., a_n.The cost of a subsegment of the array [l, r], 1 <=q l <=q r <=q n, is the value f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r), where \operatorname{sum}(l, r) = a_l + a_{l+1} + ... + a_r, and \operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus ... \oplus a_r (\oplus stands for bitwise XOR).You will have q queries. Each query is given by a pair of numbers L_i, R_i, where 1 <=q L_i <=q R_i <=q n. You need to find the subsegment [l, r], L_i <=q l <=q r <=q R_i, with maximum value f(l, r). If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of r - l + 1. | Input: ['61 101 12 25 101 22 23 30 2 41 31 22 34 40 12 8 31 41 32 42 35 521 32 32 32 101 51 41 32 53 57 70 1 0 1 0 1 01 73 62 51 44 72 62 7'] Output:['1 1', '1 1', '2 2', '1 1', '1 1', '2 2', '2 3', '2 3', '2 3', '2 3', '2 3', '2 3', '2 3', '2 3', '3 4', '2 4', '4 6', '2 4', '2 4', '4 6', '2 4', '2 4', ''] | [
0,
2,
4
] |
This is the easy version of the problem. The only difference is that in this version q = 1.You are given an array of integers a_1, a_2, ..., a_n.The cost of a subsegment of the array [l, r], 1 <=q l <=q r <=q n, is the value f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r), where \operatorname{sum}(l, r) = a_l + a_{l+1} + ... + a_r, and \operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus ... \oplus a_r (\oplus stands for bitwise XOR).You will have q = 1 query. Each query is given by a pair of numbers L_i, R_i, where 1 <=q L_i <=q R_i <=q n. You need to find the subsegment [l, r], L_i <=q l <=q r <=q R_i, with maximum value f(l, r). If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of r - l + 1. | Input: ['61 101 12 15 101 23 10 2 41 34 10 12 8 31 45 121 32 32 32 101 57 10 1 0 1 0 1 01 7'] Output:['1 1', '1 1', '1 1', '2 3', '2 3', '2 4', ''] | [
2,
4
] |
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string s_1 s_2 ... s_n. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the following: Select an arbitrary index 1 <=q i <=q n in the string; For all j >=q i, change the value in the j-th position to the opposite, that is, if s_j = 1, then make s_j = 0, and vice versa.What is the minimum number of operations needed to make the string non-decreasing? | Input: ['811210310141100511001610001010000011000070101010'] Output:['0', '1', '2', '1', '2', '3', '1', '5', ''] | [
0,
2
] |
You are given an array a consisting of n integers a_1, a_2, ..., a_n. Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to 1. In one operation, you can do the following: Select an arbitrary index in the array 1 <=q i <=q n; Make a_i = \gcd(a_i, i), where \gcd(x, y) denotes the GCD of integers x and y. The cost of such an operation is n - i + 1.You need to find the minimum total cost of operations we need to perform so that the GCD of the all array numbers becomes equal to 1. | Input: ['9111222 433 6 945 10 15 205120 60 80 40 806150 90 180 120 60 3062 4 6 9 12 18630 60 90 120 125 125'] Output:['0', '1', '2', '2', '1', '3', '3', '0', '1', ''] | [
0,
3
] |
Suppose you have an integer array a_1, a_2, ..., a_n.Let \operatorname{lsl}(i) be the number of indices j (1 <= j < i) such that a_j < a_i.Analogically, let \operatorname{grr}(i) be the number of indices j (i < j <= n) such that a_j > a_i.Let's name position i good in the array a if \operatorname{lsl}(i) < \operatorname{grr}(i).Finally, let's define a function f on array a f(a) as the sum of all a_i such that i is good in a.Given two integers n and k, find the sum of f(a) over all arrays a of size n such that 1 <=q a_i <=q k for all 1 <=q i <=q n modulo 998\,244\,353. | Input: ['3 3', ''] Output:['28', ''] | [
0,
3
] |
You are given an initially empty undirected graph with n nodes, numbered from 1 to n (i. e. n nodes and 0 edges). You want to add m edges to the graph, so the graph won't contain any self-loop or multiple edges.If an edge connecting two nodes u and v is added, its weight must be equal to the greatest common divisor of u and v, i. e. \gcd(u, v).In order to add edges to the graph, you can repeat the following process any number of times (possibly zero): choose an integer k >= 1; add exactly k edges to the graph, each having a weight equal to k + 1. Adding these k edges costs k + 1 in total. Note that you can't create self-loops or multiple edges. Also, if you can't add k edges of weight k + 1, you can't choose such k.For example, if you can add 5 more edges to the graph of weight 6, you may add them, and it will cost 6 for the whole pack of 5 edges. But if you can only add 4 edges of weight 6 to the graph, you can't perform this operation for k = 5.Given two integers n and m, find the minimum total cost to form a graph of n vertices and exactly m edges using the operation above. If such a graph can't be constructed, output -1.Note that the final graph may consist of several connected components. | Input: ['44 16 109 410 11'] Output:['2', '-1', '7', '21', ''] | [
2,
3
] |
Game studio "DbZ Games" wants to introduce another map in their popular game "Valiant". This time, the map named "Panvel" will be based on the city of Mumbai.Mumbai can be represented as n * m cellular grid. Each cell (i, j) (1 <= i <= n; 1 <= j <= m) of the grid is occupied by a cuboid building of height a_{i,j}.This time, DbZ Games want to make a map that has perfect vertical gameplay. That's why they want to choose an l * l square inside Mumbai, such that each building inside the square has a height of at least l.Can you help DbZ Games find such a square of the maximum possible size l? | Input: ['42 22 34 51 31 2 32 34 4 32 1 45 61 9 4 6 5 810 9 5 8 11 624 42 32 8 11 123 1 9 69 13 313 22 60 12 14 17'] Output:['2', '1', '1', '3', ''] | [
0,
4
] |
You are given an integer array a_1, a_2, ..., a_n (1 <= a_i <= n).Find the number of subarrays of a whose \operatorname{XOR} has an even number of divisors. In other words, find all pairs of indices (i, j) (i <= j) such that a_i \oplus a_{i + 1} \oplus ... \oplus a_j has an even number of divisors.For example, numbers 2, 3, 5 or 6 have an even number of divisors, while 1 and 4 β odd. Consider that 0 has an odd number of divisors in this task.Here \operatorname{XOR} (or \oplus) denotes the bitwise XOR operation.Print the number of subarrays but multiplied by 2022... Okay, let's stop. Just print the actual answer. | Input: ['433 1 254 2 1 5 344 4 4 475 7 3 7 1 7 3'] Output:['4', '11', '0', '20', ''] | [
0,
3
] |
Demodogs from the Upside-down have attacked Hawkins again. El wants to reach Mike and also kill as many Demodogs in the way as possible.Hawkins can be represented as an n * n grid. The number of Demodogs in a cell at the i-th row and the j-th column is i \cdot j. El is at position (1, 1) of the grid, and she has to reach (n, n) where she can find Mike. The only directions she can move are the right (from (i, j) to (i, j + 1)) and the down (from (i, j) to (i + 1, j)). She can't go out of the grid, as there are doors to the Upside-down at the boundaries. Calculate the maximum possible number of Demodogs \mathrm{ans} she can kill on the way, considering that she kills all Demodogs in cells she visits (including starting and finishing cells).Print 2022 \cdot \mathrm{ans} modulo 10^9 + 7. Modulo 10^9 + 7 because the result can be too large and multiplied by 2022 because we are never gonna see it again!(Note, you firstly multiply by 2022 and only after that take the remainder.) | Input: ['423501000000000'] Output:['14154', '44484', '171010650', '999589541', ''] | [
2,
3
] |
Joey is low on money. His friend Chandler wants to lend Joey some money, but can't give him directly, as Joey is too proud of himself to accept it. So, in order to trick him, Chandler asks Joey to play a game.In this game, Chandler gives Joey an array a_1, a_2, ..., a_n (n >=q 2) of positive integers (a_i >= 1).Joey can perform the following operation on the array any number of times: Take two indices i and j (1 <= i < j <= n). Choose two integers x and y (x, y >= 1) such that x \cdot y = a_i \cdot a_j. Replace a_i by x and a_j by y. In the end, Joey will get the money equal to the sum of elements of the final array. Find the maximum amount of money \mathrm{ans} Joey can get but print 2022 \cdot \mathrm{ans}. Why multiplied by 2022? Because we are never gonna see it again!It is guaranteed that the product of all the elements of the array a doesn't exceed 10^{12}. | Input: ['332 3 221 331000000 1000000 1'] Output:['28308', '8088', '2022000000004044', ''] | [
2,
3
] |
You have a string s consisting of digits from 0 to 9 inclusive. You can perform the following operation any (possibly zero) number of times: You can choose a position i and delete a digit d on the i-th position. Then insert the digit \min{(d + 1, 9)} on any position (at the beginning, at the end or in between any two adjacent digits). What is the lexicographically smallest string you can get by performing these operations?A string a is lexicographically smaller than a string b of the same length if and only if the following holds: in the first position where a and b differ, the string a has a smaller digit than the corresponding digit in b. | Input: ['404829901314752277691991'] Output:['02599', '9', '01', '111334567888999', ''] | [
2,
3
] |
n people live on the coordinate line, the i-th one lives at the point x_i (1 <= i <= n). They want to choose a position x_0 to meet. The i-th person will spend |x_i - x_0| minutes to get to the meeting place. Also, the i-th person needs t_i minutes to get dressed, so in total he or she needs t_i + |x_i - x_0| minutes. Here |y| denotes the absolute value of y.These people ask you to find a position x_0 that minimizes the time in which all n people can gather at the meeting place. | Input: ['710323 10 021 40 031 2 30 0 031 2 34 1 233 3 35 3 365 4 7 2 10 43 2 5 1 4 6'] Output:['0', '2', '2.5', '2', '1', '3', '6', ''] | [
2,
3,
4,
4
] |
One day, Vogons wanted to build a new hyperspace highway through a distant system with n planets. The i-th planet is on the orbit a_i, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that. The first machine in one operation can destroy any planet at cost of 1 Triganic Pu. The second machine in one operation can destroy all planets on a single orbit in this system at the cost of c Triganic Pus. Vogons can use each machine as many times as they want.Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project? | Input: ['410 12 1 4 5 2 4 5 5 1 25 23 2 1 2 22 21 12 21 2'] Output:['4', '4', '2', '2', ''] | [
2
] |
Given the string s of decimal digits (0-9) of length n.A substring is a sequence of consecutive characters of a string. The substring of this string is defined by a pair of indexes β with its left and right ends. So, each pair of indexes (l, r), where 1 <= l <= r <= n, corresponds to a substring of the string s. We will define as v(l,r) the numeric value of the corresponding substring (leading zeros are allowed in it).For example, if n=7, s="1003004", then v(1,3)=100, v(2,3)=0 and v(2,7)=3004.You are given n, s and an integer w (1 <= w < n).You need to process m queries, each of which is characterized by 3 numbers l_i, r_i, k_i (1 <= l_i <= r_i <= n; 0 <= k_i <= 8).The answer to the ith query is such a pair of substrings of length w that if we denote them as (L_1, L_1+w-1) and (L_2, L_2+w-1), then: L_1!=L_2, that is, the substrings are different; the remainder of dividing a number v(L_1, L_1+w-1) \cdot v(l_i, r_i) + v(L_2, L_2 + w - 1) by 9 is equal to k_i. If there are many matching substring pairs, then find a pair where L_1 is as small as possible. If there are many matching pairs in this case, then minimize L_2.Note that the answer may not exist. | Input: ['510030044 11 2 11795720074 22 7 32 7 41112 12 2 600001 21 4 01 4 14841 52 2 02 3 71 2 53 3 82 2 6'] Output:['2 4', '1 5', '1 2', '-1 -1', '1 2', '-1 -1', '1 3', '1 3', '-1 -1', '-1 -1', '-1 -1', ''] | [
3
] |
A group of n friends decide to go to a restaurant. Each of the friends plans to order meals for x_i burles and has a total of y_i burles (1 <= i <= n). The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all x_i values in the group must not exceed the sum of y_i values in the group.What is the maximum number of days friends can visit the restaurant?For example, let there be n = 6 friends for whom x = [8, 3, 9, 2, 4, 5] and y = [5, 3, 1, 4, 5, 10]. Then: first and sixth friends can go to the restaurant on the first day. They will spend 8+5=13 burles at the restaurant, and their total budget is 5+10=15 burles. Since 15 >= 13, they can actually form a group. friends with indices 2, 4, 5 can form a second group. They will spend 3+2+4=9 burles at the restaurant, and their total budget will be 3+4+5=12 burles (12 >= 9). It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.So, the maximum number of groups the friends can split into is 2. Friends will visit the restaurant for a maximum of two days. Note that the 3-rd friend will not visit the restaurant at all.Output the maximum number of days the friends can visit the restaurant for given n, x and y. | Input: ['668 3 9 2 4 55 3 1 4 5 1041 2 3 41 1 2 232 3 71 3 1062 3 6 9 5 73 2 7 10 6 1065 4 2 1 8 1001 1 1 1 1 20061 4 1 2 4 21 3 3 2 3 4'] Output:['2', '0', '1', '3', '1', '3', ''] | [
2
] |
Polycarp has a string s consisting of lowercase Latin letters.He encodes it using the following algorithm.He goes through the letters of the string s from left to right and for each letter Polycarp considers its number in the alphabet: if the letter number is single-digit number (less than 10), then just writes it out; if the letter number is a two-digit number (greater than or equal to 10), then it writes it out and adds the number 0 after. For example, if the string s is code, then Polycarp will encode this string as follows: 'c' β is the 3-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3); 'o' β is the 15-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150); 'd' β is the 4-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504); 'e' β is the 5-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045.You are given a string t resulting from encoding the string s. Your task is to decode it (get the original string s by t). | Input: ['963150454110071213121612012018315045615018035190711111107111110051111142606'] Output:['code', 'aj', 'abacaba', 'll', 'codeforces', 'aaaak', 'aaaaj', 'aaaaa', 'zf', ''] | [
2
] |
Vlad went into his appartment house entrance, now he is on the 1-th floor. He was going to call the elevator to go up to his apartment.There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor a (it is currently motionless), the second elevator is located on floor b and goes to floor c (b!=c). Please note, if b=1, then the elevator is already leaving the floor 1 and Vlad does not have time to enter it. If you call the first elevator, it will immediately start to go to the floor 1. If you call the second one, then first it will reach the floor c and only then it will go to the floor 1. It takes |x - y| seconds for each elevator to move from floor x to floor y.Vlad wants to call an elevator that will come to him faster. Help him choose such an elevator. | Input: ['31 2 33 1 23 2 1'] Output:['1', '3', '2', ''] | [
3
] |
Consider a segment [0, d] of the coordinate line. There are n lanterns and m points of interest in this segment.For each lantern, you can choose its power β an integer between 0 and d (inclusive). A lantern with coordinate x illuminates the point of interest with coordinate y if |x - y| is less than or equal to the power of the lantern.A way to choose the power values for all lanterns is considered valid if every point of interest is illuminated by at least one lantern.You have to process q queries. Each query is represented by one integer f_i. To answer the i-th query, you have to: add a lantern on coordinate f_i; calculate the number of valid ways to assign power values to all lanterns, and print it modulo 998244353; remove the lantern you just added. | Input: ['6 1 1', '4', '3', '3', '2 1 5', ''] Output:['48', '47', '47', ''] | [
0,
3,
4
] |
There are n fishermen who have just returned from a fishing trip. The i-th fisherman has caught a fish of size a_i.The fishermen will choose some order in which they are going to tell the size of the fish they caught (the order is just a permutation of size n). However, they are not entirely honest, and they may "increase" the size of the fish they have caught.Formally, suppose the chosen order of the fishermen is [p_1, p_2, p_3, ..., p_n]. Let b_i be the value which the i-th fisherman in the order will tell to the other fishermen. The values b_i are chosen as follows: the first fisherman in the order just honestly tells the actual size of the fish he has caught, so b_1 = a_{p_1}; every other fisherman wants to tell a value that is strictly greater than the value told by the previous fisherman, and is divisible by the size of the fish that the fisherman has caught. So, for i > 1, b_i is the smallest integer that is both strictly greater than b_{i-1} and divisible by a_{p_i}. For example, let n = 7, a = [1, 8, 2, 3, 2, 2, 3]. If the chosen order is p = [1, 6, 7, 5, 3, 2, 4], then: b_1 = a_{p_1} = 1; b_2 is the smallest integer divisible by 2 and greater than 1, which is 2; b_3 is the smallest integer divisible by 3 and greater than 2, which is 3; b_4 is the smallest integer divisible by 2 and greater than 3, which is 4; b_5 is the smallest integer divisible by 2 and greater than 4, which is 6; b_6 is the smallest integer divisible by 8 and greater than 6, which is 8; b_7 is the smallest integer divisible by 3 and greater than 8, which is 9. You have to choose the order of fishermen in a way that yields the minimum possible \sum\limits_{i=1}^{n} b_i. | Input: ['7', '1 8 2 3 2 2 3', ''] Output:['33', ''] | [
2
] |
Monocarp is going to host a party for his friends. He prepared n dishes and is about to serve them. First, he has to add some powdered pepper to each of them β otherwise, the dishes will be pretty tasteless.The i-th dish has two values a_i and b_i β its tastiness with red pepper added or black pepper added, respectively. Monocarp won't add both peppers to any dish, won't add any pepper multiple times, and won't leave any dish without the pepper added.Before adding the pepper, Monocarp should first purchase the said pepper in some shop. There are m shops in his local area. The j-th of them has packages of red pepper sufficient for x_j servings and packages of black pepper sufficient for y_j servings.Monocarp goes to exactly one shop, purchases multiple (possibly, zero) packages of each pepper in such a way that each dish will get the pepper added once, and no pepper is left. More formally, if he purchases x red pepper packages and y black pepper packages, then x and y should be non-negative and x \cdot x_j + y \cdot y_j should be equal to n.For each shop, determine the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages in the said way, print -1. | Input: ['3', '5 10', '100 50', '2 2', '4', '2 3', '1 1', '3 2', '2 2', ''] Output:['62', '112', '107', '-1', ''] | [
0,
2,
3
] |
Let's define f(x) for a positive integer x as the length of the base-10 representation of x without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that.You are given two arrays a and b, each containing n positive integers. In one operation, you do the following: pick some integer i from 1 to n; assign either f(a_i) to a_i or f(b_i) to b_i. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e.βg. a_i = b_i for all i from 1 to n).What's the smallest number of operations required to make a and b similar to each other? | Input: ['411100041 2 3 43 1 4 232 9 31 100 91075019 709259 5 611271314 9024533 81871864 9 3 6 48659503 2 371245467 6 7 37376159 8 364036498 52295554 169'] Output:['2', '0', '2', '18', ''] | [
2
] |
Let's define the value of the permutation p of n integers 1, 2, ..., n (a permutation is an array where each element from 1 to n occurs exactly once) as follows: initially, an integer variable x is equal to 0; if x < p_1, then add p_1 to x (set x = x + p_1), otherwise assign 0 to x; if x < p_2, then add p_2 to x (set x = x + p_2), otherwise assign 0 to x; ... if x < p_n, then add p_n to x (set x = x + p_n), otherwise assign 0 to x; the value of the permutation is x at the end of this process. For example, for p = [4, 5, 1, 2, 3, 6], the value of x changes as follows: 0, 4, 9, 0, 2, 5, 11, so the value of the permutation is 11.You are given an integer n. Find a permutation p of size n with the maximum possible value among all permutations of size n. If there are several such permutations, you can print any of them. | Input: ['3456'] Output:['2 1 3 4', '1 2 3 4 5', '4 5 1 2 3 6'] | [
2
] |
The title is a reference to the very first Educational Round from our writers team, Educational Round 18.There is a bag, containing colored balls. There are n different colors of balls, numbered from 1 to n. There are \mathit{cnt}_i balls of color i in the bag. The total amount of balls in the bag is odd (e.βg. \mathit{cnt}_1 + \mathit{cnt}_2 + ... + \mathit{cnt}_n is odd).In one move, you can choose two balls with different colors and take them out of the bag.At some point, all the remaining balls in the bag will have the same color. That's when you can't make moves anymore.Find any possible color of the remaining balls. | Input: ['331 1 11924 7'] Output:['3', '1', '2', ''] | [
0,
2
] |
Mainak has a convex polygon \mathcal P with n vertices labelled as A_1, A_2, ..., A_n in a counter-clockwise fashion. The coordinates of the i-th point A_i are given by (x_i, y_i), where x_i and y_i are both integers.Further, it is known that the interior angle at A_i is either a right angle or a proper obtuse angle. Formally it is known that: 90 ^ \circ <= \angle A_{i - 1}A_{i}A_{i + 1} < 180 ^ \circ, \forall i \in \{1, 2, ..., n\} where we conventionally consider A_0 = A_n and A_{n + 1} = A_1. Mainak's friend insisted that all points Q such that there exists a chord of the polygon \mathcal P passing through Q with length not exceeding 1, must be coloured \color{red}{\text{red}}. Mainak wants you to find the area of the coloured region formed by the \color{red}{\text{red}} points.Formally, determine the area of the region \mathcal S = \{Q \in \mathcal{P} | Q \text{ is coloured } \color{red}{\text{red}}\}.Recall that a chord of a polygon is a line segment between two points lying on the boundary (i.e. vertices or points on edges) of the polygon. | Input: ['4', '4 5', '4 1', '7 1', '7 5', ''] Output:['1.17809724510'] | [
3,
4
] |
There are n people at a party. The i-th person has an amount of happiness a_i.Every person has a certain kind of personality which can be represented as a binary integer b. If b = 0, it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If b = 1, it means the happiness of the person will increase if he tells the story to someone strictly more happy than them.Let us define a speaking order as an ordering of the people from left to right. Now the following process occurs. We go from left to right. The current person tells the story to all people other than himself. Note that all happiness values stay constant while this happens. After the person is done, he counts the number of people who currently have strictly less/more happiness than him as per his kind of personality, and his happiness increases by that value. Note that only the current person's happiness value increases.As the organizer of the party, you don't want anyone to leave sad. Therefore, you want to count the number of speaking orders such that at the end of the process all n people have equal happiness.Two speaking orders are considered different if there exists at least one person who does not have the same position in the two speaking orders. | Input: ['4', '1 2 4 4', '1 1 0 0', ''] Output:['2', ''] | [
2
] |
This problem was copied by the author from another online platform. Codeforces strongly condemns this action and therefore further submissions to this problem are not accepted.Debajyoti has a very important meeting to attend and he is already very late. Harsh, his driver, needs to take Debajyoti to the destination for the meeting as fast as possible.Harsh needs to pick up Debajyoti from his home and take him to the destination so that Debajyoti can attend the meeting in time. A straight road with n traffic lights connects the home and the destination for the interview. The traffic lights are numbered in order from 1 to n.Each traffic light cycles after t seconds. The i-th traffic light is \color{green}{\text{green}} (in which case Harsh can cross the traffic light) for the first g_i seconds, and \color{red}{\text{red}} (in which case Harsh must wait for the light to turn \color{green}{\text{green}}) for the remaining (tβg_i) seconds, after which the pattern repeats. Each light's cycle repeats indefinitely and initially, the i-th light is c_i seconds into its cycle (a light with c_i=0 has just turned \color{green}{\text{green}}). In the case that Harsh arrives at a light at the same time it changes colour, he will obey the new colour. Formally, the i-th traffic light is \color{green}{\text{green}} from [0,g_i) and \color{red}{\text{red}} from [g_i,t) (after which it repeats the cycle). The i-th traffic light is initially at the c_i-th second of its cycle.From the i-th traffic light, exactly d_i seconds are required to travel to the next traffic light (that is to the (i+1)-th light). Debajyoti's home is located just before the first light and Debajyoti drops for the interview as soon as he passes the n-th light. In other words, no time is required to reach the first light from Debajyoti's home or to reach the interview centre from the n-th light.Harsh does not know how much longer it will take for Debajyoti to get ready. While waiting, he wonders what is the minimum possible amount of time he will spend driving provided he starts the moment Debajyoti arrives, which can be anywhere between 0 to \infty seconds from now. Can you tell Harsh the minimum possible amount of time he needs to spend on the road?Please note that Harsh can only start or stop driving at integer moments of time. | Input: ['5 10', '4 2', '7 3', '3 6', '5 2', '8 0', '1 2 3 4', ''] Output:['11', ''] | [
2
] |
A permutation p of length n is called almost perfect if for all integer 1 <=q i <=q n, it holds that \lvert p_i - p^{-1}_i \rvert <= 1, where p^{-1} is the inverse permutation of p (i.e. p^{-1}_{k_1} = k_2 if and only if p_{k_2} = k_1).Count the number of almost perfect permutations of length n modulo 998244353. | Input: ['32350'] Output:['2', '4', '830690567', ''] | [
3
] |
You are given a connected, undirected and unweighted graph with n vertices and m edges. Notice the limit on the number of edges: m <= n + 2.Let's say we color some of the edges red and the remaining edges blue. Now consider only the red edges and count the number of connected components in the graph. Let this value be c_1. Similarly, consider only the blue edges and count the number of connected components in the graph. Let this value be c_2.Find an assignment of colors to the edges such that the quantity c_1+c_2 is minimised. | Input: ['45 71 22 33 44 55 11 33 54 41 22 31 43 46 71 21 33 44 51 45 66 22 11 2'] Output:['0111010', '1001', '0001111', '0', ''] | [
0
] |
Last summer, Feluda gifted Lalmohan-Babu a balanced bracket sequence s of length 2 n.Topshe was bored during his summer vacations, and hence he decided to draw an undirected graph of 2 n vertices using the balanced bracket sequence s. For any two distinct vertices i and j (1 <= i < j <= 2 n), Topshe draws an edge (undirected and unweighted) between these two nodes if and only if the subsegment s[i ... j] forms a balanced bracket sequence.Determine the number of connected components in Topshe's graph.See the Notes section for definitions of the underlined terms. | Input: ['41()3()(())3((()))4(())(())'] Output:['1', '2', '3', '3', ''] | [
2
] |
Mainak has two positive integers n and m.Mainak finds a sequence a_1, a_2, ..., a_n of n positive integers interesting, if for all integers i (1 <= i <= n), the bitwise XOR of all elements in a which are strictly less than a_i is 0. Formally if p_i is the bitwise XOR of all elements in a which are strictly less than a_i, then a is an interesting sequence if p_1 = p_2 = ... = p_n = 0.For example, sequences [1,3,2,3,1,2,3], [4,4,4,4], [25] are interesting, whereas [1,2,3,4] (p_2 = 1!=0), [4,1,1,2,4] (p_1 = 1 \oplus 1 \oplus 2 = 2!=0), [29,30,30] (p_2 = 29!=0) aren't interesting.Here a \oplus b denotes bitwise XOR of integers a and b.Find any interesting sequence a_1, a_2, ..., a_n (or report that there exists no such sequence) such that the sum of the elements in the sequence a is equal to m, i.e. a_1 + a_2 ... + a_n = m.As a reminder, the bitwise XOR of an empty sequence is considered to be 0. | Input: ['4', '1 3', '6 12', '2 1', '3 6', ''] Output:['Yes', '3', 'Yes', '1 3 2 2 3 1', 'No', 'Yes', '2 2 2', ''] | [
3
] |
Mainak has an array a_1, a_2, ..., a_n of n positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers l and r, such that 1 <= l <= r <= n, and any positive integer k. Repeat this k times: set a_l=a_{l+1}, a_{l+1}=a_{l+2}, ..., a_{r-1}=a_r, a_r=a_l (all changes happen at the same time). Mainak wants to maximize the value of (a_n - a_1) after exactly one such operation. Determine the maximum value of (a_n - a_1) that he can obtain. | Input: ['561 3 9 11 5 712039 99 99942 1 8 132 1 5'] Output:['10', '0', '990', '7', '4', ''] | [
2,
3
] |
One day, you are accepted as being Dr. Chanek's assistant. The first task given by Dr. Chanek to you is to take care and store his magical stones.Dr. Chanek has N magical stones with N being an even number. Those magical stones are numbered from 1 to N. Magical stone i has a strength of A_i. A magical stone can be painted with two colours, namely the colour black or the colour white. You are tasked to paint the magical stones with the colour black or white and store the magical stones into a magic box with a magic coefficient Z (0 <=q Z <=q 2). The painting of the magical stones must be done in a way such that there are \frac{N}{2} black magical stones and \frac{N}{2} white magical stones.Define \text{concat}(x, y) for two integers x and y as the result of concatenating the digits of x to the left of y in their decimal representation without changing the order. As an example, \text{concat}(10, 24) will result in 1024.For a magic box with a magic coefficient Z, magical stone i will react with magical stone j if the colours of both stones are different and \text{concat}(A_i, A_j) * \text{concat}(A_j, A_i) + A_i * A_j \equiv Z \mod 3. A magical stone that is reacting will be very hot and dangerous. Because of that, you must colour the magical stones and determine the magic coefficient Z of the magic box in a way such that there is no magical stone that reacts, or report if it is impossible. | Input: ['4', '4 10 9 14', ''] Output:['0', '1001', ''] | [
3
] |
Pak Chanek plans to build a garage. He wants the garage to consist of a square and a right triangle that are arranged like the following illustration. Define a and b as the lengths of two of the sides in the right triangle as shown in the illustration. An integer x is suitable if and only if we can construct a garage with assigning positive integer values for the lengths a and b (a<b) so that the area of the square at the bottom is exactly x. As a good friend of Pak Chanek, you are asked to help him find the N-th smallest suitable number. | Input: ['3', ''] Output:['7', ''] | [
3,
4
] |
In the country of Dengkleknesia, there are N factories numbered from 1 to N. Factory i has an electrical coefficient of A_i. There are also N-1 power lines with the j-th power line connecting factory U_j and factory V_j. It can be guaranteed that each factory in Dengkleknesia is connected to all other factories in Dengkleknesia through one or more power lines. In other words, the collection of factories forms a tree. Each pair of different factories in Dengkleknesia can use one or more existing power lines to transfer electricity to each other. However, each power line needs to be turned on first so that electricity can pass through it.Define f(x, y, z) as the minimum number of power lines that need to be turned on so that factory x can make electrical transfers to factory y and factory z. Also define g(x, y, z) as the number of distinct prime factors of \text{GCD}(A_x, A_y, A_z).To measure the electrical efficiency, you must find the sum of f(x, y, z) * g(x, y, z) for all combinations of (x, y, z) such that 1 <=q x < y < z <=q N. Because the answer can be very large, you just need to output the answer modulo 998\,244\,353.Note: \text{GCD}(k_1, k_2, k_3) is the greatest common divisor of k_1, k_2, and k_3, which is the biggest integer that simultaneously divides k_1, k_2, and k_3. | Input: ['3', '1 2 3', '1 2', '2 3', ''] Output:['0', ''] | [
3
] |
Let's say Pak Chanek has an array A consisting of N positive integers. Pak Chanek will do a number of operations. In each operation, Pak Chanek will do the following: Choose an index p (1 <=q p <=q N). Let c be the number of operations that have been done on index p before this operation. Decrease the value of A_p by 2^c. Multiply the value of A_p by 2. After each operation, all elements of A must be positive integers.An array A is said to be sortable if and only if Pak Chanek can do zero or more operations so that A_1 < A_2 < A_3 < A_4 < ... < A_N.Pak Chanek must find an array A that is sortable with length N such that A_1 + A_2 + A_3 + A_4 + ... + A_N is the minimum possible. If there are more than one possibilities, Pak Chanek must choose the array that is lexicographically minimum among them.Pak Chanek must solve the following things: Pak Chanek must print the value of A_1 + A_2 + A_3 + A_4 + ... + A_N for that array. Q questions will be given. For the i-th question, an integer P_i is given. Pak Chanek must print the value of A_{P_i}. Help Pak Chanek solve the problem.Note: an array B of size N is said to be lexicographically smaller than an array C that is also of size N if and only if there exists an index i such that B_i < C_i and for each j < i, B_j = C_j. | Input: ['6 3', '1', '4', '5', ''] Output:['17', '1', '3', '4', ''] | [
3,
4
] |
Pak Chanek has a mirror in the shape of a circle. There are N lamps on the circumference numbered from 1 to N in clockwise order. The length of the arc from lamp i to lamp i+1 is D_i for 1 <=q i <=q N-1. Meanwhile, the length of the arc between lamp N and lamp 1 is D_N.Pak Chanek wants to colour the lamps with M different colours. Each lamp can be coloured with one of the M colours. However, there cannot be three different lamps such that the colours of the three lamps are the same and the triangle made by considering the three lamps as vertices is a right triangle (triangle with one of its angles being exactly 90 degrees).The following are examples of lamp colouring configurations on the circular mirror. Figure 1. an example of an incorrect colouring because lamps 1, 2, and 3 form a right triangleFigure 2. an example of a correct colouringFigure 3. an example of a correct colouring Before colouring the lamps, Pak Chanek wants to know the number of distinct colouring configurations he can make. Count the number of distinct possible lamp colouring configurations, modulo 998\,244\,353. | Input: ['4 2', '10 10 6 14', ''] Output:['10', ''] | [
3,
4
] |
A basketball competition is held where the number of players in a team does not have a maximum or minimum limit (not necessarily 5 players in one team for each match). There are N candidate players in the competition that will be trained by Pak Chanek, the best basketball coach on earth. The i-th candidate player has a power of P_i.Pak Chanek will form zero or more teams from the N candidate players on the condition that each candidate player may only join in at most one team. Each of Pak Chanek's teams will be sent to compete once with an enemy team that has a power of D. In each match, the team sent is said to defeat the enemy team if the sum of powers from the formed players is strictly greater than D.One of Pak Chanek's skills is that when a team that has been formed plays in a match, he can change the power of each player in the team to be equal to the biggest player power from the team.Determine the maximum number of wins that can be achieved by Pak Chanek. | Input: ['6 180', '90 80 70 60 50 100', ''] Output:['2', ''] | [
2,
4
] |
Pak Chanek has a grid that has N rows and M columns. Each row is numbered from 1 to N from top to bottom. Each column is numbered from 1 to M from left to right.Each tile in the grid contains a number. The numbers are arranged as follows: Row 1 contains integers from 1 to M from left to right. Row 2 contains integers from M+1 to 2 * M from left to right. Row 3 contains integers from 2 * M+1 to 3 * M from left to right. And so on until row N. A domino is defined as two different tiles in the grid that touch by their sides. A domino is said to be tight if and only if the two numbers in the domino have a difference of exactly 1. Count the number of distinct tight dominoes in the grid.Two dominoes are said to be distinct if and only if there exists at least one tile that is in one domino, but not in the other. | Input: ['3 4', ''] Output:['9', ''] | [
3
] |
Given an integer n, find any array a of n distinct nonnegative integers less than 2^{31} such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices. | Input: ['7', '8', '3', '4', '5', '6', '7', '9', ''] Output:['4 2 1 5 0 6 7 3', '2 1 3', '2 1 3 0', '2 0 4 5 3', '4 1 2 12 3 8', '1 2 3 4 5 6 7', '8 2 3 7 4 0 5 6 9'] | [
2
] |
You have n rectangles, the i-th rectangle has height h_i and width w_i.You are asked q queries of the form h_s \ w_s \ h_b \ w_b. For each query output, the total area of rectangles you own that can fit a rectangle of height h_s and width w_s while also fitting in a rectangle of height h_b and width w_b. In other words, print \sum h_i \cdot w_i for i such that h_s < h_i < h_b and w_s < w_i < w_b. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). | Input: ['32 12 33 21 1 3 45 51 12 23 34 45 53 3 6 62 1 4 51 1 2 101 1 100 1001 1 3 33 1999 999999 999999 9981 1 1000 1000'] Output:['6', '41', '9', '0', '54', '4', '2993004', ''] | [
0
] |
There are n people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are [0, 3, 2, 3, 4], and the value is 0+3+2+3+4=12.You are given the initial arrangement of people in the line. For each k from 1 to n, determine the maximum value of the line if you can change the direction of at most k people. | Input: ['63LLR5LRRLL1L12LRRRLLLRLLRL10LLLLLRRRRR9LRLRLRLRL'] Output:['3 5 5 ', '16 16 16 16 16 ', '0 ', '86 95 98 101 102 102 102 102 102 102 102 102 ', '29 38 45 52 57 62 65 68 69 70 ', '44 50 54 56 56 56 56 56 56 ', ''] | [
2
] |
You are given a bipartite graph with n_1 vertices in the first part, n_2 vertices in the second part, and m edges. The maximum matching in this graph is the maximum possible (by size) subset of edges of this graph such that no vertex is incident to more than one chosen edge.You have to process two types of queries to this graph: 1 β remove the minimum possible number of vertices from this graph so that the size of the maximum matching gets reduced exactly by 1, and print the vertices that you have removed. Then, find any maximum matching in this graph and print the sum of indices of edges belonging to this matching; 2 β query of this type will be asked only after a query of type 1. As the answer to this query, you have to print the edges forming the maximum matching you have chosen in the previous query. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. | Input: ['3 4 4 4', '2 2', '1 3', '2 1', '3 4', '1', '2', '1', '2', ''] Output:['1', '-4', '3', '===', '2', '1 2', '===', '1', '2', '2', '===', '1', '2', ''] | [
0
] |
You are given two arrays a and b, consisting of n integers each.Let's define a function f(a, b) as follows: let's define an array c of size n, where c_i = a_i \oplus b_i (\oplus denotes bitwise XOR); the value of the function is c_1 \mathbin{\&} c_2 \mathbin{\&} \cdots \mathbin{\&} c_n (i.e. bitwise AND of the entire array c). Find the maximum value of the function f(a, b) if you can reorder the array b in an arbitrary way (leaving the initial order is also an option). | Input: ['351 0 0 3 32 3 2 1 031 1 10 0 380 1 2 3 4 5 6 77 6 5 4 3 2 1 0'] Output:['2', '0', '7', ''] | [
2
] |
You are given an array a_1, a_2, ..., a_n, which is sorted in non-descending order. You decided to perform the following steps to create array b_1, b_2, ..., b_n: Create an array d consisting of n arbitrary non-negative integers. Set b_i = a_i + d_i for each b_i. Sort the array b in non-descending order. You are given the resulting array b. For each index i, calculate what is the minimum and maximum possible value of d_i you can choose in order to get the given array b.Note that the minimum (maximum) d_i-s are independent of each other, i. e. they can be obtained from different possible arrays d. | Input: ['432 3 57 11 1311000500041 2 3 41 2 3 4410 20 30 4022 33 33 55'] Output:['5 4 2', '11 10 8', '4000', '4000', '0 0 0 0', '0 0 0 0', '12 2 3 15', '23 13 3 15', ''] | [
2,
4
] |
You have an image file of size 2 * 2, consisting of 4 pixels. Each pixel can have one of 26 different colors, denoted by lowercase Latin letters.You want to recolor some of the pixels of the image so that all 4 pixels have the same color. In one move, you can choose no more than two pixels of the same color and paint them into some other color (if you choose two pixels, both should be painted into the same color).What is the minimum number of moves you have to make in order to fulfill your goal? | Input: ['5rbbrccwbaaaaabcdyyxx'] Output:['1', '2', '0', '3', '1', ''] | [
2
] |
Misha has a square n * n matrix, where the number in row i and column j is equal to a_{i, j}. Misha wants to modify the matrix to contain exactly k distinct integers. To achieve this goal, Misha can perform the following operation zero or more times: choose any square submatrix of the matrix (you choose (x_1,y_1), (x_2,y_2), such that x_1 <=q x_2, y_1 <=q y_2, x_2 - x_1 = y_2 - y_1, then submatrix is a set of cells with coordinates (x, y), such that x_1 <=q x <=q x_2, y_1 <=q y <=q y_2), choose an integer k, where 1 <=q k <=q n^2, replace all integers in the submatrix with k. Please find the minimum number of operations that Misha needs to achieve his goal. | Input: ['3 4', '1 1 1', '1 1 2', '3 4 5', ''] Output:['1'] | [
2,
3
] |
It is the easy version of the problem. The only difference is that in this version a_i <= 200.You are given an array of n integers a_0, a_1, a_2, ... a_{n - 1}. Bryap wants to find the longest beautiful subsequence in the array.An array b = [b_0, b_1, ..., b_{m-1}], where 0 <= b_0 < b_1 < ... < b_{m - 1} < n, is a subsequence of length m of the array a.Subsequence b = [b_0, b_1, ..., b_{m-1}] of length m is called beautiful, if the following condition holds: For any p (0 <= p < m - 1) holds: a_{b_p} \oplus b_{p+1} < a_{b_{p+1}} \oplus b_p. Here a \oplus b denotes the bitwise XOR of a and b. For example, 2 \oplus 4 = 6 and 3 \oplus 1=2.Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question. | Input: ['321 255 2 4 3 1103 8 8 2 9 1 6 2 8 3'] Output:['2', '3', '6', ''] | [
0
] |
You are given a matrix consisting of n rows and m columns. Each cell of this matrix contains 0 or 1.Let's call a square of size 2 * 2 without one corner cell an L-shape figure. In one operation you can take one L-shape figure, with at least one cell containing 1 and replace all numbers in it with zeroes.Find the maximum number of operations that you can do with the given matrix. | Input: ['44 31011110111103 41110011101112 200002 21111'] Output:['8', '9', '0', '2', ''] | [
2
] |
You are given an array a that contains n integers. You can choose any proper subsegment a_l, a_{l + 1}, ..., a_r of this array, meaning you can choose any two integers 1 <= l <= r <= n, where r - l + 1 < n. We define the beauty of a given subsegment as the value of the following expression:\max(a_{1}, a_{2}, ..., a_{l-1}, a_{r+1}, a_{r+2}, ..., a_{n}) - \min(a_{1}, a_{2}, ..., a_{l-1}, a_{r+1}, a_{r+2}, ..., a_{n}) + \max(a_{l}, ..., a_{r}) - \min(a_{l}, ..., a_{r}).Please find the maximum beauty among all proper subsegments. | Input: ['4', '8', '1 2 2 3 1 5 6 1', '5', '1 2 3 100 200', '4', '3 3 3 3', '6', '7 8 3 1 1 8', ''] Output:['9', '297', '0', '14', ''] | [
0,
2,
3
] |
Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions (\frac{a}{b} and \frac{c}{d}) with integer numerators and denominators. Then children are commanded to play with their fractions.Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by 0). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps! | Input: ['82 1 1 16 3 2 11 2 2 30 1 0 1000 1 228 179100 3 25 6999999999 300000000 666666666 10000000033 15 0 84'] Output:['1', '0', '2', '0', '1', '1', '1', '1', ''] | [
3
] |
Burenka is about to watch the most interesting sporting event of the year β a fighting tournament organized by her friend Tonya.n athletes participate in the tournament, numbered from 1 to n. Burenka determined the strength of the i-th athlete as an integer a_i, where 1 <=q a_i <=q n. All the strength values are different, that is, the array a is a permutation of length n. We know that in a fight, if a_i > a_j, then the i-th participant always wins the j-th.The tournament goes like this: initially, all n athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.Burenka decided to ask Tonya q questions. In each question, Burenka asks how many victories the i-th participant gets in the first k rounds of the competition for some given numbers i and k. Tonya is not very good at analytics, so he asks you to help him answer all the questions. | Input: ['33 13 1 21 24 21 3 4 24 53 25 21 2 3 5 45 10000000004 6'] Output:['2', '0', '1', '0', '4', ''] | [
4
] |
A new entertainment has appeared in Buryatia β a mathematical circus! The magician shows two numbers to the audience β n and k, where n is even. Next, he takes all the integers from 1 to n, and splits them all into pairs (a, b) (each integer must be in exactly one pair) so that for each pair the integer (a + k) \cdot b is divisible by 4 (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers n and k.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it. | Input: ['44 12 012 1014 11'] Output:['YES', '1 2', '3 4', 'NO', 'YES', '3 4', '7 8', '11 12', '2 1', '6 5', '10 9', 'YES', '1 2', '3 4', '5 6', '7 8', '9 10', '11 12', '13 14', ''] | [
3
] |
Burenka and Tonya are playing an old Buryat game with a chip on a board of n * m cells.At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right and up in one move). The one who cannot make a move loses.Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way). Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move. | Input: ['61 11 45 62 26 3999999999 1000000000'] Output:['Tonya', 'Burenka', 'Burenka', 'Tonya', 'Burenka', 'Burenka', ''] | [
3
] |
Eugene got Burenka an array a of length n of integers from 1 to m for her birthday. Burenka knows that Eugene really likes coprime integers (integers x and y such that they have only one common factor (equal to 1)) so she wants to to ask Eugene q questions about the present. Each time Burenka will choose a subsegment a_l, a_{l + 1}, ..., a_r of array a, and compute the product of these numbers p = a_l \cdot a_{l + 1} \cdot ... \cdot a_r. Then she will ask Eugene to count the number of integers between 1 and C inclusive which are coprime with p. Help Eugene answer all the questions! | Input: ['5 5 5 3', '1 2 3 2 5', '1 1', '2 4', '4 5', ''] Output:['5', '2', '2', ''] | [
3
] |
Burenka has two pictures a and b, which are tables of the same size n * m. Each cell of each painting has a color β a number from 0 to 2 \cdot 10^5, and there are no repeating colors in any row or column of each of the two paintings, except color 0.Burenka wants to get a picture b from the picture a. To achieve her goal, Burenka can perform one of 2 operations: swap any two rows of a or any two of its columns. Tell Burenka if she can fulfill what she wants, and if so, tell her the sequence of actions.The rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to m from left to right. | Input: ['3 3', '1 0 2', '0 0 0', '2 0 1', '2 0 1', '0 0 0', '1 0 2', ''] Output:['1', '1 1 3', ''] | [
3
] |
We call an array a pure if all elements in it are pairwise distinct. For example, an array [1, 7, 9] is pure, [1, 3, 3, 7] isn't, because 3 occurs twice in it.A pure array b is similar to a pure array c if their lengths n are the same and for all pairs of indices l, r, such that 1 <= l <= r <= n, it's true that \operatorname{argmax}([b_l, b_{l + 1}, ..., b_r]) = \operatorname{argmax}([c_l, c_{l + 1}, ..., c_r]), where \operatorname{argmax}(x) is defined as the index of the largest element in x (which is unique for pure arrays). For example, \operatorname{argmax}([3, 4, 2]) = 2, \operatorname{argmax}([1337, 179, 57]) = 1.Recently, Tonya found out that Burenka really likes a permutation p of length n. Tonya decided to please her and give her an array a similar to p. He already fixed some elements of a, but exactly k elements are missing (in these positions temporarily a_i = 0). It is guaranteed that k >= 2. Also, he has a set S of k - 1 numbers.Tonya realized that he was missing one number to fill the empty places of a, so he decided to buy it. He has q options to buy. Tonya thinks that the number d suits him, if it is possible to replace all zeros in a with numbers from S and the number d, so that a becomes a pure array similar to p. For each option of d, output whether this number is suitable for him or not. | Input: ['44 31 4 3 25 0 7 069145 31 2 5 4 30 5 10 0 03 918115 21 4 3 2 50 0 0 0 07 9 1 561004 24 1 3 20 5 3 0246'] Output:['YES', 'NO', 'NO', 'YES', 'YES', 'NO', 'YES', 'YES', 'NO', 'NO', ''] | [
2,
3
] |
Tonya was given an array of a of length n written on a postcard for his birthday. For some reason, the postcard turned out to be a cyclic array, so the index of the element located strictly to the right of the n-th is 1. Tonya wanted to study it better, so he bought a robot "Burenka-179".A program for Burenka is a pair of numbers (s, k), where 1 <=q s <=q n, 1 <=q k <=q n-1. Note that k cannot be equal to n. Initially, Tonya puts the robot in the position of the array s. After that, Burenka makes exactly n steps through the array. If at the beginning of a step Burenka stands in the position i, then the following happens: The number a_{i} is added to the usefulness of the program. "Burenka" moves k positions to the right (i := i + k is executed, if i becomes greater than n, then i := i - n). Help Tonya find the maximum possible usefulness of a program for "Burenka" if the initial usefulness of any program is 0.Also, Tony's friend Ilyusha asks him to change the array q times. Each time he wants to assign a_p := x for a given index p and a value x. You need to find the maximum possible usefulness of the program after each of these changes. | Input: ['42 11 21 34 44 1 3 22 64 61 13 119 31 7 9 4 5 2 3 6 83 12 19 16 31 1 1 1 1 11 54 43 8'] Output:['3', '5', '14', '16', '24', '24', '24', '57', '54', '36', '36', '6', '18', '27', '28', ''] | [
2,
3
] |
In all schools in Buryatia, in the 1 class, everyone is told the theory of Fibonacci strings."A block is a subsegment of a string where all the letters are the same and are bounded on the left and right by the ends of the string or by letters other than the letters in the block. A string is called a Fibonacci string if, when it is divided into blocks, their lengths in the order they appear in the string form the Fibonacci sequence (f_0 = f_1 = 1, f_i = f_{i-2} + f_{i-1}), starting from the zeroth member of this sequence. A string is called semi-Fibonacci if it possible to reorder its letters to get a Fibonacci string."Burenka decided to enter the Buryat State University, but at the entrance exam she was given a difficult task. She was given a string consisting of the letters of the Buryat alphabet (which contains exactly k letters), and was asked if the given string is semi-Fibonacci. The string can be very long, so instead of the string, she was given the number of appearances of each letter (c_i for the i-th letter) in that string. Unfortunately, Burenka no longer remembers the theory of Fibonacci strings, so without your help she will not pass the exam. | Input: ['61121 121 233 1 327 5626 8 3 4 13 34'] Output:['YES', 'YES', 'NO', 'YES', 'NO', 'YES', ''] | [
2,
3
] |
This is the hard version of this problem. The difference between easy and hard versions is only the constraints on a_i and on n. You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the n-th queen of the country. There is an ancient tradition in Buryatia β before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the n-th ruler, the inhabitants of the country give them an array of a of exactly n numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices l and r, so that 1 <= l <= r <= n and a non-negative integer x, then for all l <=q i <=q r assign a_i := a_i \oplus x, where \oplus denotes the bitwise XOR operation. It takes <=ft\lceil \frac{r-l+1}{2} \right\rceil seconds to do this operation, where \lceil y \rceil denotes y rounded up to the nearest integer. Help Burenka calculate how much time she will need. | Input: ['745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55'] Output:['2', '2', '0', '2', '4', '7', '4', ''] | [
2
] |
This is the easy version of this problem. The difference between easy and hard versions is only the constraints on a_i and on n. You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the n-th queen of the country. There is an ancient tradition in Buryatia β before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the n-th ruler, the inhabitants of the country give them an array of a of exactly n numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices l and r, so that 1 <= l <= r <= n and a non-negative integer x, then for all l <=q i <=q r assign a_i := a_i \oplus x, where \oplus denotes the bitwise XOR operation. It takes <=ft\lceil \frac{r-l+1}{2} \right\rceil seconds to do this operation, where \lceil y \rceil denotes y rounded up to the nearest integer. Help Burenka calculate how much time she will need. | Input: ['745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55'] Output:['2', '2', '0', '2', '4', '7', '4', ''] | [
2
] |
Madoka wants to enter to "Novosibirsk State University", but in the entrance exam she came across a very difficult task:Given an integer n, it is required to calculate \sum{\operatorname{lcm}(c, \gcd(a, b))}, for all triples of positive integers (a, b, c), where a + b + c = n.In this problem \gcd(x, y) denotes the greatest common divisor of x and y, and \operatorname{lcm}(x, y) denotes the least common multiple of x and y.Solve this problem for Madoka and help her to enter to the best university! | Input: ['3', ''] Output:['1', ''] | [
3
] |
Madoka decided to entrust the organization of a major computer game tournament "OSU"!In this tournament, matches are held according to the "Olympic system". In other words, there are 2^n participants in the tournament, numbered with integers from 1 to 2^n. There are n rounds in total in the tournament. In the i-th round there are 2^{n - i} matches between two players (one of whom is right, the other is left), after which the winners go further along the tournament grid, and the losing participants are eliminated from the tournament. Herewith, the relative order in the next round does not change. And the winner of the tournament β is the last remaining participant.But the smaller the participant's number, the more he will pay Madoka if he wins, so Madoka wants the participant with the lowest number to win. To do this, she can arrange the participants in the first round as she likes, and also determine for each match who will win β the participant on the left or right.But Madoka knows that tournament sponsors can change the winner in matches no more than k times. (That is, if the participant on the left won before the change, then the participant on the right will win after the change, and if the participant on the right won, then the participant on the left will win after the change). So, the first image shows the tournament grid that Madoka made, where the red lines denote who should win the match. And the second one shows the tournament grid, after one change in the outcome of the match by sponsors (a match between 1 and 3 players). Print the minimum possible number of the winner in the tournament, which Madoka can get regardless of changes in sponsors. But since the answer can be very large, output it modulo 10^9 + 7. Note that we need to minimize the answer, and only then take it modulo. | Input: ['1 1', ''] Output:['2', ''] | [
2,
3
] |
Given an array of integer a_1, a_2, ..., a_n. In one operation you can make a_i := a_i + 1 if i < n and a_i <=q a_{i + 1}, or i = n and a_i <=q a_1.You need to check whether the array a_1, a_2, ..., a_n can become equal to the array b_1, b_2, ..., b_n in some number of operations (possibly, zero). Two arrays a and b of length n are called equal if a_i = b_i for all integers i from 1 to n. | Input: ['531 2 51 2 522 21 343 4 1 26 4 2 532 4 14 5 351 2 3 4 56 5 6 7 6'] Output:['YES', 'NO', 'NO', 'NO', 'YES', ''] | [
2
] |
Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers (a, b) exist, where 1 <=q a, b <=q n, for which \frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} <=q 3.In this problem, \operatorname{gcd}(a, b) denotes the greatest common divisor of the numbers a and b, and \operatorname{lcm}(a, b) denotes the smallest common multiple of the numbers a and b. | Input: ['612345100000000'] Output:['1', '4', '7', '10', '11', '266666666', ''] | [
3
] |
There are n bags, each bag contains m balls with numbers from 1 to m. For every i \in [1, m], there is exactly one ball with number i in each bag.You have to take exactly one ball from each bag (all bags are different, so, for example, taking the ball 1 from the first bag and the ball 2 from the second bag is not the same as taking the ball 2 from the first bag and the ball 1 from the second bag). After that, you calculate the number of balls with odd numbers among the ones you have taken. Let the number of these balls be F.Your task is to calculate the sum of F^k over all possible ways to take n balls, one from each bag. | Input: ['52 3 81 1 11 5 103 7 20001337666 42424242 2000'] Output:['1028', '1', '3', '729229716', '652219904', ''] | [
3
] |
There is a chip on the coordinate line. Initially, the chip is located at the point 0. You can perform any number of moves; each move increases the coordinate of the chip by some positive integer (which is called the length of the move). The length of the first move you make should be divisible by k, the length of the second move β by k+1, the third β by k+2, and so on.For example, if k=2, then the sequence of moves may look like this: 0 \rightarrow 4 \rightarrow 7 \rightarrow 19 \rightarrow 44, because 4 - 0 = 4 is divisible by 2 = k, 7 - 4 = 3 is divisible by 3 = k + 1, 19 - 7 = 12 is divisible by 4 = k + 2, 44 - 19 = 25 is divisible by 5 = k + 3.You are given two positive integers n and k. Your task is to count the number of ways to reach the point x, starting from 0, for every x \in [1, n]. The number of ways can be very large, so print it modulo 998244353. Two ways are considered different if they differ as sets of visited positions. | Input: ['8 1', ''] Output:['1 1 2 2 3 4 5 6 '] | [
0,
3
] |
There is a grid, consisting of 2 rows and m columns. The rows are numbered from 1 to 2 from top to bottom. The columns are numbered from 1 to m from left to right.The robot starts in a cell (1, 1). In one second, it can perform either of two actions: move into a cell adjacent by a side: up, right, down or left; remain in the same cell. The robot is not allowed to move outside the grid.Initially, all cells, except for the cell (1, 1), are locked. Each cell (i, j) contains a value a_{i,j} β the moment that this cell gets unlocked. The robot can only move into a cell (i, j) if at least a_{i,j} seconds have passed before the move.The robot should visit all cells without entering any cell twice or more (cell (1, 1) is considered entered at the start). It can finish in any cell.What is the fastest the robot can achieve that? | Input: ['430 0 14 3 250 4 8 12 162 6 10 14 1840 10 10 1010 10 10 1020 00 0'] Output:['5', '19', '17', '3', ''] | [
2,
4
] |
A permutation of length n is a sequence of integers from 1 to n such that each integer appears in it exactly once.Let the fixedness of a permutation p be the number of fixed points in it β the number of positions j such that p_j = j, where p_j is the j-th element of the permutation p.You are asked to build a sequence of permutations a_1, a_2, ..., starting from the identity permutation (permutation a_1 = [1, 2, ..., n]). Let's call it a permutation chain. Thus, a_i is the i-th permutation of length n.For every i from 2 onwards, the permutation a_i should be obtained from the permutation a_{i-1} by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation a_i should be strictly lower than the fixedness of the permutation a_{i-1}.Consider some chains for n = 3: a_1 = [1, 2, 3], a_2 = [1, 3, 2] β that is a valid chain of length 2. From a_1 to a_2, the elements on positions 2 and 3 get swapped, the fixedness decrease from 3 to 1. a_1 = [2, 1, 3], a_2 = [3, 1, 2] β that is not a valid chain. The first permutation should always be [1, 2, 3] for n = 3. a_1 = [1, 2, 3], a_2 = [1, 3, 2], a_3 = [1, 2, 3] β that is not a valid chain. From a_2 to a_3, the elements on positions 2 and 3 get swapped but the fixedness increase from 1 to 3. a_1 = [1, 2, 3], a_2 = [3, 2, 1], a_3 = [3, 1, 2] β that is a valid chain of length 3. From a_1 to a_2, the elements on positions 1 and 3 get swapped, the fixedness decrease from 3 to 1. From a_2 to a_3, the elements on positions 2 and 3 get swapped, the fixedness decrease from 1 to 0. Find the longest permutation chain. If there are multiple longest answers, print any of them. | Input: ['223'] Output:['2', '1 2', '2 1', '3', '1 2 3', '3 2 1', '3 1 2', ''] | [
3
] |
You are standing at the point 0 on a coordinate line. Your goal is to reach the point n. In one minute, you can move by 2 or by 3 to the left or to the right (i.βe., if your current coordinate is x, it can become x-3, x-2, x+2 or x+3). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point 0 to the point n.You have to answer t independent test cases. | Input: ['413412'] Output:['2', '1', '2', '4', ''] | [
2,
3
] |
This is an interactive problem.Farmer Stanley grows corn on a rectangular field of size n * m meters with corners in points (0, 0), (0, m), (n, 0), (n, m). This year the harvest was plentiful and corn covered the whole field.The night before harvest aliens arrived and poisoned the corn in a single 1 * 1 square with sides parallel to field borders. The corn inside the square must not be eaten, but you cannot distinguish it from ordinary corn by sight. Stanley can only collect a sample of corn from an arbitrary polygon and bring it to the laboratory, where it will be analyzed and Stanley will be told the amount of corn in the sample that was poisoned. Since the harvest will soon deteriorate, such a study can be carried out no more than 5 times.More formally, it is allowed to make no more than 5 queries, each of them calculates the area of intersection of a chosen polygon with a square of poisoned corn. It is necessary to find out the coordinates of the lower-left corner of the drawn square (the vertex of the square with the smallest x and y coordinates). | Input: ['3 3', '', '', '', '', '', '0.5', '', '', '', '', '', '0.5'] Output:['? 4', '0 0', '2 0', '2 3', '0 3', '', '? 4', '0 0', '0 1', '3 1', '3 0', '', '! 1.5 0.5'] | [
3
] |
Stanley lives in a country that consists of n cities (he lives in city 1). There are bidirectional roads between some of the cities, and you know how long it takes to ride through each of them. Additionally, there is a flight between each pair of cities, the flight between cities u and v takes (u - v)^2 time.Stanley is quite afraid of flying because of watching "Sully: Miracle on the Hudson" recently, so he can take at most k flights. Stanley wants to know the minimum time of a journey to each of the n cities from the city 1. | Input: ['3 1 2', '1 3 1', ''] Output:['0 1 1 '] | [
2
] |
The Narrator has an integer array a of length n, but he will only tell you the size n and q statements, each of them being three integers i, j, x, which means that a_i \mid a_j = x, where | denotes the bitwise OR operation.Find the lexicographically smallest array a that satisfies all the statements.An array a is lexicographically smaller than an array b of the same length if and only if the following holds: in the first position where a and b differ, the array a has a smaller element than the corresponding element in b. | Input: ['4 3', '1 2 3', '1 3 2', '4 1 2', ''] Output:['0 3 2 2 '] | [
2
] |
Stanley has decided to buy a new desktop PC made by the company "Monoblock", and to solve captcha on their website, he needs to solve the following task.The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array [1, 1, 1] is 1; [5, 7] is 2, as it could be split into blocks [5] and [7]; [1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9] is 3, as it could be split into blocks [1], [7, 7, 7, 7, 7, 7, 7], and [9, 9, 9, 9, 9, 9, 9, 9, 9]. You are given an array a of length n. There are m queries of two integers i, x. A query i, x means that from now on the i-th element of the array a is equal to x.After each query print the sum of awesomeness values among all subsegments of array a. In other words, after each query you need to calculate \sum\limits_{l = 1}^n \sum\limits_{r = l}^n g(l, r), where g(l, r) is the awesomeness of the array b = [a_l, a_{l + 1}, ..., a_r]. | Input: ['5 5', '1 2 3 4 5', '3 2', '4 2', '3 1', '2 1', '2 2', ''] Output:['29', '23', '35', '25', '35', ''] | [
3
] |
Stanley defines the beauty of an array a of length n, which contains non-negative integers, as follows: \sum\limits_{i = 1}^{n} <=ft \lfloor \frac{a_{i}}{k} \right \rfloor, which means that we divide each element by k, round it down, and sum up the resulting values.Stanley told Sam the integer k and asked him to find an array a of n non-negative integers, such that the beauty is equal to b and the sum of elements is equal to s. Help Sam β find any of the arrays satisfying the conditions above. | Input: ['81 6 3 1003 6 3 123 6 3 195 4 7 385 4 7 8099978 1000000000 100000000 10000000000000000001 1 0 04 1000000000 1000000000 1000000000000000000'] Output:['-1', '-1', '0 0 19', '0 3 3 3 29', '-1', '-1', '0', '0 0 0 1000000000000000000', ''] | [
2,
3
] |
Stanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with n rows and m columns. Stanley and Megan can move to an adjacent cell using 1 unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use 1 unit of power to teleport to any other cell with a portal, including Megan's starting cell.They decided to split up: Stanley will go from the upper-left cell (cell with coordinates (1, 1)) to the lower-right cell (cell with coordinates (n, m)), whilst Megan needs to get from the lower-left cell (cell with coordinates (n, 1)) to the upper-right cell (cell with coordinates (1, m)).What is the minimum total energy needed for them both to do that?Note that they can choose the time they move. Time does not affect energy. | Input: ['77 55 71 1100000 10000057 2281 55 1'] Output:['15', '15', '0', '299998', '340', '5', '5', ''] | [
2,
3
] |
You are given a rooted tree. It contains n vertices, which are numbered from 1 to n. The root is the vertex 1.Each edge has two positive integer values. Thus, two positive integers a_j and b_j are given for each edge.Output n-1 numbers r_2, r_3, ..., r_n, where r_i is defined as follows.Consider the path from the root (vertex 1) to i (2 <= i <= n). Let the sum of the costs of a_j along this path be A_i. Then r_i is equal to the length of the maximum prefix of this path such that the sum of b_j along this prefix does not exceed A_i. Example for n=9. The blue color shows the costs of a_j, and the red color shows the costs of b_j. Consider an example. In this case: r_2=0, since the path to 2 has an amount of a_j equal to 5, only the prefix of this path of length 0 has a smaller or equal amount of b_j; r_3=3, since the path to 3 has an amount of a_j equal to 5+9+5=19, the prefix of length 3 of this path has a sum of b_j equal to 6+10+1=17 ( the number is 17 <= 19); r_4=1, since the path to 4 has an amount of a_j equal to 5+9=14, the prefix of length 1 of this path has an amount of b_j equal to 6 (this is the longest suitable prefix, since the prefix of length 2 already has an amount of b_j equal to 6+10=16, which is more than 14); r_5=2, since the path to 5 has an amount of a_j equal to 5+9+2=16, the prefix of length 2 of this path has a sum of b_j equal to 6+10=16 (this is the longest suitable prefix, since the prefix of length 3 already has an amount of b_j equal to 6+10+1=17, what is more than 16); r_6=1, since the path up to 6 has an amount of a_j equal to 2, the prefix of length 1 of this path has an amount of b_j equal to 1; r_7=1, since the path to 7 has an amount of a_j equal to 5+3=8, the prefix of length 1 of this path has an amount of b_j equal to 6 (this is the longest suitable prefix, since the prefix of length 2 already has an amount of b_j equal to 6+3=9, which is more than 8); r_8=2, since the path up to 8 has an amount of a_j equal to 2+4=6, the prefix of length 2 of this path has an amount of b_j equal to 1+3=4; r_9=3, since the path to 9 has an amount of a_j equal to 2+4+1=7, the prefix of length 3 of this path has a sum of b_j equal to 1+3+3=7. | Input: ['491 5 64 5 12 9 104 2 11 2 12 3 36 4 38 1 341 1 1002 1 13 101 141 100 12 1 13 1 101101 1 42 3 52 5 13 4 33 1 55 3 55 2 11 3 26 2 1'] Output:['0 3 1 2 1 1 2 3 ', '0 0 3 ', '1 2 2 ', '0 1 2 1 1 2 2 1 1 ', ''] | [
4
] |
You are given an array of n integers a_1, a_2, ..., a_nYou can apply the following operation an arbitrary number of times: select an index i (1 <= i <= n) and replace the value of the element a_i with the value a_i + (a_i \bmod 10), where a_i \bmod 10 is the remainder of the integer dividing a_i by 10. For a single index (value i), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of a_i is taken into account each time. For example, if a_i=47 then after the first operation we get a_i=47+7=54, and after the second operation we get a_i=54+4=58.Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.For example, you have an array [6, 11]. Let's apply this operation to the first element of the array. Let's replace a_1 = 6 with a_1 + (a_1 \bmod 10) = 6 + (6 \bmod 10) = 6 + 6 = 12. We get the array [12, 11]. Then apply this operation to the second element of the array. Let's replace a_2 = 11 with a_2 + (a_2 \bmod 10) = 11 + (11 \bmod 10) = 11 + 1 = 12. We get the array [12, 12]. Thus, by applying 2 operations, you can make all elements of an array equal. | Input: ['1026 1132 18 2255 10 5 10 541 2 4 824 5393 96 102240 6250 30222 4421 5'] Output:['Yes', 'No', 'Yes', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'No', ''] | [
0,
3
] |
You are given some text t and a set of n strings s_1, s_2, ..., s_n. In one step, you can choose any occurrence of any string s_i in the text t and color the corresponding characters of the text in red. For example, if t=\texttt{bababa} and s_1=\texttt{ba}, s_2=\texttt{aba}, you can get t=\color{red}{\texttt{ba}}\texttt{baba}, t=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba} or t=\texttt{bab}\color{red}{\texttt{aba}} in one step.You want to color all the letters of the text t in red. When you color a letter in red again, it stays red.In the example above, three steps are enough: Let's color t[2 ... 4]=s_2=\texttt{aba} in red, we get t=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba}; Let's color t[1 ... 2]=s_1=\texttt{ba} in red, we get t=\color{red}{\texttt{baba}}\texttt{ba}; Let's color t[4 ... 6]=s_2=\texttt{aba} in red, we get t=\color{red}{\texttt{bababa}}. Each string s_i can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.Determine the minimum number of steps needed to color all letters t in red and how to do it. If it is impossible to color all letters of the text t in red, output -1. | Input: ['6bababa2baabacaba2bacacababacabaca3ababacacabaca3acbcodeforces4defcodeefoforcesaaaabbbbcccceeee4eeeeccccaaaabbbb'] Output:['3', '2 2', '1 1', '2 4', '-1', '4', '1 1', '2 6', '3 3', '3 7', '4', '3 1', '1 2', '2 3', '1 4', '2', '4 5', '2 1', '4', '3 1', '4 5', '2 9', '1 13'] | [
0,
2
] |
Find the minimum number with the given sum of digits s such that all digits in it are distinct (i.e. all digits are unique).For example, if s=20, then the answer is 389. This is the minimum number in which all digits are different and the sum of the digits is 20 (3+8+9=20).For the given s print the required number. | Input: ['42084510'] Output:['389', '8', '123456789', '19', ''] | [
2
] |
Polycarp was presented with some sequence of integers a of length n (1 <= a_i <= n). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he can: remove the first (leftmost) element of the sequence. For example, in one move, the sequence [3, 1, 4, 3] will produce the sequence [1, 4, 3], which consists of different numbers.Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence a, after removing which all values in the sequence will be unique. | Input: ['543 1 4 351 1 1 1 11166 5 4 3 2 171 2 1 7 1 2 1'] Output:['1', '4', '0', '0', '5', ''] | [
2
] |
Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do n things, each at a certain time. For each of these things, he has an alarm clock set, the i-th of them is triggered on h_i hours m_i minutes every day (0 <= h_i < 24, 0 <= m_i < 60). Vlad uses the 24-hour time format, so after h=12, m=59 comes h=13, m=0 and after h=23, m=59 comes h=0, m=0.This time Vlad went to bed at H hours M minutes (0 <= H < 24, 0 <= M < 60) and asks you to answer: how much he will be able to sleep until the next alarm clock.If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length 0. | Input: ['31 6 138 03 6 012 3014 456 02 23 3520 1510 30'] Output:['1 47', '0 0', '10 55', ''] | [
3
] |
My orzlers, we can optimize this problem from O(S^3) to O<=ft(T^\frac{5}{9}\right)!β Spyofgame, founder of Orzlim religionA long time ago, Spyofgame invented the famous array a (1-indexed) of length n that contains information about the world and life. After that, he decided to convert it into the matrix b (0-indexed) of size (n + 1) * (n + 1) which contains information about the world, life and beyond.Spyofgame converted a into b with the following rules. b_{i,0} = 0 if 0 <=q i <=q n; b_{0,i} = a_{i} if 1 <=q i <=q n; b_{i,j} = b_{i,j-1} \oplus b_{i-1,j} if 1 <=q i, j <=q n. Here \oplus denotes the bitwise XOR operation.Today, archaeologists have discovered the famous matrix b. However, many elements of the matrix has been lost. They only know the values of b_{i,n} for 1 <=q i <=q n (note that these are some elements of the last column, not the last row).The archaeologists want to know what a possible array of a is. Can you help them reconstruct any array that could be a? | Input: ['3', '0 2 1', ''] Output:['1 2 3 ', ''] | [
3
] |
You are given a square matrix A of size n * n whose elements are integers. We will denote the element on the intersection of the i-th row and the j-th column as A_{i,j}.You can perform operations on the matrix. In each operation, you can choose an integer k, then for each index i (1 <=q i <=q n), swap A_{i, k} with A_{k, i}. Note that cell A_{k, k} remains unchanged.For example, for n = 4 and k = 3, this matrix will be transformed like this: The operation k = 3 swaps the blue row with the green column. You can perform this operation any number of times. Find the lexicographically smallest matrix^ you can obtain after performing arbitrary number of operations.{}^ For two matrices A and B of size n * n, let a_{(i-1) \cdot n + j} = A_{i,j} and b_{(i-1) \cdot n + j} = B_{i,j}. Then, the matrix A is lexicographically smaller than the matrix B when there exists an index i (1 <=q i <=q n^2) such that a_i < b_i and for all indices j such that 1 <=q j < i, a_j = b_j. | Input: ['232 1 22 1 21 1 243 3 1 21 1 3 13 2 3 22 3 3 1'] Output:['2 1 1', '2 1 1', '2 2 2', '3 1 1 2', '3 1 2 1', '3 3 3 3', '2 3 2 1', ''] | [
2
] |
This is an interactive problem.There was a tournament consisting of 2^n contestants. The 1-st contestant competed with the 2-nd, the 3-rd competed with the 4-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament.You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers a and b, which are the indices of two contestants. The jury will return 1 if a won more matches than b, 2 if b won more matches than a, or 0 if their number of wins was equal.Find the winner in no more than <=ft \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil queries. Here \lceil x \rceil denotes the value of x rounded up to the nearest integer.Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries. | Input: ['1', '3', '', '2', '', '0', '', '2', '', ''] Output:['', '', '? 1 4', '', '? 1 6', '', '? 5 7', '', '! 7'] | [
2
] |
A \mathbf{0}-indexed array a of size n is called good if for all valid indices i (0 <= i <= n-1), a_i + i is a perfect square^.Given an integer n. Find a permutation^\ddagger p of [0,1,2,...,n-1] that is good or determine that no such permutation exists.^ An integer x is said to be a perfect square if there exists an integer y such that x = y^2.^\ddagger An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3]. | Input: ['3347'] Output:['1 0 2 ', '0 3 2 1 ', '1 0 2 6 5 4 3 ', ''] | [
3
] |
You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point (x, y), you can: go left, and move to (x - 1, y), or go right, and move to (x + 1, y), or go up, and move to (x, y + 1), or go down, and move to (x, y - 1). There are n boxes on this plane. The i-th box has coordinates (x_i,y_i). It is guaranteed that the boxes are either on the x-axis or the y-axis. That is, either x_i=0 or y_i=0.You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point (0,0). | Input: ['340 -21 0-1 00 230 2-3 00 -110 0'] Output:['12', '12', '0', ''] | [
2
] |
β What is my mission? β To count graph diameters.You and Your SubmissionA tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.You are given a weighted tree with n vertices, each edge has a weight of 1. Let L be the set of vertices with degree equal to 1. You have to answer q independent queries. In the i-th query: You are given a positive integer x_i. For all u,v \in L such that u < v, add edge (u, v) with weight x_i to the graph (initially the given tree). Find the diameter of the resulting graph. The diameter of a graph is equal to \max\limits_{1 <= u < v <= n}{\operatorname{d}(u, v)}, where \operatorname{d}(u, v) is the length of the shortest path between vertex u and vertex v. | Input: ['4', '1 2 2', '4', '1 2 3 4', ''] Output:['1 2 2 2 ', ''] | [
4
] |