The hamster Shustrik received an unusual calculator as a gift from the hedgehog Shurshik. Initially, the number $$$A$$$ is displayed on the calculator screen.
The instructions for the calculator specify a fixed number $$$M$$$. All arithmetic operations are performed with this number. Additionally, Shurshik has thought of numbers $$$N$$$ and $$$B$$$ and informed Shustrik about them.
The calculator can perform only four operations, and in one step, one of the following operations can be applied:
After performing some (possibly empty) sequence of operations, the number $$$A'$$$ will be obtained.
The hedgehog Shurshik became curious whether Shustrik can obtain such a number $$$A'$$$ that
$$$$$$ A' \equiv B \pmod N. $$$$$$
In other words, the numbers $$$A'$$$ and $$$B$$$ must have the same remainders when divided by $$$N$$$.
Your task is to help the hamster find any sequence of operations of the smallest length, after which the specified condition will be satisfied, or determine that it is impossible to obtain such a number.
In a single line, four integers are given separated by spaces: $$$A$$$, $$$M$$$, $$$B$$$, and $$$N$$$ ($$$|A| \leq 10^9$$$, $$$1 \leq M \leq 10^9$$$, $$$0 \leq B \lt N \leq 2 \cdot 10^6$$$).
If the desired sequence of operations does not exist, output "-1" in a single line.
Otherwise, in the first line, output the length $$$x$$$ of the minimal suitable sequence. In the second line, output a string of length $$$x$$$ consisting of the characters "+", "-", "*", and "%" — the sequence of operations that transforms the initial $$$A$$$ into such $$$A'$$$ that $$$A' \equiv B \pmod N$$$.
If there are multiple such sequences, output any of them.
It can be proven that the length of the answer does not exceed $$$N$$$.
Points for each subtask are awarded only if all tests for that subtask and necessary subtasks are successfully passed.
| Group | Points | Additional constraints | Required groups |
| 1 | 7 | $$$|A|, M, N \le 8$$$ | – |
| 2 | 6 | $$$M = 1$$$ | – |
| 3 | 8 | $$$M = N$$$ | – |
| 4 | 12 | $$$M \equiv 0 \pmod N$$$ | 3 |
| 5 | 15 | $$$N \equiv 0 \pmod M$$$ | 2–3 |
| 6 | 16 | $$$N \cdot M \le 2 \cdot 10^{6}$$$ | 1–2 |
| 7 | 18 | $$$A = 0$$$ | – |
| 8 | 18 | – | 1–7 |
1 3 2 5
2 +*
6 7 2 4
0
-1 3 2 4
1 +
5 6 1 6
-1
0 6 3 7
3 ---
5 9 1 8
3 *%+
Consider the answer to the first example of the input data:
On the monorail line, there are $$$n$$$ stations, numbered in order from $$$1$$$ to $$$n$$$.
Each station $$$i$$$ has a name $$$s_i$$$ — a string of no more than 20 Latin letters. Each letter is written on a separate nameplate.
As part of the urban infrastructure upgrade project, it was decided to rename all the stations to some new names. The order for renaming has already been signed and will take effect tomorrow. However, there is a problem: no one has had time to replace the names on the nameplates at the stations. Now it is your task.
Unfortunately, there is no time to make new nameplates. Therefore, the only possible option is to move the nameplates from one station to another.
Formally, in one operation, you can choose two indices $$$1 \le i, j \le n$$$, as well as any letter from the string $$$s_i$$$, and move that letter to any position in the string $$$s_j$$$. This operation takes $$$1 + |i - j|$$$ precious units of time.
For example, if $$$s_i = \texttt{ab}$$$ and $$$s_j = \texttt{cd}$$$, then in one operation of moving a letter from $$$i$$$ to $$$j$$$, you can do:
Note that it is also allowed to choose $$$i = j$$$ in the operation.
You need to replace the nameplates with names at each station as quickly as possible so that they correspond to the new names. Determine whether this is possible, and if so, the minimum necessary amount of time.
The first line of the test contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of stations on the line.
The second line of the test contains $$$n$$$ strings: $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le |s_i| \le 20$$$) — the original names of the stations.
The third line of the test contains $$$n$$$ strings: $$$t_1, t_2, \ldots, t_n$$$ ($$$1 \le |t_i| \le 20$$$) — the desired new names of the stations.
It is guaranteed that all strings in the test consist of lowercase Latin letters, characters from 'a' to 'z'.
If there is no way to rename all the stations, output $$$-1$$$ in a single line. Otherwise, output a single number: the minimum possible total time required for the renaming operations.
| Group | Points | Additional constraints | Required groups |
| 1 | 14 | $$$n = 1$$$ | – |
| 2 | 17 | $$$\operatorname{sorted}(s_i) = \operatorname{sorted}(t_i) $$$ | – |
| 3 | 25 | $$$s_i$$$ and $$$t_i$$$ have no common letters | – |
| 4 | 13 | $$$n \leq 1000; |s_i|, |t_i| \leq 10$$$ | – |
| 5 | 11 | $$$|s_i| = 1$$$, $$$|t_i| = 1$$$ | – |
| 6 | 11 | All strings consist of the letter a | – |
| 7 | 9 | – | 1–6 |
Here, $$$\operatorname{sorted}(s_i)$$$ denotes the string $$$s_i$$$ sorted in alphabetical order. For example, $$$\operatorname{sorted}(\texttt{abacaba}) = \texttt{aaaabbc}$$$
3no iislop neponinno polis open
10
3eleven plus twotwelve plus one
12
1evillive
3
2a bba ab
-1
2indian catinit canada
-1
4aaaa aaa aa aa aa aaa aaaa
14
7s e a s i d ed i s e a s e
22
Persik — a shark in the world of programming — decided to create a schedule for all tracks of the Innopolis Open olympiad.
There are $$$D$$$ days in the academic year, numbered from $$$1$$$ to $$$D$$$. In total, $$$n$$$ olympiads are held.
The olympiads are numbered from $$$1$$$ to $$$n$$$ in non-decreasing order of their initially scheduled dates. Each olympiad takes place on exactly one day. For each olympiad, its initially scheduled date and its benefit for the younger generation are known.
You may change the date of any olympiad, but only to a later day within the same academic year (that is, the new date cannot be earlier than the original one and cannot exceed $$$D$$$).
After all changes, the olympiads must remain in non-decreasing order of their scheduled dates.
The total benefit of the olympiad season is defined as follows. For each day, consider all olympiads scheduled on that day. If at least one olympiad is scheduled on a given day, the benefit of that day is the maximum benefit among those olympiads. If no olympiads are scheduled on that day, the benefit of that day is $$$0$$$.
The benefit of the season is the sum of the benefits over all $$$D$$$ days.
For the benefit of the students, determine the maximum possible total benefit of the season.
The first line contains three integers $$$n$$$, $$$D$$$, and $$$t$$$ — the number of olympiads and the number of days in the academic year, as well as the type of answer ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le D \le 2 \cdot 10^9$$$; $$$1 \le t \le 2$$$).
In the $$$i$$$-th of the following $$$n$$$ lines, two integers $$$d_i$$$ and $$$b_i$$$ are given — the scheduled date (day number) of the $$$i$$$-th olympiad and its benefit ($$$1 \le d_i \le D$$$; $$$1 \le b_i \le 2 \cdot 10^9$$$).
It is guaranteed that $$$d_i \le d_j$$$ for all $$$i$$$ and $$$j$$$ such that $$$i \lt j$$$.
If $$$t = 1$$$, output a single integer — the maximum possible benefit of the olympiad season.
If $$$t = 2$$$, then in the first line output a single integer — the maximum possible benefit of the olympiad season; and in the second line output the schedule of the olympiads — $$$n$$$ integers, where the $$$i$$$-th is the final date of the $$$i$$$-th olympiad. The specified final dates must achieve the maximum possible benefit of the olympiad season.
The tests for this problem consist of thirteen groups. Points for each group are awarded only if all tests of the group and all tests necessary for it are passed.
| Group | Points | Additional constraints | Necessary groups |
| 1 | 10 | $$$n, D \le 20$$$; $$$b_i \le 20$$$ for all $$$i$$$; $$$t = 1$$$ | – |
| 2 | 7 | $$$n, D \le 20$$$; $$$b_i \le 20$$$ for all $$$i$$$ | 1 |
| 3 | 11 | $$$n, D \le 100$$$; $$$b_i \le 100$$$ for all $$$i$$$ | 1, 2 |
| 4 | 9 | $$$n, D \le 800$$$; $$$b_i \le 800$$$ for all $$$i$$$ | 1, 2, 3 |
| 5 | 5 | $$$n, D \le 2500$$$; $$$b_i \le 2500$$$ for all $$$i$$$; $$$t = 1$$$ | 1 |
| 6 | 4 | $$$n, D \le 2500$$$; $$$b_i \le 2500$$$ for all $$$i$$$ | 1–5 |
| 7 | 5 | $$$b_i = b_j$$$ for all $$$i$$$, $$$j$$$ | – |
| 8 | 5 | $$$d_i \neq d_j$$$ for all $$$i$$$ and $$$j$$$, such that $$$i \neq j$$$ | – |
| 9 | 5 | $$$d_i = 1$$$ for all $$$i$$$; $$$t = 1$$$ | – |
| 10 | 5 | $$$d_i = d_j$$$ for all $$$i$$$, $$$j$$$ | 9 |
| 11 | 8 | $$$D \le 2 \cdot 10^6$$$ | 1–6 |
| 12 | 10 | $$$t = 1$$$ | 1, 5, 9 |
| 13 | 16 | – | 1–12 |
3 10 11 31 51 2
10
3 10 21 31 51 2
10 1 2 3
5 6 21 72 65 85 96 4
30 1 3 5 6 6
Note that the answer for the second test will not be suitable as an answer for the first, as for $$$t = 1$$$ only one number needs to be output.
In the second example, the benefit of the first day is $$$3$$$, the benefit of the second is $$$5$$$, the third is $$$2$$$, and the benefits of days from the $$$4$$$th to the $$$10$$$th are $$$0$$$. Thus, the benefit of the olympiad season is $$$3+5+2+0+\ldots+0=10$$$. Note that the schedule of olympiads $$$[2, 1, 3]$$$ is not possible, as the olympiads must remain in non-decreasing order of their scheduled dates.
In the third example, a schedule of olympiads $$$[1, 2, 5, 6, 6]$$$ is also suitable, but $$$[1, 2, 3, 5, 6]$$$ is not suitable, as the third olympiad cannot take place before the $$$5$$$th day
This is an interactive problem.
Cyber-shark Zubastik$$$^{\dagger}$$$ works in a warehouse and stacks boxes into a single vertical tower. The height of the tower is unlimited, but the shark can only remove and place the top box on the tower. There is also a buffer nearby — a free area where boxes can be temporarily held (any number of boxes can be in the buffer).
Boxes are numbered in the order they arrive, starting from $$$1$$$. For each box, the moment in time $$$t_j$$$ when it needs to be extracted from the warehouse is known.
During the work, there are two types of events:
Actions. During the processing of an event, the shark can perform the following operations:
Rules for processing events.
Your task is to output a set of operations for each event that correctly processes that event.
$$$^{\dagger}$$$ see fig.
Cyber-shark Zubastik The first line contains $$$T$$$ — the number of moments in time to consider.
In the $$$i$$$-th of the subsequent $$$T$$$ lines, the description of the next event occurring at time $$$i$$$ is given in the following format:
For each event, you need to output a sequence of operations that will process it.
First, an integer $$$K$$$ — the number of operations.
Then a list of $$$K$$$ operations — for each, two numbers:
The total number of operations output across all events must not exceed $$$10^6$$$. If this limit is violated, the solution will receive a verdict of Wrong Answer.
First, your program must read an integer $$$T$$$ — the number of events.
Then for each $$$i = 1,2,\dots,T$$$, the interactor outputs a line with the description of the event occurring at time $$$i$$$:
After receiving the next event, your program must output the response for it in the following format:
To receive the next event, you must output a correct response for the current event and end the line with a newline character.
After outputting the response, be sure to flush the output buffer:
Note that for faster output, it is advisable to call flush (or endl) after outputting all $$$K$$$ operations, rather than after each operation.
The interactor in this problem is non-adaptive: within a single test, the sequence of events is fixed and does not change based on your responses.
In this problem, there are two types of tests.
The first type of test — an array [1, 1, 2, 2, ..., N, N] is generated and shuffled randomly, resulting in an array $$$[a_1, a_2, \dots, a_T]$$$ of size $$$T$$$, where $$$T = 2N$$$. A box is added at time $$$t_1$$$ and taken out at time $$$t_2$$$, if $$$t_1 \lt t_2$$$ and $$$a[t_1] = a[t_2]$$$.
The second type of test — it is guaranteed that the boxes are taken out in the same order they are added.
For each value of $$$N$$$ (the number of boxes), one test of each type is generated.
| Group | Tests | Values of $$$N$$$ | Points | Cumulative Sum | Dependencies |
| 1 | 1-2 | Tests from the statement | 0 per test | 0 | - |
| 2 | 3-10 | 100, 200, 330, 420 | 1 per test | 8 | - |
| 3 | 11-18 | 480, 720, 820, 1080 | 1 per test | 16 | 1 |
| 4 | 19-26 | 1240, 1420, 1630, 1870 | 1 per test | 24 | 1-2 |
| 5 | 27-30 | 2150, 2470 | 1 per test | 28 | 1-3 |
| 5 | 31-34 | 2840, 3260 | 2 per test | 36 | 1-3 |
| 6 | 35-42 | 3740, 4300, 4940, 5680 | 2 per test | 52 | 1-4 |
| 7 | 43-50 | 6530, 7500, 8620, 9910 | 2 per test | 68 | 1-5 |
| 8 | 51-58 | 11390, 13090, 15050, 17300 | 2 per test | 84 | 1-6 |
| 9 | 59-66 | 17500, 18000, 18500, 19000 | 2 per test | 100 | 1-7 |
4+ 3+ 4- 1- 2
1 1 1 3 2 1 1 2 1 1 1 2 1 1 2 2
8+ 8+ 3- 2+ 7+ 6- 4- 3- 1
1 1 1 1 1 2 1 2 2 1 1 3 1 1 4 1 2 4 1 2 3 1 2 1
Elephant Filimon decided to visit his panda-friend Cheng Shi from Chengdu, China. Cheng Shi loves bamboo and showed her collection of bamboo inscriptions. Each inscription is a string of lowercase Latin letters.
Cheng Shi's collection is a dynamic multiset of strings. Initially, it is empty.
The panda can perform operations of two types:
Elephant Filimon became curious about the minimum number of actions required after each operation to make all strings in the current multiset identical.
Allowed actions on one string from the collection:
The string to which all others are brought can be any (including empty) and does not have to be present in the multiset.
Note: The strings in the multiset are not actually changed. After each query, it is only necessary to compute the minimum number of actions required to make all strings identical. For subsequent operations, the multiset remains unchanged.
Help Cheng Shi quickly respond to Filimon's question after each change.
The first line contains a single integer $$$q$$$ ($$$1 \le q \le 5 \cdot 10^5$$$) — the number of operations.
Each of the following $$$q$$$ lines describes one operation and has the form:
+ s or - s
where $$$s$$$ is a non-empty string consisting of lowercase Latin letters.
In all queries, $$$1 \le |s| \le 5 \cdot 10^5$$$. The total length of all strings in all operations does not exceed $$$5 \cdot 10^5$$$.
After each operation, output a single integer — the minimum number of actions required to make all strings in the current multiset identical.
If the multiset is empty after the operation, output $$$0$$$.
| Additional constraints | |||||
| Subgroup | Points | $$$q$$$ | $$$\sum |s_i|$$$ | Type of operations | Required subgroups |
| 1 | 10 | $$$q \le 100$$$ | $$$\sum |s_i| \le 1000$$$ | + and - | – |
| 2 | 10 | $$$q \le 1000$$$ | $$$\sum |s_i| \le 10^4$$$ | + and - | 1 |
| 3 | 15 | $$$q \le 50\,000$$$ | $$$\sum |s_i| \le 10^5$$$ | + and - | 1,2 |
| 4 | 10 | $$$q \le 5 \cdot 10^5$$$ | $$$\sum |s_i| \le 5 \cdot 10^5$$$, $$$|s_i| \le 10$$$ | + | – |
| 5 | 10 | $$$q \le 5 \cdot 10^5$$$ | $$$\sum |s_i| \le 5 \cdot 10^5$$$, $$$|s_i| \le 10$$$ | + and - | 4 |
| 6 | 20 | $$$q \le 5 \cdot 10^5$$$ | $$$\sum |s_i| \le 5 \cdot 10^5$$$ | + | 4 |
| 7 | 25 | – | – | + and - | 1–6 |
5+ abc+ ab+ a- ab+ b
0 1 2 2 4
Alexander and Babin live on an abandoned island. Their only entertainment consists of trees, stones, and bombs (don't ask where they came from). They are very bored and want to play a game. Alexander suggested playing "Minesweeper", while Babin suggested "Nim". As a compromise, they decided to play Nimesweeper!
With no other choice, the guys chose a tree as the game board. Recall that a tree is a connected undirected graph without cycles.
In the course of the overall game, Alexander starts playing the subgame "Minesweeper". Initially, none of the vertices of the tree contain bombs. On his turn, Alexander chooses any vertex without a bomb and places a bomb in it. After that, in each vertex that does not contain a bomb, a number of stones equal to the number of bombs in its neighboring vertices is placed.
Babin can interrupt Alexander after any of his moves. Once this happens, they start playing "Nim" with the resulting piles of stones, with Babin going first.
If Babin does not interrupt Alexander, Alexander makes the next move (places another bomb), after which the piles of stones are updated, and so on, until Babin starts the game of "Nim".
The player who cannot make a move in "Nim" loses.
Babin really wants to win, so he will interrupt Alexander at the earliest possible moment when he can guarantee his victory.
Alexander, well aware that he cannot win this game, decided instead to maximize his enjoyment, so he wants to place as many bombs as possible before Babin starts the game of "Nim". Please help Alexander find the maximum number of bombs he can place!
Recall that in the game of "Nim", the first player wins if and only if the bitwise XOR of the sizes of the piles is non-zero.
Recall that XOR or Exclusive OR is a binary operation that is applied to each pair of bits in the same position in the binary representations of numbers such that the result at that position is 0 if the bits are equal, and 1 otherwise. For example, 3 XOR 5 = 6, since $$$3_{10}$$$ = $$$011_2$$$, $$$5_{10}$$$ = $$$101_2$$$, and therefore after applying the operation, the second and third bits become equal to 1, while the first bit becomes 0, resulting in $$$110_2$$$ = $$$6_{10}$$$.
The first line of the input contains a single integer $$$n$$$ ($$$2 \leq n \leq 369$$$) — the number of vertices in the tree.
The following $$$n - 1$$$ lines contain pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$) — the edges of the tree. It is guaranteed that these edges indeed form a tree.
In a single line, output a single number — the maximum number of bombs that can be placed.
| Group | Points | Additional Constraints | Required Groups |
| 1 | 8 | $$$n \le 8$$$ | – |
| 2 | 11 | $$$n \le 18$$$ | 1 |
| 3 | 20 | $$$n \le 45$$$ | 1–2 |
| 4 | 19 | $$$n \leq 128$$$ | 1–3 |
| 5 | 18 | $$$n \leq 243$$$ | 1–4 |
| 6 | 20 | Degrees of all vertices $$$\le 3$$$ | – |
| 7 | 4 | – | 1–6 |
31 21 3
2
91 22 33 44 55 66 75 88 9
5
137 67 57 47 37 27 11 122 113 104 95 86 13
8