Dataset Viewer
Auto-converted to Parquet Duplicate
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\n18 19\r\n19 20\r\n19 21\r\n19 22\r\n", "output": "9\r\n" }, { "input": "1\r\n16\r\n1 2\r\n1 3\r\n1 4\r\n2 5\r\n2 6\r\n5 7\r\n6 8\r\n3 9\r\n3 10\r\n9 11\r\n10 12\r\n4 13\r\n4 14\r\n13 15\r\n14 16\r\n", "output": "5\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 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 200\r\n2\r\n1000 200\r\n4\r\n99 198 260 283\r\n2\r\n2000000000 2027422256\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 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\n1000000000000000000 0 999999999999999999\r\n1000000000000000000 0 999999999999999998\r\n1000000000000000000 0 999999999999999997\r\n1000000000000000000 0 120931\r\n", "output": "1000000000000000000\r\n999999999999999999\r\n999999999999999999\r\n-1\r\n-1\r\n" }, { "input": "1\r\n100000000000000 1 1\r\n", "output": "1\r\n" }, { "input": "1\r\n100000000000 1 1\r\n", "output": "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: 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 83 11 38 95 71 96\r\n", "output": "73\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 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 4 2 3 5\r\n1 2 5 3 4\r\n", "output": "YES\r\n3\r\nq 2\r\nk 3\r\nj 5\r\n" }, { "input": "1\r\n6\r\n1 2 4 5 3 6\r\n3 2 5 1 6 4\r\n1 2 3 4 5 6\r\n", "output": "YES\r\n3\r\nk 4\r\nq 5\r\nk 6\r\n" }, { "input": "1\r\n8\r\n1 3 2 5 4 7 6 8\r\n2 1 4 3 6 5 8 7\r\n1 2 3 4 5 6 7 8\r\n", "output": "YES\r\n7\r\nk 2\r\nq 3\r\nk 4\r\nq 5\r\nk 6\r\nq 7\r\nk 8\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 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\nYES\r\nNO\r\nNO\r\nNO\r\n" }, { "input": "1\r\n100 10000\r\n100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 100\r\n", "output": "YES\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: 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 -11 7\r\n-2 -54 8\r\n46 8 9\r\n100 100 10\r\n-35 34 11\r\n87 5 12\r\n42 -44 13\r\n66 -32 14\r\n71 -31 15\r\n9 -51 16\r\n90 -62 17\r\n86 -52 18\r\n7 88 19\r\n25 -14 20\r\n-7 37 21\r\n-55 98 22\r\n64 51 23\r\n34 -85 24\r\n8 -18 25\r\n34 64 26\r\n93 -37 27\r\n16 80 28\r\n-70 -28 29\r\n63 31 30\r\n", "output": "-84 -60\n-40 -99\n-42 -101\n9 -7\n7 -9\n8 -8\n-51 -61\n-50 -60\n-53 -63\n-54 -64\n-60 -75\n-59 -74\n-62 -77\n-63 -78\n-61 -76\n-51 -51\n-50 -50\n-49 -49\n-53 -53\n-54 -54\n-55 -55\n15 -10\n16 -9\n17 -8\n13 -12\n12 -13\n11 -14\n14 -11\n-1 -53\n0 -52\n1 -51\n2 -50\n-3 -55\n-4 -56\n-5 -57\n-6 -58\n47 9\n48 10\n49 11\n50 12\n45 7\n44 6\n43 5\n42 4\n46 8\n101 101\n102 102\n103 103\n104 104\n105 105\n99 99\n98 98\n97 97\n96 96\n95 95\n-34 35\n-33 36\n-32 37\n-31 38\n-30 39\n-36 33\n-37 32\n-38 31\n-39 30\n-40 29\n-35 34\n88 6\n89 7\n90 8\n91 9\n92 10\n93 11\n86 4\n85 3\n84 2\n83 1\n82 0\n81 -1\n43 -43\n44 -42\n45 -41\n46 -40\n47 -39\n48 -38\n41 -45\n40 -46\n39 -47\n38 -48\n37 -49\n36 -50\n42 -44\n67 -31\n68 -30\n69 -29\n70 -28\n71 -27\n72 -26\n73 -25\n65 -33\n64 -34\n63 -35\n62 -36\n61 -37\n60 -38\n59 -39\n72 -30\n73 -29\n74 -28\n75 -27\n76 -26\n77 -25\n78 -24\n70 -32\n69 -33\n68 -34\n67 -35\n66 -36\n65 -37\n64 -38\n71 -31\n10 -50\n11 -49\n12 -48\n13 -47\n14 -46\n15 -45\n16 -44\n17 -43\n8 -52\n7 -53\n6 -54\n5 -55\n4 -56\n3 -57\n2 -58\n1 -59\n91 -61\n92 -60\n93 -59\n94 -58\n95 -57\n96 -56\n97 -55\n98 -54\n89 -63\n88 -64\n87 -65\n86 -66\n85 -67\n84 -68\n83 -69\n82 -70\n90 -62\n87 -51\n88 -50\n89 -49\n90 -48\n91 -47\n92 -46\n93 -45\n94 -44\n95 -43\n85 -53\n84 -54\n83 -55\n82 -56\n81 -57\n80 -58\n79 -59\n78 -60\n77 -61\n8 89\n9 90\n10 91\n11 92\n12 93\n13 94\n14 95\n15 96\n16 97\n6 87\n5 86\n4 85\n3 84\n2 83\n1 82\n0 81\n-1 80\n-2 79\n7 88\n26 -13\n27 -12\n28 -11\n29 -10\n30 -9\n31 -8\n32 -7\n33 -6\n34 -5\n35 -4\n24 -15\n23 -16\n22 -17\n21 -18\n20 -19\n19 -20\n18 -21\n17 -22\n16 -23\n15 -24\n-6 38\n-5 39\n-4 40\n-3 41\n-2 42\n-1 43\n0 44\n1 45\n2 46\n3 47\n-8 36\n-9 35\n-10 34\n-11 33\n-12 32\n-13 31\n-14 30\n-15 29\n-16 28\n-17 27\n-7 37\n-54 99\n-53 100\n-52 101\n-51 102\n-50 103\n-49 104\n-48 105\n-47 106\n-46 107\n-45 108\n-44 109\n-56 97\n-57 96\n-58 95\n-59 94\n-60 93\n-61 92\n-62 91\n-63 90\n-64 89\n-65 88\n-66 87\n65 52\n66 53\n67 54\n68 55\n69 56\n70 57\n71 58\n72 59\n73 60\n74 61\n75 62\n63 50\n62 49\n61 48\n60 47\n59 46\n58 45\n57 44\n56 43\n55 42\n54 41\n53 40\n64 51\n35 -84\n36 -83\n37 -82\n38 -81\n39 -80\n40 -79\n41 -78\n42 -77\n43 -76\n44 -75\n45 -74\n46 -73\n33 -86\n32 -87\n31 -88\n30 -89\n29 -90\n28 -91\n27 -92\n26 -93\n25 -94\n24 -95\n23 -96\n22 -97\n9 -17\n10 -16\n11 -15\n12 -14\n13 -13\n14 -12\n15 -11\n16 -10\n17 -9\n18 -8\n19 -7\n20 -6\n7 -19\n6 -20\n5 -21\n4 -22\n3 -23\n2 -24\n1 -25\n0 -26\n-1 -27\n-2 -28\n-3 -29\n-4 -30\n8 -18\n35 65\n36 66\n37 67\n38 68\n39 69\n40 70\n41 71\n42 72\n43 73\n44 74\n45 75\n46 76\n47 77\n33 63\n32 62\n31 61\n30 60\n29 59\n28 58\n27 57\n26 56\n25 55\n24 54\n23 53\n22 52\n21 51\n94 -36\n95 -35\n96 -34\n97 -33\n98 -32\n99 -31\n100 -30\n101 -29\n102 -28\n103 -27\n104 -26\n105 -25\n106 -24\n92 -38\n91 -39\n90 -40\n89 -41\n88 -42\n87 -43\n86 -44\n85 -45\n84 -46\n83 -47\n82 -48\n81 -49\n80 -50\n93 -37\n17 81\n18 82\n19 83\n20 84\n21 85\n22 86\n23 87\n24 88\n25 89\n26 90\n27 91\n28 92\n29 93\n30 94\n15 79\n14 78\n13 77\n12 76\n11 75\n10 74\n9 73\n8 72\n7 71\n6 70\n5 69\n4 68\n3 67\n2 66\n-69 -27\n-68 -26\n-67 -25\n-66 -24\n-65 -23\n-64 -22\n-63 -21\n-62 -20\n-61 -19\n-60 -18\n-59 -17\n-58 -16\n-57 -15\n-56 -14\n-71 -29\n-72 -30\n-73 -31\n-74 -32\n-75 -33\n-76 -34\n-77 -35\n-78 -36\n-79 -37\n-80 -38\n-81 -39\n-82 -40\n-83 -41\n-84 -42\n-70 -28\n64 32\n65 33\n66 34\n67 35\n68 36\n69 37\n70 38\n71 39\n72 40\n73 41\n74 42\n75 43\n76 44\n77 45\n78 46\n62 30\n61 29\n60 28\n59 27\n58 26\n57 25\n56 24\n55 23\n54 22\n53 21\n52 20\n51 19\n50 18\n49 17\n48 16\n" }, { "input": "1\r\n-100 -100 1000\r\n", "output": "-99 -99\n-98 -98\n-97 -97\n-96 -96\n-95 -95\n-94 -94\n-93 -93\n-92 -92\n-91 -91\n-90 -90\n-89 -89\n-88 -88\n-87 -87\n-86 -86\n-85 -85\n-84 -84\n-83 -83\n-82 -82\n-81 -81\n-80 -80\n-79 -79\n-78 -78\n-77 -77\n-76 -76\n-75 -75\n-74 -74\n-73 -73\n-72 -72\n-71 -71\n-70 -70\n-69 -69\n-68 -68\n-67 -67\n-66 -66\n-65 -65\n-64 -64\n-63 -63\n-62 -62\n-61 -61\n-60 -60\n-59 -59\n-58 -58\n-57 -57\n-56 -56\n-55 -55\n-54 -54\n-53 -53\n-52 -52\n-51 -51\n-50 -50\n-49 -49\n-48 -48\n-47 -47\n-46 -46\n-45 -45\n-44 -44\n-43 -43\n-42 -42\n-41 -41\n-40 -40\n-39 -39\n-38 -38\n-37 -37\n-36 -36\n-35 -35\n-34 -34\n-33 -33\n-32 -32\n-31 -31\n-30 -30\n-29 -29\n-28 -28\n-27 -27\n-26 -26\n-25 -25\n-24 -24\n-23 -23\n-22 -22\n-21 -21\n-20 -20\n-19 -19\n-18 -18\n-17 -17\n-16 -16\n-15 -15\n-14 -14\n-13 -13\n-12 -12\n-11 -11\n-10 -10\n-9 -9\n-8 -8\n-7 -7\n-6 -6\n-5 -5\n-4 -4\n-3 -3\n-2 -2\n-1 -1\n0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n11 11\n12 12\n13 13\n14 14\n15 15\n16 16\n17 17\n18 18\n19 19\n20 20\n21 21\n22 22\n23 23\n24 24\n25 25\n26 26\n27 27\n28 28\n29 29\n30 30\n31 31\n32 32\n33 33\n34 34\n35 35\n36 36\n37 37\n38 38\n39 39\n40 40\n41 41\n42 42\n43 43\n44 44\n45 45\n46 46\n47 47\n48 48\n49 49\n50 50\n51 51\n52 52\n53 53\n54 54\n55 55\n56 56\n57 57\n58 58\n59 59\n60 60\n61 61\n62 62\n63 63\n64 64\n65 65\n66 66\n67 67\n68 68\n69 69\n70 70\n71 71\n72 72\n73 73\n74 74\n75 75\n76 76\n77 77\n78 78\n79 79\n80 80\n81 81\n82 82\n83 83\n84 84\n85 85\n86 86\n87 87\n88 88\n89 89\n90 90\n91 91\n92 92\n93 93\n94 94\n95 95\n96 96\n97 97\n98 98\n99 99\n100 100\n101 101\n102 102\n103 103\n104 104\n105 105\n106 106\n107 107\n108 108\n109 109\n110 110\n111 111\n112 112\n113 113\n114 114\n115 115\n116 116\n117 117\n118 118\n119 119\n120 120\n121 121\n122 122\n123 123\n124 124\n125 125\n126 126\n127 127\n128 128\n129 129\n130 130\n131 131\n132 132\n133 133\n134 134\n135 135\n136 136\n137 137\n138 138\n139 139\n140 140\n141 141\n142 142\n143 143\n144 144\n145 145\n146 146\n147 147\n148 148\n149 149\n150 150\n151 151\n152 152\n153 153\n154 154\n155 155\n156 156\n157 157\n158 158\n159 159\n160 160\n161 161\n162 162\n163 163\n164 164\n165 165\n166 166\n167 167\n168 168\n169 169\n170 170\n171 171\n172 172\n173 173\n174 174\n175 175\n176 176\n177 177\n178 178\n179 179\n180 180\n181 181\n182 182\n183 183\n184 184\n185 185\n186 186\n187 187\n188 188\n189 189\n190 190\n191 191\n192 192\n193 193\n194 194\n195 195\n196 196\n197 197\n198 198\n199 199\n200 200\n201 201\n202 202\n203 203\n204 204\n205 205\n206 206\n207 207\n208 208\n209 209\n210 210\n211 211\n212 212\n213 213\n214 214\n215 215\n216 216\n217 217\n218 218\n219 219\n220 220\n221 221\n222 222\n223 223\n224 224\n225 225\n226 226\n227 227\n228 228\n229 229\n230 230\n231 231\n232 232\n233 233\n234 234\n235 235\n236 236\n237 237\n238 238\n239 239\n240 240\n241 241\n242 242\n243 243\n244 244\n245 245\n246 246\n247 247\n248 248\n249 249\n250 250\n251 251\n252 252\n253 253\n254 254\n255 255\n256 256\n257 257\n258 258\n259 259\n260 260\n261 261\n262 262\n263 263\n264 264\n265 265\n266 266\n267 267\n268 268\n269 269\n270 270\n271 271\n272 272\n273 273\n274 274\n275 275\n276 276\n277 277\n278 278\n279 279\n280 280\n281 281\n282 282\n283 283\n284 284\n285 285\n286 286\n287 287\n288 288\n289 289\n290 290\n291 291\n292 292\n293 293\n294 294\n295 295\n296 296\n297 297\n298 298\n299 299\n300 300\n301 301\n302 302\n303 303\n304 304\n305 305\n306 306\n307 307\n308 308\n309 309\n310 310\n311 311\n312 312\n313 313\n314 314\n315 315\n316 316\n317 317\n318 318\n319 319\n320 320\n321 321\n322 322\n323 323\n324 324\n325 325\n326 326\n327 327\n328 328\n329 329\n330 330\n331 331\n332 332\n333 333\n334 334\n335 335\n336 336\n337 337\n338 338\n339 339\n340 340\n341 341\n342 342\n343 343\n344 344\n345 345\n346 346\n347 347\n348 348\n349 349\n350 350\n351 351\n352 352\n353 353\n354 354\n355 355\n356 356\n357 357\n358 358\n359 359\n360 360\n361 361\n362 362\n363 363\n364 364\n365 365\n366 366\n367 367\n368 368\n369 369\n370 370\n371 371\n372 372\n373 373\n374 374\n375 375\n376 376\n377 377\n378 378\n379 379\n380 380\n381 381\n382 382\n383 383\n384 384\n385 385\n386 386\n387 387\n388 388\n389 389\n390 390\n391 391\n392 392\n393 393\n394 394\n395 395\n396 396\n397 397\n398 398\n399 399\n400 400\n-101 -101\n-102 -102\n-103 -103\n-104 -104\n-105 -105\n-106 -106\n-107 -107\n-108 -108\n-109 -109\n-110 -110\n-111 -111\n-112 -112\n-113 -113\n-114 -114\n-115 -115\n-116 -116\n-117 -117\n-118 -118\n-119 -119\n-120 -120\n-121 -121\n-122 -122\n-123 -123\n-124 -124\n-125 -125\n-126 -126\n-127 -127\n-128 -128\n-129 -129\n-130 -130\n-131 -131\n-132 -132\n-133 -133\n-134 -134\n-135 -135\n-136 -136\n-137 -137\n-138 -138\n-139 -139\n-140 -140\n-141 -141\n-142 -142\n-143 -143\n-144 -144\n-145 -145\n-146 -146\n-147 -147\n-148 -148\n-149 -149\n-150 -150\n-151 -151\n-152 -152\n-153 -153\n-154 -154\n-155 -155\n-156 -156\n-157 -157\n-158 -158\n-159 -159\n-160 -160\n-161 -161\n-162 -162\n-163 -163\n-164 -164\n-165 -165\n-166 -166\n-167 -167\n-168 -168\n-169 -169\n-170 -170\n-171 -171\n-172 -172\n-173 -173\n-174 -174\n-175 -175\n-176 -176\n-177 -177\n-178 -178\n-179 -179\n-180 -180\n-181 -181\n-182 -182\n-183 -183\n-184 -184\n-185 -185\n-186 -186\n-187 -187\n-188 -188\n-189 -189\n-190 -190\n-191 -191\n-192 -192\n-193 -193\n-194 -194\n-195 -195\n-196 -196\n-197 -197\n-198 -198\n-199 -199\n-200 -200\n-201 -201\n-202 -202\n-203 -203\n-204 -204\n-205 -205\n-206 -206\n-207 -207\n-208 -208\n-209 -209\n-210 -210\n-211 -211\n-212 -212\n-213 -213\n-214 -214\n-215 -215\n-216 -216\n-217 -217\n-218 -218\n-219 -219\n-220 -220\n-221 -221\n-222 -222\n-223 -223\n-224 -224\n-225 -225\n-226 -226\n-227 -227\n-228 -228\n-229 -229\n-230 -230\n-231 -231\n-232 -232\n-233 -233\n-234 -234\n-235 -235\n-236 -236\n-237 -237\n-238 -238\n-239 -239\n-240 -240\n-241 -241\n-242 -242\n-243 -243\n-244 -244\n-245 -245\n-246 -246\n-247 -247\n-248 -248\n-249 -249\n-250 -250\n-251 -251\n-252 -252\n-253 -253\n-254 -254\n-255 -255\n-256 -256\n-257 -257\n-258 -258\n-259 -259\n-260 -260\n-261 -261\n-262 -262\n-263 -263\n-264 -264\n-265 -265\n-266 -266\n-267 -267\n-268 -268\n-269 -269\n-270 -270\n-271 -271\n-272 -272\n-273 -273\n-274 -274\n-275 -275\n-276 -276\n-277 -277\n-278 -278\n-279 -279\n-280 -280\n-281 -281\n-282 -282\n-283 -283\n-284 -284\n-285 -285\n-286 -286\n-287 -287\n-288 -288\n-289 -289\n-290 -290\n-291 -291\n-292 -292\n-293 -293\n-294 -294\n-295 -295\n-296 -296\n-297 -297\n-298 -298\n-299 -299\n-300 -300\n-301 -301\n-302 -302\n-303 -303\n-304 -304\n-305 -305\n-306 -306\n-307 -307\n-308 -308\n-309 -309\n-310 -310\n-311 -311\n-312 -312\n-313 -313\n-314 -314\n-315 -315\n-316 -316\n-317 -317\n-318 -318\n-319 -319\n-320 -320\n-321 -321\n-322 -322\n-323 -323\n-324 -324\n-325 -325\n-326 -326\n-327 -327\n-328 -328\n-329 -329\n-330 -330\n-331 -331\n-332 -332\n-333 -333\n-334 -334\n-335 -335\n-336 -336\n-337 -337\n-338 -338\n-339 -339\n-340 -340\n-341 -341\n-342 -342\n-343 -343\n-344 -344\n-345 -345\n-346 -346\n-347 -347\n-348 -348\n-349 -349\n-350 -350\n-351 -351\n-352 -352\n-353 -353\n-354 -354\n-355 -355\n-356 -356\n-357 -357\n-358 -358\n-359 -359\n-360 -360\n-361 -361\n-362 -362\n-363 -363\n-364 -364\n-365 -365\n-366 -366\n-367 -367\n-368 -368\n-369 -369\n-370 -370\n-371 -371\n-372 -372\n-373 -373\n-374 -374\n-375 -375\n-376 -376\n-377 -377\n-378 -378\n-379 -379\n-380 -380\n-381 -381\n-382 -382\n-383 -383\n-384 -384\n-385 -385\n-386 -386\n-387 -387\n-388 -388\n-389 -389\n-390 -390\n-391 -391\n-392 -392\n-393 -393\n-394 -394\n-395 -395\n-396 -396\n-397 -397\n-398 -398\n-399 -399\n-400 -400\n-401 -401\n-402 -402\n-403 -403\n-404 -404\n-405 -405\n-406 -406\n-407 -407\n-408 -408\n-409 -409\n-410 -410\n-411 -411\n-412 -412\n-413 -413\n-414 -414\n-415 -415\n-416 -416\n-417 -417\n-418 -418\n-419 -419\n-420 -420\n-421 -421\n-422 -422\n-423 -423\n-424 -424\n-425 -425\n-426 -426\n-427 -427\n-428 -428\n-429 -429\n-430 -430\n-431 -431\n-432 -432\n-433 -433\n-434 -434\n-435 -435\n-436 -436\n-437 -437\n-438 -438\n-439 -439\n-440 -440\n-441 -441\n-442 -442\n-443 -443\n-444 -444\n-445 -445\n-446 -446\n-447 -447\n-448 -448\n-449 -449\n-450 -450\n-451 -451\n-452 -452\n-453 -453\n-454 -454\n-455 -455\n-456 -456\n-457 -457\n-458 -458\n-459 -459\n-460 -460\n-461 -461\n-462 -462\n-463 -463\n-464 -464\n-465 -465\n-466 -466\n-467 -467\n-468 -468\n-469 -469\n-470 -470\n-471 -471\n-472 -472\n-473 -473\n-474 -474\n-475 -475\n-476 -476\n-477 -477\n-478 -478\n-479 -479\n-480 -480\n-481 -481\n-482 -482\n-483 -483\n-484 -484\n-485 -485\n-486 -486\n-487 -487\n-488 -488\n-489 -489\n-490 -490\n-491 -491\n-492 -492\n-493 -493\n-494 -494\n-495 -495\n-496 -496\n-497 -497\n-498 -498\n-499 -499\n-500 -500\n-501 -501\n-502 -502\n-503 -503\n-504 -504\n-505 -505\n-506 -506\n-507 -507\n-508 -508\n-509 -509\n-510 -510\n-511 -511\n-512 -512\n-513 -513\n-514 -514\n-515 -515\n-516 -516\n-517 -517\n-518 -518\n-519 -519\n-520 -520\n-521 -521\n-522 -522\n-523 -523\n-524 -524\n-525 -525\n-526 -526\n-527 -527\n-528 -528\n-529 -529\n-530 -530\n-531 -531\n-532 -532\n-533 -533\n-534 -534\n-535 -535\n-536 -536\n-537 -537\n-538 -538\n-539 -539\n-540 -540\n-541 -541\n-542 -542\n-543 -543\n-544 -544\n-545 -545\n-546 -546\n-547 -547\n-548 -548\n-549 -549\n-550 -550\n-551 -551\n-552 -552\n-553 -553\n-554 -554\n-555 -555\n-556 -556\n-557 -557\n-558 -558\n-559 -559\n-560 -560\n-561 -561\n-562 -562\n-563 -563\n-564 -564\n-565 -565\n-566 -566\n-567 -567\n-568 -568\n-569 -569\n-570 -570\n-571 -571\n-572 -572\n-573 -573\n-574 -574\n-575 -575\n-576 -576\n-577 -577\n-578 -578\n-579 -579\n-580 -580\n-581 -581\n-582 -582\n-583 -583\n-584 -584\n-585 -585\n-586 -586\n-587 -587\n-588 -588\n-589 -589\n-590 -590\n-591 -591\n-592 -592\n-593 -593\n-594 -594\n-595 -595\n-596 -596\n-597 -597\n-598 -598\n-599 -599\n-600 -600\n" }, { "input": "1\r\n2 2 4\r\n", "output": "3 2\r\n1 2\r\n4 2\r\n0 2\r\n" }, { "input": "1\r\n1 1 2\r\n", "output": "2 1\r\n0 1\r\n" }, { "input": "1\r\n1 2 4\r\n", "output": "2 2\r\n0 2\r\n3 2\r\n-1 2\r\n" }, { "input": "1\r\n2 3 6\r\n", "output": "3 3\r\n1 3\r\n4 3\r\n0 3\r\n5 3\r\n-1 3\r\n" }, { "input": "1\r\n3 3 5\r\n", "output": "4 3\r\n2 3\r\n5 3\r\n1 3\r\n3 3\r\n" }, { "input": "1\r\n1 3 4\r\n", "output": "2 3\r\n0 3\r\n3 3\r\n-1 3\r\n" }, { "input": "1\r\n-5 20 2\r\n", "output": "-4 20\r\n-6 20\r\n" }, { "input": "1\r\n3 3 6\r\n", "output": "4 3\r\n2 3\r\n5 3\r\n1 3\r\n6 3\r\n0 3\r\n" }, { "input": "1\r\n1 1 4\r\n", "output": "2 1\r\n0 1\r\n3 1\r\n-1 1\r\n" }, { "input": "1\r\n2 -2 4\r\n", "output": "3 -2\r\n1 -2\r\n4 -2\r\n0 -2\r\n" }, { "input": "1\r\n1 2 2\r\n", "output": "2 2\r\n0 2\r\n" }, { "input": "1\r\n1 1 6\r\n", "output": "2 1\r\n0 1\r\n3 1\r\n-1 1\r\n4 1\r\n-2 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: 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\n13\r\n21\r\n26\r\n8\r\n3000000000\r\n" }, { "input": "1\r\n2 1000000000\r\n1000000000 1000000000\r\n1 1\r\n", "output": "3000000000\r\n" }, { "input": "1\r\n2 1000000000\r\n1000000000 999999999\r\n0 1\r\n", "output": "2999999999\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: 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
43