Constructive Algorithms
Constructive Algorithms
Constructive Algorithms
Constructive Algorithms
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.
For a given number n, you need to make a permutation p such that two
requirements are satisfied at the same time:
• For each element pi , at least one of its neighbors has a value that
differs from the value of pi by one. That is, for each element pi
(1 ≤ i ≤ n), at least one of its neighboring elements (standing to the
left or right of pi ) must be pi+1 or pi−1 .
• The permutation must have no fixed points. That is, for every i
(1 ≤ i ≤ n), pi ̸= i must be satisfied.
1
Construction
4. Little C loves number 3 very much. He loves all things about it. Now he
has a positive integer n. He wants to split n into 3 positive integers a, b,
c, such that a + b + c = n and none of the 3 integers is a multiple of 3.
Help him to find a solution.
5. Madoka finally found the administrator password for her computer. Her
father is a well-known popularizer of mathematics, so the password is the
answer to the following problem.
Find the maximum decimal number without zeroes and with no equal
digits in a row, such that the sum of its digits is n.
Madoka is too tired of math to solve it herself, so help her to solve this
problem!
2
Construction
If there are multiple answers, you can print any. It is not guaranteed
that the answer exists.
8. There are n cats in a line, labeled from 1 to n, with the i-th cat at po-
sition i. They are bored of gyrating in the same spot all day, so they
want to reorder themselves such that no cat is in the same place as be-
fore. They are also lazy, so they want to minimize the total distance they
move. Help them decide what cat should be at each location after the
reordering.
For example, if there are 3 cats, this is a valid reordering: [3,1,2]. No cat
is in its original position. The total distance the cats move is 1+1+2 = 4
as cat 1 moves one place to the right, cat 2 moves one place to the right,
and cat 3 moves two places to the left.
3
Construction
Pari is trying to love them too, but only very special and gifted peo-
ple can understand the beauty behind palindrome numbers. Pari loves
integers with even length (i.e. the numbers with even number of digits),
so she tries to see a lot of big palindrome numbers with even length (like
a 2-digit 11 or 6-digit 122221), so maybe she could see something in them.
Now Pari asks you to write a program that gets a huge integer n from the
input and tells what is the n-th even-length positive palindrome number?
10. You are given a positive integer n. You have to find 4 positive integers
a, b, c, d such that
• a + b + c + d = n and
• gcd(a, b) = lcm(c, d).
If there are several possible answers you can output any of them. It is
possible to show that the answer always exists.
11. Given n, find any array a1 , a2 , . . . , an of integers such that all of the
following conditions hold:
• 1 ≤ ai ≤ 109 for every i from 1 to n.
• a1 < a2 < . . . < an
• For every i from 2 to n, ai isn’t divisible by ai−1 .
It can be shown that such an array always exists under the constraints
of the problem.
4
Construction
12. In the beginning of the new year Keivan decided to reverse his name. He
doesn’t like palindromes, so he changed Naviek to Navick.
14. Apart from having lots of holidays throughout the year, residents of
Berland also have whole lucky years. Year is considered lucky if it has
no more than 1 non-zero digit in its number. So years 100, 40000, 5 are
lucky and 12, 3001 and 12345 are not.
You are given current year in Berland. Your task is to find how long
will residents of Berland wait till the next lucky year.
15. At the store, the salespeople want to make all prices round.
So, if an item is worth m bourles (the value of the item is not greater than
109 ), the sellers want to change its value to the nearest round number
that is not greater than m. They ask you: by how many bourles should
5
Construction
you decrease the value of the item to make it worth exactly 10k bourles,
where the value of k — is the maximum possible (k — any non-negative
integer).
For example, let the item have a value of 178-bourles. Then the new
price of the item will be 100, and the answer will be 178 − 100 = 78.
6
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).
18. You are given an integer n. You have to construct a permutation of size n.
20. Pak Chanek has a prime number n. Find a prime number m such that
n + m is not prime.