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.
|
{}
|
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- -