problems
stringlengths
691
4.89k
Andi and Budi were given an assignment to tidy up their bookshelf of n books. Each book is represented by the book title β€” a string s_i numbered from 1 to n, each with length m. Andi really wants to sort the book lexicographically ascending, while Budi wants to sort it lexicographically descending. Settling their figh...
Mr. Chanek lives in a city represented as a plane. He wants to build an amusement park in the shape of a circle of radius r. The circle must touch the origin (point (0, 0)). There are n bird habitats that can be a photo spot for the tourists in the park. The i-th bird habitat is at point p_i = (x_i, y_i). Find the m...
Denote a cyclic sequence of size n as an array s such that s_n is adjacent to s_1. The segment s[r, l] where l < r is the concatenation of s[r, n] and s[1, l]. You are given an array a consisting of n integers. Define b as the cyclic sequence obtained from concatenating m copies of a. Note that b has size n β‹… m. You ...
Mr. Chanek has an integer represented by a string s. Zero or more digits have been erased and are denoted by the character _. There are also zero or more digits marked by the character X, meaning they're the same digit. Mr. Chanek wants to count the number of possible integer s, where s is divisible by 25. Of course, ...
There is a city park represented as a tree with n attractions as its vertices and n - 1 rails as its edges. The i-th attraction has happiness value a_i. Each rail has a color. It is either black if t_i = 0, or white if t_i = 1. Black trains only operate on a black rail track, and white trains only operate on a white r...
Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says. Define an array b (0 ≀ b_i < k) with n integers. While there exists a pair (i, j) such that b_i β‰  b_j, do the following operation: * Randomly pick a number i satisfying 0 ≀ i < n. Note that each number i has a...
Mr. Chanek has an array a of n integers. The prettiness value of a is denoted as: $$$βˆ‘_{i=1}^{n} {βˆ‘_{j=1}^{n} {\gcd(a_i, a_j) β‹… \gcd(i, j)}}$$$ where \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y. In other words, the prettiness value of an array a is the total sum of \gcd(a_i, a_j) β‹… \gcd(...
The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string a of length n. His favorite nephew has another binary string b of length m (m ≀ n). Mr. Chanek's nephew loves the non-negative integer k. His nephew wants exactly k occurre...
Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions. The map of the labyrinth forms a tree with n rooms numbered from 1 to n and n - 1 tunnels connecting them such that it is possible to travel between each pair of rooms throug...
Mr. Chanek has a new game called Dropping Balls. Initially, Mr. Chanek has a grid a of size n Γ— m Each cell (x,y) contains an integer a_{x,y} denoting the direction of how the ball will move. * a_{x,y}=1 β€” the ball will move to the right (the next cell is (x, y + 1)); * a_{x,y}=2 β€” the ball will move to the bott...
Mr. Chanek wants to knit a batik, a traditional cloth from Indonesia. The cloth forms a grid a with size n Γ— m. There are k colors, and each cell in the grid can be one of the k colors. Define a sub-rectangle as an ordered pair of two cells ((x_1, y_1), (x_2, y_2)), denoting the top-left cell and bottom-right cell (in...
Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequen...
Mr. Chanek's city can be represented as a plane. He wants to build a housing complex in the city. There are some telephone poles on the plane, which is represented by a grid a of size (n + 1) Γ— (m + 1). There is a telephone pole at (x, y) if a_{x, y} = 1. For each point (x, y), define S(x, y) as the square of the Euc...
Casimir has a string s which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions: * he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent); * or he can ...
The new generation external memory contains an array of integers a[1 … n] = [a_1, a_2, …, a_n]. This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back into the same ...
Casimir has a rectangular piece of paper with a checkered field of size n Γ— m. Initially, all cells of the field are white. Let us denote the cell with coordinates i vertically and j horizontally by (i, j). The upper left cell will be referred to as (1, 1) and the lower right cell as (n, m). Casimir draws ticks of di...
An important meeting is to be held and there are exactly n people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting. Each person has limited sociability. The sociability of the i-th person is a non-negative integer a_...
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array a[1 … n] = [a_1, a_2, …, a_n]. Let us consider an empty [deque](https://tinyurl.com/pfeucbux) (double-ended queue). A deque is a data structure that supports adding el...
You are given an array a[0 … n - 1] = [a_0, a_1, …, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one. In one step, the array a is replaced by another array of length n according to the following rules: 1. First, a new array a^{...
You are given n lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate 0. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the ...
CQXYM found a rectangle A of size n Γ— m. There are n rows and m columns of blocks. Each block of the rectangle is an obsidian block or empty. CQXYM can change an obsidian block to an empty block or an empty block to an obsidian block in one operation. A rectangle M size of a Γ— b is called a portal if and only if it sa...
Let c_1, c_2, …, c_n be a permutation of integers 1, 2, …, n. Consider all subsegments of this permutation containing an integer x. Given an integer m, we call the integer x good if there are exactly m different values of maximum on these subsegments. Cirno is studying mathematics, and the teacher asks her to count th...
Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains. There are n models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following m days, one train will be added, or one t...
Alice has an integer sequence a of length n and all elements are different. She will choose a subsequence of a of length m, and defines the value of a subsequence a_{b_1},a_{b_2},…,a_{b_m} as $$$βˆ‘_{i = 1}^m (m β‹… a_{b_i}) - βˆ‘_{i = 1}^m βˆ‘_{j = 1}^m f(min(b_i, b_j), max(b_i, b_j)), where f(i, j) denotes \min(a_i, a_{i + 1...
Because the railway system in Gensokyo is often congested, as an enthusiastic engineer, Kawasiro Nitori plans to construct more railway to ease the congestion. There are n stations numbered from 1 to n and m two-way railways in Gensokyo. Every two-way railway connects two different stations and has a positive integer ...
XYMXYM and CQXYM will prepare n problems for Codeforces. The difficulty of the problem i will be an integer a_i, where a_i β‰₯ 0. The difficulty of the problems must satisfy a_i+a_{i+1}<m (1 ≀ i < n), and a_1+a_n<m, where m is a fixed integer. XYMXYM wants to know how many plans of the difficulty of the problems there ar...
CQXYM is counting permutations length of 2n. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). A...
CQXYM wants to create a connected undirected graph with n nodes and m edges, and the diameter of the graph must be strictly less than k-1. Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one ed...
Luntik has decided to try singing. He has a one-minute songs, b two-minute songs and c three-minute songs. He wants to distribute all songs into two concerts such that every song should be included to exactly one concert. He wants to make the absolute difference of durations of the concerts as small as possible. The d...
Luntik came out for a morning stroll and found an array a of length n. He calculated the sum s of the elements of the array (s= βˆ‘_{i=1}^{n} a_i). Luntik calls a subsequence of the array a nearly full if the sum of the numbers in that subsequence is equal to s-1. Luntik really wants to know the number of nearly full su...
Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s of length n. Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is ...
Vupsen and Pupsen were gifted an integer array. Since Vupsen doesn't like the number 0, he threw away all numbers equal to 0 from the array. As a result, he got an array a of length n. Pupsen, on the contrary, likes the number 0 and he got upset when he saw the array without zeroes. To cheer Pupsen up, Vupsen decided ...
Pchelyonok decided to give Mila a gift. Pchelenok has already bought an array a of length n, but gifting an array is too common. Instead of that, he decided to gift Mila the segments of that array! Pchelyonok wants his gift to be beautiful, so he decided to choose k non-overlapping segments of the array [l_1,r_1], [l_...
This is an easier version of the problem with smaller constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all in...
This is a harder version of the problem with bigger constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all inte...
Kuzya started going to school. He was given math homework in which he was given an array a of length n and an array of symbols b of length n, consisting of symbols '*' and '/'. Let's denote a path of calculations for a segment [l; r] (1 ≀ l ≀ r ≀ n) in the following way: * Let x=1 initially. For every i from l to ...
A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person. You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of n (n β‰₯ 3) positive distinct integers (i.e. different, no duplicates are allowed). Find the largest subset (i.e. having th...
Lord Omkar would like to have a tree with n nodes (3 ≀ n ≀ 10^5) and has asked his disciples to construct the tree. However, Lord Omkar has created m (1 ≀ m < n) restrictions to ensure that the tree will be as heavenly as possible. A tree with n nodes is an connected undirected graph with n nodes and n-1 edges. Note ...
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in ...
It turns out that the meaning of life is a permutation p_1, p_2, …, p_n of the integers 1, 2, …, n (2 ≀ n ≀ 100). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries. A query consists of an array a_1, a_2, …, a_n of integers between 1 and n. a is not required ...
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang. Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of n nodes with m edges. You also have q queries. Each query consists...
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right? You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city. Liyue can be represented ...
El Psy Kongroo. Omkar is watching Steins;Gate. In Steins;Gate, Okabe Rintarou needs to complete n tasks (1 ≀ n ≀ 2 β‹… 10^5). Unfortunately, he doesn't know when he needs to complete the tasks. Initially, the time is 0. Time travel will now happen according to the following rules: * For each k = 1, 2, …, n, Okabe w...
Omkar is hosting tours of his country, Omkarland! There are n cities in Omkarland, and, rather curiously, there are exactly n-1 bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network. Every city has an enjoyment value e. Each r...
Omkar is creating a mosaic using colored square tiles, which he places in an n Γ— n grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells. A completed mosaic will be a mastapeece if and only if each tile is adjace...
Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given n days from Petya's live and you have to determine what happened with his flower in the end. The flower grows as follows: * If the flower isn't watered for two days in a row, it dies. * If the flower ...
You are given an array a of length n. Let's define the eversion operation. Let x = a_n. Then array a is partitioned into two parts: left and right. The left part contains the elements of a that are not greater than x (≀ x). The right part contains the elements of a that are strictly greater than x (> x). The order of ...
A total of n depots are located on a number line. Depot i lies at the point x_i for 1 ≀ i ≀ n. You are a salesman with n bags of goods, attempting to deliver one bag to each of the n depots. You and the n bags are initially at the origin 0. You can carry up to k bags at a time. You must collect the required number of ...
Petya has an array of integers a_1, a_2, …, a_n. He only likes sorted arrays. Unfortunately, the given array could be arbitrary, so Petya wants to sort it. Petya likes to challenge himself, so he wants to sort array using only 3-cycles. More formally, in one operation he can pick 3 pairwise distinct indices i, j, and ...
Petya has a rooted tree with an integer written on each vertex. The vertex 1 is the root. You are to answer some questions about the tree. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a node v is the next vertex on the shortest path from v to the root. ...
You are given an array of n positive integers a_1, a_2, …, a_n. Your task is to calculate the number of arrays of n positive integers b_1, b_2, …, b_n such that: * 1 ≀ b_i ≀ a_i for every i (1 ≀ i ≀ n), and * b_i β‰  b_{i+1} for every i (1 ≀ i ≀ n - 1). The number of such arrays can be very large, so print it ...
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese). You are given an integer n. You need to find two integers l and r such that -10^{18} ≀ l < r ≀ 10^{18} and l + (l + 1) + … + (r - 1) + r = n. Input The first line contains a single integ...
Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers. Let's call a positive number special if it can be written as a sum of different non-negative powers of n. For example, for n = 4 number 17 is special, because...
Theofanis has a string s_1 s_2 ... s_n and a character c. He wants to make all characters of the string equal to c using the minimum number of operations. In one operation he can choose a number x (1 ≀ x ≀ n) and for every position i, where i is not divisible by x, replace s_i with c. Find the minimum number of oper...
Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus). In each game, Theofanis plays with n other players. Since they all have the same name, they are numbered from 1 to n. The playe...
It is the easy version of the problem. The difference is that in this version, there are no nodes with already chosen colors. Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem? You have a perfect binary tree of 2^k...
It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors. Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem? You have a perfect binary tree of 2^k - ...
Theofanis decided to visit his uncle's farm. There are s animals and n animal pens on the farm. For utility purpose, animal pens are constructed in one row. Uncle told Theofanis that a farm is lucky if you can distribute all animals in all pens in such a way that there are no empty pens and there is at least one conti...
Monocarp is playing a computer game. Now he wants to complete the first level of this game. A level is a rectangular grid of 2 rows and n columns. Monocarp controls a character, which starts in cell (1, 1) β€” at the intersection of the 1-st row and the 1-st column. Monocarp's character can move from one cell to anothe...
n students attended the first meeting of the Berland SU programming course (n is even). All students will be divided into two groups. Each group will be attending exactly one lesson each week during one of the five working days (Monday, Tuesday, Wednesday, Thursday and Friday), and the days chosen for the groups must b...
Monocarp has got an array a consisting of n integers. Let's denote k as the mathematic mean of these elements (note that it's possible that k is not an integer). The mathematic mean of an array of n elements is the sum of elements divided by the number of these elements (i. e. sum divided by n). Monocarp wants to de...
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams. Monocarp has n problems that none of his students have seen yet. The i-th problem has a topic a_i (an integer from 1 to n) and a difficulty b_i (an integer from 1 to n). All...
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right. Each cell of the matrix can be either free or locked. Let's call a path in the matrix a staircase if it: * starts and ends in the free cell; * visits only free cells; * ha...
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: * bracket sequen...
Let's call a positive integer good if there is no digit 0 in its decimal representation. For an array of a good numbers a, one found out that the sum of some two neighboring elements is equal to x (i.e. x = a_i + a_{i + 1} for some i). x had turned out to be a good number as well. Then the elements of the array a wer...
You are given an array A of length N weights of masses A_1, A_2...A_N. No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order A_1,...,A_N. There is also a string S consisting of characters "L" and "R", meaning that after putting the...
Alice and Bob always had hard time choosing restaurant for the dinner. Previously they performed Eenie Meenie Miney Mo game, but eventually as their restaurant list grew, they had to create a new game. This new game starts as they write restaurant names on N cards and align the cards in one line. Before the game begins...
Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players. Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the gam...
There are N bubbles in a coordinate plane. Bubbles are so tiny that it can be assumed that each bubble is a point (X_i, Y_i). Q Bubble Cup finalists plan to play with the bubbles. Each finalist would link to use infinitely long Bubble Cup stick to pop some bubbles. The i-th finalist would like to place the stick in t...
You are given two integer arrays of length N, A1 and A2. You are also given Q queries of 4 types: 1 k l r x: set Ak_i:=min(Ak_i, x) for each l ≀ i ≀ r. 2 k l r x: set Ak_i:=max(Ak_i, x) for each l ≀ i ≀ r. 3 k l r x: set Ak_i:=Ak_i+x for each l ≀ i ≀ r. 4 l r: find the (βˆ‘_{i=l}^r F(A1_i+A2_i)) \% (10^9+7) where F(...
In the year 2420 humans have finally built a colony on Mars thanks to the work of Elon Tusk. There are 10^9+7 cities arranged in a circle in this colony and none of them are connected yet. Elon Tusk wants to connect some of those cities using only roads of the same size in order to lower the production cost of those ro...
You are given N points on an infinite plane with the Cartesian coordinate system on it. N-1 points lay on one line, and one point isn't on that line. You are on point K at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. What is the min...
This is an interactive problem! As part of your contribution in the Great Bubble War, you have been tasked with finding the newly built enemy fortress. The world you live in is a giant 10^9 Γ— 10^9 grid, with squares having both coordinates between 1 and 10^9. You know that the enemy base has the shape of a rectangle...
You are given an undirected graph of N nodes and M edges, E_1, E_2, ... E_M. A connected graph is a cactus if each of it's edges belogs to at most one simple cycle. A graph is a desert if each of it's connected components is a cactus. Find the number of pairs (L, R), (1 ≀ L ≀ R ≀ M) such that, if we delete all the e...
Bob really likes playing with arrays of numbers. That's why for his birthday, his friends bought him a really interesting machine – an array beautifier. The array beautifier takes an array A consisting of N integers, and it outputs a new array B of length N that it constructed based on the array given to it. The arra...
Alice and Bob are playing a game. They are given an array A of length N. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the s...
On the great island of Baltia, there live N people, numbered from 1 to N. There are exactly M pairs of people that are friends with each other. The people of Baltia want to organize a successful party, but they have very strict rules on what a party is and when the party is successful. On the island of Baltia, a party ...
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the ...
You are given array a_1, a_2, …, a_n, consisting of non-negative integers. Let's define operation of "elimination" with integer parameter k (1 ≀ k ≀ n) as follows: * Choose k distinct array indices 1 ≀ i_1 < i_2 < … < i_k ≀ n. * Calculate x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ … ~ \& ~ a_{i_k}, where \& denotes the [...
Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of n meters depth. Now Gorf is on the bottom of the well and has a long way up. The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf...
You are given two arrays of integers a_1, a_2, …, a_n and b_1, b_2, …, b_m. You need to insert all elements of b into a in an arbitrary way. As a result you will get an array c_1, c_2, …, c_{n+m} of size n + m. Note that you are not allowed to change the order of elements in a, while you can insert elements of b at a...
A group of n alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer d. Each alpinist can be described by two integers s and a, where s is his skill of climbing mountains and a is his neatness. An alpinist of skill level s is able to climb a...
Students of one unknown college don't have PE courses. That's why q of them decided to visit a gym nearby by themselves. The gym is open for n days and has a ticket system. At the i-th day, the cost of one ticket is equal to a_i. You are free to buy more than one ticket per day. You can activate a ticket purchased at ...
Integers from 1 to n (inclusive) were sorted lexicographically (considering integers as strings). As a result, array a_1, a_2, ..., a_n was obtained. Calculate value of (βˆ‘_{i = 1}^n ((i - a_i) mod 998244353)) mod 10^9 + 7. x mod y here means the remainder after division x by y. This remainder is always non-negative a...
For two positive integers l and r (l ≀ r) let c(l, r) denote the number of integer pairs (i, j) such that l ≀ i ≀ j ≀ r and \operatorname{gcd}(i, j) β‰₯ l. Here, \operatorname{gcd}(i, j) is the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers i and j. YouKn0wWho has two ...
A sequence of integers b_1, b_2, …, b_m is called good if max(b_1, b_2, …, b_m) β‹… min(b_1, b_2, …, b_m) β‰₯ b_1 + b_2 + … + b_m. A sequence of integers a_1, a_2, …, a_n is called perfect if every non-empty subsequence of a is good. YouKn0wWho has two integers n and M, M is prime. Help him find the number, modulo M, of ...
It was October 18, 2017. Shohag, a melancholic soul, made a strong determination that he will pursue Competitive Programming seriously, by heart, because he found it fascinating. Fast forward to 4 years, he is happy that he took this road. He is now creating a contest on Codeforces. He found an astounding problem but h...
Shohag has an integer sequence a_1, a_2, …, a_n. He can perform the following operation any number of times (possibly, zero): * Select any positive integer k (it can be different in different operations). * Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two e...
YouKn0wWho has an integer sequence a_1, a_2, … a_n. Now he will split the sequence a into one or more consecutive subarrays so that each element of a belongs to exactly one subarray. Let k be the number of resulting subarrays, and h_1, h_2, …, h_k be the lengths of the longest increasing subsequences of corresponding s...
YouKn0wWho has an integer sequence a_1, a_2, …, a_n. He will perform the following operation until the sequence becomes empty: select an index i such that 1 ≀ i ≀ |a| and a_i is not divisible by (i + 1), and erase this element from the sequence. Here |a| is the length of sequence a at the moment of operation. Note that...
YouKn0wWho has two even integers x and y. Help him to find an integer n such that 1 ≀ n ≀ 2 β‹… 10^{18} and n mod x = y mod n. Here, a mod b denotes the remainder of a after division by b. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints. Inp...
For an array b of n integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make b non-decreasing: * Select an index i such that 1 ≀ i ≀ |b|, where |b| is the current length of b. * Replace b_i with two elements x and y such that x ...
You are given a string s of length n consisting of characters a and/or b. Let \operatorname{AB}(s) be the number of occurrences of string ab in s as a substring. Analogically, \operatorname{BA}(s) is the number of occurrences of ba in s as a substring. In one step, you can choose any index i and replace s_i with char...
Berland State University has received a new update for the operating system. Initially it is installed only on the 1-st computer. Update files should be copied to all n computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them usin...
In Berland, n different types of banknotes are used. Banknotes of the i-th type have denomination 10^{a_i} burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly 1. Let's denote f(s) as the minimum number of banknotes required to represent exactly s burles. For exa...
You are given a matrix, consisting of n rows and m columns. The j-th cell of the i-th row contains an integer a_{ij}. First, you have to color each row of the matrix either red or blue in such a way that at least one row is colored red and at least one row is colored blue. Then, you have to choose an integer k (1 ≀ k...
There are n heroes fighting in the arena. Initially, the i-th hero has a_i health points. The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals 1 damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than 1 at the end of t...
You are given a tree consisting of n vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 1. You have to process q queries. In each query, you are given a vertex of the tree v and an integer k. To process a query, you may delete any vertices from the tree in an...
You are given a keyboard that consists of 26 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter. You have to type the word s on this keyboard. It also consists only of lowercase Latin letters. To type a word, you need to type all its letters...
The grasshopper is located on the numeric axis at the point with coordinate x_0. Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate x with a distance d to the left moves the grasshopper to a point with a coordinate x - d, while jumping to the righ...