question
large_stringlengths
265
13.2k
Solve the programming task below in a Python markdown code block. Given any integer x, Aoki can do the operation below. Operation: Replace x with the absolute difference of x and K. You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more ti...
Solve the programming task below in a Python markdown code block. Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount. You are given an array a of length n and an integer c. The value of some array b of length k is the sum of its elements except for the $\...
Solve the programming task below in a Python markdown code block. Consider a sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2...
Solve the programming task below in a Python markdown code block. <image> A directed acyclic graph (DAG) can be used to represent the ordering of tasks. Tasks are represented by vertices and constraints where one task can begin before another, are represented by edges. For example, in the above example, you can unde...
Solve the programming task below in a Python markdown code block. Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and want...
Solve the programming task below in a Python markdown code block. There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on ...
Solve the programming task below in a Python markdown code block. During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed. The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so t...
Solve the programming task below in a Python markdown code block. You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers: In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is n...
Solve the programming task below in a Python markdown code block. There are N squares in a row. The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty. Aohashi would like to fill the empty squares with integers so that the following condition is satisfied: *...
Solve the programming task below in a Python markdown code block. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Let next(x) be the minimum lucky ...
Solve the programming task below in a Python markdown code block. A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares...
Solve the programming task below in a Python markdown code block. One day, Ikta, an elementary school student, received a piece of paper with mathematical formulas from his grandfather. Apparently, the grandfather will give you as much money as the answer to the formula. Ikta has only learned addition, subtraction, an...
Solve the programming task below in a Python markdown code block. Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping s...
Solve the programming task below in a Python markdown code block. Example Input 6 2 3 1 1 4 2 Output Yes Yes Yes No Yes No Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
Solve the programming task below in a Python markdown code block. A famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradi...
Solve the programming task below in a Python markdown code block. It's now the season of TAKOYAKI FESTIVAL! This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i. As is commonly known, when you eat two takoyaki of deliciousness x and y t...
Solve the programming task below in a Python markdown code block. There are four points: $A(x_1, y_1)$, $B(x_2, y_2)$, $C(x_3, y_3)$, and $D(x_4, y_4)$. Write a program which determines whether the line $AB$ and the line $CD$ are parallel. If those two lines are parallel, your program should prints "YES" and if not pr...
Solve the programming task below in a Python markdown code block. # Task Suppose there are `n` people standing in a circle and they are numbered 1 through n in order. Person 1 starts off with a sword and kills person 2. He then passes the sword to the next person still standing, in this case person 3. Person 3 th...
Solve the programming task below in a Python markdown code block. To introduce the problem think to my neighbor who drives a tanker truck. The level indicator is down and he is worried because he does not know if he will be able to make deliveries. We put the truck on a horizontal ground and measured the height of t...
Solve the programming task below in a Python markdown code block. You're given a string of dominos. For each slot, there are 3 options: * "|" represents a standing domino * "/" represents a knocked over domino * " " represents a space where there is no domino For example: ```python "||| ||||//| |/" ``` Wh...
Solve the programming task below in a Python markdown code block. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented b...
Solve the programming task below in a Python markdown code block. Problem Let $ f (x) $ be the sum of each digit when the non-negative integer $ x $ is expressed in binary. Given a positive integer $ N $, output the largest of $ f (0) $, $ f (1) $, ..., $ f (N) $. Example of calculating the function $ f (5) $: When...
Solve the programming task below in a Python markdown code block. All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position x_{i} — a coordinate on the Ox axis. No two cities are located at a single point. Lineland residents love to send letters to each other. A per...
Solve the programming task below in a Python markdown code block. You are given an array of positive integers $a = [a_0, a_1, \dots, a_{n - 1}]$ ($n \ge 2$). In one step, the array $a$ is replaced with another array of length $n$, in which each element is the greatest common divisor (GCD) of two neighboring elements ...
Solve the programming task below in a Python markdown code block. There is a grid with H rows and W columns. The square at the i-th row and j-th column contains a string S_{i,j} of length 5. The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from `A...
Solve the programming task below in a Python markdown code block. In some other world, today is December D-th. Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22. -----Constraints----- - 22 \leq D \leq 25 - D is an integer. ----...
Solve the programming task below in a Python markdown code block. Your company’s next product will be a new game, which is a three-dimensional variant of the classic game “Tic-Tac-Toe”. Two players place balls in a three-dimensional space (board), and try to make a sequence of a certain length. People believe that it...
Solve the programming task below in a Python markdown code block. This is the easy version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming ...
Solve the programming task below in a Python markdown code block. Read problems statements in Mandarin Chinese and Russian. You are given two positive integers – A and B. You have to check whether A is divisible by all the prime divisors of B. ------ Input ------ The first line of the input contains an integer T...
Solve the programming task below in a Python markdown code block. The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation $p_i$ of numbe...
Solve the programming task below in a Python markdown code block. # Task Given an integer `product`, find the smallest positive integer the product of whose digits is equal to product. If there is no such integer, return -1 instead. # Example For `product = 1`, the output should be `11`; `1 x 1 = 1` (1 is not a...
Solve the programming task below in a Python markdown code block. We are still with squared integers. Given 4 integers `a, b, c, d` we form the sum of the squares of `a` and `b` and then the sum of the squares of `c` and `d`. We multiply the two sums hence a number `n` and we try to decompose `n` in a sum of two squa...
Solve the programming task below in a Python markdown code block. There is a new attraction in Singapore Zoo: The Infinite Zoo. The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $1,2,3,\ldots$. There is a directed edge from vertex $u$ to vertex $u+v$ if and only if $u\&v=v$, w...
Solve the programming task below in a Python markdown code block. Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out. **Example:** Given an input string of: ``` apples, pears # and bananas grapes ...
Solve the programming task below in a Python markdown code block. Most C/C++ programmers know about excellent opportunities that preprocessor #define directives give; but many know as well about the problems that can arise because of their careless use. In this problem we consider the following model of #define const...
Solve the programming task below in a Python markdown code block. Find the length between 2 co-ordinates. The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array: (0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ] The function must return the answer rounded to 2 decimal...
Solve the programming task below in a Python markdown code block. A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle. A simple cycle in a undirected graph is a sequence of distinct vertices v1, v2, ..., vt (t > 2), such that for any i (1 ≤ i < t) e...
Solve the programming task below in a Python markdown code block. There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marke...
Solve the programming task below in a Python markdown code block. Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on. Kyota Ootori has just...
Solve the programming task below in a Python markdown code block. Create a function named `divisors`/`Divisors` that takes an integer `n > 1` and returns an array with all of the integer's divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string '(integer) is pri...
Solve the programming task below in a Python markdown code block. ```if-not:sql Implement a function that receives two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one). ``` ```if:sql Given a database of first and last IPv4 addresses, calculate the numb...
Solve the programming task below in a Python markdown code block. This problem is same as the previous one, but has larger constraints. It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power stati...
Solve the programming task below in a Python markdown code block. The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems. Kirk has a binary string $s$ (a string which consists of z...
Solve the programming task below in a Python markdown code block. Surrounding Area Land fence English text is not available in this practice contest. Two real estate agents were boarding a passenger ship to the southern island. Blue sky, refreshing breeze ... The two enjoyed a voyage with other passengers. However,...
Solve the programming task below in a Python markdown code block. Consider some set of distinct characters $A$ and some string $S$, consisting of exactly $n$ characters, where each character is present in $A$. You are given an array of $m$ integers $b$ ($b_1 < b_2 < \dots < b_m$). You are allowed to perform the fol...
Solve the programming task below in a Python markdown code block. There are n employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote. Each of the employees belongs to one of two fractions: depubli...
Solve the programming task below in a Python markdown code block. Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his re...
Solve the programming task below in a Python markdown code block. Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles. Vasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candl...
Solve the programming task below in a Python markdown code block. Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob. Eve prepared $a_1 + a_2 + a_3 + a_4$ jokes to tell, grouped by their type: type 1: both Alice and Bob like them; type 2: Alice likes them, bu...
Solve the programming task below in a Python markdown code block. After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer. At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue b...
Solve the programming task below in a Python markdown code block. This is the easy version of this problem. The only difference between easy and hard versions is the constraints on $k$ and $m$ (in this version $k=2$ and $m=3$). Also, in this version of the problem, you DON'T NEED to output the answer by modulo. You a...
Solve the programming task below in a Python markdown code block. You are given an array $a$ of length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times: you select two different numbers in the array $a_i$ and $a_j$; you remove $i$-th ...
Solve the programming task below in a Python markdown code block. You are given n patterns p_1, p_2, ..., p_n and m strings s_1, s_2, ..., s_m. Each pattern p_i consists of k characters that are either lowercase Latin letters or wildcard characters (denoted by underscores). All patterns are pairwise distinct. Each str...
Solve the programming task below in a Python markdown code block. You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one fro...
Solve the programming task below in a Python markdown code block. Given is a positive integer N. We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K. - Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K. In how many cho...
Solve the programming task below in a Python markdown code block. During a New Year special offer the "Sudislavl Bars" offered n promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar "Mosquito Shelter". Of course, all the promocodes differ. As the "Mosquito Shelte...
Solve the programming task below in a Python markdown code block. For a given sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$ which is sorted by ascending order, find a specific value $k$ given as a query. Constraints * $1 \leq n \leq 100,000$ * $1 \leq q \leq 200,000$ * $0 \leq a_0 \leq a_1 \leq ... \leq a_{n-1} \leq 1...
Solve the programming task below in a Python markdown code block. Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good. Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three condi...
Solve the programming task below in a Python markdown code block. Polycarp knows that if the sum of the digits of a number is divisible by $3$, then the number itself is divisible by $3$. He assumes that the numbers, the sum of the digits of which is divisible by $4$, are also somewhat interesting. Thus, he considers ...
Solve the programming task below in a Python markdown code block. Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison. Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachac...
Solve the programming task below in a Python markdown code block. In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of...
Solve the programming task below in a Python markdown code block. Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, .....
Solve the programming task below in a Python markdown code block. Let's denote a function $f(x)$ in such a way: we add $1$ to $x$, then, while there is at least one trailing zero in the resulting number, we remove that zero. For example, $f(599) = 6$: $599 + 1 = 600 \rightarrow 60 \rightarrow 6$; $f(7) = 8$: $7 + 1...
Solve the programming task below in a Python markdown code block. Implement the function which should return `true` if given object is a vowel (meaning `a, e, i, o, u`), and `false` otherwise. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Encl...
Solve the programming task below in a Python markdown code block. Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th star on the pedal...
Solve the programming task below in a Python markdown code block. Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as foll...
Solve the programming task below in a Python markdown code block. A number is called powerful if it is a power of two or a factorial. In other words, the number $m$ is powerful if there exists a non-negative integer $d$ such that $m=2^d$ or $m=d!$, where $d!=1\cdot 2\cdot \ldots \cdot d$ (in particular, $0! = 1$). For...
Solve the programming task below in a Python markdown code block. Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a. Wh...
Solve the programming task below in a Python markdown code block. You're given an array $b$ of length $n$. Let's define another array $a$, also of length $n$, for which $a_i = 2^{b_i}$ ($1 \leq i \leq n$). Valerii says that every two non-intersecting subarrays of $a$ have different sums of elements. You want to dete...
Solve the programming task below in a Python markdown code block. F: Tea Party Yun decided to hold a tea party at the company. The shop sells $ N $ sets of bread, each containing $ A_1, A_2, A_3, \ dots, A_N $. Yun decided to make a sandwich by combining two breads into a pair. Yun-san is very careful, so I want t...
Solve the programming task below in a Python markdown code block. On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G. You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b. ...
Solve the programming task below in a Python markdown code block. When Mr. Kay was browsing a certain SNS as usual, the problem that "there are people who can solve IQ150 or more" came to the timeline. Mr. Kay has an IQ of over 150, so he solved the problem in an instant without even looking at it. For him, he doesn't...
Solve the programming task below in a Python markdown code block. You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected...
Solve the programming task below in a Python markdown code block. Takahashi has A cookies, and Aoki has B cookies. Takahashi will do the following action K times: - If Takahashi has one or more cookies, eat one of his cookies. - Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies. - If they both h...
Solve the programming task below in a Python markdown code block. Write a program which prints $n$-th fibonacci number for a given integer $n$. The $n$-th fibonacci number is defined by the following recursive formula: \begin{equation*} fib(n)= \left \\{ \begin{array}{ll} 1 & (n = 0) \\\ 1 & (n = 1) \\\ fib(n - 1) + ...
Solve the programming task below in a Python markdown code block. # Task You are given a decimal number `n` as a **string**. Transform it into an array of numbers (given as **strings** again), such that each number has only one nonzero digit and their sum equals n. Each number in the output array should be written ...
Solve the programming task below in a Python markdown code block. Everlasting Sa-Ga, a new, hot and very popular role-playing game, is out on October 19, 2008. Fans have been looking forward to a new title of Everlasting Sa-Ga. Little Jimmy is in trouble. He is a seven-year-old boy, and he obtained the Everlasting Sa...
Solve the programming task below in a Python markdown code block. Tanechka is shopping in the toy shop. There are exactly $n$ toys in the shop for sale, the cost of the $i$-th toy is $i$ burles. She wants to choose two toys in such a way that their total cost is $k$ burles. How many ways to do that does she have? Eac...
Solve the programming task below in a Python markdown code block. There are N integers written on a blackboard. The i-th integer is A_i, and the greatest common divisor of these integers is 1. Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately pe...
Solve the programming task below in a Python markdown code block. In this Kata, you will be given a number `n` (`n > 0`) and your task will be to return the smallest square number `N` (`N > 0`) such that `n + N` is also a perfect square. If there is no answer, return `-1` (`nil` in Clojure, `Nothing` in Haskell, `None...
Solve the programming task below in a Python markdown code block. A game is played on a strip consisting of N cells consecutively numbered from 1 to N. Alice has her token on cell A. Borys has his token on a different cell B. Players take turns, Alice moves first. The moving player must shift his or her token from i...
Solve the programming task below in a Python markdown code block. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each ...
Solve the programming task below in a Python markdown code block. Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to c...
Solve the programming task below in a Python markdown code block. Monocarp has forgotten the password to his mobile phone. The password consists of $4$ digits from $0$ to $9$ (note that it can start with the digit $0$). Monocarp remembers that his password had exactly two different digits, and each of these digits ap...
Solve the programming task below in a Python markdown code block. The Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required item...
Solve the programming task below in a Python markdown code block. Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties: In one second we can add an arbitrary (possibly negat...
Solve the programming task below in a Python markdown code block. problem There are $ N $ propositions, named $ 1, 2, \ cdots, N $, respectively. Also, $ M $ information about the propositions is given. The $ i $ th information is "$ a_i $$". Given in the form "b_i $", which means that $ a_i $ is $ b_i $. ("If" is a ...