rating int64 800 3.5k | tags listlengths 0 11 | official_tests listlengths 1 150 | prompt stringlengths 1.11k 5.74k |
|---|---|---|---|
800 | [
"math",
"number theory"
] | [
{
"input": "6\r\n1 2\r\n1 10\r\n49 49\r\n69 420\r\n1 1\r\n9982 44353\r\n",
"output": "1\r\n9\r\n0\r\n351\r\n1\r\n34371\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Today, Little John used all his savings to buy a segment. He wants to build a house on this segment.
A segment of positive integers $$$[l,r]$$$ is called coprime if $$$l$$$ and $$$r$$$ are coprime$$$^{\text{∗}}$$$.
A coprime segment $$$[l,r]$$$ is called minimal coprime if it does not contain$$$^{\text{†}}$$$ any coprime segment not equal to itself. To better understand this statement, you can refer to the notes.
Given $$$[l,r]$$$, a segment of positive integers, find the number of minimal coprime segments contained in $$$[l,r]$$$.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The only line of each test case consists of two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 10^9$$$).
## Output Format
For each test case, output the number of minimal coprime segments contained in $$$[l,r]$$$, on a separate line.
## Examples
```input
6
1 2
1 10
49 49
69 420
1 1
9982 44353
```
```output
1
9
0
351
1
34371
```
## Note
On the first test case, the given segment is $$$[1,2]$$$. The segments contained in $$$[1,2]$$$ are as follows.
- $$$[1,1]$$$: This segment is coprime, since the numbers $$$1$$$ and $$$1$$$ are coprime, and this segment does not contain any other segment inside. Thus, $$$[1,1]$$$ is minimal coprime.
- $$$[1,2]$$$: This segment is coprime. However, as it contains $$$[1,1]$$$, which is also coprime, $$$[1,2]$$$ is not minimal coprime.
- $$$[2,2]$$$: This segment is not coprime because $$$2$$$ and $$$2$$$ share $$$2$$$ positive common divisors: $$$1$$$ and $$$2$$$.
Therefore, the segment $$$[1,2]$$$ contains $$$1$$$ minimal coprime segment.
Now solve the problem and return the code.
|
1,100 | [
"constructive algorithms",
"data structures",
"greedy",
"sortings"
] | [
{
"input": "6\r\n2 1 1\r\n2 1\r\n3 2 3\r\n1 2 3\r\n3 1 3\r\n3 1 2\r\n4 2 3\r\n1 2 2 2\r\n5 2 5\r\n3 3 2 3 5\r\n6 1 3\r\n3 6 6 4 3 2\r\n",
"output": "1\r\n3\r\n6\r\n3\r\n11\r\n8\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.5 seconds
Memory limit: 256.0 MB
# Problem
After Little John borrowed expansion screws from auntie a few hundred times, eventually she decided to come and take back the unused ones.But as they are a crucial part of home design, Little John decides to hide some in the most unreachable places — under the eco-friendly wood veneers.
You are given an integer sequence $$$a_1, a_2, \ldots, a_n$$$, and a segment $$$[l,r]$$$ ($$$1 \le l \le r \le n$$$).
You must perform the following operation on the sequence exactly once.
- Choose any subsequence$$$^{\text{∗}}$$$ of the sequence $$$a$$$, and reverse it. Note that the subsequence does not have to be contiguous.
Formally, choose any number of indices $$$i_1,i_2,\ldots,i_k$$$ such that $$$1 \le i_1 < i_2 < \ldots < i_k \le n$$$. Then, change the $$$i_x$$$-th element to the original value of the $$$i_{k-x+1}$$$-th element simultaneously for all $$$1 \le x \le k$$$.
Find the minimum value of $$$a_l+a_{l+1}+\ldots+a_{r-1}+a_r$$$ after performing the operation.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n \le 10^5$$$) — the length of $$$a$$$, and the segment $$$[l,r]$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_{i} \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
## Output Format
For each test case, output the minimum value of $$$a_l+a_{l+1}+\ldots+a_{r-1}+a_r$$$ on a separate line.
## Examples
```input
6
2 1 1
2 1
3 2 3
1 2 3
3 1 3
3 1 2
4 2 3
1 2 2 2
5 2 5
3 3 2 3 5
6 1 3
3 6 6 4 3 2
```
```output
1
3
6
3
11
8
```
## Note
On the second test case, the array is $$$a=[1,2,3]$$$ and the segment is $$$[2,3]$$$.
After choosing the subsequence $$$a_1,a_3$$$ and reversing it, the sequence becomes $$$[3,2,1]$$$. Then, the sum $$$a_2+a_3$$$ becomes $$$3$$$. It can be shown that the minimum possible value of the sum is $$$3$$$.
Now solve the problem and return the code.
|
1,600 | [
"brute force",
"data structures",
"dfs and similar",
"dp",
"graphs",
"greedy",
"sortings",
"trees"
] | [
{
"input": "3\r\n2\r\n1 2\r\n4\r\n1 2\r\n2 3\r\n2 4\r\n7\r\n1 2\r\n1 3\r\n2 4\r\n4 5\r\n5 6\r\n5 7\r\n",
"output": "0\r\n2\r\n4\r\n"
},
{
"input": "1\r\n22\r\n1 2\r\n3 4\r\n5 6\r\n7 8\r\n9 10\r\n11 12\r\n13 14\r\n15 16\r\n17 2\r\n17 4\r\n17 6\r\n17 8\r\n18 10\r\n18 12\r\n18 14\r\n18 16\r\n17 19\r\n1... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Recently, Little John got a tree from his aunt to decorate his house. But as it seems, just one tree is not enough to decorate the entire house. Little John has an idea. Maybe he can remove a few vertices from the tree. That will turn it into more trees! Right?
You are given a tree$$$^{\text{∗}}$$$ of $$$n$$$ vertices. You must perform the following operation exactly twice.
- Select a vertex $$$v$$$;
- Remove all edges incident to $$$v$$$, and also the vertex $$$v$$$.
Please find the maximum number of connected components after performing the operation exactly twice.
Two vertices $$$x$$$ and $$$y$$$ are in the same connected component if and only if there exists a path from $$$x$$$ to $$$y$$$. For clarity, note that the graph with $$$0$$$ vertices has $$$0$$$ connected components by definition.$$$^{\text{†}}$$$
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).
Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$, denoting the two vertices connected by an edge ($$$1 \le u_i,v_i \le n$$$, $$$u_i \neq v_i$$$). It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
## Output Format
For each test case, output the maximum number of connected components on a separate line.
## Examples
```input
3
2
1 2
4
1 2
2 3
2 4
7
1 2
1 3
2 4
4 5
5 6
5 7
```
```output
0
2
4
```
## Note
On the first test case, removing a vertex twice will make the graph empty. By definition, the number of connected components in the graph with $$$0$$$ vertices is $$$0$$$. Therefore, the answer is $$$0$$$.
On the second test case, removing two vertices $$$1$$$ and $$$2$$$ leaves $$$2$$$ connected components. As it is impossible to make $$$3$$$ connected components with $$$2$$$ vertices, the answer is $$$2$$$.
On the third test case, removing two vertices $$$1$$$ and $$$5$$$ leaves $$$4$$$ connected components, which are $$$\left\{ 2,4\right\}$$$, $$$\left\{ 3\right\}$$$, $$$\left\{ 6\right\}$$$, and $$$\left\{ 7\right\}$$$. It can be shown that it is impossible to make $$$5$$$ connected components. Therefore, the answer is $$$4$$$.
Now solve the problem and return the code.
|
2,000 | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math",
"ternary search",
"two pointers"
] | [
{
"input": "5\r\n1 3\r\n0\r\n0 1 -1\r\n2 4\r\n0 100\r\n-100 -50 0 50\r\n2 4\r\n0 1000\r\n-100 -50 0 50\r\n6 6\r\n20 1 27 100 43 42\r\n100 84 1 24 22 77\r\n8 2\r\n564040265 -509489796 469913620 198872582 -400714529 553177666 131159391 -20796763\r\n-1000000000 1000000000\r\n",
"output": "1\r\n2\r\n2\r\n150 20... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Even Little John needs money to buy a house. But he recently lost his job; how will he earn money now? Of course, by playing a game that gives him money as a reward! Oh well, maybe not those kinds of games you are thinking about.
There are $$$n+m$$$ distinct points $$$(a_1,0), (a_2,0), \ldots, (a_{n},0), (b_1,2), (b_2,2), \ldots, (b_{m},2)$$$ on the plane. Initially, your score is $$$0$$$. To increase your score, you can perform the following operation:
- Choose three distinct points which are not collinear;
- Increase your score by the area of the triangle formed by these three points;
- Then, erase the three points from the plane.
An instance of the game, where the operation is performed twice.
Let $$$k_{\max}$$$ be the maximum number of operations that can be performed. For example, if it is impossible to perform any operation, $$$k_\max$$$ is $$$0$$$. Additionally, define $$$f(k)$$$ as the maximum possible score achievable by performing the operation exactly $$$k$$$ times. Here, $$$f(k)$$$ is defined for all integers $$$k$$$ such that $$$0 \le k \le k_{\max}$$$.
Find the value of $$$k_{\max}$$$, and find the values of $$$f(x)$$$ for all integers $$$x=1,2,\ldots,k_{\max}$$$ independently.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le {3 \cdot 10^4}$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 2 \cdot 10^5$$$).
The second line of each test case contains $$$n$$$ pairwise distinct integers $$$a_1,a_2,\ldots,a_{n}$$$ — the points on $$$y=0$$$ ($$$-10^9 \le a_i \le 10^9$$$).
The third line of each test case contains $$$m$$$ pairwise distinct integers $$$b_1,b_2,\ldots,b_{m}$$$ — the points on $$$y=2$$$ ($$$-10^9 \le b_i \le 10^9$$$).
It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$.
## Output Format
For each test case, given that the maximum number of operations is $$$k_{\max}$$$, you must output at most two lines:
- The first line contains the value of $$$k_{\max}$$$;
- The second line contains $$$k_{\max}$$$ integers denoting $$$f(1),f(2),\ldots,f(k_{\max})$$$. You are allowed to omit this line if $$$k_{\max}$$$ is $$$0$$$.
Note that under the constraints of this problem, it can be shown that all values of $$$f(x)$$$ are integers no greater than $$$10^{16}$$$.
## Examples
```input
5
1 3
0
0 1 -1
2 4
0 100
-100 -50 0 50
2 4
0 1000
-100 -50 0 50
6 6
20 1 27 100 43 42
100 84 1 24 22 77
8 2
564040265 -509489796 469913620 198872582 -400714529 553177666 131159391 -20796763
-1000000000 1000000000
```
```output
1
2
2
150 200
2
1000 200
4
99 198 260 283
2
2000000000 2027422256
```
## Note
On the first test case, there are $$$1+3=4$$$ points $$$(0,0),(0,2),(1,2),(-1,2)$$$.
It can be shown that you cannot perform two or more operations. The value of $$$k_{\max}$$$ is $$$1$$$, and you are only asked for the value of $$$f(1)$$$.
You can choose $$$(0,0)$$$, $$$(-1,2)$$$, and $$$(1,2)$$$ as the three vertices of the triangle. After that, your score is increased by the area of the triangle, which is $$$2$$$. Then, the three points are erased from the plane. It can be shown that the maximum value of your score after performing one operation is $$$2$$$. Therefore, the value of $$$f(1)$$$ is $$$2$$$.
On the fifth test case, there are $$$8+2=10$$$ points.
It can be shown that you cannot perform three or more operations. The value of $$$k_{\max}$$$ is $$$2$$$, and you are asked for the values $$$f(1)$$$ and $$$f(2)$$$.
To maximize the score with only one operation, you can choose three points $$$(198\,872\,582,0)$$$, $$$(-1\,000\,000\,000,2)$$$, and $$$(1\,000\,000\,000,2)$$$. Then, the three points are erased from the plane. It can be shown that the maximum value of your score after performing one operation is $$$2\,000\,000\,000$$$. Therefore, the value of $$$f(1)$$$ is $$$2\,000\,000\,000$$$.
To maximize the score with exactly two operations, you can choose the following sequence of operations.
- Choose three points $$$(-509\,489\,796,0)$$$, $$$(553\,177\,666,0)$$$, and $$$(-1\,000\,000\,000,2)$$$. The three points are erased.
- Choose three points $$$(-400\,714\,529,0)$$$, $$$(564\,040\,265,0)$$$, and $$$(1\,000\,000\,000,2)$$$. The three points are erased.
Then, the score after two operations becomes $$$2\,027\,422\,256$$$. It can be shown that the maximum value of your score after performing exactly two operations is $$$2\,027\,422\,256$$$. Therefore, the value of $$$f(2)$$$ is $$$2\,027\,422\,256$$$.
Now solve the problem and return the code.
|
2,300 | [
"data structures",
"dfs and similar",
"dp",
"greedy",
"trees"
] | [
{
"input": "4\r\n3\r\n1 2\r\n1 3\r\n3\r\n1 2\r\n3 2\r\n5\r\n2 3\r\n1 5\r\n4 2\r\n1 2\r\n11\r\n2 1\r\n2 3\r\n2 4\r\n4 5\r\n6 5\r\n5 7\r\n4 8\r\n8 9\r\n7 10\r\n10 11\r\n",
"output": "1\r\n0\r\n4\r\n29\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 512.0 MB
# Problem
One day, a giant tree grew in the countryside. Little John, with his childhood eagle, decided to make it his home. Little John will build a structure on the tree with galvanized square steel. However, little did he know, he could not build what is physically impossible.
You are given a rooted tree$$$^{\text{∗}}$$$ containing $$$n$$$ vertices rooted at vertex $$$1$$$. A pair of vertices $$$(u,v)$$$ is called a good pair if $$$u$$$ is not an ancestor$$$^{\text{†}}$$$ of $$$v$$$ and $$$v$$$ is not an ancestor of $$$u$$$. For any two vertices, $$$\text{dist}(u,v)$$$ is defined as the number of edges on the unique simple path from $$$u$$$ to $$$v$$$, and $$$\text{lca}(u,v)$$$ is defined as their lowest common ancestor.
A function $$$f(u,v)$$$ is defined as follows.
- If $$$(u,v)$$$ is a good pair, $$$f(u,v)$$$ is the number of distinct integer values $$$x$$$ such that there exists a non-degenerate triangle$$$^{\text{‡}}$$$ formed by side lengths $$$\text{dist}(u,\text{lca}(u,v))$$$, $$$\text{dist}(v,\text{lca}(u,v))$$$, and $$$x$$$.
- Otherwise, $$$f(u,v)$$$ is $$$0$$$.
You need to find the following value: $$$$$$\sum_{i = 1}^{n-1} \sum_{j = i+1}^n f(i,j).$$$$$$
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$).
Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$, denoting the two vertices connected by an edge ($$$1 \le u_i,v_i \le n$$$, $$$u_i \neq v_i$$$).
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
## Output Format
For each test case, output the answer on a separate line.
## Examples
```input
4
3
1 2
1 3
3
1 2
3 2
5
2 3
1 5
4 2
1 2
11
2 1
2 3
2 4
4 5
6 5
5 7
4 8
8 9
7 10
10 11
```
```output
1
0
4
29
```
## Note
On the first test case, the only good pair $$$(i,j)$$$ satisfying $$$i<j$$$ is $$$(2,3)$$$. Here, $$$\text{lca}(2,3)$$$ is $$$1$$$, and the two distances are $$$1$$$ and $$$1$$$.
There is only one value of $$$x$$$ for two side lengths $$$1$$$ and $$$1$$$, which is $$$1$$$. Therefore, the answer for the first test case is $$$1$$$.
On the second test case, there is no good pair. Therefore, the answer for the second test case is $$$0$$$.
On the third test case, the good pairs $$$(i,j)$$$ satisfying $$$i<j$$$ are as follows.
- $$$(2,5)$$$: $$$\text{lca}(2,5)$$$ is $$$1$$$, distances are $$$1$$$ and $$$1$$$. There is only one possible value of $$$x$$$, which is $$$1$$$.
- $$$(3,4)$$$: $$$\text{lca}(3,4)$$$ is $$$2$$$, distances are $$$1$$$ and $$$1$$$. There is only one possible value of $$$x$$$, which is $$$1$$$.
- $$$(3,5)$$$: $$$\text{lca}(3,5)$$$ is $$$1$$$, distances are $$$2$$$ and $$$1$$$. There is only one possible value of $$$x$$$, which is $$$2$$$.
- $$$(4,5)$$$: $$$\text{lca}(4,5)$$$ is $$$1$$$, distances are $$$2$$$ and $$$1$$$. There is only one possible value of $$$x$$$, which is $$$2$$$.
Therefore, the answer for the third test case is $$$1+1+1+1=4$$$.
Now solve the problem and return the code.
|
2,400 | [
"combinatorics",
"data structures",
"dfs and similar",
"dp",
"dsu",
"graphs",
"hashing",
"implementation",
"math",
"trees"
] | [
{
"input": "3\r\n3\r\n2 5\r\n1 6\r\n3 4\r\n4\r\n1 6\r\n7 8\r\n2 3\r\n4 5\r\n6\r\n2 3\r\n1 6\r\n7 8\r\n9 12\r\n10 11\r\n4 5\r\n",
"output": "5 1 1 1\r\n14 2 2 1 1\r\n132 42 5 2 1 1 1\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 512.0 MB
# Problem
This is the easy version of the problem. The difference between the versions is that in this version, the limits on $$$t$$$ and $$$n$$$ are smaller. You can hack only if you solved all versions of this problem.
Now Little John is rich, and so he finally buys a house big enough to fit himself and his favorite bracket sequence. But somehow, he ended up with a lot of brackets! Frustrated, he penetrates through the ceiling with the "buddha palm".
A bracket sequence is called balanced if it can be constructed by the following formal grammar.
1. The empty sequence $$$\varnothing$$$ is balanced.
2. If the bracket sequence $$$A$$$ is balanced, then $$$\mathtt{(}A\mathtt{)}$$$ is also balanced.
3. If the bracket sequences $$$A$$$ and $$$B$$$ are balanced, then the concatenated sequence $$$A B$$$ is also balanced.
For example, the sequences "(())()", "()", "(()(()))", and the empty sequence are balanced, while "(()" and "(()))(" are not.
Given a balanced bracket sequence $$$s$$$, a pair of indices $$$(i,j)$$$ ($$$i<j$$$) is called a good pair if $$$s_i$$$ is '(', $$$s_j$$$ is ')', and the two brackets are added simultaneously with respect to Rule 2 while constructing the sequence $$$s$$$. For example, the sequence "(())()" has three different good pairs, which are $$$(1,4)$$$, $$$(2,3)$$$, and $$$(5,6)$$$. One can show that any balanced bracket sequence of $$$2n$$$ brackets contains exactly $$$n$$$ different good pairs, and using any order of rules to construct the same bracket sequence will yield the same set of good pairs.
Emily will play a bracket guessing game with John. The game is played as follows.
Initially, John has a balanced bracket sequence $$$s$$$ containing $$$n$$$ different good pairs, which is not known to Emily. John tells Emily the value of $$$n$$$ and asks Emily to guess the sequence.
Throughout $$$n$$$ turns, John gives Emily the following kind of clue on each turn.
- $$$l\;r$$$: The sequence $$$s$$$ contains a good pair $$$(l,r)$$$.
The clues that John gives Emily are pairwise distinct and do not contradict each other.
At a certain point, Emily can be certain that the balanced bracket sequence satisfying the clues given so far is unique. For example, assume Emily knows that $$$s$$$ has $$$3$$$ good pairs, and it contains the good pair $$$(2,5)$$$. Out of $$$5$$$ balanced bracket sequences with $$$3$$$ good pairs, there exists only one such sequence "((()))" with the good pair $$$(2,5)$$$. Therefore, one can see that Emily does not always need $$$n$$$ turns to guess $$$s$$$.
To find out the content of $$$s$$$ as early as possible, Emily wants to know the number of different balanced bracket sequences that match the clues after each turn. Surely, this is not an easy job for Emily, especially when she is given so many good pairs. Now it is your turn to help Emily. Given the clues, you must find the answer before and after each turn. As the answers may be huge, you need to find them modulo $$$998\,244\,353$$$.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows.
The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 5000$$$) — the number of good pairs.
Then, each of the $$$n$$$ following lines contains two integers $$$l_i$$$ and $$$r_i$$$ representing the $$$i$$$-th clue ($$$1 \le l_i < r_i \le 2n$$$).
The clues in one test case are pairwise distinct and do not contradict each other.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
## Output Format
For each test case, output $$$n+1$$$ integers on a separate line:
- The first integer is the answer before all clues, modulo $$$998\,244\,353$$$.
- For all $$$i \ge 1$$$, the $$$i+1$$$-th integer is the answer after the $$$i$$$-th clue, modulo $$$998\,244\,353$$$.
## Examples
```input
3
3
2 5
1 6
3 4
4
1 6
7 8
2 3
4 5
6
2 3
1 6
7 8
9 12
10 11
4 5
```
```output
5 1 1 1
14 2 2 1 1
132 42 5 2 1 1 1
```
## Note
The first test case of the example is explained in the problem description.
The third test case of the example is explained as follows. It can be shown that there are $$$132$$$ balanced bracket sequences with $$$6$$$ good pairs. The answers after each clue are given as follows:
1. You are given the good pair $$$(2,3)$$$. There are $$$42$$$ balanced bracket sequences having the good pair $$$(2,3)$$$.
2. You are given the good pair $$$(1,6)$$$. There are $$$5$$$ balanced bracket sequences having good pairs $$$(2,3)$$$, $$$(1,6)$$$.
3. You are given the good pair $$$(7,8)$$$. There are $$$2$$$ balanced bracket sequences having the three good pairs. The strings are "(()())()(())" and "(()())()()()", respectively.
4. You are given the good pair $$$(9,12)$$$. There is only one balanced bracket sequence having the four good pairs. The content of $$$s$$$ is therefore the only string, which is "(()())()(())".
Then, the number of bracket sequences after the fifth and the sixth clue are both $$$1$$$ as you already know the content of $$$s$$$.
Now solve the problem and return the code.
|
2,700 | [
"combinatorics",
"data structures",
"dfs and similar",
"dsu",
"graphs",
"implementation",
"trees"
] | [
{
"input": "3\r\n3\r\n2 5\r\n1 6\r\n3 4\r\n4\r\n1 6\r\n7 8\r\n2 3\r\n4 5\r\n6\r\n2 3\r\n1 6\r\n7 8\r\n9 12\r\n10 11\r\n4 5\r\n",
"output": "5 1 1 1\r\n14 2 2 1 1\r\n132 42 5 2 1 1 1\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 512.0 MB
# Problem
This is the hard version of the problem. The difference between the versions is that in this version, the limits on $$$t$$$ and $$$n$$$ are bigger. You can hack only if you solved all versions of this problem.
Now Little John is rich, and so he finally buys a house big enough to fit himself and his favorite bracket sequence. But somehow, he ended up with a lot of brackets! Frustrated, he penetrates through the ceiling with the "buddha palm".
A bracket sequence is called balanced if it can be constructed by the following formal grammar.
1. The empty sequence $$$\varnothing$$$ is balanced.
2. If the bracket sequence $$$A$$$ is balanced, then $$$\mathtt{(}A\mathtt{)}$$$ is also balanced.
3. If the bracket sequences $$$A$$$ and $$$B$$$ are balanced, then the concatenated sequence $$$A B$$$ is also balanced.
For example, the sequences "(())()", "()", "(()(()))", and the empty sequence are balanced, while "(()" and "(()))(" are not.
Given a balanced bracket sequence $$$s$$$, a pair of indices $$$(i,j)$$$ ($$$i<j$$$) is called a good pair if $$$s_i$$$ is '(', $$$s_j$$$ is ')', and the two brackets are added simultaneously with respect to Rule 2 while constructing the sequence $$$s$$$. For example, the sequence "(())()" has three different good pairs, which are $$$(1,4)$$$, $$$(2,3)$$$, and $$$(5,6)$$$. One can show that any balanced bracket sequence of $$$2n$$$ brackets contains exactly $$$n$$$ different good pairs, and using any order of rules to construct the same bracket sequence will yield the same set of good pairs.
Emily will play a bracket guessing game with John. The game is played as follows.
Initially, John has a balanced bracket sequence $$$s$$$ containing $$$n$$$ different good pairs, which is not known to Emily. John tells Emily the value of $$$n$$$ and asks Emily to guess the sequence.
Throughout $$$n$$$ turns, John gives Emily the following kind of clue on each turn.
- $$$l\;r$$$: The sequence $$$s$$$ contains a good pair $$$(l,r)$$$.
The clues that John gives Emily are pairwise distinct and do not contradict each other.
At a certain point, Emily can be certain that the balanced bracket sequence satisfying the clues given so far is unique. For example, assume Emily knows that $$$s$$$ has $$$3$$$ good pairs, and it contains the good pair $$$(2,5)$$$. Out of $$$5$$$ balanced bracket sequences with $$$3$$$ good pairs, there exists only one such sequence "((()))" with the good pair $$$(2,5)$$$. Therefore, one can see that Emily does not always need $$$n$$$ turns to guess $$$s$$$.
To find out the content of $$$s$$$ as early as possible, Emily wants to know the number of different balanced bracket sequences that match the clues after each turn. Surely, this is not an easy job for Emily, especially when she is given so many good pairs. Now it is your turn to help Emily. Given the clues, you must find the answer before and after each turn. As the answers may be huge, you need to find them modulo $$$998\,244\,353$$$.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of good pairs.
Then, each of the $$$n$$$ following lines contains two integers $$$l_i$$$ and $$$r_i$$$ representing the $$$i$$$-th clue ($$$1 \le l_i < r_i \le 2n$$$).
The clues in one test case are pairwise distinct and do not contradict each other.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
## Output Format
For each test case, output $$$n+1$$$ integers on a separate line:
- The first integer is the answer before all clues, modulo $$$998\,244\,353$$$.
- For all $$$i \ge 1$$$, the $$$i+1$$$-th integer is the answer after the $$$i$$$-th clue, modulo $$$998\,244\,353$$$.
## Examples
```input
3
3
2 5
1 6
3 4
4
1 6
7 8
2 3
4 5
6
2 3
1 6
7 8
9 12
10 11
4 5
```
```output
5 1 1 1
14 2 2 1 1
132 42 5 2 1 1 1
```
## Note
The first test case of the example is explained in the problem description.
The third test case of the example is explained as follows. It can be shown that there are $$$132$$$ balanced bracket sequences with $$$6$$$ good pairs. The answers after each clue are given as follows:
1. You are given the good pair $$$(2,3)$$$. There are $$$42$$$ balanced bracket sequences having the good pair $$$(2,3)$$$.
2. You are given the good pair $$$(1,6)$$$. There are $$$5$$$ balanced bracket sequences having good pairs $$$(2,3)$$$, $$$(1,6)$$$.
3. You are given the good pair $$$(7,8)$$$. There are $$$2$$$ balanced bracket sequences having the three good pairs. The strings are "(()())()(())" and "(()())()()()", respectively.
4. You are given the good pair $$$(9,12)$$$. There is only one balanced bracket sequence having the four good pairs. The content of $$$s$$$ is therefore the only string, which is "(()())()(())".
Then, the number of bracket sequences after the fifth and the sixth clue are both $$$1$$$ as you already know the content of $$$s$$$.
Now solve the problem and return the code.
|
900 | [
"brute force",
"implementation",
"math"
] | [
{
"input": "6\r\n2 2 2\r\nNE\r\n3 2 2\r\nNNE\r\n6 2 1\r\nNNEESW\r\n6 10 10\r\nNNEESW\r\n3 4 2\r\nNEE\r\n4 5 5\r\nNEWS\r\n",
"output": "YES\r\nNO\r\nYES\r\nYES\r\nYES\r\nNO\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $$$(0, 0)$$$, and the Red Queen is at position $$$(a, b)$$$. Alice can only move in the four cardinal directions (north, east, south, west).
More formally, if Alice is at the point $$$(x, y)$$$, she will do one of the following:
- go north (represented by N), moving to $$$(x, y+1)$$$;
- go east (represented by E), moving to $$$(x+1, y)$$$;
- go south (represented by S), moving to $$$(x, y-1)$$$; or
- go west (represented by W), moving to $$$(x-1, y)$$$.
Alice's movements are predetermined. She has a string $$$s$$$ representing a sequence of moves that she performs from left to right. Once she reaches the end of the sequence, she repeats the same pattern of moves forever.
Can you help Alice figure out if she will ever meet the Red Queen?
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$1 \le n$$$, $$$a$$$, $$$b \le 10$$$) — the length of the string and the initial coordinates of the Red Queen.
The second line contains a string $$$s$$$ of length $$$n$$$ consisting only of the characters N, E, S, or W.
## Output Format
For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
## Examples
```input
6
2 2 2
NE
3 2 2
NNE
6 2 1
NNEESW
6 10 10
NNEESW
3 4 2
NEE
4 5 5
NEWS
```
```output
YES
NO
YES
YES
YES
NO
```
## Note
In the first test case, Alice follows the path $$$(0,0) \xrightarrow[\texttt{N}]{} (0,1) \xrightarrow[\texttt{E}]{} (1,1) \xrightarrow[\texttt{N}]{} (1,2) \xrightarrow[\texttt{E}]{} (2,2)$$$.
In the second test case, Alice can never reach the Red Queen.
Now solve the problem and return the code.
|
1,400 | [
"binary search",
"implementation",
"math"
] | [
{
"input": "7\r\n10 1 0\r\n1 2 3\r\n100 2 1\r\n3 0 1\r\n3 0 0\r\n1000000000000000000 0 0\r\n1000000000000000000 1000000000000000000 1000000000000000000\r\n",
"output": "0\r\n1\r\n50\r\n2\r\n-1\r\n-1\r\n1000000000000000000\r\n"
},
{
"input": "5\r\n1000000000000000000 0 1000000000000000000\r\n10000000... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
Alice mixed up the words transmutation and permutation! She has an array $$$a$$$ specified via three integers $$$n$$$, $$$b$$$, $$$c$$$: the array $$$a$$$ has length $$$n$$$ and is given via $$$a_i = b\cdot (i - 1) + c$$$ for $$$1\le i\le n$$$. For example, if $$$n=3$$$, $$$b=2$$$, and $$$c=1$$$, then $$$a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]$$$.
Now, Alice really enjoys permutations of $$$[0, \ldots, n-1]$$$$$$^{\text{∗}}$$$ and would like to transform $$$a$$$ into a permutation. In one operation, Alice replaces the maximum element of $$$a$$$ with the $$$\operatorname{MEX}$$$$$$^{\text{†}}$$$ of $$$a$$$. If there are multiple maximum elements in $$$a$$$, Alice chooses the leftmost one to replace.
Can you help Alice figure out how many operations she has to do for $$$a$$$ to become a permutation for the first time? If it is impossible, you should report it.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.
The only line of each test case contains three integers $$$n$$$, $$$b$$$, $$$c$$$ ($$$1\le n\le 10^{18}$$$; $$$0\le b$$$, $$$c\le 10^{18}$$$) — the parameters of the array.
## Output Format
For each test case, if the array can never become a permutation, output $$$-1$$$. Otherwise, output the minimum number of operations for the array to become a permutation.
## Examples
```input
7
10 1 0
1 2 3
100 2 1
3 0 1
3 0 0
1000000000000000000 0 0
1000000000000000000 1000000000000000000 1000000000000000000
```
```output
0
1
50
2
-1
-1
1000000000000000000
```
## Note
In the first test case, the array is already $$$[0, 1, \ldots, 9]$$$, so no operations are required.
In the third test case, the starting array is $$$[1, 3, 5, \ldots, 199]$$$. After the first operation, the $$$199$$$ gets transformed into a $$$0$$$. In the second operation, the $$$197$$$ gets transformed into a $$$2$$$. If we continue this, it will take exactly $$$50$$$ operations to get the array $$$[0, 1, 2, 3, \ldots, 99]$$$.
In the fourth test case, two operations are needed: $$$[1,1,1] \to [0,1,1] \to [0,2,1]$$$.
In the fifth test case, the process is $$$[0,0,0] \to [1,0,0] \to [2,0,0] \to [1,0,0] \to [2,0,0]$$$. This process repeats forever, so the array is never a permutation and the answer is $$$-1$$$.
Now solve the problem and return the code.
|
1,600 | [
"binary search",
"dp",
"greedy",
"two pointers"
] | [
{
"input": "7\r\n6 2 1\r\n1 1 10 1 1 10\r\n6 2 2\r\n1 1 10 1 1 10\r\n6 2 3\r\n1 1 10 1 1 10\r\n6 2 10\r\n1 1 10 1 1 10\r\n6 2 11\r\n1 1 10 1 1 10\r\n6 2 12\r\n1 1 10 1 1 10\r\n6 2 12\r\n1 1 1 1 10 10\r\n",
"output": "22\r\n12\r\n2\r\n2\r\n2\r\n0\r\n-1\r\n"
},
{
"input": "1\r\n10 4 75\r\n44 29 42 44 ... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Alice is at the Mad Hatter's tea party! There is a long sheet cake made up of $$$n$$$ sections with tastiness values $$$a_1, a_2, \ldots, a_n$$$. There are $$$m$$$ creatures at the tea party, excluding Alice.
Alice will cut the cake into $$$m + 1$$$ pieces. Formally, she will partition the cake into $$$m + 1$$$ subarrays, where each subarray consists of some number of adjacent sections. The tastiness of a piece is the sum of tastiness of its sections. Afterwards, she will divvy these $$$m + 1$$$ pieces up among the $$$m$$$ creatures and herself (her piece can be empty). However, each of the $$$m$$$ creatures will only be happy when the tastiness of its piece is $$$v$$$ or more.
Alice wants to make sure every creature is happy. Limited by this condition, she also wants to maximize the tastiness of her own piece. Can you help Alice find the maximum tastiness her piece can have? If there is no way to make sure every creature is happy, output $$$-1$$$.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n, m, v$$$ ($$$1\le m\le n\le 2\cdot 10^5$$$; $$$1\le v\le 10^9$$$) — the number of sections, the number of creatures, and the creatures' minimum requirement for tastiness, respectively.
The next line contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the tastinesses of the sections.
The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
## Output Format
For each test case, output the maximum tastiness Alice can achieve for her piece, or $$$-1$$$ if there is no way to make sure every creature is happy.
## Examples
```input
7
6 2 1
1 1 10 1 1 10
6 2 2
1 1 10 1 1 10
6 2 3
1 1 10 1 1 10
6 2 10
1 1 10 1 1 10
6 2 11
1 1 10 1 1 10
6 2 12
1 1 10 1 1 10
6 2 12
1 1 1 1 10 10
```
```output
22
12
2
2
2
0
-1
```
## Note
For the first test case, Alice can give the first and second section as their own pieces, and then take the remaining $$$10 + 1 + 1 + 10 = 22$$$ tastiness for herself. We can show that she cannot do any better.
For the second test case, Alice could give the first and second section as one piece, and the sixth section as one piece. She can then take the remaining $$$10 + 1 + 1 = 12$$$ tastiness for herself. We can show that she cannot do any better.
For the seventh test case, Alice cannot give each creature a piece of at least $$$12$$$ tastiness.
Now solve the problem and return the code.
|
2,000 | [
"constructive algorithms",
"data structures",
"dp",
"graphs",
"greedy",
"implementation",
"ternary search"
] | [
{
"input": "2\r\n3\r\n1 3 2\r\n2 1 3\r\n1 2 3\r\n4\r\n2 3 1 4\r\n1 2 3 4\r\n1 4 2 3\r\n",
"output": "YES\r\n2\r\nk 2\r\nq 3\r\nNO\r\n"
},
{
"input": "2\r\n2\r\n1 2\r\n1 2\r\n1 2\r\n2\r\n1 2\r\n1 2\r\n2 1\r\n",
"output": "NO\r\nYES\r\n1\r\nj 2\r\n"
},
{
"input": "1\r\n5\r\n3 2 4 1 5\r\n1 ... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are $$$n$$$ different types of cards in their card game. Alice currently has a card of type $$$1$$$ and needs a card of type $$$n$$$ to escape Wonderland. The other players have one of each kind of card.
In this card game, Alice can trade cards with the three other players. Each player has different preferences for the $$$n$$$ types of cards, which can be described by permutations$$$^{\text{∗}}$$$ $$$q$$$, $$$k$$$, and $$$j$$$ for the Queen, King, and Jack, respectively.
A player values card $$$a$$$ more than card $$$b$$$ if for their permutation $$$p$$$, $$$p_a > p_b$$$. Then, this player is willing to trade card $$$b$$$ to Alice in exchange for card $$$a$$$. Alice's preferences are straightforward: she values card $$$a$$$ more than card $$$b$$$ if $$$a > b$$$, and she will also only trade according to these preferences.
Determine if Alice can trade up from card $$$1$$$ to card $$$n$$$ subject to these preferences, and if it is possible, give a possible set of trades to do it.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$2\le n\le 2\cdot 10^5$$$) — the number of card types.
The next three lines contain the preferences of the Queen, King, and Jack respectively. Each of these lines contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1\le p_i\le n$$$) — a permutation corresponding to the player's preferences.
The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
## Output Format
For each test case, on the first line output a single string "YES" or "NO" (without the quotes) denoting whether Alice can trade up to card $$$n$$$.
If the first line was "YES", then on the next line output $$$k$$$ — the number of trades Alice will make. On the next $$$k$$$ lines output space separated a character $$$c\in \{\texttt{q}, \texttt{k}, \texttt{j}\}$$$ and integer $$$x$$$, denoting that Alice trades with player $$$c$$$ to get card $$$x$$$. It must be the case that on the $$$k$$$'th line, $$$x = n$$$. If there are multiple solutions, print any of them.
You can output this answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. The same goes for the character $$$c$$$ denoting the player in the trade ($$$\texttt{Q}, \texttt{K}, \texttt{J}$$$ will all be accepted alongside their lowercase variants).
## Examples
```input
2
3
1 3 2
2 1 3
1 2 3
4
2 3 1 4
1 2 3 4
1 4 2 3
```
```output
YES
2
k 2
q 3
NO
```
## Note
In the first testcase, Alice can trade with the King to get card $$$2$$$. She can then trade with the Queen to get card $$$3$$$.
In the second testcase, even though Alice can trade with the Queen to get card $$$3$$$, with the King to get card $$$2$$$, and then with the Jack to get card $$$4$$$, this is not a valid solution since it doesn't respect Alice's preferences. We can show that there is no way for Alice to get to card $$$4$$$.
Now solve the problem and return the code.
|
2,300 | [
"combinatorics",
"dfs and similar",
"dp",
"games",
"greedy",
"math",
"probabilities",
"trees"
] | [
{
"input": "2\r\n5\r\n1 2\r\n1 3\r\n2 4\r\n3 5\r\n9\r\n1 2\r\n2 3\r\n4 5\r\n5 6\r\n7 8\r\n8 9\r\n2 4\r\n5 7\r\n",
"output": "1 499122177 499122177 0 0 \r\n1 499122177 0 332748118 166374059 0 443664157 720954255 0 \r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 2.0 seconds
Memory limit: 256.0 MB
# Problem
Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$$$^{\text{∗}}$$$ which has an exit at vertex $$$1$$$, and Alice starts at some vertex $$$v$$$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.
Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$$$^{\text{†}}$$$ of the tree, Alice loses.
Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $$$1\le v\le n$$$. Since these probabilities can be very small, output them modulo $$$998\,244\,353$$$.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the exact answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2\le n\le 2\cdot 10^5$$$) — the number of vertices in the tree.
The $$$i$$$-th of the next $$$n - 1$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$ and $$$x_i \neq y_i$$$) — the edges of the tree. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
## Output Format
For each test case, output $$$n$$$ integers on one line — the probabilities of Alice escaping starting from vertex $$$1, 2, \ldots, n$$$. Since these probabilities can be very small, output them modulo $$$998\,244\,353$$$.
## Examples
```input
2
5
1 2
1 3
2 4
3 5
9
1 2
2 3
4 5
5 6
7 8
8 9
2 4
5 7
```
```output
1 499122177 499122177 0 0
1 499122177 0 332748118 166374059 0 443664157 720954255 0
```
## Note
For the first test case:
1. Alice escapes from the root (vertex $$$1$$$) by definition with probability $$$1$$$.
2. Alice immediately loses from vertices $$$4$$$ and $$$5$$$ since they are leaves.
3. From the other two vertices, Alice escapes with probability $$$\frac 12$$$ since the Queen will pull her to the leaves.
Now solve the problem and return the code.
|
2,700 | [
"bitmasks",
"brute force",
"dp",
"implementation"
] | [
{
"input": "6\r\n5 4\r\n2 1 1 1 2\r\n5 5\r\n2 1 1 1 2\r\n5 6\r\n2 1 1 1 2\r\n5 7\r\n2 1 1 1 2\r\n5 8\r\n2 1 1 1 2\r\n5 6\r\n2 0 2 2 3\r\n",
"output": "YES\r\nYES\r\nYES\r\nYES\r\nNO\r\nYES\r\n"
},
{
"input": "5\r\n1 1\r\n0\r\n1 1\r\n1\r\n1 1\r\n2\r\n1 1\r\n3\r\n1 1\r\n4\r\n",
"output": "NO\r\nYE... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 32.0 MB
# Problem
Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ and a target $$$m$$$, is there a way to insert $$$+$$$ and $$$\times$$$ into the circles of the expression $$$$$$a_1 \circ a_2 \circ \cdots \circ a_n = m$$$$$$ to make it true? We follow the usual order of operations: $$$\times$$$ is done before $$$+$$$.
Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!
## Input Format
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n, m$$$ ($$$1\le n\le 2\cdot 10^5$$$; $$$1\le m\le 10^4$$$) — the number of integers and the target, respectively.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\le a_i\le 10^4$$$) — the elements of the array $$$a$$$.
The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
## Output Format
For each test case, output "YES" without quotes if it is possible to get the target by inserting $$$+$$$ or $$$\times$$$ and "NO" otherwise.
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
## Examples
```input
6
5 4
2 1 1 1 2
5 5
2 1 1 1 2
5 6
2 1 1 1 2
5 7
2 1 1 1 2
5 8
2 1 1 1 2
5 6
2 0 2 2 3
```
```output
YES
YES
YES
YES
NO
YES
```
## Note
Possible solutions for the first four test cases are shown below. $$$$$$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\ 2 \times 1 + 1 + 1 + 2 &= 6 \\ 2 + 1 + 1 + 1 + 2 &= 7 \\ \end{align*}$$$$$$ It is impossible to get a result of $$$8$$$ in the fifth test case.
Now solve the problem and return the code.
|
800 | [
"brute force",
"dp",
"greedy"
] | [
{
"input": "4\r\n3\r\n5 4 5\r\n3\r\n4 5 4\r\n10\r\n3 3 3 3 4 1 2 3 4 5\r\n9\r\n17 89 92 42 29 92 14 70 45\r\n",
"output": "7\r\n6\r\n10\r\n97\r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a_1, a_2, \ldots, a_n$$$ of positive integers.
You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $$$1 \leq i \leq n-1$$$, at least one of $$$a_i$$$ and $$$a_{i+1}$$$ must not be red).
Your score is the maximum value of a red element plus the number of red elements. Find the maximum score you can get.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — the given array.
## Output Format
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
## Examples
```input
4
3
5 4 5
3
4 5 4
10
3 3 3 3 4 1 2 3 4 5
9
17 89 92 42 29 92 14 70 45
```
```output
7
6
10
97
```
## Note
In the first test case, you can color the array as follows: $$$[\color{red}{5}, 4, \color{red}{5}]$$$. Your score is $$$\max([5, 5]) + \text{size}([5, 5]) = 5+2 = 7$$$. This is the maximum score you can get.
In the second test case, you can color the array as follows: $$$[\color{red}{4}, 5, \color{red}{4}]$$$. Your score is $$$\max([4, 4]) + \text{size}([4, 4]) = 4+2 = 6$$$. This is the maximum score you can get.
In the third test case, you can color the array as follows: $$$[\color{red}{3}, 3, \color{red}{3}, 3, \color{red}{4}, 1, 2, \color{red}{3}, 4, \color{red}{5}]$$$. Your score is $$$\max([3, 3, 4, 3, 5]) + \text{size}([3, 3, 4, 3, 5]) = 5+5 = 10$$$. This is the maximum score you can get.
Now solve the problem and return the code.
|
1,200 | [
"implementation",
"math"
] | [
{
"input": "3\r\n2 2\r\n101 200\r\n2 1\r\n6 15\r\n1 2 3 5 6 7\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\r\n5 8\r\n254618033 265675151 461318786 557391198 848083778\r\n6 9 15 10 6 9 4 4294967300\r\n",
"output": "0 100 \r\n0 0 0 0 2 0 0 0 3 0 2 0 0 0 0 \r\n291716045 0 0 0 291716045 0 301749698 0 \r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.5 seconds
Memory limit: 256.0 MB
# Problem
You are given $$$n$$$ points on the $$$x$$$ axis, at increasing positive integer coordinates $$$x_1 < x_2 < \ldots < x_n$$$.
For each pair $$$(i, j)$$$ with $$$1 \leq i < j \leq n$$$, you draw the segment $$$[x_i, x_j]$$$. The segments are closed, i.e., a segment $$$[a, b]$$$ contains the points $$$a, a+1, \ldots, b$$$.
You are given $$$q$$$ queries. In the $$$i$$$-th query, you are given a positive integer $$$k_i$$$, and you have to determine how many points with integer coordinates are contained in exactly $$$k_i$$$ segments.
## Input Format
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$, $$$q$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le q \le 10^5$$$) — the number of points and the number of queries.
The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^9$$$) — the coordinates of the $$$n$$$ points.
The third line of each test case contains $$$q$$$ integers $$$k_1, k_2, \ldots, k_q$$$ ($$$1 \leq k_i \leq 10^{18}$$$) — the parameters of the $$$q$$$ queries.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$.
## Output Format
For each test case, output a single line with $$$q$$$ integers: the $$$i$$$-th integer is the answer to the $$$i$$$-th query.
## Examples
```input
3
2 2
101 200
2 1
6 15
1 2 3 5 6 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5 8
254618033 265675151 461318786 557391198 848083778
6 9 15 10 6 9 4 4294967300
```
```output
0 100
0 0 0 0 2 0 0 0 3 0 2 0 0 0 0
291716045 0 0 0 291716045 0 301749698 0
```
## Note
In the first example, you only draw the segment $$$[101, 200]$$$. No point is contained in exactly $$$2$$$ segments, and the $$$100$$$ points $$$101, 102, \ldots, 200$$$ are contained in exactly $$$1$$$ segment.
In the second example, you draw $$$15$$$ segments: $$$[1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [2, 3], [2, 5], [2, 6], [2, 7], [3, 5], [3, 6], [3, 7], [5, 6], [5, 7], [6, 7]$$$. Points $$$1, 7$$$ are contained in exactly $$$5$$$ segments; points $$$2, 4, 6$$$ are contained in exactly $$$9$$$ segments; points $$$3, 5$$$ are contained in exactly $$$11$$$ segments.
Now solve the problem and return the code.
|
800 | [
"constructive algorithms",
"implementation",
"math"
] | [
{
"input": "4\r\n10 10 1\r\n0 0 3\r\n-5 -8 8\r\n4 -5 3\r\n",
"output": "10 10\r\n1 0\r\n-1 0\r\n0 0\r\n-4 -8\r\n-6 -8\r\n-3 -8\r\n-7 -8\r\n-2 -8\r\n-8 -8\r\n-1 -8\r\n-9 -8\r\n5 -5\r\n3 -5\r\n4 -5\r\n"
},
{
"input": "30\r\n-84 -60 1\r\n-41 -100 2\r\n8 -8 3\r\n-52 -62 4\r\n-61 -76 5\r\n-52 -52 6\r\n14... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.0 seconds
Memory limit: 256.0 MB
# Problem
You are given three integers $$$x_c$$$, $$$y_c$$$, and $$$k$$$ ($$$-100 \leq x_c, y_c \leq 100$$$, $$$1 \leq k \leq 1000$$$).
You need to find $$$k$$$ distinct points ($$$x_1, y_1$$$), ($$$x_2, y_2$$$), $$$\ldots$$$, ($$$x_k, y_k$$$), having integer coordinates, on the 2D coordinate plane such that:
- their center$$$^{\text{∗}}$$$ is ($$$x_c, y_c$$$)
- $$$-10^9 \leq x_i, y_i \leq 10^9$$$ for all $$$i$$$ from $$$1$$$ to $$$k$$$
It can be proven that at least one set of $$$k$$$ distinct points always exists that satisfies these conditions.
## Input Format
The first line contains $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
Each test case contains three integers $$$x_c$$$, $$$y_c$$$, and $$$k$$$ ($$$-100 \leq x_c, y_c \leq 100$$$, $$$1 \leq k \leq 1000$$$) — the coordinates of the center and the number of distinct points you must output.
It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.
## Output Format
For each test case, output $$$k$$$ lines, the $$$i$$$-th line containing two space separated integers, $$$x_i$$$ and $$$y_i$$$, ($$$-10^9 \leq x_i, y_i \leq 10^9$$$) — denoting the position of the $$$i$$$-th point.
If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constraints.
## Examples
```input
4
10 10 1
0 0 3
-5 -8 8
4 -5 3
```
```output
10 10
-1 -1
5 -1
-4 2
-6 -7
-5 -7
-4 -7
-4 -8
-4 -9
-5 -9
-6 -9
-6 -8
1000 -1000
-996 995
8 -10
```
## Note
For the first test case, $$$\left( \frac{10}{1}, \frac{10}{1} \right) = (10, 10)$$$.
For the second test case, $$$\left( \frac{-1 + 5 - 4}{3}, \frac{-1 -1 + 2}{3} \right) = (0, 0)$$$.
Now solve the problem and return the code.
|
1,000 | [
"constructive algorithms",
"math",
"number theory"
] | [
{
"input": "3\r\n2\r\n1 2\r\n5\r\n1 2 3 4 5\r\n7\r\n4 7 5 1 2 6 3\r\n",
"output": "2 1 \r\n2 3 4 5 1 \r\n7 5 1 2 6 3 4 \r\n"
}
] | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 1.5 seconds
Memory limit: 256.0 MB
# Problem
You are given a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$.
Find a permutation $$$q$$$ of length $$$n$$$ that minimizes the number of pairs ($$$i, j$$$) ($$$1 \leq i \leq j \leq n$$$) such that $$$p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$$$.
## Input Format
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$).
The following line contains $$$n$$$ space-separated integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$) — denoting the permutation $$$p$$$ of length $$$n$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
## Output Format
For each test case, output one line containing any permutation of length $$$n$$$ (the permutation $$$q$$$) such that $$$q$$$ minimizes the number of pairs.
## Examples
```input
3
2
1 2
5
1 2 3 4 5
7
4 7 5 1 2 6 3
```
```output
2 1
3 5 4 2 1
6 2 1 4 7 3 5
```
## Note
For the first test, there exists only one pair ($$$i, j$$$) ($$$1 \leq i \leq j \leq n$$$) such that $$$p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$$$, which is ($$$1, 2$$$). It can be proven that no such $$$q$$$ exists for which there are no pairs.
Now solve the problem and return the code.
|
1,900 | [
"binary search",
"brute force",
"constructive algorithms",
"greedy",
"implementation"
] | [
{
"input": "8\r\n2 10\r\n3 3\r\n1 1\r\n3 10\r\n3 3 3\r\n0 0 0\r\n4 4\r\n2 1 5 1\r\n0 1 0 1\r\n5 4\r\n7 5 2 5 4\r\n0 0 1 0 1\r\n5 1\r\n5 15 15 2 11\r\n1 0 0 1 1\r\n5 2\r\n10 11 4 10 15\r\n1 1 0 1 0\r\n4 4\r\n1 1 2 5\r\n1 1 0 0\r\n2 1000000000\r\n1000000000 1000000000\r\n1 1\r\n",
"output": "16\r\n6\r\n8\r\n1... | 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 python3.
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:
```python
<your code here>
```
Execution time limit: 3.0 seconds
Memory limit: 256.0 MB
# Problem
You are given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$. You are also given a binary array $$$b$$$ of length $$$n$$$.
You can perform the following operation at most $$$k$$$ times:
- Select an index $$$i$$$ ($$$1 \leq i \leq n$$$) such that $$$b_i = 1$$$. Set $$$a_i = a_i + 1$$$ (i.e., increase $$$a_i$$$ by $$$1$$$).
Your score is defined to be $$$\max\limits_{i = 1}^{n} \left( a_i + \operatorname{median}(c_i) \right)$$$, where $$$c_i$$$ denotes the array of length $$$n-1$$$ that you get by deleting $$$a_i$$$ from $$$a$$$. In other words, your score is the maximum value of $$$a_i + \operatorname{median}(c_i)$$$ over all $$$i$$$ from $$$1$$$ to $$$n$$$.
Find the maximum score that you can achieve if you perform the operations optimally.
For an arbitrary array $$$p$$$, $$$\operatorname{median}(p)$$$ is defined as the $$$\left\lfloor \frac{|p|+1}{2} \right\rfloor$$$-th smallest element of $$$p$$$. For example, $$$\operatorname{median} \left( [3,2,1,3] \right) = 2$$$ and $$$\operatorname{median} \left( [6,2,4,5,1] \right) = 4$$$.
## Input Format
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
Each test case begins with two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$, $$$0 \leq k \leq 10^9$$$) — the length of the $$$a$$$ and the number of operations you can perform.
The following line contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — denoting the array $$$a$$$.
The following line contains $$$n$$$ space separated integers $$$b_1, b_2, \ldots, b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$) — denoting the array $$$b$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
## Output Format
For each test case, output the maximum value of score you can get on a new line.
## Examples
```input
8
2 10
3 3
1 1
3 10
3 3 3
0 0 0
4 4
2 1 5 1
0 1 0 1
5 4
7 5 2 5 4
0 0 1 0 1
5 1
5 15 15 2 11
1 0 0 1 1
5 2
10 11 4 10 15
1 1 0 1 0
4 4
1 1 2 5
1 1 0 0
2 1000000000
1000000000 1000000000
1 1
```
```output
16
6
8
13
21
26
8
3000000000
```
## Note
For the first test case, it is optimal to perform $$$5$$$ operations on both elements so $$$a = [8,8]$$$. So, the maximum score we can achieve is $$$\max(8 + \operatorname{median}[8], 8 + \operatorname{median}[8]) = 16$$$, as $$$c_1 = [a_2] = [8]$$$. It can be proven that you cannot get a better score.
For the second test case, you are not able to perform operations on any elements, so $$$a$$$ remains $$$[3,3,3]$$$. So, the maximum score we can achieve is $$$3 + \operatorname{median}[3, 3] = 6$$$, as $$$c_1 = [a_2, a_3] = [3, 3]$$$. It can be proven that you cannot get a better score.
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
- 10