If you visit the prehistoric stone circle of Stonehenge at dawn on the longest day of the year, the druids will challenge you to participate in the ancient and sacred pebble game. First, you will be blindfolded, so that you are unable to see anything.
Then, the chief druid will tell you that she has $$$N$$$ piles of stones in a line, where $$$N$$$ is a power of two ($$$N = 2^k$$$ for some integer $$$k$$$).
Piles are indexed from $$$0$$$ to $$$N-1$$$. Each pile has a height $$$H_i$$$, which is a positive integer, and the piles are ordered from smallest to tallest ($$$H_0 \le H_1 \le \ldots \le H_{N-1}$$$). The chief druid tells you the value of $$$N$$$, but not the initial heights.
You can then choose actions of one of the following types:
Your goal is to reach a configuration where all piles have the same height. You take at most $$$Q_\mathrm{add}$$$ actions of the first type, and at most $$$Q_\mathrm{compare}$$$ actions of the second type (see section Scoring).
Implementation Details
You will have to submit a single .cpp source file.
Among this task's attachments you will find a template equal.cpp with a sample implementation.
You have to implement the following function:
void make_all_equal(int N, int Q_add, int Q_compare);
You can call the following functions:
void add(vector<int> S, long long X);
bool compare(int i, int j);
The task's directory contains a simplified version of the jury grader, which you can use to test your solution locally. The simplified grader reads the input data from stdin, calls the functions that you must implement, and finally writes the output to stdout.
The input is made up of two lines, containing:
Constraints
If your program is judged as Accepted, the sample grader prints Accepted: add=U, compare=V where $$$U$$$ and $$$V$$$ are the number of times you called add and compare.
If your program is judged as Wrong Answer, the sample grader prints Wrong Answer: MSG, where MSG is one of:
| Message | Meaning |
| too many calls to add | You called add more than $$$Q_\mathrm{add}$$$ times. |
| X out of range | The integer $$$X$$$ given to add is not between $$$0$$$ and $$$10^{12}$$$ inclusive. |
| index in S out of range | An element of the vector $$$S$$$ given to add is not between $$$0$$$ and $$$N-1$$$ inclusive. |
| indices in S not distinct | There are two equal elements in the vector $$$S$$$ given to add. |
| too many calls to compare | You called compare more than $$$Q_\mathrm{compare}$$$ times. |
| i out of range | The integer $$$i$$$ given to compare is not between $$$0$$$ and $$$N-1$$$ inclusive. |
| j out of range | The integer $$$j$$$ given to compare is not between $$$0$$$ and $$$N-1$$$ inclusive. |
| heights are not equal | After calling make_all_equal, there are two piles with different heights. |
Example interaction.
Hampton Court Palace (in Richmond, just southwest of London) is famous for its maze, which was planted by King William III in the 1690s. Since the palace is now open to the public as a tourist attraction, the royal authorities have decided to replace the maze with an updated version. They have estimated the number of tourists $$$K$$$ they expect to visit during the season, and they would like the new maze to have exactly $$$K$$$ possible routes through it, allowing each visitor to have a unique experience.
The maze is a grid of $$$N$$$ rows and $$$M$$$ columns, where each cell can be either empty or contain a hedge. The maze is surrounded by a fence, and there is a single entrance and a single exit. The entrance is in the top-left cell, and the exit is in the bottom-right cell. The visitor must complete the maze by making only right and down moves. Due to space constraints, the maze can have no more than 200 rows and no more than 200 columns.
Your task is to design a maze with exactly $$$K$$$ routes from the entrance to the exit involving only right and down moves.
Implementation Details
You will have to submit a single .cpp source file. Among this task's attachments you will find a template mazes.cpp with a sample implementation.
You have to implement the following function:
vector<vector<char>> solve(long long K);
The grader will call the function solve and will print its return value to the output file.
The task's directory contains a simplified version of the jury grader, which you can use to test your solution locally. The simplified grader reads the input data from stdin, calls the function that you must implement, and finally writes the output to stdout.
The input is made up of $$$1$$$ line, containing a single integer $$$K$$$.
Constraints
$$$1 \le K \le 10^{18}$$$.
The output is made up of several lines, containing:
Your program will be tested on a set of test cases grouped by subtask. To obtain the score associated to a subtask, you need to correctly solve all the test cases it contains.
1
2 2 .# ..
3
8 8 ........ .######. ..#...#. .#..#.#. ...##... .#...##. .###.##. ........
In the first sample case, there is obviously just one way to go through the maze.
In the second sample case, there are three possible ways to go through the maze. The start cell is shaded in green, the end cell in red, and the cells that form the paths are shaded in blue.
Second sample case.
In order to deliver parcels more efficiently, the Post Office has constructed a network of pneumatic tubes beneath the streets of London. The network consists of $$$N$$$ routing stations connected by $$$N-1$$$ undirected tubes. There is a unique path between any pair of stations, and parcels will be sent along the path from their source to their destination.
When a parcel is at routing station $$$i$$$, there are two options for sending it on towards its destination. It can be fired at low power at a cost of $$$A_i$$$, in which case it will travel along a single tube to the next station along its route. Alternatively, it can be fired at high power. In this case, the operator will select a dial setting $$$k\geq 1$$$, and the parcel will travel along the next $$$k$$$ tubes of its route, at a cost of $$$B_i + k\cdot C$$$.
The Post Office will route parcels so as to minimise total cost, but in order to avoid congestion on the network parcels must stay on the direct route from their source to their destination. Your task is to find the minimum costs for a series of $$$Q$$$ parcels to be sent in this network.
Implementation Details
You will have to submit a single .cpp source file.
Among this task's attachments you will find a template multihop.cpp with a sample implementation.
You have to implement the following functions:
void init(int N, int C, vector<int> A, vector<int> B, vector<int> U, vector<int> V);
long long query(int X, int Y);
The grader will call the function init, and then will call query $$$Q$$$ times, printing its return value to the output file.
The task's directory contains a simplified version of the jury grader, which you can use to test your solution locally. The simplified grader reads the input data from stdin, calls the functions that you must implement, and finally writes the output to stdout.
The input is made up of $$$N + Q + 2$$$ lines, containing:
Constraints
The output is made up of $$$Q$$$ lines, containing the values returned by the function query.
Your program will be tested on a set of test cases grouped by subtask. To obtain the score associated to a subtask, you need to correctly solve all the test cases it contains.
5 1 4 2 8 6 9 2 2 5 9 5 2 3 0 2 3 4 2 1 4 0 1
16
5 5 3 9 7 9 4 5 5 10 8 9 7 4 3 0 4 2 0 1 2 4 0 3 1 0 3 3 0 1 4
5 20 11 9 19
In the first sample case, we can fire the parcel from routing station $$$0$$$ to $$$4$$$ at high power, for a cost of $$$14$$$, and then from $$$4$$$ to $$$1$$$ at low power, for a cost of $$$2$$$. The final cost is then $$$16$$$, which is optimal.
The British spy agency MI6 is planning to infiltrate a network of double agents into the sinister criminal organisation SPECTRE. SPECTRE has $$$N$$$ employees, numbered from $$$0$$$ to $$$N-1$$$, and their organisation chart is a tree on these $$$N$$$ vertices.
MI6 will select a non-empty set $$$S$$$ of SPECTRE employees to turn into double agents. Because of the risk of further treachery (i.e. possible triple-agents), it is essential that the chain of communication between any two double-agents passes only through 'innocent' employees. That is, for any two distinct employees $$$a$$$ and $$$b$$$ in $$$S$$$, the simple path between $$$a$$$ and $$$b$$$ in the tree must not contain any other employees in $$$S$$$.
Your task is to count the number of possible sets $$$S$$$ of double agents. Since this value can be quite large, output the value modulo $$$10^9 + 7$$$.
Implementation Details
You will have to submit a single .cpp source file.
Among this task's attachments you will find a template trees.cpp with a sample implementation.
You have to implement the following function:
int count_sets(int N, vector<int> U, vector<int> V);
The grader will call the function count and will print its return value to the output file.
The task's directory contains a simplified version of the jury grader, which you can use to test your solution locally. The simplified grader reads the input data from stdin, calls the functions that you must implement, and finally writes the output to stdout.
The input is made up of $$$N$$$ lines, containing:
Constraints
The output is made up of a single line, containing the value returned by the function count.
Your program will be tested on a set of test cases grouped by subtask. To obtain the score associated to a subtask, you need to correctly solve all the test cases it contains.
3 0 1 1 2
6
5 0 1 0 2 1 3 1 4
17
In the first sample case, the tree is a path $$$(0 - 1 - 2)$$$. There are $$$6$$$ possible sets in this tree: $$$\{0\}, \{1\}, \{0, 1\}, \{2\}, \{0, 2\}, \{1, 2\}$$$.
The set $$$\{0, 1, 2\}$$$ is not permitted as $$$1$$$ lies on the simple path between $$$0$$$ and $$$2$$$.
In the second sample case, the tree is a path $$$(3 - 1 - 0 - 2)$$$ with a single additional leaf $$$(4)$$$. There are $$$17$$$ possible sets in this tree.