Educational Round #1 — Editorial
Hogwarts is hosting its traditional annual Magic Theory Olympiad for first-year students. The school's caretaker, Argus Filch, has been tasked with assigning students to classrooms.
Each of the four houses has sent its best students to the Olympiad: students from Gryffindor, from Slytherin, from Hufflepuff, and from Ravenclaw. Filch has classrooms at his disposal.
Each classroom is enchanted with an expansion charm, so it can accommodate any number of students. However, it must be taken into account that students from the same house, when placed in the same room, might take the opportunity to cheat by sharing ideas on how to solve the tasks. Therefore, the number of students from the same house in any single classroom should be minimized.
We define an optimal seating arrangement as one that minimizes the maximum number of students from the same house in any classroom.
Determine the minimum possible number of students from the same house that Filch will have to place in the same classroom, even under an optimal seating arrangement.
Input. The first line contains four integers g, s, h, and r~(1 \le g, s, h, r \le 1000) — the number of students representing Gryffindor, Slytherin, Hufflepuff, and Ravenclaw, respectively.
The second line contains a single integer m~(1 \le m \le 1000) — the number of available classrooms.
Output. Print a single integer — the minimum possible number of students from the same house that will have to be placed in the same classroom, even with an optimal arrangement.
4 3 4 4
2215 14 13 14
53A four-digit number is called an Armstrong number if the sum of the fourth powers of its digits equals the number itself.
For example: 8208 = 8^4 + 2^4 + 0^4 + 8^4, therefore, is an Armstrong number.
Print all Armstrong numbers in the range from to .
Input. Two integers and b~(1000 \le a \le b \le 9999).
Output. Print all Armstrong numbers in a single line in the range from to .
1000 30001634Divide the numbers 1, 2, ..., n into two groups so that the absolute difference between the sums of the elements in these groups is the smallest possible.
Input. One integer n~(2 \le n \le 10^5).
Output. In the first line, print two integers — the number of elements in the first and second groups.
In the second line, print the elements of the first group, and in the third line — the elements of the second group.
The elements within each group can be printed in any order. Each number from to must be included in exactly one of the groups.
42 2
1 4
2 353 2
1 2 4
3 5Define an infinite sequence as follows:
Given the values n, p, q, x and , compute A_n.
Input. Five integers n, p, q, x, y~(0 \le n \le 10^{13}, 2 \le p, q \le 10^9, 0 \le x, y \le 10^9).
Output. Print the value of A_n.
12 2 3 1 0810000000 2 3 10000000 100000002Given two integers and , find a real number such that the value of f(1 / 2 + a) is maximized. It is known that
It can be proven that the maximum value of the function is rational. Print this value in the form of an irreducible fraction.
Input. Contains no more than 10^4 test cases. Each test case consists of a single line containing two integers and m~(1 \le n, m \le 10^9).
Output. For each test case, print the maximum value of f(1 / 2 + a) in the form of an irreducible fraction , where q > 0.
1 1
1 21/2
1/4An array of integers is given. In how many ways can you choose a subset of its elements such that their sum is equal to ?
Input. The first line contains two integers: n~(1 \le n \le 40) — the size of the array, and s~(1 \le s \le 10^9) — the required sum.
The second line contains integers: t_1, t_2, \dots, t_n~(1 \le t_i \le 10^9) — the elements of the array.
Output. Print a single integer — the number of ways to choose a subset of the array elements whose sum is equal to .
4 5
1 2 3 236 7
1 3 2 2 1 48Genetic scientists from the planet Olympia are once again conducting experiments with the DNA of primitive organisms. The genome of an organism is a sequence of genes, each of which can be encoded by a natural number. Genes encoded by the same number are considered identical, while genes encoded by different numbers are considered different.
The scientists have already created a primitive organism and want to modify its genome in such a way as to produce an ideal organism. They believe that this will help develop cures for many diseases in the future.
An organism is considered ideal if any two identical genes are either located on adjacent positions in the genome or have at least one identical gene between them.
In one operation, scientists can select one or more identical genes in the genome, remove them, and then place them back into the genome, possibly at different positions. Since each such operation weakens the organism, the scientists aim to minimize the number of operations needed to achieve their goal.
Write a program that, given a representation of the genome, determines the minimum number of operations required to create an ideal organism.
Input. The first line contains the number of genes n~(1 \le n \le 10^5) in the genome of a primitive organism. The second line contains positive integers, each of which does not exceed — the sequence of genes in the genome.
Output. Print the minimum number of operations required to create an ideal organism.
1 2 1 3 1 3 2 4 52The army of the Polish–Lithuanian Commonwealth is marching from the city of Kostroma to the village of Domnino. The army is led by two hetmans — Stefan and Konstantin.
Stefan has a map of the Kostroma region, which shows all the roads between villages. Each night, he leads the army from one village to another using one of these roads. Konstantin, on the other hand, has obtained a map of secret trails, and during the day, he leads the army along one of these paths. Before each movement, each hetman consults the guide, Ivan Susanin, to determine the best route.
Stefan's map shows the length of each road, so he can calculate the shortest distance from any village to Domnino using his map. Similarly, Konstantin knows the shortest distances according to his map of trails.
Ivan Susanin, being a secret agent, wants to avoid raising suspicion. Therefore, each time he selects a road (for Stefan) and a trail (for Konstantin) such that the shortest distance to the village of Domnino, according to the respective hetman's map, strictly decreases.

