problem_id
stringlengths 16
23
| task_type
stringclasses 1
value | prompt
stringlengths 935
8.9k
| verification_info
stringclasses 1
value |
|---|---|---|---|
codeforces_1012_1012/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array of $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$. You can perform the following operation any number of times: select several distinct indices $$$i_1, i_2, \dots, i_k$$$ ($$$1 \le i_j \le n$$$) and move the number standing at the position $$$i_1$$$ to the position $$$i_2$$$, the number at the position $$$i_2$$$ to the position $$$i_3$$$, ..., the number at the position $$$i_k$$$ to the position $$$i_1$$$. In other words, the operation cyclically shifts elements: $$$i_1 \to i_2 \to \ldots i_k \to i_1$$$.
For example, if you have $$$n=4$$$, an array $$$a_1=10, a_2=20, a_3=30, a_4=40$$$, and you choose three indices $$$i_1=2$$$, $$$i_2=1$$$, $$$i_3=4$$$, then the resulting array would become $$$a_1=20, a_2=40, a_3=30, a_4=10$$$.
Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number $$$s$$$. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well.
## Input Format
The first line of the input contains two integers $$$n$$$ and $$$s$$$ ($$$1 \leq n \leq 200\,000$$$, $$$0 \leq s \leq 200\,000$$$)—the number of elements in the array and the upper bound on the sum of cycle lengths.
The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$—elements of the array ($$$1 \leq a_i \leq 10^9$$$).
## Output Format
If it's impossible to sort the array using cycles of total length not exceeding $$$s$$$, print a single number "-1" (quotes for clarity).
Otherwise, print a single number $$$q$$$— the minimum number of operations required to sort the array.
On the next $$$2 \cdot q$$$ lines print descriptions of operations in the order they are applied to the array. The description of $$$i$$$-th operation begins with a single line containing one integer $$$k$$$ ($$$1 \le k \le n$$$)—the length of the cycle (that is, the number of selected indices). The next line should contain $$$k$$$ distinct integers $$$i_1, i_2, \dots, i_k$$$ ($$$1 \le i_j \le n$$$)—the indices of the cycle.
The sum of lengths of these cycles should be less than or equal to $$$s$$$, and the array should be sorted after applying these $$$q$$$ operations.
If there are several possible answers with the optimal $$$q$$$, print any of them.
## Examples
```input
5 5
3 2 3 1 1
```
```output
1
5
1 4 2 3 5
```
-----
```input
4 3
2 1 4 3
```
```output
-1
```
-----
```input
2 0
2 2
```
```output
0
```
## Note
In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle $$$1 \to 4 \to 1$$$ (of length 2), then apply the cycle $$$2 \to 3 \to 5 \to 2$$$ (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case.
In the second example, it's possible to the sort the array with two cycles of total length 4 ($$$1 \to 2 \to 1$$$ and $$$3 \to 4 \to 3$$$). However, it's impossible to achieve the same using shorter cycles, which is required by $$$s=3$$$.
In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during $$$n$$$ consecutive days. During the $$$i$$$-th day you have to write exactly $$$a_i$$$ names.". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).
Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly $$$m$$$ names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.
Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from $$$1$$$ to $$$n$$$.
## Input Format
The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ means the number of names you will write in the notebook during the $$$i$$$-th day.
## Output Format
Print exactly $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$, where $$$t_i$$$ is the number of times you will turn the page during the $$$i$$$-th day.
## Examples
```input
3 5
3 7 9
```
```output
0 2 1
```
-----
```input
4 20
10 9 19 2
```
```output
0 0 1 1
```
-----
```input
1 100
99
```
```output
0
```
## Note
In the first example pages of the Death Note will look like this $$$[1, 1, 1, 2, 2], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [3, 3, 3, 3]$$$. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters.
The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, \dots, s_r$$$ without changing the order.
Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 \le i \le |b| - |a| + 1$$$) such that $$$b[i..i + |a| - 1] = a$$$ ($$$|a|$$$ is the length of string $$$a$$$).
You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.
## Input Format
The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 10^3$$$, $$$1 \le q \le 10^5$$$) — the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively.
The second line is a string $$$s$$$ ($$$|s| = n$$$), consisting only of lowercase Latin letters.
The third line is a string $$$t$$$ ($$$|t| = m$$$), consisting only of lowercase Latin letters.
Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the arguments for the $$$i$$$-th query.
## Output Format
Print $$$q$$$ lines — the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.
## Examples
```input
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
```
```output
0
1
0
1
```
-----
```input
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
```
```output
4
0
3
```
-----
```input
3 5 2
aaa
baaab
1 3
1 1
```
```output
0
0
```
## Note
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.
Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.
Help Vasya! Calculate the maximum total weight of mushrooms he can collect.
## Input Format
The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.
The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.
## Output Format
Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.
## Examples
```input
3
1 2 3
6 5 4
```
```output
70
```
-----
```input
3
1 1000 10000
10 100 100000
```
```output
543210
```
## Note
In the first test case, the optimal route is as follows:
In the second test case, the optimal route is as follows:
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!
Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively.
Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.
## Input Format
The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix.
The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i.
The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.
## Output Format
If there is no matrix satisfying the given constraints in the first line, output "NO".
Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix.
If there are several suitable matrices, it is allowed to print any of them.
## Examples
```input
2 3
2 9
5 3 13
```
```output
YES
3 4 5
6 7 8
```
-----
```input
3 3
1 7 6
2 15 12
```
```output
NO
```
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
There is a light source on the plane. This source is so small that it can be represented as point. The light source is moving from point $$$(a, s_y)$$$ to the $$$(b, s_y)$$$ $$$(s_y < 0)$$$ with speed equal to $$$1$$$ unit per second. The trajectory of this light source is a straight segment connecting these two points.
There is also a fence on $$$OX$$$ axis represented as $$$n$$$ segments $$$(l_i, r_i)$$$ (so the actual coordinates of endpoints of each segment are $$$(l_i, 0)$$$ and $$$(r_i, 0)$$$). The point $$$(x, y)$$$ is in the shade if segment connecting $$$(x,y)$$$ and the current position of the light source intersects or touches with any segment of the fence.
You are given $$$q$$$ points. For each point calculate total time of this point being in the shade, while the light source is moving from $$$(a, s_y)$$$ to the $$$(b, s_y)$$$.
## Input Format
First line contains three space separated integers $$$s_y$$$, $$$a$$$ and $$$b$$$ ($$$-10^9 \le s_y < 0$$$, $$$1 \le a < b \le 10^9$$$) — corresponding coordinates of the light source.
Second line contains single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — number of segments in the fence.
Next $$$n$$$ lines contain two integers per line: $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i < r_i \le 10^9$$$, $$$r_{i - 1} < l_i$$$) — segments in the fence in increasing order. Segments don't intersect or touch each other.
Next line contains single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — number of points to check.
Next $$$q$$$ lines contain two integers per line: $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le 10^9$$$) — points to process.
## Output Format
Print $$$q$$$ lines. The $$$i$$$-th line should contain one real number — total time of the $$$i$$$-th point being in the shade, while the light source is moving from $$$(a, s_y)$$$ to the $$$(b, s_y)$$$. The answer is considered as correct if its absolute of relative error doesn't exceed $$$10^{-6}$$$.
## Examples
```input
-3 1 6
2
2 4
6 7
5
3 1
1 3
6 1
6 4
7 6
```
```output
5.000000000000000
3.000000000000000
0.000000000000000
1.500000000000000
2.000000000000000
```
## Note
- The 1-st point is always in the shade;
- the 2-nd point is in the shade while light source is moving from $$$(3, -3)$$$ to $$$(6, -3)$$$;
- the 3-rd point is in the shade while light source is at point $$$(6, -3)$$$.
- the 4-th point is in the shade while light source is moving from $$$(1, -3)$$$ to $$$(2.5, -3)$$$ and at point $$$(6, -3)$$$;
- the 5-th point is in the shade while light source is moving from $$$(1, -3)$$$ to $$$(2.5, -3)$$$ and from $$$(5.5, -3)$$$ to $$$(6, -3)$$$;
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
There are $$$n$$$ cities in the country of Berland. Some of them are connected by bidirectional roads in such a way that there exists exactly one path, which visits each road no more than once, between every pair of cities. Each road has its own length. Cities are numbered from $$$1$$$ to $$$n$$$.
The travelling time between some cities $$$v$$$ and $$$u$$$ is the total length of the roads on the shortest path from $$$v$$$ to $$$u$$$.
The two most important cities in Berland are cities $$$1$$$ and $$$n$$$.
The Berland Ministry of Transport decided to build a single new road to decrease the traffic between the most important cities. However, lots of people are used to the current travelling time between the most important cities, so the new road shouldn't change it too much.
The new road can only be built between such cities $$$v$$$ and $$$u$$$ that $$$v \neq u$$$ and $$$v$$$ and $$$u$$$ aren't already connected by some road.
They came up with $$$m$$$ possible projects. Each project is just the length $$$x$$$ of the new road.
Polycarp works as a head analyst at the Berland Ministry of Transport and it's his job to deal with all those $$$m$$$ projects. For the $$$i$$$-th project he is required to choose some cities $$$v$$$ and $$$u$$$ to build the new road of length $$$x_i$$$ between such that the travelling time between the most important cities is maximal possible.
Unfortunately, Polycarp is not a programmer and no analyst in the world is capable to process all projects using only pen and paper.
Thus, he asks you to help him to calculate the maximal possible travelling time between the most important cities for each project. Note that the choice of $$$v$$$ and $$$u$$$ can differ for different projects.
## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 3 \cdot 10^5$$$, $$$1 \le m \le 3 \cdot 10^5$$$) — the number of cities and the number of projects, respectively.
Each of the next $$$n - 1$$$ lines contains three integers $$$v_i$$$, $$$u_i$$$ and $$$w_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$1 \le w_i \le 10^9$$$) — the description of the $$$i$$$-th road. It is guaranteed that there exists exactly one path, which visits each road no more than once, between every pair of cities.
Each of the next $$$m$$$ lines contains a single integer $$$x_j$$$ ($$$1 \le x_j \le 10^9$$$) — the length of the road for the $$$j$$$-th project.
## Output Format
Print $$$m$$$ lines, the $$$j$$$-th line should contain a single integer — the maximal possible travelling time between the most important cities for the $$$j$$$-th project.
## Examples
```input
7 2
1 2 18
2 3 22
3 4 24
4 7 24
2 6 4
3 5 12
1
100
```
```output
83
88
```
## Note
The road network from the first example:
You can build the road with length $$$1$$$ between cities $$$5$$$ and $$$6$$$ to get $$$83$$$ as the travelling time between $$$1$$$ and $$$7$$$ ($$$1 \rightarrow 2 \rightarrow 6 \rightarrow 5 \rightarrow 3 \rightarrow 4 \rightarrow 7$$$ $$$=$$$ $$$18 + 4 + 1 + 12 + 24 + 24 = 83$$$). Other possible pairs of cities will give answers less or equal to $$$83$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1016_1016/G
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Since next season are coming, you'd like to form a team from two or three participants. There are $$$n$$$ candidates, the $$$i$$$-th candidate has rank $$$a_i$$$. But you have weird requirements for your teammates: if you have rank $$$v$$$ and have chosen the $$$i$$$-th and $$$j$$$-th candidate, then $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$ must be met.
You are very experienced, so you can change your rank to any non-negative integer but $$$X$$$ and $$$Y$$$ are tied with your birthdate, so they are fixed.
Now you want to know, how many are there pairs $$$(i, j)$$$ such that there exists an integer $$$v$$$ meeting the following constraints: $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$. It's possible that $$$i = j$$$ and you form a team of two.
$$$GCD$$$ is the greatest common divisor of two number, $$$LCM$$$ — the least common multiple.
## Input Format
First line contains three integers $$$n$$$, $$$X$$$ and $$$Y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le X \le Y \le 10^{18}$$$) — the number of candidates and corresponding constants.
Second line contains $$$n$$$ space separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$) — ranks of candidates.
## Output Format
Print the only integer — the number of pairs $$$(i, j)$$$ such that there exists an integer $$$v$$$ meeting the following constraints: $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$. It's possible that $$$i = j$$$.
## Examples
```input
12 2 2
1 2 3 4 5 6 7 8 9 10 11 12
```
```output
12
```
-----
```input
12 1 6
1 3 5 7 9 11 12 10 8 6 4 2
```
```output
30
```
## Note
In the first example next pairs are valid: $$$a_j = 1$$$ and $$$a_i = [2, 4, 6, 8, 10, 12]$$$ or $$$a_j = 2$$$ and $$$a_i = [2, 4, 6, 8, 10, 12]$$$. The $$$v$$$ in both cases can be equal to $$$2$$$.
In the second example next pairs are valid:
- $$$a_j = 1$$$ and $$$a_i = [1, 5, 7, 11]$$$;
- $$$a_j = 2$$$ and $$$a_i = [1, 5, 7, 11, 10, 8, 4, 2]$$$;
- $$$a_j = 3$$$ and $$$a_i = [1, 3, 5, 7, 9, 11]$$$;
- $$$a_j = 6$$$ and $$$a_i = [1, 3, 5, 7, 9, 11, 12, 10, 8, 6, 4, 2]$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1023_1023/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.
The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.
For example, if $$$s=$$$"aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba".
If the given string $$$t$$$ matches the given string $$$s$$$, print "YES", otherwise print "NO".
## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the length of the string $$$t$$$, respectively.
The second line contains string $$$s$$$ of length $$$n$$$, which consists of lowercase Latin letters and at most one wildcard character '*'.
The third line contains string $$$t$$$ of length $$$m$$$, which consists only of lowercase Latin letters.
## Output Format
Print "YES" (without quotes), if you can obtain the string $$$t$$$ from the string $$$s$$$. Otherwise print "NO" (without quotes).
## Examples
```input
6 10
code*s
codeforces
```
```output
YES
```
-----
```input
6 5
vk*cup
vkcup
```
```output
YES
```
-----
```input
1 1
v
k
```
```output
NO
```
-----
```input
9 6
gfgf*gfgf
gfgfgf
```
```output
NO
```
## Note
In the first example a wildcard character '*' can be replaced with a string "force". So the string $$$s$$$ after this replacement is "codeforces" and the answer is "YES".
In the second example a wildcard character '*' can be replaced with an empty string. So the string $$$s$$$ after this replacement is "vkcup" and the answer is "YES".
There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO".
In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $$$t$$$ so the answer is "NO".
Now solve the problem and return the code.
|
{}
|
codeforces_1023_1023/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Tanechka is shopping in the toy shop. There are exactly $$$n$$$ toys in the shop for sale, the cost of the $$$i$$$-th toy is $$$i$$$ burles. She wants to choose two toys in such a way that their total cost is $$$k$$$ burles. How many ways to do that does she have?
Each toy appears in the shop exactly once. Pairs $$$(a, b)$$$ and $$$(b, a)$$$ are considered equal. Pairs $$$(a, b)$$$, where $$$a=b$$$, are not allowed.
## Input Format
The first line of the input contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n, k \le 10^{14}$$$) — the number of toys and the expected total cost of the pair of toys.
## Output Format
Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $$$k$$$ burles.
## Examples
```input
8 5
```
```output
2
```
-----
```input
8 15
```
```output
1
```
-----
```input
7 20
```
```output
0
```
-----
```input
1000000000000 1000000000001
```
```output
500000000000
```
## Note
In the first example Tanechka can choose the pair of toys ($$$1, 4$$$) or the pair of toys ($$$2, 3$$$).
In the second example Tanechka can choose only the pair of toys ($$$7, 8$$$).
In the third example choosing any pair of toys will lead to the total cost less than $$$20$$$. So the answer is 0.
In the fourth example she can choose the following pairs: $$$(1, 1000000000000)$$$, $$$(2, 999999999999)$$$, $$$(3, 999999999998)$$$, ..., $$$(500000000000, 500000000001)$$$. The number of such pairs is exactly $$$500000000000$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1023_1023/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$.
Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment.
We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.
So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$.
Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.
If there are multiple possible arrays then print any of them.
## Input Format
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of elements of the array and the number of queries perfomed on it.
The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le q$$$) — the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$.
## Output Format
Print "YES" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 \le l_i \le r_i \le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment.
Otherwise print "NO".
If some array can be obtained then print $$$n$$$ integers on the second line — the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries.
If there are multiple possible arrays then print any of them.
## Examples
```input
4 3
1 0 2 3
```
```output
YES
1 2 2 3
```
-----
```input
3 10
10 10 10
```
```output
YES
10 10 10
```
-----
```input
5 6
6 5 6 2 2
```
```output
NO
```
-----
```input
3 5
0 0 0
```
```output
YES
5 4 2
```
## Note
In the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$.
In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$.
The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$.
There is a lot of correct resulting arrays for the fourth example.
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. $$$n$$$ is even.
For each position $$$i$$$ ($$$1 \le i \le n$$$) in string $$$s$$$ you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every position must be changed exactly once.
For example, letter 'p' should be changed either to 'o' or to 'q', letter 'a' should be changed to 'b' and letter 'z' should be changed to 'y'.
That way string "codeforces", for example, can be changed to "dpedepqbft" ('c' $$$\rightarrow$$$ 'd', 'o' $$$\rightarrow$$$ 'p', 'd' $$$\rightarrow$$$ 'e', 'e' $$$\rightarrow$$$ 'd', 'f' $$$\rightarrow$$$ 'e', 'o' $$$\rightarrow$$$ 'p', 'r' $$$\rightarrow$$$ 'q', 'c' $$$\rightarrow$$$ 'b', 'e' $$$\rightarrow$$$ 'f', 's' $$$\rightarrow$$$ 't').
String $$$s$$$ is called a palindrome if it reads the same from left to right and from right to left. For example, strings "abba" and "zz" are palindromes and strings "abca" and "zy" are not.
Your goal is to check if it's possible to make string $$$s$$$ a palindrome by applying the aforementioned changes to every position. Print "YES" if string $$$s$$$ can be transformed to a palindrome and "NO" otherwise.
Each testcase contains several strings, for each of them you are required to solve the problem separately.
## Input Format
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 50$$$) — the number of strings in a testcase.
Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th string. The first line of the pair contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$, $$$n$$$ is even) — the length of the corresponding string. The second line of the pair contains a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.
## Output Format
Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th string of the input. Print "YES" if it's possible to make the $$$i$$$-th string a palindrome by applying the aforementioned changes to every position. Print "NO" otherwise.
## Examples
```input
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
```
```output
YES
NO
YES
NO
NO
```
## Note
The first string of the example can be changed to "bcbbcb", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters.
The second string can be changed to "be", "bg", "de", "dg", but none of these resulting strings are palindromes.
The third string can be changed to "beeb" which is a palindrome.
The fifth string can be changed to "lk", "lm", "nk", "nm", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings "ll" or "mm".
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You have $$$n$$$ sticks of the given lengths.
Your task is to choose exactly four of them in such a way that they can form a rectangle. No sticks can be cut to pieces, each side of the rectangle must be formed by a single stick. No stick can be chosen multiple times. It is guaranteed that it is always possible to choose such sticks.
Let $$$S$$$ be the area of the rectangle and $$$P$$$ be the perimeter of the rectangle.
The chosen rectangle should have the value $$$\frac{P^2}{S}$$$ minimal possible. The value is taken without any rounding.
If there are multiple answers, print any of them.
Each testcase contains several lists of sticks, for each of them you are required to solve the problem separately.
## Input Format
The first line contains a single integer $$$T$$$ ($$$T \ge 1$$$) — the number of lists of sticks in the testcase.
Then $$$2T$$$ lines follow — lines $$$(2i - 1)$$$ and $$$2i$$$ of them describe the $$$i$$$-th list. The first line of the pair contains a single integer $$$n$$$ ($$$4 \le n \le 10^6$$$) — the number of sticks in the $$$i$$$-th list. The second line of the pair contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_j \le 10^4$$$) — lengths of the sticks in the $$$i$$$-th list.
It is guaranteed that for each list there exists a way to choose four sticks so that they form a rectangle.
The total number of sticks in all $$$T$$$ lists doesn't exceed $$$10^6$$$ in each testcase.
## Output Format
Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th list of the input. That is the lengths of the four sticks you choose from the $$$i$$$-th list, so that they form a rectangle and the value $$$\frac{P^2}{S}$$$ of this rectangle is minimal possible. You can print these four lengths in arbitrary order.
If there are multiple answers, print any of them.
## Examples
```input
3
4
7 2 2 7
8
2 8 1 4 8 2 1 5
5
5 5 5 5 5
```
```output
2 7 7 2
2 2 1 1
5 5 5 5
```
## Note
There is only one way to choose four sticks in the first list, they form a rectangle with sides $$$2$$$ and $$$7$$$, its area is $$$2 \cdot 7 = 14$$$, perimeter is $$$2(2 + 7) = 18$$$. $$$\frac{18^2}{14} \approx 23.143$$$.
The second list contains subsets of four sticks that can form rectangles with sides $$$(1, 2)$$$, $$$(2, 8)$$$ and $$$(1, 8)$$$. Their values are $$$\frac{6^2}{2} = 18$$$, $$$\frac{20^2}{16} = 25$$$ and $$$\frac{18^2}{8} = 40.5$$$, respectively. The minimal one of them is the rectangle $$$(1, 2)$$$.
You can choose any four of the $$$5$$$ given sticks from the third list, they will form a square with side $$$5$$$, which is still a rectangle with sides $$$(5, 5)$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $$$80\%$$$ of applicants are girls and majority of them are going to live in the university dormitory for the next $$$4$$$ (hopefully) years.
The dormitory consists of $$$n$$$ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $$$i$$$ costs $$$c_i$$$ burles. Rooms are numbered from $$$1$$$ to $$$n$$$.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room $$$i$$$ in second $$$t$$$ then it will run to room $$$a_i$$$ in second $$$t + 1$$$ without visiting any other rooms inbetween ($$$i = a_i$$$ means that mouse won't leave room $$$i$$$). It's second $$$0$$$ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.
That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $$$1$$$ to $$$n$$$ at second $$$0$$$.
What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
## Input Format
The first line contains as single integers $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of rooms in the dormitory.
The second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 10^4$$$) — $$$c_i$$$ is the cost of setting the trap in room number $$$i$$$.
The third line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — $$$a_i$$$ is the room the mouse will run to the next second after being in room $$$i$$$.
## Output Format
Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
## Examples
```input
5
1 2 3 2 10
1 3 4 3 3
```
```output
3
```
-----
```input
4
1 10 2 10
2 4 2 2
```
```output
10
```
-----
```input
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
```
```output
2
```
## Note
In the first example it is enough to set mouse trap in rooms $$$1$$$ and $$$4$$$. If mouse starts in room $$$1$$$ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $$$4$$$.
In the second example it is enough to set mouse trap in room $$$2$$$. If mouse starts in room $$$2$$$ then it gets caught immideately. If mouse starts in any other room then it runs to room $$$2$$$ in second $$$1$$$.
Here are the paths of the mouse from different starts from the third example:
- $$$1 \rightarrow 2 \rightarrow 2 \rightarrow \dots$$$;
- $$$2 \rightarrow 2 \rightarrow \dots$$$;
- $$$3 \rightarrow 2 \rightarrow 2 \rightarrow \dots$$$;
- $$$4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots$$$;
- $$$5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots$$$;
- $$$6 \rightarrow 7 \rightarrow 6 \rightarrow \dots$$$;
- $$$7 \rightarrow 6 \rightarrow 7 \rightarrow \dots$$$;
So it's enough to set traps in rooms $$$2$$$ and $$$6$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a square board, consisting of $$$n$$$ rows and $$$n$$$ columns. Each tile in it should be colored either white or black.
Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well.
Let's call some coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least $$$k$$$ tiles.
Your task is to count the number of suitable colorings of the board of the given size.
Since the answer can be very large, print it modulo $$$998244353$$$.
## Input Format
A single line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 500$$$, $$$1 \le k \le n^2$$$) — the number of rows and columns of the board and the maximum number of tiles inside the rectangle of the single color, respectively.
## Output Format
Print a single integer — the number of suitable colorings of the board of the given size modulo $$$998244353$$$.
## Examples
```input
1 1
```
```output
0
```
-----
```input
2 3
```
```output
6
```
-----
```input
49 1808
```
```output
359087121
```
## Note
Board of size $$$1 \times 1$$$ is either a single black tile or a single white tile. Both of them include a rectangle of a single color, consisting of $$$1$$$ tile.
Here are the beautiful colorings of a board of size $$$2 \times 2$$$ that don't include rectangles of a single color, consisting of at least $$$3$$$ tiles:
The rest of beautiful colorings of a board of size $$$2 \times 2$$$ are the following:
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 4.0 seconds
Memory limit: 256.0 MB
# Problem
Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly $$$n$$$ exams.
For the each exam $$$i$$$ there are known two days: $$$a_i$$$ — day of the first opportunity to pass the exam, $$$b_i$$$ — day of the second opportunity to pass the exam ($$$a_i < b_i$$$). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the $$$n$$$ exams.
Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the $$$n$$$ exams, or print -1 if he cannot pass all the exams at all.
## Input Format
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the number of exams.
The next $$$n$$$ lines contain two integers each: $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i < b_i \le 10^9$$$), where $$$a_i$$$ is the number of day of the first passing the $$$i$$$-th exam and $$$b_i$$$ is the number of day of the second passing the $$$i$$$-th exam.
## Output Format
If Polycarp cannot pass all the $$$n$$$ exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that.
## Examples
```input
2
1 5
1 7
```
```output
5
```
-----
```input
3
5 13
1 5
1 7
```
```output
7
```
-----
```input
3
10 40
40 80
10 80
```
```output
80
```
-----
```input
3
99 100
99 100
99 100
```
```output
-1
```
Now solve the problem and return the code.
|
{}
|
codeforces_1027_1027/G
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.5 seconds
Memory limit: 256.0 MB
# Problem
The campus has $$$m$$$ rooms numbered from $$$0$$$ to $$$m - 1$$$. Also the $$$x$$$-mouse lives in the campus. The $$$x$$$-mouse is not just a mouse: each second $$$x$$$-mouse moves from room $$$i$$$ to the room $$$i \cdot x \mod{m}$$$ (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the $$$x$$$-mouse is unknown.
You are responsible to catch the $$$x$$$-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the $$$x$$$-mouse enters a trapped room, it immediately gets caught.
And the only observation you made is $$$\text{GCD} (x, m) = 1$$$.
## Input Format
The only line contains two integers $$$m$$$ and $$$x$$$ ($$$2 \le m \le 10^{14}$$$, $$$1 \le x < m$$$, $$$\text{GCD} (x, m) = 1$$$) — the number of rooms and the parameter of $$$x$$$-mouse.
## Output Format
Print the only integer — minimum number of traps you need to install to catch the $$$x$$$-mouse.
## Examples
```input
4 3
```
```output
3
```
-----
```input
5 2
```
```output
2
```
## Note
In the first example you can, for example, put traps in rooms $$$0$$$, $$$2$$$, $$$3$$$. If the $$$x$$$-mouse starts in one of this rooms it will be caught immediately. If $$$x$$$-mouse starts in the $$$1$$$-st rooms then it will move to the room $$$3$$$, where it will be caught.
In the second example you can put one trap in room $$$0$$$ and one trap in any other room since $$$x$$$-mouse will visit all rooms $$$1..m-1$$$ if it will start in any of these rooms.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number $$$x$$$. For example, if $$$x = 4$$$ and there were $$$3$$$ keyboards in the store, then the devices had indices $$$4$$$, $$$5$$$ and $$$6$$$, and if $$$x = 10$$$ and there were $$$7$$$ of them then the keyboards had indices $$$10$$$, $$$11$$$, $$$12$$$, $$$13$$$, $$$14$$$, $$$15$$$ and $$$16$$$.
After the heist, only $$$n$$$ keyboards remain, and they have indices $$$a_1, a_2, \dots, a_n$$$. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither $$$x$$$ nor the number of keyboards in the store before the heist.
## Input Format
The first line contains single integer $$$n$$$ $$$(1 \le n \le 1\,000)$$$ — the number of keyboards in the store that remained after the heist.
The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^{9})$$$ — the indices of the remaining keyboards. The integers $$$a_i$$$ are given in arbitrary order and are pairwise distinct.
## Output Format
Print the minimum possible number of keyboards that have been stolen if the staff remember neither $$$x$$$ nor the number of keyboards in the store before the heist.
## Examples
```input
4
10 13 12 8
```
```output
2
```
-----
```input
5
7 5 6 4 8
```
```output
0
```
## Note
In the first example, if $$$x=8$$$ then minimum number of stolen keyboards is equal to $$$2$$$. The keyboards with indices $$$9$$$ and $$$11$$$ were stolen during the heist.
In the second example, if $$$x=4$$$ then nothing was stolen during the heist.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than $$$a$$$ and screen height not greater than $$$b$$$. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is $$$w$$$, and the height of the screen is $$$h$$$, then the following condition should be met: $$$\frac{w}{h} = \frac{x}{y}$$$.
There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers $$$w$$$ and $$$h$$$ there is a TV set with screen width $$$w$$$ and height $$$h$$$ in the shop.
Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers $$$w$$$ and $$$h$$$, beforehand, such that $$$(w \le a)$$$, $$$(h \le b)$$$ and $$$(\frac{w}{h} = \frac{x}{y})$$$.
In other words, Monocarp wants to determine the number of TV sets having aspect ratio $$$\frac{x}{y}$$$, screen width not exceeding $$$a$$$, and screen height not exceeding $$$b$$$. Two TV sets are considered different if they have different screen width or different screen height.
## Input Format
The first line contains four integers $$$a$$$, $$$b$$$, $$$x$$$, $$$y$$$ ($$$1 \le a, b, x, y \le 10^{18}$$$) — the constraints on the screen width and height, and on the aspect ratio.
## Output Format
Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.
## Examples
```input
17 15 5 3
```
```output
3
```
-----
```input
14 16 7 22
```
```output
0
```
-----
```input
4 2 6 4
```
```output
1
```
-----
```input
1000000000000000000 1000000000000000000 999999866000004473 999999822000007597
```
```output
1000000063
```
## Note
In the first example, there are $$$3$$$ possible variants: $$$(5, 3)$$$, $$$(10, 6)$$$, $$$(15, 9)$$$.
In the second example, there is no TV set meeting the constraints.
In the third example, there is only one variant: $$$(3, 2)$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Recently Monocarp got a job. His working day lasts exactly $$$m$$$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $$$n$$$ minutes $$$a_1, a_2, \dots, a_n$$$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute).
However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $$$a_i$$$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $$$d$$$ minutes pass between any two coffee breaks. Monocarp also wants to take these $$$n$$$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $$$d$$$ minutes pass between the end of any working day and the start of the following working day.
For each of the $$$n$$$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.
## Input Format
The first line contains three integers $$$n$$$, $$$m$$$, $$$d$$$ $$$(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$$$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.
The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le m)$$$, where $$$a_i$$$ is some minute when Monocarp wants to have a coffee break.
## Output Format
In the first line, write the minimum number of days required to make a coffee break in each of the $$$n$$$ given minutes.
In the second line, print $$$n$$$ space separated integers. The $$$i$$$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $$$a_i$$$. Days are numbered from $$$1$$$. If there are multiple optimal solutions, you may print any of them.
## Examples
```input
4 5 3
3 5 1 2
```
```output
3
3 1 1 2
```
-----
```input
10 10 1
10 5 7 4 6 3 2 1 9 8
```
```output
2
2 1 1 2 2 1 2 1 1 2
```
## Note
In the first example, Monocarp can take two coffee breaks during the first day (during minutes $$$1$$$ and $$$5$$$, $$$3$$$ minutes will pass between these breaks). One break during the second day (at minute $$$2$$$), and one break during the third day (at minute $$$3$$$).
In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
A plane is flying at a constant height of $$$h$$$ meters above the ground surface. Let's consider that it is flying from the point $$$(-10^9, h)$$$ to the point $$$(10^9, h)$$$ parallel with $$$Ox$$$ axis.
A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to $$$Ox$$$ axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.
There are ascending air flows on certain segments, each such segment is characterized by two numbers $$$x_1$$$ and $$$x_2$$$ ($$$x_1 < x_2$$$) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along $$$Ox$$$ axis, covering one unit of distance every second.
If the glider jumps out at $$$1$$$, he will stop at $$$10$$$. Otherwise, if he jumps out at $$$2$$$, he will stop at $$$12$$$.
Determine the maximum distance along $$$Ox$$$ axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is $$$0$$$.
## Input Format
The first line contains two integers $$$n$$$ and $$$h$$$ $$$(1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9})$$$ — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.
Each of the next $$$n$$$ lines contains two integers $$$x_{i1}$$$ and $$$x_{i2}$$$ $$$(1 \le x_{i1} < x_{i2} \le 10^{9})$$$ — the endpoints of the $$$i$$$-th ascending air flow segment. No two segments intersect, and they are given in ascending order.
## Output Format
Print one integer — the maximum distance along $$$Ox$$$ axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.
## Examples
```input
3 4
2 5
7 9
10 11
```
```output
10
```
-----
```input
5 10
5 7
11 12
16 20
25 26
30 33
```
```output
18
```
-----
```input
1 1000000000
1 1000000000
```
```output
1999999999
```
## Note
In the first example if the glider can jump out at $$$(2, 4)$$$, then the landing point is $$$(12, 0)$$$, so the distance is $$$12-2 = 10$$$.
In the second example the glider can fly from $$$(16,10)$$$ to $$$(34,0)$$$, and the distance is $$$34-16=18$$$.
In the third example the glider can fly from $$$(-100,1000000000)$$$ to $$$(1999999899,0)$$$, so the distance is $$$1999999899-(-100)=1999999999$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $$$1$$$ to $$$n$$$. For every edge $$$e$$$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $$$e$$$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $$$n - 1$$$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
## Input Format
The first line contains one integer $$$n$$$ ($$$2 \le n \le 1\,000$$$) — the number of vertices in the tree.
Each of the next $$$n-1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ each ($$$1 \le a_i < b_i \le n$$$) — the maximal indices of vertices in the components formed if the $$$i$$$-th edge is removed.
## Output Format
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $$$n - 1$$$ lines. Each of the last $$$n - 1$$$ lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$) — vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
## Examples
```input
4
3 4
1 4
3 4
```
```output
YES
1 3
3 2
2 4
```
-----
```input
3
1 3
1 3
```
```output
NO
```
-----
```input
3
1 2
2 3
```
```output
NO
```
## Note
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.
Now solve the problem and return the code.
|
{}
|
codeforces_1041_1041/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a tube which is reflective inside represented as two non-coinciding, but parallel to $$$Ox$$$ lines. Each line has some special integer points — positions of sensors on sides of the tube.
You are going to emit a laser ray in the tube. To do so, you have to choose two integer points $$$A$$$ and $$$B$$$ on the first and the second line respectively (coordinates can be negative): the point $$$A$$$ is responsible for the position of the laser, and the point $$$B$$$ — for the direction of the laser ray. The laser ray is a ray starting at $$$A$$$ and directed at $$$B$$$ which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor.
Examples of laser rays. Note that image contains two examples. The $$$3$$$ sensors (denoted by black bold points on the tube sides) will register the blue ray but only $$$2$$$ will register the red.
Calculate the maximum number of sensors which can register your ray if you choose points $$$A$$$ and $$$B$$$ on the first and the second lines respectively.
## Input Format
The first line contains two integers $$$n$$$ and $$$y_1$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le y_1 \le 10^9$$$) — number of sensors on the first line and its $$$y$$$ coordinate.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — $$$x$$$ coordinates of the sensors on the first line in the ascending order.
The third line contains two integers $$$m$$$ and $$$y_2$$$ ($$$1 \le m \le 10^5$$$, $$$y_1 < y_2 \le 10^9$$$) — number of sensors on the second line and its $$$y$$$ coordinate.
The fourth line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$0 \le b_i \le 10^9$$$) — $$$x$$$ coordinates of the sensors on the second line in the ascending order.
## Output Format
Print the only integer — the maximum number of sensors which can register the ray.
## Examples
```input
3 1
1 5 6
1 3
3
```
```output
3
```
## Note
One of the solutions illustrated on the image by pair $$$A_2$$$ and $$$B_2$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1059_1059/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Vasya has recently got a job as a cashier at a local store. His day at work is $$$L$$$ minutes long. Vasya has already memorized $$$n$$$ regular customers, the $$$i$$$-th of which comes after $$$t_{i}$$$ minutes after the beginning of the day, and his service consumes $$$l_{i}$$$ minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer.
Vasya is a bit lazy, so he likes taking smoke breaks for $$$a$$$ minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day?
## Input Format
The first line contains three integers $$$n$$$, $$$L$$$ and $$$a$$$ ($$$0 \le n \le 10^{5}$$$, $$$1 \le L \le 10^{9}$$$, $$$1 \le a \le L$$$).
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$t_{i}$$$ and $$$l_{i}$$$ ($$$0 \le t_{i} \le L - 1$$$, $$$1 \le l_{i} \le L$$$). It is guaranteed that $$$t_{i} + l_{i} \le t_{i + 1}$$$ and $$$t_{n} + l_{n} \le L$$$.
## Output Format
Output one integer — the maximum number of breaks.
## Examples
```input
2 11 3
0 1
1 1
```
```output
3
```
-----
```input
0 5 2
```
```output
2
```
-----
```input
1 3 2
1 2
```
```output
0
```
## Note
In the first sample Vasya can take $$$3$$$ breaks starting after $$$2$$$, $$$5$$$ and $$$8$$$ minutes after the beginning of the day.
In the second sample Vasya can take $$$2$$$ breaks starting after $$$0$$$ and $$$2$$$ minutes after the beginning of the day.
In the third sample Vasya can't take any breaks.
Now solve the problem and return the code.
|
{}
|
codeforces_1059_1059/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn't give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor's handwriting to make a counterfeit certificate of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor's signature is impossible to forge. Or is it?
For simplicity, the signature is represented as an $$$n\times m$$$ grid, where every cell is either filled with ink or empty. Andrey's pen can fill a $$$3\times3$$$ square without its central cell if it is completely contained inside the grid, as shown below.
xxxx.xxxx
Determine whether is it possible to forge the signature on an empty $$$n\times m$$$ grid.
## Input Format
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n, m \le 1000$$$).
Then $$$n$$$ lines follow, each contains $$$m$$$ characters. Each of the characters is either '.', representing an empty cell, or '#', representing an ink filled cell.
## Output Format
If Andrey can forge the signature, output "YES". Otherwise output "NO".
You can print each letter in any case (upper or lower).
## Examples
```input
3 3
###
#.#
###
```
```output
YES
```
-----
```input
3 3
###
###
###
```
```output
NO
```
-----
```input
4 3
###
###
###
###
```
```output
YES
```
-----
```input
5 7
.......
.#####.
.#.#.#.
.#####.
.......
```
```output
YES
```
## Note
In the first sample Andrey can paint the border of the square with the center in $$$(2, 2)$$$.
In the second sample the signature is impossible to forge.
In the third sample Andrey can paint the borders of the squares with the centers in $$$(2, 2)$$$ and $$$(3, 2)$$$:
1. we have a clear paper: ............
2. use the pen with center at $$$(2, 2)$$$. ####.####...
3. use the pen with center at $$$(3, 2)$$$. ############
In the fourth sample Andrey can paint the borders of the squares with the centers in $$$(3, 3)$$$ and $$$(3, 5)$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1059_1059/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Let's call the following process a transformation of a sequence of length $$$n$$$.
If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of $$$n$$$ integers: the greatest common divisors of all the elements in the sequence before each deletion.
You are given an integer sequence $$$1, 2, \dots, n$$$. Find the lexicographically maximum result of its transformation.
A sequence $$$a_1, a_2, \ldots, a_n$$$ is lexicographically larger than a sequence $$$b_1, b_2, \ldots, b_n$$$, if there is an index $$$i$$$ such that $$$a_j = b_j$$$ for all $$$j < i$$$, and $$$a_i > b_i$$$.
## Input Format
The first and only line of input contains one integer $$$n$$$ ($$$1\le n\le 10^6$$$).
## Output Format
Output $$$n$$$ integers — the lexicographically maximum result of the transformation.
## Examples
```input
3
```
```output
1 1 3
```
-----
```input
2
```
```output
1 2
```
-----
```input
1
```
```output
1
```
## Note
In the first sample the answer may be achieved this way:
- Append GCD$$$(1, 2, 3) = 1$$$, remove $$$2$$$.
- Append GCD$$$(1, 3) = 1$$$, remove $$$1$$$.
- Append GCD$$$(3) = 3$$$, remove $$$3$$$.
We get the sequence $$$[1, 1, 3]$$$ as the result.
Now solve the problem and return the code.
|
{}
|
codeforces_1059_1059/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a rooted tree on $$$n$$$ vertices, its root is the vertex number $$$1$$$. The $$$i$$$-th vertex contains a number $$$w_i$$$. Split it into the minimum possible number of vertical paths in such a way that each path contains no more than $$$L$$$ vertices and the sum of integers $$$w_i$$$ on each path does not exceed $$$S$$$. Each vertex should belong to exactly one path.
A vertical path is a sequence of vertices $$$v_1, v_2, \ldots, v_k$$$ where $$$v_i$$$ ($$$i \ge 2$$$) is the parent of $$$v_{i - 1}$$$.
## Input Format
The first line contains three integers $$$n$$$, $$$L$$$, $$$S$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le L \le 10^5$$$, $$$1 \le S \le 10^{18}$$$) — the number of vertices, the maximum number of vertices in one path and the maximum sum in one path.
The second line contains $$$n$$$ integers $$$w_1, w_2, \ldots, w_n$$$ ($$$1 \le w_i \le 10^9$$$) — the numbers in the vertices of the tree.
The third line contains $$$n - 1$$$ integers $$$p_2, \ldots, p_n$$$ ($$$1 \le p_i < i$$$), where $$$p_i$$$ is the parent of the $$$i$$$-th vertex in the tree.
## Output Format
Output one number — the minimum number of vertical paths. If it is impossible to split the tree, output $$$-1$$$.
## Examples
```input
3 1 3
1 2 3
1 1
```
```output
3
```
-----
```input
3 3 6
1 2 3
1 1
```
```output
2
```
-----
```input
1 1 10000
10001
```
```output
-1
```
## Note
In the first sample the tree is split into $$$\{1\},\ \{2\},\ \{3\}$$$.
In the second sample the tree is split into $$$\{1,\ 2\},\ \{3\}$$$ or $$$\{1,\ 3\},\ \{2\}$$$.
In the third sample it is impossible to split the tree.
Now solve the problem and return the code.
|
{}
|
codeforces_1068_1068/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Ivan is collecting coins. There are only $$$N$$$ different collectible coins, Ivan has $$$K$$$ of them. He will be celebrating his birthday soon, so all his $$$M$$$ freinds decided to gift him coins. They all agreed to three terms:
- Everyone must gift as many coins as others.
- All coins given to Ivan must be different.
- Not less than $$$L$$$ coins from gifts altogether, must be new in Ivan's collection.
But his friends don't know which coins have Ivan already got in his collection. They don't want to spend money so they want to buy minimum quantity of coins, that satisfy all terms, irrespective of the Ivan's collection. Help them to find this minimum number of coins or define it's not possible to meet all the terms.
## Input Format
The only line of input contains 4 integers $$$N$$$, $$$M$$$, $$$K$$$, $$$L$$$ ($$$1 \le K \le N \le 10^{18}$$$; $$$1 \le M, \,\, L \le 10^{18}$$$) — quantity of different coins, number of Ivan's friends, size of Ivan's collection and quantity of coins, that must be new in Ivan's collection.
## Output Format
Print one number — minimal number of coins one friend can gift to satisfy all the conditions. If it is impossible to satisfy all three conditions print "-1" (without quotes).
## Examples
```input
20 15 2 3
```
```output
1
```
-----
```input
10 11 2 4
```
```output
-1
```
## Note
In the first test, one coin from each friend is enough, as he will be presented with 15 different coins and 13 of them will definitely be new.
In the second test, Ivan has 11 friends, but there are only 10 different coins. So all friends can't present him different coins.
Now solve the problem and return the code.
|
{}
|
codeforces_1068_1068/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Ivan has number $$$b$$$. He is sorting through the numbers $$$a$$$ from $$$1$$$ to $$$10^{18}$$$, and for every $$$a$$$ writes $$$\frac{[a, \,\, b]}{a}$$$ on blackboard. Here $$$[a, \,\, b]$$$ stands for least common multiple of $$$a$$$ and $$$b$$$. Ivan is very lazy, that's why this task bored him soon. But he is interested in how many different numbers he would write on the board if he would finish the task. Help him to find the quantity of different numbers he would write on the board.
## Input Format
The only line contains one integer — $$$b$$$ $$$(1 \le b \le 10^{10})$$$.
## Output Format
Print one number — answer for the problem.
## Examples
```input
1
```
```output
1
```
-----
```input
2
```
```output
2
```
## Note
In the first example $$$[a, \,\, 1] = a$$$, therefore $$$\frac{[a, \,\, b]}{a}$$$ is always equal to $$$1$$$.
In the second example $$$[a, \,\, 2]$$$ can be equal to $$$a$$$ or $$$2 \cdot a$$$ depending on parity of $$$a$$$. $$$\frac{[a, \,\, b]}{a}$$$ can be equal to $$$1$$$ and $$$2$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1068_1068/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other.
Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} \times 10^{9}$$$.
Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal.
Ivan wants his arrangement of rooks to have following properties:
- For any color there is a rook of this color on a board;
- For any color the set of rooks of this color is connected;
- For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other.
Please help Ivan find such an arrangement.
## Input Format
The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 100$$$, $$$0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$$$) — number of colors and number of pairs of colors which harmonize with each other.
In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list.
## Output Format
Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color.
In the first line of block print one number $$$a_{i}$$$ ($$$1 \le a_{i} \le 5000$$$) — number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, \,\, y \le 10^{9}$$$) — coordinates of the next rook.
All rooks must be on different cells.
Total number of rooks must not exceed $$$5000$$$.
It is guaranteed that the solution exists.
## Examples
```input
3 2
1 2
2 3
```
```output
2
3 4
1 4
4
1 2
2 2
2 4
5 4
1
5 1
```
-----
```input
3 3
1 2
2 3
3 1
```
```output
1
1 1
1
1 2
1
1 3
```
-----
```input
3 1
1 3
```
```output
1
1 1
1
2 2
1
3 1
```
## Note
Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).
Now solve the problem and return the code.
|
{}
|
codeforces_1070_1070/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for $$$n$$$ consecutive days, which are numbered from $$$1$$$ to $$$n$$$. Buber requires $$$k$$$ CPU cores each day.
The cloud provider offers $$$m$$$ tariff plans, the $$$i$$$-th tariff plan is characterized by the following parameters:
- $$$l_i$$$ and $$$r_i$$$ — the $$$i$$$-th tariff plan is available only on days from $$$l_i$$$ to $$$r_i$$$, inclusive,
- $$$c_i$$$ — the number of cores per day available for rent on the $$$i$$$-th tariff plan,
- $$$p_i$$$ — the price of renting one core per day on the $$$i$$$-th tariff plan.
Buber can arbitrarily share its computing core needs between the tariff plans. Every day Buber can rent an arbitrary number of cores (from 0 to $$$c_i$$$) on each of the available plans. The number of rented cores on a tariff plan can vary arbitrarily from day to day.
Find the minimum amount of money that Buber will pay for its work for $$$n$$$ days from $$$1$$$ to $$$n$$$. If on a day the total number of cores for all available tariff plans is strictly less than $$$k$$$, then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly $$$k$$$ cores this day.
## Input Format
The first line of the input contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \le n,k \le 10^6, 1 \le m \le 2\cdot10^5$$$) — the number of days to analyze, the desired daily number of cores, the number of tariff plans.
The following $$$m$$$ lines contain descriptions of tariff plans, one description per line. Each line contains four integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$, $$$p_i$$$ ($$$1 \le l_i \le r_i \le n$$$, $$$1 \le c_i, p_i \le 10^6$$$), where $$$l_i$$$ and $$$r_i$$$ are starting and finishing days of the $$$i$$$-th tariff plan, $$$c_i$$$ — number of cores, $$$p_i$$$ — price of a single core for daily rent on the $$$i$$$-th tariff plan.
## Output Format
Print a single integer number — the minimal amount of money that Buber will pay.
## Examples
```input
5 7 3
1 4 5 3
1 3 5 2
2 5 10 1
```
```output
44
```
-----
```input
7 13 5
2 3 10 7
3 5 10 10
1 2 10 6
4 5 10 9
3 4 10 8
```
```output
462
```
-----
```input
4 100 3
3 3 2 5
1 1 3 2
2 4 4 4
```
```output
64
```
Now solve the problem and return the code.
|
{}
|
codeforces_1070_1070/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
Elections in Berland are coming. There are only two candidates — Alice and Bob.
The main Berland TV channel plans to show political debates. There are $$$n$$$ people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views:
- supporting none of candidates (this kind is denoted as "00"),
- supporting Alice but not Bob (this kind is denoted as "10"),
- supporting Bob but not Alice (this kind is denoted as "01"),
- supporting both candidates (this kind is denoted as "11").
The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions:
- at least half of spectators support Alice (i.e. $$$2 \cdot a \ge m$$$, where $$$a$$$ is number of spectators supporting Alice and $$$m$$$ is the total number of spectators),
- at least half of spectators support Bob (i.e. $$$2 \cdot b \ge m$$$, where $$$b$$$ is number of spectators supporting Bob and $$$m$$$ is the total number of spectators),
- the total influence of spectators is maximal possible.
Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.
## Input Format
The first line contains integer $$$n$$$ ($$$1 \le n \le 4\cdot10^5$$$) — the number of people who want to take part in the debate as a spectator.
These people are described on the next $$$n$$$ lines. Each line describes a single person and contains the string $$$s_i$$$ and integer $$$a_i$$$ separated by space ($$$1 \le a_i \le 5000$$$), where $$$s_i$$$ denotes person's political views (possible values — "00", "10", "01", "11") and $$$a_i$$$ — the influence of the $$$i$$$-th person.
## Output Format
Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.
## Examples
```input
6
11 6
10 4
01 3
00 3
00 7
00 9
```
```output
22
```
-----
```input
5
11 1
01 1
00 100
10 1
01 1
```
```output
103
```
-----
```input
6
11 19
10 22
00 18
00 29
11 29
10 28
```
```output
105
```
-----
```input
3
00 5000
00 5000
00 5000
```
```output
0
```
## Note
In the first example $$$4$$$ spectators can be invited to maximize total influence: $$$1$$$, $$$2$$$, $$$3$$$ and $$$6$$$. Their political views are: "11", "10", "01" and "00". So in total $$$2$$$ out of $$$4$$$ spectators support Alice and $$$2$$$ out of $$$4$$$ spectators support Bob. The total influence is $$$6+4+3+9=22$$$.
In the second example the direction can select all the people except the $$$5$$$-th person.
In the third example the direction can select people with indices: $$$1$$$, $$$4$$$, $$$5$$$ and $$$6$$$.
In the fourth example it is impossible to select any non-empty set of spectators.
Now solve the problem and return the code.
|
{}
|
codeforces_1070_1070/J
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
Berhattan is the capital of Berland. There are $$$n$$$ streets running parallel in the east-west direction (horizontally), and there are $$$m$$$ avenues running parallel in the south-north direction (vertically). Each street intersects with each avenue, forming a crossroad. So in total there are $$$n \cdot m$$$ crossroads in Berhattan.
Recently, the government has changed in Berland. The new government wants to name all avenues and all streets after heroes of revolution.
The special committee prepared the list of $$$k$$$ names. Only these names can be used as new names for streets and avenues. Each name can be used at most once.
The members of committee want to name streets and avenues in the way that minimizes inconvenience for residents. They believe that if street and avenue names start with the same letter, then their crossroad will be inconvenient. Hence only the first letter of each name matters.
Given first letters of $$$k$$$ names, find $$$C$$$ — minimal possible number of inconvenient crossroads in Berhattan after the naming process.
## Input Format
Input contains one or several test cases to process. The first line contains $$$t$$$ ($$$1 \le t \le 30000$$$) — the number of test cases. Solve test cases separately, test cases are completely independent and do not affect each other.
The description of $$$t$$$ test cases follows. Each test case starts with line with space-separated numbers $$$n, m, k$$$ ($$$1 \le n,m \le 30000$$$; $$$n+m \le k \le 2\cdot10^5$$$) — the number of streets, number of avenues and the number of names in the committee's list, respectively.
The the second line of each test case contains a string of $$$k$$$ uppercase English letters. $$$i$$$-th letter of the string is the first letter of $$$i$$$-th name from the committee's list.
It's guaranteed that the sum of numbers $$$n$$$ from all test cases is not greater than $$$30000$$$. Similarly, the sum of numbers $$$m$$$ from all test cases is not greater than $$$30000$$$. The sum of numbers $$$k$$$ from all test cases is not greater than $$$2\cdot10^5$$$.
## Output Format
For each test case print single number $$$C$$$ in the separate line — minimal possible number of inconvenient crossroads in Berhattan after the naming process.
## Examples
```input
2
2 3 9
EEZZEEZZZ
2 7 9
EEZZEEZZZ
```
```output
0
4
```
-----
```input
2
4 4 8
CZBBCZBC
1 1 4
TTCT
```
```output
1
0
```
Now solve the problem and return the code.
|
{}
|
codeforces_1070_1070/L
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 5.0 seconds
Memory limit: 256.0 MB
# Problem
Berland has $$$n$$$ cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.
The president of Berland decided to split country into $$$r$$$ states in such a way that each city will belong to exactly one of these $$$r$$$ states.
After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads.
The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it.
Please help president to find smallest possible $$$r$$$ for which such a split exists.
## Input Format
The input contains one or several test cases. The first input line contains a single integer number $$$t$$$ — number of test cases. Then, $$$t$$$ test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.
Then $$$t$$$ blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers $$$n$$$, $$$m$$$ ($$$1 \le n \le 2000$$$, $$$0 \le m \le 10000$$$) — the number of cities and number of roads in the Berland. Each of the next $$$m$$$ lines contains two space-separated integers — $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$; $$$x_i \ne y_i$$$), which denotes that the $$$i$$$-th road connects cities $$$x_i$$$ and $$$y_i$$$. Each pair of cities are connected by at most one road.
Sum of values $$$n$$$ across all test cases doesn't exceed $$$2000$$$. Sum of values $$$m$$$ across all test cases doesn't exceed $$$10000$$$.
## Output Format
For each test case first print a line containing a single integer $$$r$$$ — smallest possible number of states for which required split is possible. In the next line print $$$n$$$ space-separated integers in range from $$$1$$$ to $$$r$$$, inclusive, where the $$$j$$$-th number denotes number of state for the $$$j$$$-th city. If there are multiple solutions, print any.
## Examples
```input
2
5 3
1 2
2 5
1 5
6 5
1 2
2 3
3 4
4 2
4 1
```
```output
1
1 1 1 1 1
2
2 1 1 1 1 1
```
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games.
He came up with the following game. The player has a positive integer $$$n$$$. Initially the value of $$$n$$$ equals to $$$v$$$ and the player is able to do the following operation as many times as the player want (possibly zero): choose a positive integer $$$x$$$ that $$$x<n$$$ and $$$x$$$ is not a divisor of $$$n$$$, then subtract $$$x$$$ from $$$n$$$. The goal of the player is to minimize the value of $$$n$$$ in the end.
Soon, Chouti found the game trivial. Can you also beat the game?
## Input Format
The input contains only one integer in the first line: $$$v$$$ ($$$1 \le v \le 10^9$$$), the initial value of $$$n$$$.
## Output Format
Output a single integer, the minimum value of $$$n$$$ the player can get.
## Examples
```input
8
```
```output
1
```
-----
```input
1
```
```output
1
```
## Note
In the first example, the player can choose $$$x=3$$$ in the first turn, then $$$n$$$ becomes $$$5$$$. He can then choose $$$x=4$$$ in the second turn to get $$$n=1$$$ as the result. There are other ways to get this minimum. However, for example, he cannot choose $$$x=2$$$ in the first turn because $$$2$$$ is a divisor of $$$8$$$.
In the second example, since $$$n=1$$$ initially, the player can do nothing.
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced.
Chouti remembered that $$$n$$$ persons took part in that party. To make the party funnier, each person wore one hat among $$$n$$$ kinds of weird hats numbered $$$1, 2, \ldots n$$$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone.
After the party, the $$$i$$$-th person said that there were $$$a_i$$$ persons wearing a hat differing from his own.
It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $$$b_i$$$ be the number of hat type the $$$i$$$-th person was wearing, Chouti wants you to find any possible $$$b_1, b_2, \ldots, b_n$$$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all.
## Input Format
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the number of persons in the party.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n-1$$$), the statements of people.
## Output Format
If there is no solution, print a single line "Impossible".
Otherwise, print "Possible" and then $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$).
If there are multiple answers, print any of them.
## Examples
```input
3
0 0 0
```
```output
Possible
1 1 1
```
-----
```input
5
3 3 2 2 2
```
```output
Possible
1 1 2 2 2
```
-----
```input
4
0 1 2 3
```
```output
Impossible
```
## Note
In the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind $$$1$$$.
In the answer to the second example, the first and the second person wore the hat with type $$$1$$$ and all other wore a hat of type $$$2$$$.
So the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own.
In the third example, it can be shown that no solution exists.
In the first and the second example, other possible configurations are possible.
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $$$n$$$ bricks lined in a row on the ground. Chouti has got $$$m$$$ paint buckets of different colors at hand, so he painted each brick in one of those $$$m$$$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $$$k$$$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $$$998\,244\,353$$$.
## Input Format
The first and only line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \leq n,m \leq 2000, 0 \leq k \leq n-1$$$) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
## Output Format
Print one integer — the number of ways to color bricks modulo $$$998\,244\,353$$$.
## Examples
```input
3 3 0
```
```output
3
```
-----
```input
3 2 1
```
```output
4
```
## Note
In the first example, since $$$k=0$$$, the color of every brick should be the same, so there will be exactly $$$m=3$$$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $$$4$$$ possible colorings.
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. There are $$$k$$$ special vertices: $$$x_1, x_2, \ldots, x_k$$$.
Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.
For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.
The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.
## Input Format
The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \leq k \leq n \leq 10^5$$$, $$$n-1 \leq m \leq 10^5$$$) — the number of vertices, the number of edges and the number of special vertices.
The second line contains $$$k$$$ distinct integers $$$x_1, x_2, \ldots, x_k$$$ ($$$1 \leq x_i \leq n$$$).
Each of the following $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$w$$$ ($$$1 \leq u,v \leq n, 1 \leq w \leq 10^9$$$), denoting there is an edge between $$$u$$$ and $$$v$$$ of weight $$$w$$$. The given graph is undirected, so an edge $$$(u, v)$$$ can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.
## Output Format
The first and only line should contain $$$k$$$ integers. The $$$i$$$-th integer is the distance between $$$x_i$$$ and the farthest special vertex from it.
## Examples
```input
2 3 2
2 1
1 2 3
1 2 2
2 2 1
```
```output
2 2
```
-----
```input
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
```
```output
3 3 3
```
## Note
In the first example, the distance between vertex $$$1$$$ and $$$2$$$ equals to $$$2$$$ because one can walk through the edge of weight $$$2$$$ connecting them. So the distance to the farthest node for both $$$1$$$ and $$$2$$$ equals to $$$2$$$.
In the second example, one can find that distance between $$$1$$$ and $$$2$$$, distance between $$$1$$$ and $$$3$$$ are both $$$3$$$ and the distance between $$$2$$$ and $$$3$$$ is $$$2$$$.
The graph may have multiple edges between and self-loops, as in the first example.
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Chouti is working on a strange math problem.
There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a perfect square).
Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. $$$x_2, x_4, x_6, \ldots, x_n$$$. The task for him is to restore the original sequence. Again, it's your turn to help him.
The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.
## Input Format
The first line contains an even number $$$n$$$ ($$$2 \le n \le 10^5$$$).
The second line contains $$$\frac{n}{2}$$$ positive integers $$$x_2, x_4, \ldots, x_n$$$ ($$$1 \le x_i \le 2 \cdot 10^5$$$).
## Output Format
If there are no possible sequence, print "No".
Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \le x_i \le 10^{13}$$$), where $$$x_2, x_4, \ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any.
Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 \le x_i \le 10^{13}$$$.
## Examples
```input
6
5 11 44
```
```output
Yes
4 5 16 11 64 44
```
-----
```input
2
9900
```
```output
Yes
100 9900
```
-----
```input
6
314 1592 6535
```
```output
No
```
## Note
In the first example
- $$$x_1=4$$$
- $$$x_1+x_2=9$$$
- $$$x_1+x_2+x_3=25$$$
- $$$x_1+x_2+x_3+x_4=36$$$
- $$$x_1+x_2+x_3+x_4+x_5=100$$$
- $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$
In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer.
In the third example, it is possible to show, that no such sequence exists.
Now solve the problem and return the code.
|
{}
|
codeforces_1081_1081/G
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following:
Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation $$$a$$$ of $$$1, 2, \ldots, n$$$ the expected number of inversions after calling MergeSort(a, 1, n, k).
It can be proved that the expected number is rational. For the given prime $$$q$$$, suppose the answer can be denoted by $$$\frac{u}{d}$$$ where $$$gcd(u,d)=1$$$, you need to output an integer $$$r$$$ satisfying $$$0 \le r<q$$$ and $$$rd \equiv u \pmod q$$$. It can be proved that such $$$r$$$ exists and is unique.
## Input Format
The first and only line contains three integers $$$n, k, q$$$ ($$$1 \leq n, k \leq 10^5, 10^8 \leq q \leq 10^9$$$, $$$q$$$ is a prime).
## Output Format
The first and only line contains an integer $$$r$$$.
## Examples
```input
3 1 998244353
```
```output
499122178
```
-----
```input
3 2 998244353
```
```output
665496236
```
-----
```input
9 3 998244353
```
```output
449209967
```
-----
```input
9 4 998244353
```
```output
665496237
```
## Note
In the first example, all possible permutations are $$$[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]$$$.
With $$$k=1$$$, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is $$$9/6=3/2$$$, and you should output $$$499122178$$$ because $$$499122178 \times 2 \equiv 3 \pmod {998244353}$$$.
In the second example, all possible permutations are $$$[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]$$$ and the corresponding outputs of MergeSort(a, 1, n, k) are $$$[1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2]$$$ respectively. Thus the answer is $$$4/6=2/3$$$, and you should output $$$665496236$$$ because $$$665496236 \times 3 \equiv 2 \pmod {998244353}$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1099_1099/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Today's morning was exceptionally snowy. Meshanya decided to go outside and noticed a huge snowball rolling down the mountain! Luckily, there are two stones on that mountain.
Initially, snowball is at height $$$h$$$ and it has weight $$$w$$$. Each second the following sequence of events happens: snowball's weights increases by $$$i$$$, where $$$i$$$ — is the current height of snowball, then snowball hits the stone (if it's present at the current height), then snowball moves one meter down. If the snowball reaches height zero, it stops.
There are exactly two stones on the mountain. First stone has weight $$$u_1$$$ and is located at height $$$d_1$$$, the second one — $$$u_2$$$ and $$$d_2$$$ respectively. When the snowball hits either of two stones, it loses weight equal to the weight of that stone. If after this snowball has negative weight, then its weight becomes zero, but the snowball continues moving as before.
Find the weight of the snowball when it stops moving, that is, it reaches height 0.
## Input Format
First line contains two integers $$$w$$$ and $$$h$$$ — initial weight and height of the snowball ($$$0 \le w \le 100$$$; $$$1 \le h \le 100$$$).
Second line contains two integers $$$u_1$$$ and $$$d_1$$$ — weight and height of the first stone ($$$0 \le u_1 \le 100$$$; $$$1 \le d_1 \le h$$$).
Third line contains two integers $$$u_2$$$ and $$$d_2$$$ — weight and heigth of the second stone ($$$0 \le u_2 \le 100$$$; $$$1 \le d_2 \le h$$$; $$$d_1 \ne d_2$$$). Notice that stones always have different heights.
## Output Format
Output a single integer — final weight of the snowball after it reaches height 0.
## Examples
```input
4 3
1 1
1 2
```
```output
8
```
-----
```input
4 3
9 2
0 1
```
```output
1
```
## Note
In the first example, initially a snowball of weight 4 is located at a height of 3, there are two stones of weight 1, at a height of 1 and 2, respectively. The following events occur sequentially:
- The weight of the snowball increases by 3 (current height), becomes equal to 7.
- The snowball moves one meter down, the current height becomes equal to 2.
- The weight of the snowball increases by 2 (current height), becomes equal to 9.
- The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.
- The snowball moves one meter down, the current height becomes equal to 1.
- The weight of the snowball increases by 1 (current height), becomes equal to 9.
- The snowball hits the stone, its weight decreases by 1 (the weight of the stone), becomes equal to 8.
- The snowball moves one meter down, the current height becomes equal to 0.
Thus, at the end the weight of the snowball is equal to 8.
Now solve the problem and return the code.
|
{}
|
codeforces_1099_1099/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Little Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw $$$n$$$ squares in the snow with a side length of $$$1$$$. For simplicity, we assume that Sofia lives on a plane and can draw only segments of length $$$1$$$, parallel to the coordinate axes, with vertices at integer points.
In order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends $$$(x, y)$$$ and $$$(x, y+1)$$$. Then Sofia looks if there is already a drawn segment with the coordinates of the ends $$$(x', y)$$$ and $$$(x', y+1)$$$ for some $$$x'$$$. If such a segment exists, then Sofia quickly draws a new segment, using the old one as a guideline. If there is no such segment, then Sofia has to take a ruler and measure a new segment for a long time. Same thing happens when Sofia wants to draw a horizontal segment, but only now she checks for the existence of a segment with the same coordinates $$$x$$$, $$$x+1$$$ and the differing coordinate $$$y$$$.
For example, if Sofia needs to draw one square, she will have to draw two segments using a ruler:
After that, she can draw the remaining two segments, using the first two as a guide:
If Sofia needs to draw two squares, she will have to draw three segments using a ruler:
After that, she can draw the remaining four segments, using the first three as a guide:
Sofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number.
## Input Format
The only line of input contains a single integer $$$n$$$ ($$$1 \le n \le 10^{9}$$$), the number of squares that Sofia wants to draw.
## Output Format
Print single integer, the minimum number of segments that Sofia will have to draw with a ruler without a guide in order to draw $$$n$$$ squares in the manner described above.
## Examples
```input
1
```
```output
2
```
-----
```input
2
```
```output
3
```
-----
```input
4
```
```output
4
```
Now solve the problem and return the code.
|
{}
|
codeforces_1099_1099/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Andrey received a postcard from Irina. It contained only the words "Hello, Andrey!", and a strange string consisting of lowercase Latin letters, snowflakes and candy canes. Andrey thought that this string is an encrypted message, and decided to decrypt it.
Andrey noticed that snowflakes and candy canes always stand after the letters, so he supposed that the message was encrypted as follows. Candy cane means that the letter before it can be removed, or can be left. A snowflake means that the letter before it can be removed, left, or repeated several times.
For example, consider the following string:
This string can encode the message «happynewyear». For this, candy canes and snowflakes should be used as follows:
- candy cane 1: remove the letter w,
- snowflake 1: repeat the letter p twice,
- candy cane 2: leave the letter n,
- snowflake 2: remove the letter w,
- snowflake 3: leave the letter e.
Please note that the same string can encode different messages. For example, the string above can encode «hayewyar», «happpppynewwwwwyear», and other messages.
Andrey knows that messages from Irina usually have a length of $$$k$$$ letters. Help him to find out if a given string can encode a message of $$$k$$$ letters, and if so, give an example of such a message.
## Input Format
The first line contains the string received in the postcard. The string consists only of lowercase Latin letters, as well as the characters «*» and «?», meaning snowflake and candy cone, respectively. These characters can only appear immediately after the letter. The length of the string does not exceed $$$200$$$.
The second line contains an integer number $$$k$$$ ($$$1 \leq k \leq 200$$$), the required message length.
## Output Format
Print any message of length $$$k$$$ that the given string can encode, or «Impossible» if such a message does not exist.
## Examples
```input
hw?ap*yn?eww*ye*ar
12
```
```output
happynewyear
```
-----
```input
ab?a
2
```
```output
aa
```
-----
```input
ab?a
3
```
```output
aba
```
-----
```input
ababb
5
```
```output
ababb
```
-----
```input
ab?a
1
```
```output
Impossible
```
Now solve the problem and return the code.
|
{}
|
codeforces_1099_1099/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 512.0 MB
# Problem
Mitya and Vasya are playing an interesting game. They have a rooted tree with $$$n$$$ vertices, and the vertices are indexed from $$$1$$$ to $$$n$$$. The root has index $$$1$$$. Every other vertex $$$i \ge 2$$$ has its parent $$$p_i$$$, and vertex $$$i$$$ is called a child of vertex $$$p_i$$$.
There are some cookies in every vertex of the tree: there are $$$x_i$$$ cookies in vertex $$$i$$$. It takes exactly $$$t_i$$$ time for Mitya to eat one cookie in vertex $$$i$$$. There is also a chip, which is initially located in the root of the tree, and it takes $$$l_i$$$ time to move the chip along the edge connecting vertex $$$i$$$ with its parent.
Mitya and Vasya take turns playing, Mitya goes first.
- Mitya moves the chip from the vertex, where the chip is located, to one of its children.
- Vasya can remove an edge from the vertex, where the chip is located, to one of its children. Vasya can also decide to skip his turn.
Mitya can stop the game at any his turn. Once he stops the game, he moves the chip up to the root, eating some cookies along his way. Mitya can decide how many cookies he would like to eat in every vertex on his way. The total time spent on descend, ascend and eating cookies should not exceed $$$T$$$. Please note that in the end of the game the chip is always located in the root of the tree: Mitya can not leave the chip in any other vertex, even if he has already eaten enough cookies — he must move the chip back to the root (and every move from vertex $$$v$$$ to its parent takes $$$l_v$$$ time).
Find out what is the maximum number of cookies Mitya can eat, regardless of Vasya's actions.
## Input Format
The first line contains two integers $$$n$$$ and $$$T$$$ — the number of vertices in the tree and the time he has to accomplish his task ($$$2\le n \le 10^5$$$; $$$1\le T\le10^{18}$$$).
The second line contains $$$n$$$ integers $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ — number of cookies located in the corresponding vertex ($$$1\le x_i\le10^6$$$). The third line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ — how much time it takes Mitya to eat one cookie in vertex $$$i$$$ ($$$1\le t_i\le10^6$$$).
Each of the following $$$n - 1$$$ lines describe the tree. For every $$$i$$$ from $$$2$$$ to $$$n$$$, the corresponding line contains two integers $$$p_i$$$ and $$$l_i$$$, where $$$p_i$$$ denotes the parent of vertex $$$i$$$ and $$$l_i$$$ denotes the time it takes Mitya to move the chip along the edge from vertex $$$i$$$ to its parent ($$$1\le p_i < i$$$, $$$0\le l_i \le 10^9$$$).
## Output Format
Output a single integer — maximum number of cookies Mitya can eat.
## Examples
```input
5 26
1 5 1 7 7
1 3 2 2 2
1 1
1 1
2 0
2 0
```
```output
11
```
-----
```input
3 179
2 2 1
6 6 6
1 3
2 3
```
```output
4
```
## Note
In the first example test case, Mitya can start by moving the chip to vertex $$$2$$$. In this case no matter how Vasya plays, Mitya is able to eat at least $$$11$$$ cookies. Below you can find the detailed description of the moves:
1. Mitya moves chip to vertex $$$2$$$.
2. Vasya removes edge to vertex $$$4$$$.
3. Mitya moves chip to vertex $$$5$$$.
4. Since vertex $$$5$$$ has no children, Vasya does not remove any edges.
5. Mitya stops the game and moves the chip towards the root, eating cookies along the way ($$$7$$$ in vertex $$$5$$$, $$$3$$$ in vertex $$$2$$$, $$$1$$$ in vertex $$$1$$$).
Mitya spend $$$1+0$$$ time to go down, $$$0+1$$$ to go up, $$$7\cdot 2$$$ to eat $$$7$$$ cookies in vertex 5, $$$3\cdot 3$$$ to eat $$$3$$$ cookies in vertex 2, $$$1\cdot 1$$$ to eat $$$1$$$ cookie in vertex 1. Total time is $$$1+0+0+1+7\cdot 2+3\cdot 3+1\cdot 1=26$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given two segments $$$[l_1; r_1]$$$ and $$$[l_2; r_2]$$$ on the $$$x$$$-axis. It is guaranteed that $$$l_1 < r_1$$$ and $$$l_2 < r_2$$$. Segments may intersect, overlap or even coincide with each other.
The example of two segments on the $$$x$$$-axis.
Your problem is to find two integers $$$a$$$ and $$$b$$$ such that $$$l_1 \le a \le r_1$$$, $$$l_2 \le b \le r_2$$$ and $$$a \ne b$$$. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment $$$[l_1; r_1]$$$ and the second one belongs to the segment $$$[l_2; r_2]$$$.
It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.
You have to answer $$$q$$$ independent queries.
## Input Format
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries.
Each of the next $$$q$$$ lines contains four integers $$$l_{1_i}, r_{1_i}, l_{2_i}$$$ and $$$r_{2_i}$$$ ($$$1 \le l_{1_i}, r_{1_i}, l_{2_i}, r_{2_i} \le 10^9, l_{1_i} < r_{1_i}, l_{2_i} < r_{2_i}$$$) — the ends of the segments in the $$$i$$$-th query.
## Output Format
Print $$$2q$$$ integers. For the $$$i$$$-th query print two integers $$$a_i$$$ and $$$b_i$$$ — such numbers that $$$l_{1_i} \le a_i \le r_{1_i}$$$, $$$l_{2_i} \le b_i \le r_{2_i}$$$ and $$$a_i \ne b_i$$$. Queries are numbered in order of the input.
It is guaranteed that the answer exists. If there are multiple answers, you can print any.
## Examples
```input
5
1 2 1 2
2 6 3 4
2 4 1 3
1 2 1 3
1 4 5 8
```
```output
2 1
3 4
3 2
1 2
3 7
```
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Recently you have received two positive integer numbers $$$x$$$ and $$$y$$$. You forgot them, but you remembered a shuffled list containing all divisors of $$$x$$$ (including $$$1$$$ and $$$x$$$) and all divisors of $$$y$$$ (including $$$1$$$ and $$$y$$$). If $$$d$$$ is a divisor of both numbers $$$x$$$ and $$$y$$$ at the same time, there are two occurrences of $$$d$$$ in the list.
For example, if $$$x=4$$$ and $$$y=6$$$ then the given list can be any permutation of the list $$$[1, 2, 4, 1, 2, 3, 6]$$$. Some of the possible lists are: $$$[1, 1, 2, 4, 6, 3, 2]$$$, $$$[4, 6, 1, 1, 2, 3, 2]$$$ or $$$[1, 6, 3, 2, 4, 1, 2]$$$.
Your problem is to restore suitable positive integer numbers $$$x$$$ and $$$y$$$ that would yield the same list of divisors (possibly in different order).
It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers $$$x$$$ and $$$y$$$.
## Input Format
The first line contains one integer $$$n$$$ ($$$2 \le n \le 128$$$) — the number of divisors of $$$x$$$ and $$$y$$$.
The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^4$$$), where $$$d_i$$$ is either divisor of $$$x$$$ or divisor of $$$y$$$. If a number is divisor of both numbers $$$x$$$ and $$$y$$$ then there are two copies of this number in the list.
## Output Format
Print two positive integer numbers $$$x$$$ and $$$y$$$ — such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
## Examples
```input
10
10 2 8 1 2 4 1 20 4 5
```
```output
20 8
```
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' — colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $$$t$$$, then for each $$$i, j$$$ such that $$$t_i = t_j$$$ should be satisfied $$$|i-j|~ mod~ 3 = 0$$$. The value $$$|x|$$$ means absolute value of $$$x$$$, the operation $$$x~ mod~ y$$$ means remainder of $$$x$$$ when divided by $$$y$$$.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
## Input Format
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of lamps.
The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' — colors of lamps in the garland.
## Output Format
In the first line of the output print one integer $$$r$$$ — the minimum number of recolors needed to obtain a nice garland from the given one.
In the second line of the output print one string $$$t$$$ of length $$$n$$$ — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
## Examples
```input
3
BRB
```
```output
1
GRB
```
-----
```input
7
RGBGRBB
```
```output
3
RGBRGBR
```
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' — colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is $$$1$$$) have distinct colors.
In other words, if the obtained garland is $$$t$$$ then for each $$$i$$$ from $$$1$$$ to $$$n-1$$$ the condition $$$t_i \ne t_{i + 1}$$$ should be satisfied.
Among all ways to recolor the initial garland to make it diverse you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
## Input Format
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of lamps.
The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' — colors of lamps in the garland.
## Output Format
In the first line of the output print one integer $$$r$$$ — the minimum number of recolors needed to obtain a diverse garland from the given one.
In the second line of the output print one string $$$t$$$ of length $$$n$$$ — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
## Examples
```input
9
RBGRRBRGG
```
```output
2
RBGRGBRGR
```
-----
```input
8
BBBGBRRR
```
```output
2
BRBGBRGR
```
-----
```input
13
BBRRRRGGGGGRR
```
```output
6
BGRBRBGBGBGRG
```
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/E1
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
The only difference between easy and hard versions is a number of elements in the array.
You are given an array $$$a$$$ consisting of $$$n$$$ integers. The value of the $$$i$$$-th element of the array is $$$a_i$$$.
You are also given a set of $$$m$$$ segments. The $$$j$$$-th segment is $$$[l_j; r_j]$$$, where $$$1 \le l_j \le r_j \le n$$$.
You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $$$a = [0, 0, 0, 0, 0]$$$ and the given segments are $$$[1; 3]$$$ and $$$[2; 4]$$$ then you can choose both of them and the array will become $$$b = [-1, -2, -2, -1, 0]$$$.
You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $$$a$$$ and obtain the array $$$b$$$ then the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ will be maximum possible.
Note that you can choose the empty set.
If there are multiple answers, you can print any.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
## Input Format
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 300, 0 \le m \le 300$$$) — the length of the array $$$a$$$ and the number of segments, respectively.
The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^6 \le a_i \le 10^6$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element of the array $$$a$$$.
The next $$$m$$$ lines are contain two integers each. The $$$j$$$-th of them contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 \le l_j \le r_j \le n$$$), where $$$l_j$$$ and $$$r_j$$$ are the ends of the $$$j$$$-th segment.
## Output Format
In the first line of the output print one integer $$$d$$$ — the maximum possible value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ if $$$b$$$ is the array obtained by applying some subset of the given segments to the array $$$a$$$.
In the second line of the output print one integer $$$q$$$ ($$$0 \le q \le m$$$) — the number of segments you apply.
In the third line print $$$q$$$ distinct integers $$$c_1, c_2, \dots, c_q$$$ in any order ($$$1 \le c_k \le m$$$) — indices of segments you apply to the array $$$a$$$ in such a way that the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ of the obtained array $$$b$$$ is maximum possible.
If there are multiple answers, you can print any.
## Examples
```input
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
```
```output
6
2
1 4
```
-----
```input
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
```
```output
7
2
3 2
```
-----
```input
1 0
1000000
```
```output
0
0
```
## Note
In the first example the obtained array $$$b$$$ will be $$$[0, -4, 1, 1, 2]$$$ so the answer is $$$6$$$.
In the second example the obtained array $$$b$$$ will be $$$[2, -3, 1, -1, 4]$$$ so the answer is $$$7$$$.
In the third example you cannot do anything so the answer is $$$0$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/E2
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
The only difference between easy and hard versions is a number of elements in the array.
You are given an array $$$a$$$ consisting of $$$n$$$ integers. The value of the $$$i$$$-th element of the array is $$$a_i$$$.
You are also given a set of $$$m$$$ segments. The $$$j$$$-th segment is $$$[l_j; r_j]$$$, where $$$1 \le l_j \le r_j \le n$$$.
You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $$$a = [0, 0, 0, 0, 0]$$$ and the given segments are $$$[1; 3]$$$ and $$$[2; 4]$$$ then you can choose both of them and the array will become $$$b = [-1, -2, -2, -1, 0]$$$.
You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $$$a$$$ and obtain the array $$$b$$$ then the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ will be maximum possible.
Note that you can choose the empty set.
If there are multiple answers, you can print any.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
## Input Format
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5, 0 \le m \le 300$$$) — the length of the array $$$a$$$ and the number of segments, respectively.
The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^6 \le a_i \le 10^6$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element of the array $$$a$$$.
The next $$$m$$$ lines are contain two integers each. The $$$j$$$-th of them contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 \le l_j \le r_j \le n$$$), where $$$l_j$$$ and $$$r_j$$$ are the ends of the $$$j$$$-th segment.
## Output Format
In the first line of the output print one integer $$$d$$$ — the maximum possible value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ if $$$b$$$ is the array obtained by applying some subset of the given segments to the array $$$a$$$.
In the second line of the output print one integer $$$q$$$ ($$$0 \le q \le m$$$) — the number of segments you apply.
In the third line print $$$q$$$ distinct integers $$$c_1, c_2, \dots, c_q$$$ in any order ($$$1 \le c_k \le m$$$) — indices of segments you apply to the array $$$a$$$ in such a way that the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ of the obtained array $$$b$$$ is maximum possible.
If there are multiple answers, you can print any.
## Examples
```input
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3
```
```output
6
2
4 1
```
-----
```input
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5
```
```output
7
2
3 2
```
-----
```input
1 0
1000000
```
```output
0
0
```
## Note
In the first example the obtained array $$$b$$$ will be $$$[0, -4, 1, 1, 2]$$$ so the answer is $$$6$$$.
In the second example the obtained array $$$b$$$ will be $$$[2, -3, 1, -1, 4]$$$ so the answer is $$$7$$$.
In the third example you cannot do anything so the answer is $$$0$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1108_1108/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an undirected weighted connected graph with $$$n$$$ vertices and $$$m$$$ edges without loops and multiple edges.
The $$$i$$$-th edge is $$$e_i = (u_i, v_i, w_i)$$$; the distance between vertices $$$u_i$$$ and $$$v_i$$$ along the edge $$$e_i$$$ is $$$w_i$$$ ($$$1 \le w_i$$$). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.
A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).
You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $$$1$$$. You can increase the weight of each edge multiple (possibly, zero) times.
Suppose that the initial MST cost is $$$k$$$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $$$k$$$, but MST is unique (it means that there is only one way to choose MST in the obtained graph).
Your problem is to calculate the minimum number of operations required to do it.
## Input Format
The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5, n - 1 \le m \le 2 \cdot 10^5$$$) — the number of vertices and the number of edges in the initial graph.
The next $$$m$$$ lines contain three integers each. The $$$i$$$-th line contains the description of the $$$i$$$-th edge $$$e_i$$$. It is denoted by three integers $$$u_i, v_i$$$ and $$$w_i$$$ ($$$1 \le u_i, v_i \le n, u_i \ne v_i, 1 \le w \le 10^9$$$), where $$$u_i$$$ and $$$v_i$$$ are vertices connected by the $$$i$$$-th edge and $$$w_i$$$ is the weight of this edge.
It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $$$i$$$ from $$$1$$$ to $$$m$$$ $$$u_i \ne v_i$$$ and for each unordered pair of vertices $$$(u, v)$$$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.
## Output Format
Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST.
## Examples
```input
8 10
1 2 1
2 3 2
2 4 5
1 4 2
6 3 3
6 1 3
3 5 2
3 7 1
4 8 1
6 2 4
```
```output
1
```
-----
```input
4 3
2 1 3
4 3 4
2 4 1
```
```output
0
```
-----
```input
3 3
1 2 1
2 3 2
1 3 3
```
```output
0
```
-----
```input
3 3
1 2 1
2 3 3
1 3 3
```
```output
1
```
-----
```input
1 0
```
```output
0
```
-----
```input
5 6
1 2 2
2 3 1
4 5 3
2 4 2
1 4 2
1 5 3
```
```output
2
```
## Note
The picture corresponding to the first example:
You can, for example, increase weight of the edge $$$(1, 6)$$$ or $$$(6, 3)$$$ by $$$1$$$ to unify MST.
The picture corresponding to the last example:
You can, for example, increase weights of edges $$$(1, 5)$$$ and $$$(2, 4)$$$ by $$$1$$$ to unify MST.
Now solve the problem and return the code.
|
{}
|
codeforces_1114_1114/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Aki is fond of numbers, especially those with trailing zeros. For example, the number $$$9200$$$ has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.
However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.
Given two integers $$$n$$$ and $$$b$$$ (in decimal notation), your task is to calculate the number of trailing zero digits in the $$$b$$$-ary (in the base/radix of $$$b$$$) representation of $$$n\,!$$$ (factorial of $$$n$$$).
## Input Format
The only line of the input contains two integers $$$n$$$ and $$$b$$$ ($$$1 \le n \le 10^{18}$$$, $$$2 \le b \le 10^{12}$$$).
## Output Format
Print an only integer — the number of trailing zero digits in the $$$b$$$-ary representation of $$$n!$$$
## Examples
```input
6 9
```
```output
1
```
-----
```input
38 11
```
```output
3
```
-----
```input
5 2
```
```output
3
```
-----
```input
5 10
```
```output
1
```
## Note
In the first example, $$$6!_{(10)} = 720_{(10)} = 880_{(9)}$$$.
In the third and fourth example, $$$5!_{(10)} = 120_{(10)} = 1111000_{(2)}$$$.
The representation of the number $$$x$$$ in the $$$b$$$-ary base is $$$d_1, d_2, \ldots, d_k$$$ if $$$x = d_1 b^{k - 1} + d_2 b^{k - 2} + \ldots + d_k b^0$$$, where $$$d_i$$$ are integers and $$$0 \le d_i \le b - 1$$$. For example, the number $$$720$$$ from the first example is represented as $$$880_{(9)}$$$ since $$$720 = 8 \cdot 9^2 + 8 \cdot 9 + 0 \cdot 1$$$.
You can read more about bases here.
Now solve the problem and return the code.
|
{}
|
codeforces_1114_1114/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a line of $$$n$$$ colored squares in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. The $$$i$$$-th square initially has the color $$$c_i$$$.
Let's say, that two squares $$$i$$$ and $$$j$$$ belong to the same connected component if $$$c_i = c_j$$$, and $$$c_i = c_k$$$ for all $$$k$$$ satisfying $$$i < k < j$$$. In other words, all squares on the segment from $$$i$$$ to $$$j$$$ should have the same color.
For example, the line $$$[3, 3, 3]$$$ has $$$1$$$ connected component, while the line $$$[5, 2, 4, 4]$$$ has $$$3$$$ connected components.
The game "flood fill" is played on the given line as follows:
- At the start of the game you pick any starting square (this is not counted as a turn).
- Then, in each game turn, change the color of the connected component containing the starting square to any other color.
Find the minimum number of turns needed for the entire line to be changed into a single color.
## Input Format
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the number of squares.
The second line contains integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 5000$$$) — the initial colors of the squares.
## Output Format
Print a single integer — the minimum number of the turns needed.
## Examples
```input
4
5 2 2 1
```
```output
2
```
-----
```input
8
4 5 2 2 1 3 5 5
```
```output
4
```
-----
```input
1
4
```
```output
0
```
## Note
In the first example, a possible way to achieve an optimal answer is to pick square with index $$$2$$$ as the starting square and then play as follows:
- $$$[5, 2, 2, 1]$$$
- $$$[5, 5, 5, 1]$$$
- $$$[1, 1, 1, 1]$$$
In the second example, a possible way to achieve an optimal answer is to pick square with index $$$5$$$ as the starting square and then perform recoloring into colors $$$2, 3, 5, 4$$$ in that order.
In the third example, the line already consists of one color only.
Now solve the problem and return the code.
|
{}
|
codeforces_1114_1114/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 5.5 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a_1, a_2, \ldots, a_n$$$.
You need to perform $$$q$$$ queries of the following two types:
1. "MULTIPLY l r x" — for every $$$i$$$ ($$$l \le i \le r$$$) multiply $$$a_i$$$ by $$$x$$$.
2. "TOTIENT l r" — print $$$\varphi(\prod \limits_{i=l}^{r} a_i)$$$ taken modulo $$$10^9+7$$$, where $$$\varphi$$$ denotes Euler's totient function.
The Euler's totient function of a positive integer $$$n$$$ (denoted as $$$\varphi(n)$$$) is the number of integers $$$x$$$ ($$$1 \le x \le n$$$) such that $$$\gcd(n,x) = 1$$$.
## Input Format
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 4 \cdot 10^5$$$, $$$1 \le q \le 2 \cdot 10^5$$$) — the number of elements in array $$$a$$$ and the number of queries.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 300$$$) — the elements of array $$$a$$$.
Then $$$q$$$ lines follow, describing queries in the format given in the statement.
1. "MULTIPLY l r x" ($$$1 \le l \le r \le n$$$, $$$1 \le x \le 300$$$) — denotes a multiplication query.
2. "TOTIENT l r" ($$$1 \le l \le r \le n$$$) — denotes a query on the value of Euler's totient function.
It is guaranteed that there is at least one "TOTIENT" query.
## Output Format
For each "TOTIENT" query, print the answer to it.
## Examples
```input
4 4
5 9 1 2
TOTIENT 3 3
TOTIENT 3 4
MULTIPLY 4 4 3
TOTIENT 4 4
```
```output
1
1
2
```
## Note
In the first example, $$$\varphi(1) = 1$$$ for the first query, $$$\varphi(2) = 1$$$ for the second query and $$$\varphi(6) = 2$$$ for the third one.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 0.5 seconds
Memory limit: 256.0 MB
# Problem
You are given a string $$$s=s_1s_2\dots s_n$$$ of length $$$n$$$, which only contains digits $$$1$$$, $$$2$$$, ..., $$$9$$$.
A substring $$$s[l \dots r]$$$ of $$$s$$$ is a string $$$s_l s_{l + 1} s_{l + 2} \ldots s_r$$$. A substring $$$s[l \dots r]$$$ of $$$s$$$ is called even if the number represented by it is even.
Find the number of even substrings of $$$s$$$. Note, that even if some substrings are equal as strings, but have different $$$l$$$ and $$$r$$$, they are counted as different substrings.
## Input Format
The first line contains an integer $$$n$$$ ($$$1 \le n \le 65000$$$) — the length of the string $$$s$$$.
The second line contains a string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists only of digits $$$1$$$, $$$2$$$, ..., $$$9$$$.
## Output Format
Print the number of even substrings of $$$s$$$.
## Examples
```input
4
1234
```
```output
6
```
-----
```input
4
2244
```
```output
10
```
## Note
In the first example, the $$$[l, r]$$$ pairs corresponding to even substrings are:
- $$$s[1 \dots 2]$$$
- $$$s[2 \dots 2]$$$
- $$$s[1 \dots 4]$$$
- $$$s[2 \dots 4]$$$
- $$$s[3 \dots 4]$$$
- $$$s[4 \dots 4]$$$
In the second example, all $$$10$$$ substrings of $$$s$$$ are even substrings. Note, that while substrings $$$s[1 \dots 1]$$$ and $$$s[2 \dots 2]$$$ both define the substring "2", they are still counted as different substrings.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 \le x_i \le a_i$$$), then for all $$$1 \le j < i$$$ at least one of the following must hold:
- $$$x_j = 0$$$ (you bought zero chocolates of type $$$j$$$)
- $$$x_j < x_i$$$ (you bought less chocolates of type $$$j$$$ than of type $$$i$$$)
For example, the array $$$x = [0, 0, 1, 2, 10]$$$ satisfies the requirement above (assuming that all $$$a_i \ge x_i$$$), while arrays $$$x = [0, 1, 0]$$$, $$$x = [5, 5]$$$ and $$$x = [3, 2]$$$ don't.
Calculate the maximum number of chocolates you can buy.
## Input Format
The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$), denoting the number of types of chocolate.
The next line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$), denoting the number of chocolates of each type.
## Output Format
Print the maximum number of chocolates you can buy.
## Examples
```input
5
1 2 1 3 6
```
```output
10
```
-----
```input
5
3 2 5 4 10
```
```output
20
```
-----
```input
4
1 1 1 1
```
```output
1
```
## Note
In the first example, it is optimal to buy: $$$0 + 0 + 1 + 3 + 6$$$ chocolates.
In the second example, it is optimal to buy: $$$1 + 2 + 3 + 4 + 10$$$ chocolates.
In the third example, it is optimal to buy: $$$0 + 0 + 0 + 1$$$ chocolates.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a tree (a connected undirected graph without cycles) of $$$n$$$ vertices. Each of the $$$n - 1$$$ edges of the tree is colored in either black or red.
You are also given an integer $$$k$$$. Consider sequences of $$$k$$$ vertices. Let's call a sequence $$$[a_1, a_2, \ldots, a_k]$$$ good if it satisfies the following criterion:
- We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from $$$a_1$$$ and ending at $$$a_k$$$.
- Start at $$$a_1$$$, then go to $$$a_2$$$ using the shortest path between $$$a_1$$$ and $$$a_2$$$, then go to $$$a_3$$$ in a similar way, and so on, until you travel the shortest path between $$$a_{k-1}$$$ and $$$a_k$$$.
- If you walked over at least one black edge during this process, then the sequence is good.
Consider the tree on the picture. If $$$k=3$$$ then the following sequences are good: $$$[1, 4, 7]$$$, $$$[5, 5, 3]$$$ and $$$[2, 3, 7]$$$. The following sequences are not good: $$$[1, 4, 6]$$$, $$$[5, 5, 5]$$$, $$$[3, 7, 3]$$$.
There are $$$n^k$$$ sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo $$$10^9+7$$$.
## Input Format
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$2 \le k \le 100$$$), the size of the tree and the length of the vertex sequence.
Each of the next $$$n - 1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$ and $$$x_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$x_i \in \{0, 1\}$$$), where $$$u_i$$$ and $$$v_i$$$ denote the endpoints of the corresponding edge and $$$x_i$$$ is the color of this edge ($$$0$$$ denotes red edge and $$$1$$$ denotes black edge).
## Output Format
Print the number of good sequences modulo $$$10^9 + 7$$$.
## Examples
```input
4 4
1 2 1
2 3 1
3 4 1
```
```output
252
```
-----
```input
4 6
1 2 0
1 3 0
1 4 0
```
```output
0
```
-----
```input
3 5
1 2 1
2 3 0
```
```output
210
```
## Note
In the first example, all sequences ($$$4^4$$$) of length $$$4$$$ except the following are good:
- $$$[1, 1, 1, 1]$$$
- $$$[2, 2, 2, 2]$$$
- $$$[3, 3, 3, 3]$$$
- $$$[4, 4, 4, 4]$$$
In the second example, all edges are red, hence there aren't any good sequences.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Vivek initially has an empty array $$$a$$$ and some integer constant $$$m$$$.
He performs the following algorithm:
1. Select a random integer $$$x$$$ uniformly in range from $$$1$$$ to $$$m$$$ and append it to the end of $$$a$$$.
2. Compute the greatest common divisor of integers in $$$a$$$.
3. In case it equals to $$$1$$$, break
4. Otherwise, return to step $$$1$$$.
Find the expected length of $$$a$$$. It can be shown that it can be represented as $$$\frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are coprime integers and $$$Q\neq 0 \pmod{10^9+7}$$$. Print the value of $$$P \cdot Q^{-1} \pmod{10^9+7}$$$.
## Input Format
The first and only line contains a single integer $$$m$$$ ($$$1 \leq m \leq 100000$$$).
## Output Format
Print a single integer — the expected length of the array $$$a$$$ written as $$$P \cdot Q^{-1} \pmod{10^9+7}$$$.
## Examples
```input
1
```
```output
1
```
-----
```input
2
```
```output
2
```
-----
```input
4
```
```output
333333338
```
## Note
In the first example, since Vivek can choose only integers from $$$1$$$ to $$$1$$$, he will have $$$a=[1]$$$ after the first append operation, and after that quit the algorithm. Hence the length of $$$a$$$ is always $$$1$$$, so its expected value is $$$1$$$ as well.
In the second example, Vivek each time will append either $$$1$$$ or $$$2$$$, so after finishing the algorithm he will end up having some number of $$$2$$$'s (possibly zero), and a single $$$1$$$ in the end. The expected length of the list is $$$1\cdot \frac{1}{2} + 2\cdot \frac{1}{2^2} + 3\cdot \frac{1}{2^3} + \ldots = 2$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
There are $$$n$$$ students and $$$m$$$ clubs in a college. The clubs are numbered from $$$1$$$ to $$$m$$$. Each student has a potential $$$p_i$$$ and is a member of the club with index $$$c_i$$$. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next $$$d$$$ days. There is a coding competition every day in the technical fest.
Every day, in the morning, exactly one student of the college leaves their club. Once a student leaves their club, they will never join any club again. Every day, in the afternoon, the director of the college will select one student from each club (in case some club has no members, nobody is selected from that club) to form a team for this day's coding competition. The strength of a team is the mex of potentials of the students in the team. The director wants to know the maximum possible strength of the team for each of the coming $$$d$$$ days. Thus, every day the director chooses such team, that the team strength is maximized.
The mex of the multiset $$$S$$$ is the smallest non-negative integer that is not present in $$$S$$$. For example, the mex of the $$$\{0, 1, 1, 2, 4, 5, 9\}$$$ is $$$3$$$, the mex of $$$\{1, 2, 3\}$$$ is $$$0$$$ and the mex of $$$\varnothing$$$ (empty set) is $$$0$$$.
## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq m \leq n \leq 5000$$$), the number of students and the number of clubs in college.
The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$0 \leq p_i < 5000$$$), where $$$p_i$$$ is the potential of the $$$i$$$-th student.
The third line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq m$$$), which means that $$$i$$$-th student is initially a member of the club with index $$$c_i$$$.
The fourth line contains an integer $$$d$$$ ($$$1 \leq d \leq n$$$), number of days for which the director wants to know the maximum possible strength of the team.
Each of the next $$$d$$$ lines contains an integer $$$k_i$$$ ($$$1 \leq k_i \leq n$$$), which means that $$$k_i$$$-th student lefts their club on the $$$i$$$-th day. It is guaranteed, that the $$$k_i$$$-th student has not left their club earlier.
## Output Format
For each of the $$$d$$$ days, print the maximum possible strength of the team on that day.
## Examples
```input
5 3
0 1 2 2 0
1 2 2 3 2
5
3
2
4
5
1
```
```output
3
1
1
1
0
```
-----
```input
5 3
0 1 2 2 1
1 3 2 3 2
5
4
2
3
5
1
```
```output
3
2
2
1
0
```
-----
```input
5 5
0 1 2 4 5
1 2 3 4 5
4
2
3
5
4
```
```output
1
1
1
1
```
## Note
Consider the first example:
On the first day, student $$$3$$$ leaves their club. Now, the remaining students are $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$. We can select students $$$1$$$, $$$2$$$ and $$$4$$$ to get maximum possible strength, which is $$$3$$$. Note, that we can't select students $$$1$$$, $$$2$$$ and $$$5$$$, as students $$$2$$$ and $$$5$$$ belong to the same club. Also, we can't select students $$$1$$$, $$$3$$$ and $$$4$$$, since student $$$3$$$ has left their club.
On the second day, student $$$2$$$ leaves their club. Now, the remaining students are $$$1$$$, $$$4$$$ and $$$5$$$. We can select students $$$1$$$, $$$4$$$ and $$$5$$$ to get maximum possible strength, which is $$$1$$$.
On the third day, the remaining students are $$$1$$$ and $$$5$$$. We can select students $$$1$$$ and $$$5$$$ to get maximum possible strength, which is $$$1$$$.
On the fourth day, the remaining student is $$$1$$$. We can select student $$$1$$$ to get maximum possible strength, which is $$$1$$$.
On the fifth day, no club has students and so the maximum possible strength is $$$0$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1139_1139/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
There are $$$m$$$ people living in a city. There are $$$n$$$ dishes sold in the city. Each dish $$$i$$$ has a price $$$p_i$$$, a standard $$$s_i$$$ and a beauty $$$b_i$$$. Each person $$$j$$$ has an income of $$$inc_j$$$ and a preferred beauty $$$pref_j$$$.
A person would never buy a dish whose standard is less than the person's income. Also, a person can't afford a dish with a price greater than the income of the person. In other words, a person $$$j$$$ can buy a dish $$$i$$$ only if $$$p_i \leq inc_j \leq s_i$$$.
Also, a person $$$j$$$ can buy a dish $$$i$$$, only if $$$|b_i-pref_j| \leq (inc_j-p_i)$$$. In other words, if the price of the dish is less than the person's income by $$$k$$$, the person will only allow the absolute difference of at most $$$k$$$ between the beauty of the dish and his/her preferred beauty.
Print the number of dishes that can be bought by each person in the city.
## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq m \leq 10^5$$$), the number of dishes available in the city and the number of people living in the city.
The second line contains $$$n$$$ integers $$$p_i$$$ ($$$1 \leq p_i \leq 10^9$$$), the price of each dish.
The third line contains $$$n$$$ integers $$$s_i$$$ ($$$1 \leq s_i \leq 10^9$$$), the standard of each dish.
The fourth line contains $$$n$$$ integers $$$b_i$$$ ($$$1 \leq b_i \leq 10^9$$$), the beauty of each dish.
The fifth line contains $$$m$$$ integers $$$inc_j$$$ ($$$1 \leq inc_j \leq 10^9$$$), the income of every person.
The sixth line contains $$$m$$$ integers $$$pref_j$$$ ($$$1 \leq pref_j \leq 10^9$$$), the preferred beauty of every person.
It is guaranteed that for all integers $$$i$$$ from $$$1$$$ to $$$n$$$, the following condition holds: $$$p_i \leq s_i$$$.
## Output Format
Print $$$m$$$ integers, the number of dishes that can be bought by every person living in the city.
## Examples
```input
3 3
2 1 3
2 4 4
2 1 1
2 2 3
1 2 4
```
```output
1 2 0
```
-----
```input
4 3
1 2 1 1
3 3 1 3
2 1 3 2
1 1 3
1 2 1
```
```output
0 2 3
```
## Note
In the first example, the first person can buy dish $$$2$$$, the second person can buy dishes $$$1$$$ and $$$2$$$ and the third person can buy no dishes.
In the second example, the first person can buy no dishes, the second person can buy dishes $$$1$$$ and $$$4$$$, and the third person can buy dishes $$$1$$$, $$$2$$$ and $$$4$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1143_1143/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.
There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $$$k$$$ such that Mr. Black can exit the house after opening the first $$$k$$$ doors.
We have to note that Mr. Black opened each door at most once, and in the end all doors became open.
## Input Format
The first line contains integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of doors.
The next line contains $$$n$$$ integers: the sequence in which Mr. Black opened the doors. The $$$i$$$-th of these integers is equal to $$$0$$$ in case the $$$i$$$-th opened door is located in the left exit, and it is equal to $$$1$$$ in case it is in the right exit.
It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.
## Output Format
Print the smallest integer $$$k$$$ such that after Mr. Black opened the first $$$k$$$ doors, he was able to exit the house.
## Examples
```input
5
0 0 1 0 0
```
```output
3
```
-----
```input
4
1 0 0 1
```
```output
3
```
## Note
In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.
When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.
In the second example when the first two doors were opened, there was open closed door in each of the exit.
With three doors opened Mr. Black was able to use the left exit.
Now solve the problem and return the code.
|
{}
|
codeforces_1143_1143/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.
Help Kurt find the maximum possible product of digits among all integers from $$$1$$$ to $$$n$$$.
## Input Format
The only input line contains the integer $$$n$$$ ($$$1 \le n \le 2\cdot10^9$$$).
## Output Format
Print the maximum product of digits among all integers from $$$1$$$ to $$$n$$$.
## Examples
```input
390
```
```output
216
```
-----
```input
7
```
```output
7
```
-----
```input
1000000000
```
```output
387420489
```
## Note
In the first example the maximum product is achieved for $$$389$$$ (the product of digits is $$$3\cdot8\cdot9=216$$$).
In the second example the maximum product is achieved for $$$7$$$ (the product of digits is $$$7$$$).
In the third example the maximum product is achieved for $$$999999999$$$ (the product of digits is $$$9^9=387420489$$$).
Now solve the problem and return the code.
|
{}
|
codeforces_1143_1143/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a rooted tree with vertices numerated from $$$1$$$ to $$$n$$$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.
Ancestors of the vertex $$$i$$$ are all vertices on the path from the root to the vertex $$$i$$$, except the vertex $$$i$$$ itself. The parent of the vertex $$$i$$$ is the nearest to the vertex $$$i$$$ ancestor of $$$i$$$. Each vertex is a child of its parent. In the given tree the parent of the vertex $$$i$$$ is the vertex $$$p_i$$$. For the root, the value $$$p_i$$$ is $$$-1$$$.
An example of a tree with $$$n=8$$$, the root is vertex $$$5$$$. The parent of the vertex $$$2$$$ is vertex $$$3$$$, the parent of the vertex $$$1$$$ is vertex $$$5$$$. The ancestors of the vertex $$$6$$$ are vertices $$$4$$$ and $$$5$$$, the ancestors of the vertex $$$7$$$ are vertices $$$8$$$, $$$3$$$ and $$$5$$$
You noticed that some vertices do not respect others. In particular, if $$$c_i = 1$$$, then the vertex $$$i$$$ does not respect any of its ancestors, and if $$$c_i = 0$$$, it respects all of them.
You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $$$v$$$, all children of $$$v$$$ become connected with the parent of $$$v$$$.
An example of deletion of the vertex $$$7$$$.
Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.
## Input Format
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of vertices in the tree.
The next $$$n$$$ lines describe the tree: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$1 \le p_i \le n$$$, $$$0 \le c_i \le 1$$$), where $$$p_i$$$ is the parent of the vertex $$$i$$$, and $$$c_i = 0$$$, if the vertex $$$i$$$ respects its parents, and $$$c_i = 1$$$, if the vertex $$$i$$$ does not respect any of its parents. The root of the tree has $$$-1$$$ instead of the parent index, also, $$$c_i=0$$$ for the root. It is guaranteed that the values $$$p_i$$$ define a rooted tree with $$$n$$$ vertices.
## Output Format
In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $$$-1$$$.
## Examples
```input
5
3 1
1 1
-1 0
2 1
3 0
```
```output
1 2 4
```
-----
```input
5
-1 0
1 1
1 1
2 0
3 0
```
```output
-1
```
-----
```input
8
2 1
-1 0
1 0
1 1
1 1
4 0
5 1
7 0
```
```output
5
```
## Note
The deletion process in the first example is as follows (see the picture below, the vertices with $$$c_i=1$$$ are in yellow):
- first you will delete the vertex $$$1$$$, because it does not respect ancestors and all its children (the vertex $$$2$$$) do not respect it, and $$$1$$$ is the smallest index among such vertices;
- the vertex $$$2$$$ will be connected with the vertex $$$3$$$ after deletion;
- then you will delete the vertex $$$2$$$, because it does not respect ancestors and all its children (the only vertex $$$4$$$) do not respect it;
- the vertex $$$4$$$ will be connected with the vertex $$$3$$$;
- then you will delete the vertex $$$4$$$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);
- you will just delete the vertex $$$4$$$;
- there are no more vertices to delete.
In the second example you don't need to delete any vertex:
- vertices $$$2$$$ and $$$3$$$ have children that respect them;
- vertices $$$4$$$ and $$$5$$$ respect ancestors.
In the third example the tree will change this way:
Now solve the problem and return the code.
|
{}
|
codeforces_1172_1172/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 4.0 seconds
Memory limit: 1024.0 MB
# Problem
Nauuo is a girl who loves coding.
One day she was solving a problem which requires to calculate a sum of some numbers modulo $$$p$$$.
She wrote the following code and got the verdict "Wrong answer".
She soon discovered the bug — the ModAdd function only worked for numbers in the range $$$[0,p)$$$, but the numbers in the problem may be out of the range. She was curious about the wrong function, so she wanted to know the result of it.
However, the original code worked too slow, so she asked you to help her.
You are given an array $$$a_1,a_2,\ldots,a_n$$$ and a number $$$p$$$. Nauuo will make $$$m$$$ queries, in each query, you are given $$$l$$$ and $$$r$$$, and you have to calculate the results of Sum(a,l,r,p). You can see the definition of the Sum function in the pseudocode above.
Note that the integers won't overflow in the code above.
## Input Format
The first line contains three integers $$$n$$$, $$$m$$$, $$$p$$$ ($$$1 \le n \le 10^6$$$, $$$1 \le m \le 2 \cdot 10^5$$$, $$$1 \le p \le 10^9$$$) — the length of the given array, the number of queries and the modulus. Note that the modulus is used only in the ModAdd function.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-10^9\le a_i\le10^9$$$) — the given array.
In the following $$$m$$$ lines, each line contains two integers $$$l$$$, $$$r$$$ ($$$1\le l\le r\le n$$$) — you have to calculate the result of Sum(a,l,r,p).
## Output Format
The output contains $$$m$$$ integers to answer the queries in the given order.
## Examples
```input
4 5 6
7 2 -3 17
2 3
1 3
1 2
2 4
4 4
```
```output
-1
0
3
10
11
```
Now solve the problem and return the code.
|
{}
|
codeforces_1176_1176/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an integer $$$n$$$.
You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:
1. Replace $$$n$$$ with $$$\frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$;
2. Replace $$$n$$$ with $$$\frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$;
3. Replace $$$n$$$ with $$$\frac{4n}{5}$$$ if $$$n$$$ is divisible by $$$5$$$.
For example, you can replace $$$30$$$ with $$$15$$$ using the first operation, with $$$20$$$ using the second operation or with $$$24$$$ using the third operation.
Your task is to find the minimum number of moves required to obtain $$$1$$$ from $$$n$$$ or say that it is impossible to do it.
You have to answer $$$q$$$ independent queries.
## Input Format
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 1000$$$) — the number of queries.
The next $$$q$$$ lines contain the queries. For each query you are given the integer number $$$n$$$ ($$$1 \le n \le 10^{18}$$$).
## Output Format
Print the answer for each query on a new line. If it is impossible to obtain $$$1$$$ from $$$n$$$, print -1. Otherwise, print the minimum number of moves required to do it.
## Examples
```input
7
1
10
25
30
14
27
1000000000000000000
```
```output
0
4
6
6
-1
6
72
```
Now solve the problem and return the code.
|
{}
|
codeforces_1176_1176/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$.
In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $$$[2, 1, 4]$$$ you can obtain the following arrays: $$$[3, 4]$$$, $$$[1, 6]$$$ and $$$[2, 5]$$$.
Your task is to find the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times.
You have to answer $$$t$$$ independent queries.
## Input Format
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of queries.
The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 100$$$).
The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$).
## Output Format
For each query print one integer in a single line — the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
## Examples
```input
2
5
3 1 2 3 1
7
1 1 1 1 1 2 2
```
```output
3
3
```
## Note
In the first query of the example you can apply the following sequence of operations to obtain $$$3$$$ elements divisible by $$$3$$$: $$$[3, 1, 2, 3, 1] \rightarrow [3, 3, 3, 1]$$$.
In the second query you can obtain $$$3$$$ elements divisible by $$$3$$$ with the following sequence of operations: $$$[1, 1, 1, 1, 1, 2, 2] \rightarrow [1, 1, 1, 1, 2, 3] \rightarrow [1, 1, 1, 3, 3] \rightarrow [2, 1, 3, 3] \rightarrow [3, 3, 3]$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1176_1176/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a$$$ consisting of $$$n$$$ integers. Each $$$a_i$$$ is one of the six following numbers: $$$4, 8, 15, 16, 23, 42$$$.
Your task is to remove the minimum number of elements to make this array good.
An array of length $$$k$$$ is called good if $$$k$$$ is divisible by $$$6$$$ and it is possible to split it into $$$\frac{k}{6}$$$ subsequences $$$4, 8, 15, 16, 23, 42$$$.
Examples of good arrays:
- $$$[4, 8, 15, 16, 23, 42]$$$ (the whole array is a required sequence);
- $$$[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$$$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements);
- $$$[]$$$ (the empty array is good).
Examples of bad arrays:
- $$$[4, 8, 15, 16, 42, 23]$$$ (the order of elements should be exactly $$$4, 8, 15, 16, 23, 42$$$);
- $$$[4, 8, 15, 16, 23, 42, 4]$$$ (the length of the array is not divisible by $$$6$$$);
- $$$[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$$$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence).
## Input Format
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the number of elements in $$$a$$$.
The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ (each $$$a_i$$$ is one of the following numbers: $$$4, 8, 15, 16, 23, 42$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.
## Output Format
Print one integer — the minimum number of elements you have to remove to obtain a good array.
## Examples
```input
5
4 8 15 16 23
```
```output
5
```
-----
```input
12
4 8 4 15 16 8 23 15 16 42 23 42
```
```output
0
```
-----
```input
15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42
```
```output
3
```
Now solve the problem and return the code.
|
{}
|
codeforces_1176_1176/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 4.0 seconds
Memory limit: 256.0 MB
# Problem
Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations:
1. Firstly, let the array $$$b$$$ be equal to the array $$$a$$$;
2. Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$;
3. Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input.
Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.
Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any.
## Input Format
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$.
The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number.
## Output Format
In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.
## Examples
```input
3
3 5 2 3 2 4
```
```output
3 4 2
```
-----
```input
1
2750131 199999
```
```output
199999
```
-----
```input
1
3 6
```
```output
6
```
Now solve the problem and return the code.
|
{}
|
codeforces_1176_1176/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.
The boss battle consists of $$$n$$$ turns. During each turn, you will get several cards. Each card has two parameters: its cost $$$c_i$$$ and damage $$$d_i$$$. You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed $$$3$$$. After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once.
Your character has also found an artifact that boosts the damage of some of your actions: every $$$10$$$-th card you play deals double damage.
What is the maximum possible damage you can deal during $$$n$$$ turns?
## Input Format
The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of turns.
Then $$$n$$$ blocks of input follow, the $$$i$$$-th block representing the cards you get during the $$$i$$$-th turn.
Each block begins with a line containing one integer $$$k_i$$$ ($$$1 \le k_i \le 2 \cdot 10^5$$$) — the number of cards you get during $$$i$$$-th turn. Then $$$k_i$$$ lines follow, each containing two integers $$$c_j$$$ and $$$d_j$$$ ($$$1 \le c_j \le 3$$$, $$$1 \le d_j \le 10^9$$$) — the parameters of the corresponding card.
It is guaranteed that $$$\sum \limits_{i = 1}^{n} k_i \le 2 \cdot 10^5$$$.
## Output Format
Print one integer — the maximum damage you may deal.
## Examples
```input
5
3
1 6
1 7
1 5
2
1 4
1 3
3
1 10
3 5
2 3
3
1 15
2 4
1 10
1
1 100
```
```output
263
```
## Note
In the example test the best course of action is as follows:
During the first turn, play all three cards in any order and deal $$$18$$$ damage.
During the second turn, play both cards and deal $$$7$$$ damage.
During the third turn, play the first and the third card and deal $$$13$$$ damage.
During the fourth turn, play the first and the third card and deal $$$25$$$ damage.
During the fifth turn, play the only card, which will deal double damage ($$$200$$$).
Now solve the problem and return the code.
|
{}
|
codeforces_118_118/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it:
- deletes all the vowels,
- inserts a character "." before each consonant,
- replaces all uppercase consonants with corresponding lowercase ones.
Vowels are letters "A", "O", "Y", "E", "U", "I", and the rest are consonants. The program's input is exactly one string, it should return the output as a single string, resulting after the program's processing the initial string.
Help Petya cope with this easy task.
## Input Format
The first line represents input string of Petya's program. This string only consists of uppercase and lowercase Latin letters and its length is from 1 to 100, inclusive.
## Output Format
Print the resulting string. It is guaranteed that this string is not empty.
## Examples
```input
tour
```
```output
.t.r
```
-----
```input
Codeforces
```
```output
.c.d.f.r.c.s
```
-----
```input
aBAcAba
```
```output
.b.c.b
```
Now solve the problem and return the code.
|
{}
|
codeforces_118_118/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one.
Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one.
## Input Format
The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.
## Output Format
On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one.
## Examples
```input
6 5
898196
```
```output
4
888188
```
-----
```input
3 2
533
```
```output
0
533
```
-----
```input
10 6
0001112223
```
```output
3
0000002223
```
## Note
In the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188".
The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.
Now solve the problem and return the code.
|
{}
|
codeforces_118_118/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.
Note that all n1 + n2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves.
## Input Format
The only line contains four space-separated integers n1, n2, k1, k2 (1 ≤ n1, n2 ≤ 100, 1 ≤ k1, k2 ≤ 10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.
## Output Format
Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than k1 footmen stand successively, and no more than k2 horsemen stand successively.
## Examples
```input
2 1 1 10
```
```output
1
```
-----
```input
2 3 1 2
```
```output
5
```
-----
```input
2 4 1 1
```
```output
0
```
## Note
Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121
Now solve the problem and return the code.
|
{}
|
codeforces_118_118/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 5.0 seconds
Memory limit: 256.0 MB
# Problem
Bertown has n junctions and m bidirectional roads. We know that one can get from any junction to any other one by the existing roads.
As there were more and more cars in the city, traffic jams started to pose real problems. To deal with them the government decided to make the traffic one-directional on all the roads, thus easing down the traffic. Your task is to determine whether there is a way to make the traffic one-directional so that there still is the possibility to get from any junction to any other one. If the answer is positive, you should also find one of the possible ways to orient the roads.
## Input Format
The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, n - 1 ≤ m ≤ 3·105) which represent the number of junctions and the roads in the town correspondingly. Then follow m lines, each containing two numbers which describe the roads in the city. Each road is determined by two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of junctions it connects.
It is guaranteed that one can get from any junction to any other one along the existing bidirectional roads. Each road connects different junctions, there is no more than one road between each pair of junctions.
## Output Format
If there's no solution, print the single number 0. Otherwise, print m lines each containing two integers pi and qi — each road's orientation. That is the traffic flow will move along a one-directional road from junction pi to junction qi. You can print the roads in any order. If there are several solutions to that problem, print any of them.
## Examples
```input
6 8
1 2
2 3
1 3
4 5
4 6
5 6
2 4
3 5
```
```output
1 2
2 3
3 1
4 5
5 6
6 4
4 2
3 5
```
-----
```input
6 7
1 2
2 3
1 3
4 5
4 6
5 6
2 4
```
```output
0
```
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Your favorite shop sells $$$n$$$ Kinder Surprise chocolate eggs. You know that exactly $$$s$$$ stickers and exactly $$$t$$$ toys are placed in $$$n$$$ eggs in total.
Each Kinder Surprise can be one of three types:
- it can contain a single sticker and no toy;
- it can contain a single toy and no sticker;
- it can contain both a single sticker and a single toy.
But you don't know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other.
What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and at least one toy?
Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It's guaranteed that the answer always exists.
## Input Format
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries.
Next $$$T$$$ lines contain three integers $$$n$$$, $$$s$$$ and $$$t$$$ each ($$$1 \le n \le 10^9$$$, $$$1 \le s, t \le n$$$, $$$s + t \ge n$$$) — the number of eggs, stickers and toys.
All queries are independent.
## Output Format
Print $$$T$$$ integers (one number per query) — the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and one toy
## Examples
```input
3
10 5 7
10 10 10
2 1 1
```
```output
6
1
2
```
## Note
In the first query, we have to take at least $$$6$$$ eggs because there are $$$5$$$ eggs with only toy inside and, in the worst case, we'll buy all of them.
In the second query, all eggs have both a sticker and a toy inside, that's why it's enough to buy only one egg.
In the third query, we have to buy both eggs: one with a sticker and one with a toy.
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
The letters shop showcase is a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. As the name tells, letters are sold in the shop.
Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string $$$s$$$.
There are $$$m$$$ friends, the $$$i$$$-th of them is named $$$t_i$$$. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.
- For example, for $$$s$$$="arrayhead" and $$$t_i$$$="arya" $$$5$$$ letters have to be bought ("arrayhead").
- For example, for $$$s$$$="arrayhead" and $$$t_i$$$="harry" $$$6$$$ letters have to be bought ("arrayhead").
- For example, for $$$s$$$="arrayhead" and $$$t_i$$$="ray" $$$5$$$ letters have to be bought ("arrayhead").
- For example, for $$$s$$$="arrayhead" and $$$t_i$$$="r" $$$2$$$ letters have to be bought ("arrayhead").
- For example, for $$$s$$$="arrayhead" and $$$t_i$$$="areahydra" all $$$9$$$ letters have to be bought ("arrayhead").
It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$.
Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.
## Input Format
The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of showcase string $$$s$$$.
The second line contains string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.
The third line contains one integer $$$m$$$ ($$$1 \le m \le 5 \cdot 10^4$$$) — the number of friends.
The $$$i$$$-th of the next $$$m$$$ lines contains $$$t_i$$$ ($$$1 \le |t_i| \le 2 \cdot 10^5$$$) — the name of the $$$i$$$-th friend.
It is guaranteed that $$$\sum \limits_{i=1}^m |t_i| \le 2 \cdot 10^5$$$.
## Output Format
For each friend print the length of the shortest prefix of letters from $$$s$$$ s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount.
It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$.
## Examples
```input
9
arrayhead
5
arya
harry
ray
r
areahydra
```
```output
5
6
5
2
9
```
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Vasya has an array $$$a_1, a_2, \dots, a_n$$$.
You don't know this array, but he told you $$$m$$$ facts about this array. The $$$i$$$-th fact is a triple of numbers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$) and it means:
- if $$$t_i=1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is sorted in non-decreasing order;
- if $$$t_i=0$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter.
For example if $$$a = [2, 1, 1, 3, 2]$$$ then he could give you three facts: $$$t_1=1, l_1=2, r_1=4$$$ (the subarray $$$[a_2, a_3, a_4] = [1, 1, 3]$$$ is sorted), $$$t_2=0, l_2=4, r_2=5$$$ (the subarray $$$[a_4, a_5] = [3, 2]$$$ is not sorted), and $$$t_3=0, l_3=3, r_3=5$$$ (the subarray $$$[a_3, a_5] = [1, 3, 2]$$$ is not sorted).
You don't know the array $$$a$$$. Find any array which satisfies all the given facts.
## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 1000, 1 \le m \le 1000$$$).
Each of the next $$$m$$$ lines contains three integers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$).
If $$$t_i = 1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is sorted. Otherwise (if $$$t_i = 0$$$) subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is not sorted.
## Output Format
If there is no array that satisfies these facts in only line print NO (in any letter case).
If there is a solution, print YES (in any letter case). In second line print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.
## Examples
```input
7 4
1 1 3
1 2 5
0 5 6
1 6 7
```
```output
YES
1 2 2 3 5 4 4
```
-----
```input
4 2
1 1 4
0 2 3
```
```output
NO
```
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a_1, a_2, \dots, a_n$$$ and an array $$$b_1, b_2, \dots, b_n$$$.
For one operation you can sort in non-decreasing order any subarray $$$a[l \dots r]$$$ of the array $$$a$$$.
For example, if $$$a = [4, 2, 2, 1, 3, 1]$$$ and you choose subbarray $$$a[2 \dots 5]$$$, then the array turns into $$$[4, 1, 2, 2, 3, 1]$$$.
You are asked to determine whether it is possible to obtain the array $$$b$$$ by applying this operation any number of times (possibly zero) to the array $$$a$$$.
## Input Format
The first line contains one integer $$$t$$$ ($$$1 \le t \le 3 \cdot 10^5$$$) — the number of queries.
The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$).
The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).
The third line of each query contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$).
It is guaranteed that $$$\sum n \le 3 \cdot 10^5$$$ over all queries in a test.
## Output Format
For each query print YES (in any letter case) if it is possible to obtain an array $$$b$$$ and NO (in any letter case) otherwise.
## Examples
```input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1
```
```output
YES
YES
NO
NO
```
## Note
In first test case the can sort subarray $$$a_1 \dots a_5$$$, then $$$a$$$ will turn into $$$[1, 1, 4, 4, 7, 5, 6]$$$, and then sort subarray $$$a_5 \dots a_6$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a tree (an undirected connected acyclic graph) consisting of $$$n$$$ vertices. You are playing a game on this tree.
Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black.
Each time when you choose a vertex (even during the first turn), you gain the number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black.
Let's see the following example:
Vertices $$$1$$$ and $$$4$$$ are painted black already. If you choose the vertex $$$2$$$, you will gain $$$4$$$ points for the connected component consisting of vertices $$$2, 3, 5$$$ and $$$6$$$. If you choose the vertex $$$9$$$, you will gain $$$3$$$ points for the connected component consisting of vertices $$$7, 8$$$ and $$$9$$$.
Your task is to maximize the number of points you gain.
## Input Format
The first line contains an integer $$$n$$$ — the number of vertices in the tree ($$$2 \le n \le 2 \cdot 10^5$$$).
Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the indices of vertices it connects ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$).
It is guaranteed that the given edges form a tree.
## Output Format
Print one integer — the maximum number of points you gain if you will play optimally.
## Examples
```input
9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8
```
```output
36
```
-----
```input
5
1 2
1 3
2 4
2 5
```
```output
14
```
## Note
The first example tree is shown in the problem statement.
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.
Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.
Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$.
## Input Format
The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$.
The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$).
The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$).
## Output Format
Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$.
## Examples
```input
3
1 1 1
1 2 3
```
```output
166666673
```
-----
```input
3
3 4 5
4 5 6
```
```output
500000010
```
## Note
Let's describe all possible values of $$$x$$$ for the first sample:
- $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$;
- $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
- $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
All possible values of $$$x$$$ for the second sample:
- $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
- $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
- $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
- $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$;
- $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$;
Now solve the problem and return the code.
|
{}
|
codeforces_1187_1187/G
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 4.0 seconds
Memory limit: 512.0 MB
# Problem
The leader of some very secretive organization has decided to invite all other members to a meeting. All members of the organization live in the same town which can be represented as $$$n$$$ crossroads connected by $$$m$$$ two-directional streets. The meeting will be held in the leader's house near the crossroad $$$1$$$. There are $$$k$$$ members of the organization invited to the meeting; $$$i$$$-th of them lives near the crossroad $$$a_i$$$.
All members of the organization receive the message about the meeting at the same moment and start moving to the location where the meeting is held. In the beginning of each minute each person is located at some crossroad. He or she can either wait a minute at this crossroad, or spend a minute to walk from the current crossroad along some street to another crossroad (obviously, it is possible to start walking along the street only if it begins or ends at the current crossroad). In the beginning of the first minute each person is at the crossroad where he or she lives. As soon as a person reaches the crossroad number $$$1$$$, he or she immediately comes to the leader's house and attends the meeting.
Obviously, the leader wants all other members of the organization to come up as early as possible. But, since the organization is very secretive, the leader does not want to attract much attention. Let's denote the discontent of the leader as follows
- initially the discontent is $$$0$$$;
- whenever a person reaches the crossroad number $$$1$$$, the discontent of the leader increases by $$$c \cdot x$$$, where $$$c$$$ is some fixed constant, and $$$x$$$ is the number of minutes it took the person to reach the crossroad number $$$1$$$;
- whenever $$$x$$$ members of the organization walk along the same street at the same moment in the same direction, $$$dx^2$$$ is added to the discontent, where $$$d$$$ is some fixed constant. This is not cumulative: for example, if two persons are walking along the same street in the same direction at the same moment, then $$$4d$$$ is added to the discontent, not $$$5d$$$.
Before sending a message about the meeting, the leader can tell each member of the organization which path they should choose and where they should wait. Help the leader to establish a plan for every member of the organization so they all reach the crossroad $$$1$$$, and the discontent is minimized.
## Input Format
The first line of the input contains five integer numbers $$$n$$$, $$$m$$$, $$$k$$$, $$$c$$$ and $$$d$$$ ($$$2 \le n \le 50$$$, $$$n - 1 \le m \le 50$$$, $$$1 \le k, c, d \le 50$$$) — the number of crossroads, the number of streets, the number of persons invited to the meeting and the constants affecting the discontent, respectively.
The second line contains $$$k$$$ numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_k$$$ ($$$2 \le a_i \le n$$$) — the crossroads where the members of the organization live.
Then $$$m$$$ lines follow, each denoting a bidirectional street. Each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$) denoting a street connecting crossroads $$$x_i$$$ and $$$y_i$$$. There may be multiple streets connecting the same pair of crossroads.
It is guaranteed that every crossroad can be reached from every other crossroad using the given streets.
## Output Format
Print one integer: the minimum discontent of the leader after everyone reaches crossroad $$$1$$$.
## Examples
```input
3 2 4 2 3
3 3 3 3
1 2
2 3
```
```output
52
```
-----
```input
3 3 4 2 3
3 2 2 3
1 2
2 3
2 3
```
```output
38
```
## Note
The best course of action in the first test is the following:
- the first person goes along the street $$$2$$$ to the crossroad $$$2$$$, then goes along the street $$$1$$$ to the crossroad $$$1$$$ and attends the meeting;
- the second person waits one minute on the crossroad $$$3$$$, then goes along the street $$$2$$$ to the crossroad $$$2$$$, then goes along the street $$$1$$$ to the crossroad $$$1$$$ and attends the meeting;
- the third person waits two minutes on the crossroad $$$3$$$, then goes along the street $$$2$$$ to the crossroad $$$2$$$, then goes along the street $$$1$$$ to the crossroad $$$1$$$ and attends the meeting;
- the fourth person waits three minutes on the crossroad $$$3$$$, then goes along the street $$$2$$$ to the crossroad $$$2$$$, then goes along the street $$$1$$$ to the crossroad $$$1$$$ and attends the meeting.
Now solve the problem and return the code.
|
{}
|
codeforces_1193_1193/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 1024.0 MB
# Problem
We have a magic tree: a rooted tree on $$$n$$$ vertices. The vertices are numbered $$$1$$$ through $$$n$$$. Vertex $$$1$$$ is the root.
The magic tree gives us magic fruit. The fruit only grows in vertices of the tree other than the root. Each vertex contains at most one piece of fruit.
It is now day 0 and no fruit is ripe yet. Each fruit will only be ripe for a single day. For each fruit, we are given the vertex $$$v_j$$$ where it grows, the day $$$d_j$$$ on which it will be ripe, and the amount $$$w_j$$$ of magic juice we can extract from it if we harvest it when it is ripe.
The fruits have to be harvested by cutting some branches of the tree. On each day, you may cut as many branches of the tree as you like. The parts of the tree you cut off will fall to the ground and you can collect all the ripe fruits they contain. All fruits that fall to the ground when they are not ripe are discarded and no magic juice is collected from them.
Formally, on each day, you may erase some edges of the tree. Whenever you do so, the tree will split into multiple connected components. You then erase all components that do not contain the root and you harvest all ripe fruits those components contained.
Given is a description of the tree together with the locations, ripening days and juiciness of all $$$m$$$ fruits. Calculate the maximum total amount of magic juice we can harvest from the tree.
## Input Format
The first line contains three space-separated integers $$$n$$$ ($$$2 \le n \le 100,000$$$), $$$m$$$ ($$$1 \le m \le n-1$$$) and $$$k$$$ ($$$1 \le k \le 100,000$$$) – the number of vertices, the number of fruits, and the maximum day on which a fruit may become ripe.
The following $$$n-1$$$ lines contain the integers $$$p_2, \dots, p_n$$$, one per line. For each $$$i$$$ (from $$$2$$$ to $$$n$$$, inclusive), vertex $$$p_i$$$ ($$$1 \leq p_i \le i-1$$$) is the parent of vertex $$$i$$$.
Each of the last $$$m$$$ lines describes one fruit. The $$$j$$$-th of these lines has the form "$$$v_j\ d_j\ w_j$$$" ($$$2 \le v_j \le n$$$, $$$1 \le d_j \le k$$$, $$$1 \le w_j \le 10^9$$$).
It is guaranteed that no vertex contains more than one fruit (i.e., the values $$$v_j$$$ are distinct).
## Output Format
Output a single line with a single integer, the maximum amount of magic juice we can harvest from the tree.
## Examples
```input
6 4 10
1
2
1
4
4
3 4 5
4 7 2
5 4 1
6 9 3
```
```output
9
```
## Note
Scoring:
Subtask 1 (6 points): $$$n, k \leq 20$$$, and $$$w_j = 1$$$ for all $$$j$$$
Subtask 2 (3 points): fruits only grow in the leaves of the tree
Subtask 3 (11 points): $$$p_i = i-1$$$ for each $$$i$$$, and $$$w_j = 1$$$ for all $$$j$$$
Subtask 4 (12 points): $$$k \leq 2$$$
Subtask 5 (16 points): $$$k \leq 20$$$, and $$$w_j = 1$$$ for all $$$j$$$
Subtask 6 (13 points): $$$m \leq 1,000$$$
Subtask 7 (22 points): $$$w_j = 1$$$ for all $$$j$$$
Subtask 8 (17 points): no additional constraints
In the example input, one optimal solution looks as follows:
- On day 4, cut the edge between vertices 4 and 5 and harvest a ripe fruit with 1 unit of magic juice. On the same day, cut the edge between vertices 1 and 2 and harvest 5 units of magic juice from the ripe fruit in vertex 3.
- On day 7, do nothing. (We could harvest the fruit in vertex 4 that just became ripe, but doing so is not optimal.)
- On day 9, cut the edge between vertices 1 and 4. Discard the fruit in vertex 4 that is no longer ripe, and harvest 3 units of magic juice from the ripe fruit in vertex 6. (Alternately, we could achieve the same effect by cutting the edge between vertices 4 and 6.)
Now solve the problem and return the code.
|
{}
|
codeforces_1193_1193/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 1024.0 MB
# Problem
You are given a piece of paper in the shape of a simple polygon $$$S$$$. Your task is to turn it into a simple polygon $$$T$$$ that has the same area as $$$S$$$.
You can use two tools: scissors and tape. Scissors can be used to cut any polygon into smaller polygonal pieces. Tape can be used to combine smaller pieces into larger polygons. You can use each tool multiple times, in any order.
The polygons given in the input have integer coordinates, but you are allowed to produce shapes with non-integer coordinates in your output.
A formal definition of the task follows.
A shape $$$Q=(Q_0,\dots,Q_{n-1})$$$ is a sequence of three or more points in the plane such that:
- The closed polyline $$$Q_0Q_1Q_2\dots Q_{n-1}Q_0$$$ never touches or intersects itself, and therefore it forms the boundary of a simple polygon.
- The polyline goes around the boundary of the polygon in the counter-clockwise direction.
The polygon whose boundary is the shape $$$Q$$$ will be denoted $$$P(Q)$$$.
Two shapes are called equivalent if one can be translated and/or rotated to become identical with the other.
Note that mirroring a shape is not allowed. Also note that the order of points matters: the shape $$$(Q_1,\dots,Q_{n-1},Q_0)$$$ is not necessarily equivalent to the shape $$$(Q_0,\dots,Q_{n-1})$$$.
In the figure on the left: Shapes $$$U$$$ and $$$V$$$ are equivalent. Shape $$$W$$$ is not equivalent with them because the points of $$$W$$$ are given in a different order. Regardless of the order of points, the fourth shape is not equivalent with the previous ones either as flipping a shape is not allowed.
In both input and output, a shape with $$$n$$$ points is represented as a single line that contains $$$2n+1$$$ space-separated numbers: the number $$$n$$$ followed by the coordinates of the points: $$$Q_{0,x}$$$, $$$Q_{0,y}$$$, $$$Q_{1,x}$$$, ...
Shapes have identification numbers (IDs). The given shape $$$S$$$ has ID 0, the shapes you produce in your solutions are given IDs 1, 2, 3, ..., in the order in which they are produced.
Shapes $$$B_1,\dots,B_k$$$ form a subdivision of shape $$$A$$$ if:
- The union of all $$$P(B_i)$$$ is exactly $$$P(A)$$$.
- For each $$$i\neq j$$$, the area of the intersection of $$$P(B_i)$$$ and $$$P(B_j)$$$ is zero.
The scissors operation destroys one existing shape $$$A$$$ and produces one or more shapes $$$B_1,\dots,B_k$$$ that form a subdivision of $$$A$$$.
In the figure: Shape $$$A$$$ (square) subdivided into shapes $$$B_1$$$, $$$B_2$$$, $$$B_3$$$ (the three triangles). One valid way to describe one of the $$$B_i$$$ is "3 3 1 6 1 5.1 4".
The tape operation destroys one or more existing shapes $$$A_1,\dots,A_k$$$ and produces one new shape $$$B$$$. In order to perform this operation, you must first specify shapes $$$C_1,\dots,C_k$$$ and only then the final shape $$$B$$$. These shapes must satisfy the following:
- For each $$$i$$$, the shape $$$C_i$$$ is equivalent to the shape $$$A_i$$$.
- The shapes $$$C_1,\dots,C_k$$$ form a subdivision of the shape $$$B$$$.
Informally, you choose the shape $$$B$$$ and show how to move each of the existing $$$A_i$$$ to its correct location $$$C_i$$$ within $$$B$$$. Note that only the shape $$$B$$$ gets a new ID, the shapes $$$C_i$$$ do not.
## Input Format
The first line contains the source shape $$$S$$$.
The second line contains the target shape $$$T$$$.
Each shape has between 3 and 10 points, inclusive. Both shapes are given in the format specified above.
All coordinates in the input are integers between $$$-10^6$$$ and $$$10^6$$$, inclusive.
In each shape, no three points form an angle smaller than 3 degrees. (This includes non-consecutive points and implies that no three points are collinear.)
The polygons $$$P(S)$$$ and $$$P(T)$$$ have the same area.
## Output Format
Whenever you use the scissors operation, output a block of lines of the form:
where $$$id(A)$$$ is the ID of the shape you want to destroy, $$$k$$$ is the number of new shapes you want to produce, and $$$B_1,\dots,B_k$$$ are those shapes.
Whenever you use the tape operation, output a block of lines of the form:
where $$$k$$$ is the number of shapes you want to tape together, $$$id(A_1),\dots,id(A_k)$$$ are their IDs, $$$C_1,\dots,C_k$$$ are equivalent shapes showing their position within $$$B$$$, and $$$B$$$ is the final shape obtained by taping them together.
It is recommended to output coordinates of points to at least 10 decimal places.
The output must satisfy the following:
- All coordinates of points in the output must be between $$$-10^7$$$ and $$$10^7$$$, inclusive.
- Each shape in the output must have at most $$$100$$$ points.
- In each operation the number $$$k$$$ of shapes must be between $$$1$$$ and $$$100$$$, inclusive.
- The number of operations must not exceed $$$2000$$$.
- The total number of points in all shapes in the output must not exceed $$$20000$$$.
- In the end, there must be exactly one shape (that hasn't been destroyed), and that shape must be equivalent to $$$T$$$.
- All operations must be valid according to the checker. Solutions with small rounding errors will be accepted. (Internally, all comparisons check for absolute or relative error up to $$$10^{-3}$$$ when verifying each condition.)
## Examples
```input
6 0 0 6 0 6 4 5 4 5 9 0 9
4 0 0 7 0 7 7 0 7
```
```output
scissors
0 5
3 0 0 3 0 3 4
3 3 4 0 4 0 0
3 3 0 6 0 6 4
3 6 4 3 4 3 0
4 0 4 5 4 5 9 0 9
tape
5 1 2 5 3 4
3 0 3 0 0 4 0
3 4 0 7 0 7 4
4 0 3 4 0 7 4 3 7
3 7 4 7 7 3 7
3 3 7 0 7 0 3
4 0 0 7 0 7 7 0 7
```
-----
```input
4 0 0 3 0 3 3 0 3
4 7 -1 10 -1 11 2 8 2
```
```output
scissors
0 2
3 0 0 1 3 0 3
4 1 3 0 0 3 0 3 3
tape
2 1 2
3 110 -1 111 2 110 2
4 108 2 107 -1 110 -1 110 2
4 107 -1 110 -1 111 2 108 2
```
-----
```input
4 0 0 9 0 9 1 0 1
4 0 0 3 0 3 3 0 3
```
```output
scissors
0 2
4 1.470000000 0 9 0 9 1 1.470000000 1
4 0 0 1.470000000 0 1.470000000 1 0 1
scissors
1 2
4 1.470000000 0 6 0 6 1 1.470000000 1
4 9 0 9 1 6 1 6 0
tape
2 4 3
4 3 2 3 1 6 1 6 2
4 6 1 1.470000000 1 1.470000000 0 6 0
6 1.470000000 0 6 0 6 2 3 2 3 1 1.470000000 1
scissors
5 4
4 1.470000000 0 3 0 3 1 1.470000000 1
4 3 0 4 0 4 2 3 2
4 4 2 4 0 5 0 5 2
4 5 0 6 0 6 2 5 2
tape
5 2 6 7 8 9
4 0 0 1.470000000 0 1.470000000 1 0 1
4 1.470000000 0 3 0 3 1 1.470000000 1
4 0 2 0 1 2 1 2 2
4 0 2 2 2 2 3 0 3
4 3 3 2 3 2 1 3 1
4 0 0 3 0 3 3 0 3
```
## Note
Scoring:
A shape is called a nice rectangle if it has the form $$$((0,0),~ (x,0),~ (x,y),~ (0,y))$$$ for some positive integers $$$x$$$ and $$$y$$$.
A shape is called a nice square if additionally $$$x=y$$$.
A shape $$$A$$$ is called strictly convex if all inner angles of the polygon $$$P(A)$$$ are smaller than 180 degrees.
Subtask 1 (5 points): $$$S$$$ and $$$T$$$ are nice rectangles. All coordinates of all points are integers between 0 and 10, inclusive
Subtask 2 (13 points): $$$S$$$ is a nice rectangle with $$$x>y$$$, and $$$T$$$ is a nice square
Subtask 3 (12 points): $$$S$$$ and $$$T$$$ are nice rectangles
Subtask 4 (14 points): $$$S$$$ is a triangle and $$$T$$$ is a nice square
Subtask 5 (10 points): $$$S$$$ and $$$T$$$ are triangles
Subtask 6 (16 points): $$$S$$$ is a strictly convex polygon and $$$T$$$ is a nice rectangle
Subtask 7 (11 points): $$$T$$$ is a nice rectangle
Subtask 8 (19 points): no additional constraints
The figure below shows the first example output. On the left is the original figure after using the scissors, on the right are the corresponding $$$C_i$$$ when we tape those pieces back together.
In the second example output, note that it is sufficient if the final shape is equivalent to the target one, they do not have to be identical.
The figure below shows three stages of the third example output. First, we cut the input rectangle into two smaller rectangles, then we cut the bigger of those two rectangles into two more. State after these cuts is shown in the top left part of the figure.
Continuing, we tape the two new rectangles together to form a six-sided polygon, and then we cut that polygon into three 2-by-1 rectangles and one smaller rectangle. This is shown in the bottom left part of the figure.
Finally, we take the rectangle we still have from the first step and the four new rectangles and we assemble them into the desired 3-by-3 square.
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You are given two binary strings $$$x$$$ and $$$y$$$, which are binary representations of some two integers (let's denote these integers as $$$f(x)$$$ and $$$f(y)$$$). You can choose any integer $$$k \ge 0$$$, calculate the expression $$$s_k = f(x) + f(y) \cdot 2^k$$$ and write the binary representation of $$$s_k$$$ in reverse order (let's denote it as $$$rev_k$$$). For example, let $$$x = 1010$$$ and $$$y = 11$$$; you've chosen $$$k = 1$$$ and, since $$$2^1 = 10_2$$$, so $$$s_k = 1010_2 + 11_2 \cdot 10_2 = 10000_2$$$ and $$$rev_k = 00001$$$.
For given $$$x$$$ and $$$y$$$, you need to choose such $$$k$$$ that $$$rev_k$$$ is lexicographically minimal (read notes if you don't know what does "lexicographically" means).
It's guaranteed that, with given constraints, $$$k$$$ exists and is finite.
## Input Format
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries.
Next $$$2T$$$ lines contain a description of queries: two lines per query. The first line contains one binary string $$$x$$$, consisting of no more than $$$10^5$$$ characters. Each character is either 0 or 1.
The second line contains one binary string $$$y$$$, consisting of no more than $$$10^5$$$ characters. Each character is either 0 or 1.
It's guaranteed, that $$$1 \le f(y) \le f(x)$$$ (where $$$f(x)$$$ is the integer represented by $$$x$$$, and $$$f(y)$$$ is the integer represented by $$$y$$$), both representations don't have any leading zeroes, the total length of $$$x$$$ over all queries doesn't exceed $$$10^5$$$, and the total length of $$$y$$$ over all queries doesn't exceed $$$10^5$$$.
## Output Format
Print $$$T$$$ integers (one per query). For each query print such $$$k$$$ that $$$rev_k$$$ is lexicographically minimal.
## Examples
```input
4
1010
11
10001
110
1
1
1010101010101
11110000
```
```output
1
3
0
0
```
## Note
The first query was described in the legend.
In the second query, it's optimal to choose $$$k = 3$$$. The $$$2^3 = 1000_2$$$ so $$$s_3 = 10001_2 + 110_2 \cdot 1000_2 = 10001 + 110000 = 1000001$$$ and $$$rev_3 = 1000001$$$. For example, if $$$k = 0$$$, then $$$s_0 = 10111$$$ and $$$rev_0 = 11101$$$, but $$$rev_3 = 1000001$$$ is lexicographically smaller than $$$rev_0 = 11101$$$.
In the third query $$$s_0 = 10$$$ and $$$rev_0 = 01$$$. For example, $$$s_2 = 101$$$ and $$$rev_2 = 101$$$. And $$$01$$$ is lexicographically smaller than $$$101$$$.
The quote from Wikipedia: "To determine which of two strings of characters comes when arranging in lexicographical order, their first letters are compared. If they differ, then the string whose first letter comes earlier in the alphabet comes before the other string. If the first letters are the same, then the second letters are compared, and so on. If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order."
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$.
The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generates are starting from $$$0$$$. For example, a $$$4$$$-$$$2$$$-counter can act as follows:
1. it prints $$$0$$$, and adds $$$4$$$ to its value, so the current value is $$$4$$$, and the output is $$$0$$$;
2. it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$8$$$, and the output is $$$04$$$;
3. it prints $$$8$$$, and adds $$$4$$$ to its value, so the current value is $$$12$$$, and the output is $$$048$$$;
4. it prints $$$2$$$, and adds $$$2$$$ to its value, so the current value is $$$14$$$, and the output is $$$0482$$$;
5. it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$18$$$, and the output is $$$04824$$$.
This is only one of the possible outputs; for example, the same counter could generate $$$0246802468024$$$ as the output, if we chose to add $$$2$$$ during each step.
You wrote down a printed sequence from one of such $$$x$$$-$$$y$$$-counters. But the sequence was corrupted and several elements from the sequence could be erased.
Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $$$s$$$ — the remaining data of the sequence.
For all $$$0 \le x, y < 10$$$, calculate the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$x$$$-$$$y$$$-counter. Note that you can't change the order of digits in string $$$s$$$ or erase any of them; only insertions are allowed.
## Input Format
The first line contains a single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^6$$$, $$$s_i \in \{\text{0} - \text{9}\}$$$) — the remaining data you have. It's guaranteed that $$$s_1 = 0$$$.
## Output Format
Print a $$$10 \times 10$$$ matrix, where the $$$j$$$-th integer ($$$0$$$-indexed) on the $$$i$$$-th line ($$$0$$$-indexed too) is equal to the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$i$$$-$$$j$$$-counter, or $$$-1$$$ if there is no way to do so.
## Examples
```input
0840
```
```output
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
```
## Note
Let's take, for example, $$$4$$$-$$$3$$$-counter. One of the possible outcomes the counter could print is $$$0(4)8(1)4(7)0$$$ (lost elements are in the brackets).
One of the possible outcomes a $$$2$$$-$$$3$$$-counter could print is $$$0(35)8(1)4(7)0$$$.
The $$$6$$$-$$$8$$$-counter could print exactly the string $$$0840$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/C
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
You have a string $$$s$$$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands:
- 'W' — move one cell up;
- 'S' — move one cell down;
- 'A' — move one cell left;
- 'D' — move one cell right.
Let $$$Grid(s)$$$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $$$s$$$. For example, if $$$s = \text{DSAWWAW}$$$ then $$$Grid(s)$$$ is the $$$4 \times 3$$$ grid:
1. you can place the robot in the cell $$$(3, 2)$$$;
2. the robot performs the command 'D' and moves to $$$(3, 3)$$$;
3. the robot performs the command 'S' and moves to $$$(4, 3)$$$;
4. the robot performs the command 'A' and moves to $$$(4, 2)$$$;
5. the robot performs the command 'W' and moves to $$$(3, 2)$$$;
6. the robot performs the command 'W' and moves to $$$(2, 2)$$$;
7. the robot performs the command 'A' and moves to $$$(2, 1)$$$;
8. the robot performs the command 'W' and moves to $$$(1, 1)$$$.
You have $$$4$$$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $$$s$$$ to minimize the area of $$$Grid(s)$$$.
What is the minimum area of $$$Grid(s)$$$ you can achieve?
## Input Format
The first line contains one integer $$$T$$$ ($$$1 \le T \le 1000$$$) — the number of queries.
Next $$$T$$$ lines contain queries: one per line. This line contains single string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$, $$$s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$$$) — the sequence of commands.
It's guaranteed that the total length of $$$s$$$ over all queries doesn't exceed $$$2 \cdot 10^5$$$.
## Output Format
Print $$$T$$$ integers: one per query. For each query print the minimum area of $$$Grid(s)$$$ you can achieve.
## Examples
```input
3
DSAWWAW
D
WA
```
```output
8
2
4
```
## Note
In the first query you have to get string $$$\text{DSAWW}\underline{D}\text{AW}$$$.
In second and third queries you can not decrease the area of $$$Grid(s)$$$.
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
You are given an integer $$$n$$$.
You have to find a sequence $$$s$$$ consisting of digits $$$\{1, 3, 7\}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$.
For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$:
1. $$$337\underline{1}3\underline{3}\underline{3}7\underline{7}$$$ (you can remove the second and fifth characters);
2. $$$337\underline{1}\underline{3}3\underline{3}7\underline{7}$$$ (you can remove the third and fifth characters);
3. $$$337\underline{1}\underline{3}\underline{3}37\underline{7}$$$ (you can remove the fourth and fifth characters);
4. $$$337\underline{1}3\underline{3}\underline{3}\underline{7}7$$$ (you can remove the second and sixth characters);
5. $$$337\underline{1}\underline{3}3\underline{3}\underline{7}7$$$ (you can remove the third and sixth characters);
6. $$$337\underline{1}\underline{3}\underline{3}3\underline{7}7$$$ (you can remove the fourth and sixth characters).
Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$.
You have to answer $$$t$$$ independent queries.
## Input Format
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of queries.
Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 \le n_i \le 10^9$$$).
## Output Format
For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 \le |s_i| \le 10^5$$$) consisting of digits $$$\{1, 3, 7\}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them.
## Examples
```input
2
6
1
```
```output
113337
1337
```
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
You are given a string $$$t$$$ and $$$n$$$ strings $$$s_1, s_2, \dots, s_n$$$. All strings consist of lowercase Latin letters.
Let $$$f(t, s)$$$ be the number of occurences of string $$$s$$$ in string $$$t$$$. For example, $$$f('\text{aaabacaa}', '\text{aa}') = 3$$$, and $$$f('\text{ababa}', '\text{aba}') = 2$$$.
Calculate the value of $$$\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j)$$$, where $$$s + t$$$ is the concatenation of strings $$$s$$$ and $$$t$$$. Note that if there are two pairs $$$i_1$$$, $$$j_1$$$ and $$$i_2$$$, $$$j_2$$$ such that $$$s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}$$$, you should include both $$$f(t, s_{i_1} + s_{j_1})$$$ and $$$f(t, s_{i_2} + s_{j_2})$$$ in answer.
## Input Format
The first line contains string $$$t$$$ ($$$1 \le |t| \le 2 \cdot 10^5$$$).
The second line contains integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).
Each of next $$$n$$$ lines contains string $$$s_i$$$ ($$$1 \le |s_i| \le 2 \cdot 10^5$$$).
It is guaranteed that $$$\sum\limits_{i=1}^{n} |s_i| \le 2 \cdot 10^5$$$. All strings consist of lowercase English letters.
## Output Format
Print one integer — the value of $$$\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j)$$$.
## Examples
```input
aaabacaa
2
a
aa
```
```output
5
```
-----
```input
aaabacaa
4
a
a
a
b
```
```output
33
```
Now solve the problem and return the code.
|
{}
|
codeforces_1202_1202/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given $$$a$$$ uppercase Latin letters 'A' and $$$b$$$ letters 'B'.
The period of the string is the smallest such positive integer $$$k$$$ that $$$s_i = s_{i~mod~k}$$$ ($$$0$$$-indexed) for each $$$i$$$. Note that this implies that $$$k$$$ won't always divide $$$a+b = |s|$$$.
For example, the period of string "ABAABAA" is $$$3$$$, the period of "AAAA" is $$$1$$$, and the period of "AABBB" is $$$5$$$.
Find the number of different periods over all possible strings with $$$a$$$ letters 'A' and $$$b$$$ letters 'B'.
## Input Format
The first line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$) — the number of letters 'A' and 'B', respectively.
## Output Format
Print the number of different periods over all possible strings with $$$a$$$ letters 'A' and $$$b$$$ letters 'B'.
## Examples
```input
2 4
```
```output
4
```
-----
```input
5 3
```
```output
5
```
## Note
All the possible periods for the first example:
- $$$3$$$ "BBABBA"
- $$$4$$$ "BBAABB"
- $$$5$$$ "BBBAAB"
- $$$6$$$ "AABBBB"
All the possible periods for the second example:
- $$$3$$$ "BAABAABA"
- $$$5$$$ "BAABABAA"
- $$$6$$$ "BABAAABA"
- $$$7$$$ "BAABAAAB"
- $$$8$$$ "AAAAABBB"
Note that these are not the only possible strings for the given periods.
Now solve the problem and return the code.
|
{}
|
codeforces_1206_1206/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$A$$$, consisting of $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$, and an array $$$B$$$, consisting of $$$m$$$ positive integers $$$b_1, b_2, \dots, b_m$$$.
Choose some element $$$a$$$ of $$$A$$$ and some element $$$b$$$ of $$$B$$$ such that $$$a+b$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$.
For example, if $$$A = [2, 1, 7]$$$ and $$$B = [1, 3, 4]$$$, we can choose $$$1$$$ from $$$A$$$ and $$$4$$$ from $$$B$$$, as number $$$5 = 1 + 4$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. However, we can't choose $$$2$$$ from $$$A$$$ and $$$1$$$ from $$$B$$$, as $$$3 = 2 + 1$$$ belongs to $$$B$$$.
It can be shown that such a pair exists. If there are multiple answers, print any.
Choose and print any such two numbers.
## Input Format
The first line contains one integer $$$n$$$ ($$$1\le n \le 100$$$) — the number of elements of $$$A$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 200$$$) — the elements of $$$A$$$.
The third line contains one integer $$$m$$$ ($$$1\le m \le 100$$$) — the number of elements of $$$B$$$.
The fourth line contains $$$m$$$ different integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le 200$$$) — the elements of $$$B$$$.
It can be shown that the answer always exists.
## Output Format
Output two numbers $$$a$$$ and $$$b$$$ such that $$$a$$$ belongs to $$$A$$$, $$$b$$$ belongs to $$$B$$$, but $$$a+b$$$ doesn't belong to nor $$$A$$$ neither $$$B$$$.
If there are multiple answers, print any.
## Examples
```input
1
20
2
10 20
```
```output
20 20
```
-----
```input
3
3 2 2
5
1 5 7 7 9
```
```output
3 1
```
-----
```input
4
1 3 5 7
4
7 5 3 1
```
```output
1 1
```
## Note
In the first example, we can choose $$$20$$$ from array $$$[20]$$$ and $$$20$$$ from array $$$[10, 20]$$$. Number $$$40 = 20 + 20$$$ doesn't belong to any of those arrays. However, it is possible to choose $$$10$$$ from the second array too.
In the second example, we can choose $$$3$$$ from array $$$[3, 2, 2]$$$ and $$$1$$$ from array $$$[1, 5, 7, 7, 9]$$$. Number $$$4 = 3 + 1$$$ doesn't belong to any of those arrays.
In the third example, we can choose $$$1$$$ from array $$$[1, 3, 5, 7]$$$ and $$$1$$$ from array $$$[7, 5, 3, 1]$$$. Number $$$2 = 1 + 1$$$ doesn't belong to any of those arrays.
Now solve the problem and return the code.
|
{}
|
codeforces_1206_1206/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:
Choose one of these numbers and add or subtract $$$1$$$ from it.
In particular, we can apply this operation to the same number several times.
We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$.
For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.
What is the minimum cost we will have to pay to do that?
## Input Format
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.
## Output Format
Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.
## Examples
```input
2
-1 1
```
```output
2
```
-----
```input
4
0 0 0 0
```
```output
4
```
-----
```input
5
-5 -3 5 3 0
```
```output
13
```
## Note
In the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.
In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.
In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 512.0 MB
# Problem
Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced — their sum is equal to $$$0$$$.
Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.
There are two conditions though:
- For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$\frac{a_i}{2}$$$ as possible. It means that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = \frac{a_i}{2}$$$. Here $$$\lfloor x \rfloor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$\lceil x \rceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$.
- The modified rating changes must be perfectly balanced — their sum must be equal to $$$0$$$.
Can you help with that?
## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 13\,845$$$), denoting the number of participants.
Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$-336 \le a_i \le 1164$$$), denoting the rating change of the $$$i$$$-th participant.
The sum of all $$$a_i$$$ is equal to $$$0$$$.
## Output Format
Output $$$n$$$ integers $$$b_i$$$, each denoting the modified rating change of the $$$i$$$-th participant in order of input.
For any $$$i$$$, it must be true that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. The sum of all $$$b_i$$$ must be equal to $$$0$$$.
If there are multiple solutions, print any. We can show that a solution exists for any valid input.
## Examples
```input
3
10
-5
-5
```
```output
5
-2
-3
```
-----
```input
7
-7
-29
0
3
24
-29
38
```
```output
-3
-15
0
2
12
-15
19
```
## Note
In the first example, $$$b_1 = 5$$$, $$$b_2 = -3$$$ and $$$b_3 = -2$$$ is another correct solution.
In the second example there are $$$6$$$ possible solutions, one of them is shown in the example output.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/B
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 512.0 MB
# Problem
Consider a tunnel on a one-way road. During a particular day, $$$n$$$ cars numbered from $$$1$$$ to $$$n$$$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.
A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.
Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.
Traffic regulations prohibit overtaking inside the tunnel. If car $$$i$$$ overtakes any other car $$$j$$$ inside the tunnel, car $$$i$$$ must be fined. However, each car can be fined at most once.
Formally, let's say that car $$$i$$$ definitely overtook car $$$j$$$ if car $$$i$$$ entered the tunnel later than car $$$j$$$ and exited the tunnel earlier than car $$$j$$$. Then, car $$$i$$$ must be fined if and only if it definitely overtook at least one other car.
Find the number of cars that must be fined.
## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$), denoting the number of cars.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$), denoting the ids of cars in order of entering the tunnel. All $$$a_i$$$ are pairwise distinct.
The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$), denoting the ids of cars in order of exiting the tunnel. All $$$b_i$$$ are pairwise distinct.
## Output Format
Output the number of cars to be fined.
## Examples
```input
5
3 5 2 1 4
4 3 2 5 1
```
```output
2
```
-----
```input
7
5 2 3 6 7 1 4
2 3 6 7 1 4 5
```
```output
6
```
-----
```input
2
1 2
1 2
```
```output
0
```
## Note
The first example is depicted below:
Car $$$2$$$ definitely overtook car $$$5$$$, while car $$$4$$$ definitely overtook cars $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$. Cars $$$2$$$ and $$$4$$$ must be fined.
In the second example car $$$5$$$ was definitely overtaken by all other cars.
In the third example no car must be fined.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/C1
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 512.0 MB
# Problem
This is an easier version of the problem. In this version, $$$n \le 2000$$$.
There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.
You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.
Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate.
Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$; $$$n$$$ is even), denoting the number of points.
Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point.
No two points coincide.
## Output Format
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
## Examples
```input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
```
```output
3 6
5 1
2 4
```
-----
```input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
```
```output
4 5
1 6
2 7
3 8
```
## Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/C2
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 512.0 MB
# Problem
This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.
There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.
You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.
Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate.
Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.
## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points.
Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point.
No two points coincide.
## Output Format
Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once.
We can show that it is always possible to remove all points. If there are many solutions, output any of them.
## Examples
```input
6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0
```
```output
3 6
5 1
2 4
```
-----
```input
8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3
```
```output
4 5
1 6
2 7
3 8
```
## Note
In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/D
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 512.0 MB
# Problem
Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of $$$n$$$ tracks numbered from $$$1$$$ to $$$n$$$. The playlist is automatic and cyclic: whenever track $$$i$$$ finishes playing, track $$$i+1$$$ starts playing automatically; after track $$$n$$$ goes track $$$1$$$.
For each track $$$i$$$, you have estimated its coolness $$$a_i$$$. The higher $$$a_i$$$ is, the cooler track $$$i$$$ is.
Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness $$$x$$$ of already played tracks. Once you hear that a track with coolness strictly less than $$$\frac{x}{2}$$$ (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.
For each track $$$i$$$, find out how many tracks you will listen to before turning off the music if you start your morning with track $$$i$$$, or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.
## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$), denoting the number of tracks in the playlist.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), denoting coolnesses of the tracks.
## Output Format
Output $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$, where $$$c_i$$$ is either the number of tracks you will listen to if you start listening from track $$$i$$$ or $$$-1$$$ if you will be listening to music indefinitely.
## Examples
```input
4
11 5 2 7
```
```output
1 1 3 2
```
-----
```input
4
3 2 5 3
```
```output
5 4 3 6
```
-----
```input
3
4 3 6
```
```output
-1 -1 -1
```
## Note
In the first example, here is what will happen if you start with...
- track $$$1$$$: listen to track $$$1$$$, stop as $$$a_2 < \frac{a_1}{2}$$$.
- track $$$2$$$: listen to track $$$2$$$, stop as $$$a_3 < \frac{a_2}{2}$$$.
- track $$$3$$$: listen to track $$$3$$$, listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < \frac{\max(a_3, a_4, a_1)}{2}$$$.
- track $$$4$$$: listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < \frac{\max(a_4, a_1)}{2}$$$.
In the second example, if you start with track $$$4$$$, you will listen to track $$$4$$$, listen to track $$$1$$$, listen to track $$$2$$$, listen to track $$$3$$$, listen to track $$$4$$$ again, listen to track $$$1$$$ again, and stop as $$$a_2 < \frac{max(a_4, a_1, a_2, a_3, a_4, a_1)}{2}$$$. Note that both track $$$1$$$ and track $$$4$$$ are counted twice towards the result.
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/E
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 512.0 MB
# Problem
Recall that a binary search tree is a rooted binary tree, whose nodes each store a key and each have at most two distinguished subtrees, left and right. The key in each node must be greater than any key stored in the left subtree, and less than any key stored in the right subtree.
The depth of a vertex is the number of edges on the simple path from the vertex to the root. In particular, the depth of the root is $$$0$$$.
Let's call a binary search tree perfectly balanced if there doesn't exist a binary search tree with the same number of vertices that has a strictly smaller sum of depths of its vertices.
Let's call a binary search tree with integer keys striped if both of the following conditions are satisfied for every vertex $$$v$$$:
- If $$$v$$$ has a left subtree whose root is $$$u$$$, then the parity of the key of $$$v$$$ is different from the parity of the key of $$$u$$$.
- If $$$v$$$ has a right subtree whose root is $$$w$$$, then the parity of the key of $$$v$$$ is the same as the parity of the key of $$$w$$$.
You are given a single integer $$$n$$$. Find the number of perfectly balanced striped binary search trees with $$$n$$$ vertices that have distinct integer keys between $$$1$$$ and $$$n$$$, inclusive. Output this number modulo $$$998\,244\,353$$$.
## Input Format
The only line contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$), denoting the required number of vertices.
## Output Format
Output the number of perfectly balanced striped binary search trees with $$$n$$$ vertices and distinct integer keys between $$$1$$$ and $$$n$$$, inclusive, modulo $$$998\,244\,353$$$.
## Examples
```input
4
```
```output
1
```
-----
```input
3
```
```output
0
```
## Note
In the first example, this is the only tree that satisfies the conditions:
In the second example, here are various trees that don't satisfy some condition:
Now solve the problem and return the code.
|
{}
|
codeforces_1237_1237/F
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 512.0 MB
# Problem
Consider a square grid with $$$h$$$ rows and $$$w$$$ columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino.
Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells covered by two different dominoes. In other words, every row and column may contain no covered cells, one covered cell, or two covered cells that belong to the same domino.
You are given a perfectly balanced placement of dominoes on a grid. Find the number of ways to place zero or more extra dominoes on this grid to keep the placement perfectly balanced. Output this number modulo $$$998\,244\,353$$$.
## Input Format
The first line contains three integers $$$h$$$, $$$w$$$, and $$$n$$$ ($$$1 \le h, w \le 3600$$$; $$$0 \le n \le 2400$$$), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from $$$1$$$ to $$$h$$$, and the columns are numbered from $$$1$$$ to $$$w$$$.
Each of the next $$$n$$$ lines contains four integers $$$r_{i, 1}, c_{i, 1}, r_{i, 2}, c_{i, 2}$$$ ($$$1 \le r_{i, 1} \le r_{i, 2} \le h$$$; $$$1 \le c_{i, 1} \le c_{i, 2} \le w$$$), denoting the row id and the column id of the cells covered by the $$$i$$$-th domino. Cells $$$(r_{i, 1}, c_{i, 1})$$$ and $$$(r_{i, 2}, c_{i, 2})$$$ are distinct and share a common side.
The given domino placement is perfectly balanced.
## Output Format
Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo $$$998\,244\,353$$$.
## Examples
```input
5 7 2
3 1 3 2
4 4 4 5
```
```output
8
```
-----
```input
5 4 2
1 2 2 2
4 3 4 4
```
```output
1
```
-----
```input
23 42 0
```
```output
102848351
```
## Note
In the first example, the initial grid looks like this:
Here are $$$8$$$ ways to place zero or more extra dominoes to keep the placement perfectly balanced:
In the second example, the initial grid looks like this:
No extra dominoes can be placed here.
Now solve the problem and return the code.
|
{}
|
codeforces_1249_1249/A
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are a coach of a group consisting of $$$n$$$ students. The $$$i$$$-th student has programming skill $$$a_i$$$. All students have distinct programming skills. You want to divide them into teams in such a way that:
- No two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than $$$1$$$);
- the number of teams is the minimum possible.
You have to answer $$$q$$$ independent queries.
## Input Format
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Then $$$q$$$ queries follow.
The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of students in the query. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$, all $$$a_i$$$ are distinct), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student.
## Output Format
For each query, print the answer on it — the minimum number of teams you can form if no two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than $$$1$$$)
## Examples
```input
4
4
2 10 1 20
2
3 6
5
2 3 4 99 100
1
42
```
```output
2
1
2
1
```
## Note
In the first query of the example, there are $$$n=4$$$ students with the skills $$$a=[2, 10, 1, 20]$$$. There is only one restriction here: the $$$1$$$-st and the $$$3$$$-th students can't be in the same team (because of $$$|a_1 - a_3|=|2-1|=1$$$). It is possible to divide them into $$$2$$$ teams: for example, students $$$1$$$, $$$2$$$ and $$$4$$$ are in the first team and the student $$$3$$$ in the second team.
In the second query of the example, there are $$$n=2$$$ students with the skills $$$a=[3, 6]$$$. It is possible to compose just a single team containing both students.
Now solve the problem and return the code.
|
{}
|
codeforces_1249_1249/B1
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
The only difference between easy and hard versions is constraints.
There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.
For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.
Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.
Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids:
- after the $$$1$$$-st day it will belong to the $$$5$$$-th kid,
- after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid,
- after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid,
- after the $$$4$$$-th day it will belong to the $$$1$$$-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
You have to answer $$$q$$$ independent queries.
## Input Format
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow.
The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid.
## Output Format
For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query.
## Examples
```input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
```
```output
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4
```
Now solve the problem and return the code.
|
{}
|
codeforces_1249_1249/B2
|
codeforces
|
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.
Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.
Put your final solution within a single code block:
```cpp
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
The only difference between easy and hard versions is constraints.
There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.
For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.
Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.
Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids:
- after the $$$1$$$-st day it will belong to the $$$5$$$-th kid,
- after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid,
- after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid,
- after the $$$4$$$-th day it will belong to the $$$1$$$-st kid.
So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
You have to answer $$$q$$$ independent queries.
## Input Format
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 1000$$$) — the number of queries. Then $$$q$$$ queries follow.
The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid.
It is guaranteed that $$$\sum n \le 2 \cdot 10^5$$$ (sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$).
## Output Format
For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query.
## Examples
```input
6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3
```
```output
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4
```
Now solve the problem and return the code.
|
{}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.