Sieve of Eratosthenes

2021 VietMX 2

Sieve of Eratosthenes is an algorithm for finding all the prime numbers in a segment $[1;n]$ using $O(n \log \log n)$ operations. The algorithm is […]

Fibonacci Numbers

2021 VietMX 0

The Fibonacci sequence is defined as follows: $$F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}$$ The first elements of the sequence (OEIS […]

Extended Euclidean Algorithm

2021 VietMX 3

While the Euclidean algorithm calculates only the greatest common divisor (GCD) of two integers $a$ and $b$, the extended version also finds a way to represent GCD […]

Binary Exponentiation

2021 VietMX 4

Binary exponentiation (also known as exponentiation by squaring) is a trick which allows to calculate $a^n$ using only $O(\log n)$ multiplications (instead of $O(n)$ multiplications […]

Cow and Exercise

2020 VietMX 98

Farmer John is obsessed with making Bessie exercise more! Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ directed […]

Cow and Vacation

2020 VietMX 92

Bessie is planning a vacation! In Cow-lifornia, there are $n$ cities, with $n-1$ bidirectional roads connecting them. It is guaranteed that one can reach any […]

Two Regular Polygons

2020 VietMX 0

You are given two integers $n$ and $m$ ($m < n$). Consider a convex regular polygon of $n$ vertices. Recall that a regular polygon is a polygon […]

Bogosort

2020 VietMX 2

You are given an array $a_1, a_2, \dots , a_n$. Array is good if for each pair of indexes $i < j$ the condition $j […]

Adding Powers

2020 VietMX 0

Suppose you are performing the following algorithm. There is an array $v_1, v_2, \dots, v_n$ filled with zeroes at start. The following operation is applied […]

Count the Arrays

2020 VietMX 0

Your task is to calculate the number of arrays such that: each array contains $n$ elements; each element is an integer from $1$ to $m$; […]

Array Shrinking

2020 VietMX 1

You are given an array $a_1, a_2, \dots, a_n$. You can perform the following operation any number of times: Choose a pair of two neighboring […]

Auto completion

2020 VietMX 0

You are given a set of strings $S$. Each string consists of lowercase Latin letters. For each string in this set, you want to calculate […]

Ehab the Xorcist

2020 VietMX 1

Given 2 integers $u$ and $v$, find the shortest array such that bitwise-xor of its elements is $u$, and the sum of its elements is $v$. Input […]

Sum of Odd Integers

2020 VietMX 0

You are given two integers $n$ and $k$. Your task is to find if $n$ can be represented as a sum of $k$ distinct positive odd (not […]