Help Ivan determine the maximum possible length of the route to the village of Domnino.
Input. The first line contains three integers , , and t~(2 \le n \le 1000, 1 \le s, t \le n) — the number of villages in the Kostroma region, the starting village number, and the village number of Domnino, respectively. Villages are numbered from to . It is guaranteed that s \ne t.
Then follow two blocks, each describing a map: the first block is Stefan’s map, the second one is Konstantin’s.
The first line of each block contains an integer m~(n - 1 \le m \le 10^5) — the number of roads or trails. Each of the following lines contains three integers , , and l~(1 \le a, b \le n; 1 \le l \le 10^6) describing a connection between villages and of length .
Movement along roads and trails is allowed in both directions. It is guaranteed that each map allows travel between any pair of villages. The army begins its journey in the evening from village , traveling one road per night and one trail per day.
Output. Print a single integer — the maximum possible length of the route to village Domnino (considering alternating road and trail movement). If Ivan Susanin can lead the army indefinitely without reaching Domnino, print "".
5 1 5
5
1 2 2
1 4 2
2 3 1
3 4 1
5 3 1
4
1 2 2
2 4 2
2 3 1
2 5 2-13 1 3
4
1 2 10
2 3 10
1 3 20
2 3 30
4
2 1 10
1 3 10
1 1 10
2 3 1020Alice is a magician, and she creates a new trick. She has cards with different numbers from to written on them. First, she asks an audience member to shuffle the deck and put the cards in a row. Let the number on the -th card from the left be a_i.
Then Alice selects two permutations and . There is a restriction on and : permutations can’t have fixed points. In other words, ∀ i: p_i \ne i and q_i \ne i.
Once permutations are chosen, Alice shuffles the cards according to them. Now the -th card from the left becomes the card a[p[q[i]]. The trick is considered successful if, after shuffling, the number on the -th card from the left is .
Help Alice choose the permutations and , or determine if it is impossible for the given starting permutation .
Input. The first line contains the number of test cases t~(1 \le t \le 10^5).
Each test is described in two lines. The first line contains one integer n~(1 \le n \le 10^5) — the number of cards. The second line contains integers a_i~(1 \le a_i \le n, ∀ i \ne j: a_i \ne a_j) — the initial permutation of the cards.
It is guaranteed that the sum of over all tests does not exceed 10^5.
Output. Print the answer for each test case in the order they appear in the input.
For each test case, print “Impossible” on a single line if no solution exists.
Otherwise, print "Possible" on the first line, followed by two lines containing the permutations and .
4
2
2 1
3
1 2 3
4
2 1 4 3
5
5 1 4 2 3Impossible
Possible
3 1 2
2 3 1
Possible
3 4 2 1
3 4 2 1
Possible
4 1 2 5 3
3 1 4 5 2
Comments