You are given a tree with $$$n$$$ nodes. Each node has a color between $$$1$$$ and $$$k$$$.
For each color $$$i$$$ from $$$1$$$ to $$$k$$$, print the number of simple paths that contain at least one node with color $$$i$$$.
A simple path is a sequence of edges $$$(u_1,u_2),(u_2,u_3),\ldots,(u_{k-1},u_k)$$$ for which all $$$u_i$$$ are pairwise distinct. Two paths are considered different if their corresponding sequences of edges are distinct.
Each test contains multiple testcases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.
The first line of each testcase contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 3 \cdot 10^5$$$) — the number of nodes and colors, respectively.
The second line contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$1 \le c_i \le k$$$) — the colors of the $$$n$$$ nodes.
Each of the next $$$n-1$$$ lines contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i,v_i \le n$$$, $$$u_i \neq v_i$$$), meaning that an undirected edge exists between nodes $$$u_i$$$ and $$$v_i$$$.
It is guaranteed that the given graph is a tree (an undirected connected graph without cycles).
It is also guaranteed that the sum of $$$n$$$ across all testcases does not exceed $$$3 \cdot 10^5$$$.
For each testcase, print $$$k$$$ integers, the number of simple paths that contain at least one node with color $$$i$$$, for every color $$$i$$$ from $$$1$$$ to $$$k$$$.
43 31 1 31 21 31 115 31 2 3 2 11 21 33 43 58 71 1 2 3 5 6 7 21 21 31 42 52 63 75 8
8 0 5 1 20 16 19 54 38 15 0 27 15 15
Explanation of the first testcase:
There are $$$8$$$ simple paths that contain at least one node with color $$$1$$$:
Since there are no nodes with color $$$2$$$, there are no paths that contain a node with color $$$2$$$.
Finally, there are $$$5$$$ simple paths that contain at least one node with color $$$3$$$:
In the second testcase, the only path ($$$[1]$$$) contains one node with color $$$1$$$.
Two players alternatively play the following game:
There are initially $$$n$$$ coins. In one move, supposing that the current number of coins is $$$x$$$, the current player can either take one coin, or take multiple coins, provided that the remaining number of coins is a divisor of $$$x$$$.
The game ends when there are no coins left. The player who took the last coin is considered the winner.
Given the number of coins $$$n$$$, your task is to find which player will win the game, supposing that both of them play optimally.
Each test contains multiple testcases. The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of testcases.
Each testcase consists of one line, containing $$$n$$$ ($$$1 \le n \le 10^9$$$) — the number of coins.
For each testcase, print either First or Second, depending on the player who will win the game.
12123456789102785653439
First Second First First Second First Second First Second First First Second
You are given a connected undirected graph with $$$n$$$ nodes and $$$n$$$ edges. Each node $$$i$$$ has an associated cost $$$a_i$$$.
A cut of the given graph is obtained by removing some edges in order to "split" the graph into exactly $$$2$$$ disjoint connected subgraphs.
If the sum of the costs of the nodes from the first subgraph is $$$s_1$$$ and the sum of the costs of the nodes from the second subgraph is $$$s_2$$$, then the cost of the cut is equal to $$$|s_1-s_2|$$$, where $$$|x|$$$ denotes the absolute value of $$$x$$$.
What is the smallest possible cost of a cut of the given graph?
The first line of input contains a single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of the nodes and edges of the given graph.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the associated costs of the $$$n$$$ nodes.
The following $$$n$$$ lines contain the edges of the graph. Each line contains two integers, $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n, u \neq v$$$) — the endpoints of an edge.
It is guaranteed that there are no multiple edges and that the given graph is connected.
Output a single integer, the minimum possible cost of a cut.
3 1 7 3 1 2 2 3 3 1
3
5 1 7 3 3 6 3 5 5 4 1 4 4 2 2 5
2
8 5 7 9 12 13 19 7 16 1 2 2 3 3 1 1 4 4 5 4 6 3 7 7 8
0
Explanation of the first testcase:
Let's call $$$f(p)$$$ applied to a permutation$$$^\dagger$$$ of length $$$n$$$ as the number of pairs $$$(i,j)$$$ such that $$$i \lt j$$$ and $$$p_i+1 = p_{j}$$$. For example $$$f([1,2,3]) = 2$$$, while $$$f([3,1,2]) = 1$$$ and $$$f([3,2,1]) = 0$$$.
Given $$$n$$$ and a permutation $$$p$$$ of length $$$n$$$, we define a permutation $$$p'$$$ of length $$$n$$$ to be $$$k$$$-special if and only if:
Your task is to count for each $$$0 \le k \le n - 1$$$ the number of $$$k$$$-special permutations modulo $$$m$$$.
$$$^\dagger$$$ A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
$$$^\ddagger$$$ A permutation $$$a$$$ of length $$$n$$$ is lexicographically smaller than a permutation $$$b$$$ of length $$$n$$$ if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the permutation $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 10^9$$$) — the length of the permutation and the required modulo.
The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$.
Print $$$n$$$ integers, where the $$$k$$$-th integer is the number of $$$(k-1)$$$-special permutations modulo $$$m$$$.
4 666012 1 3 4 2
0 0 2 1
10 60 10 9 8 7 6 5 4 3 2 1
0 53 20 32 14 14 32 20 53 1
In the first example, the permutations that are lexicographically smaller than $$$[1,3,4,2]$$$ are:
Thus our answer is $$$[0,0,2,1]$$$.
You are given a binary string of length $$$n$$$. A binary string is considered good if none of its substrings are equal to "101" or "010".
What is the smallest number of characters that need to be deleted from the given string in order to make it good?
Each test contains multiple testcases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of testcases.
The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the length of the given string.
The second line of each testcase contains a binary string $$$\overline{s_1s_2\ldots s_n}$$$ ($$$s_i \in \{'0','1'\}$$$).
It is guaranteed that the sum of $$$n$$$ across all testcases does not exceed $$$3 \cdot 10^5$$$.
For each testcase, print smallest number of characters that need to be deleted from the given string in order to make it good.
761011016001010108001010111500110101010110120130010110101010110101011010101101
2 1 0 2 4 0 9
In a company there are $$$n$$$ employees. The skill level of the $$$i$$$-th employee is denoted by an integer $$$a_i$$$.
The company wants to start a new project, which requires some of the $$$n$$$ employees. A project is considered balanced if the difference in skill between any two employees assigned to the project is at most $$$k$$$.
For every $$$i$$$ from $$$1$$$ to $$$n$$$, print the maximum number of employees that can be part of a balanced project which contains employee $$$i$$$.
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le 10^9$$$).
The second line of input contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the skill levels of the $$$n$$$ employees.
For every $$$i$$$ from $$$1$$$ to $$$n$$$, print the maximum number of employees that can be part of a balanced project which contains employee $$$i$$$.
6 2 1 2 3 4 6 9
3 3 3 3 2 1
15 78 98 190 175 67 109 139 297 175 789 162 109 87 165 243 72
8 6 8 7 8 8 2 8 1 8 8 7 8 5 7
Explanation of the first testcase:
You are given an array $$$a_1,a_2,...,a_n$$$. In one move, you can select an element $$$a_i$$$ and flip its sign (that is, perform $$$a_i:=-a_i$$$).
Let $$$\text{diff}([x_1,x_2,...,x_k])$$$ be the number of distinct elements in an array $$$x$$$ of length $$$k$$$.
If we perform the aformentioned moves optimally, what is the maximum possible value of:
$$$$$$\sum_{i=1}^n\sum_{j=i}^n \text{diff}([a_i,a_{i+1},...,a_j])$$$$$$
Note: You do not have to minimize the number of moves.
The first line of input contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the length of array $$$a$$$.
The second line of input contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of array $$$a$$$.
The first line of output contains the maximum possible value of $$${\sum} \text{diff}([a_i,a_{i+1},...,a_j])$$$, after performing the sign flipping operations optimally.
4 1 1 0 -1
19
3 0 0 0
6
15 2 -2 0 1 -3 0 0 3 1 -1 -4 0 -1 -2 3
510
In the first testcase, if we flip the signs of $$$a_2$$$ and $$$a_4$$$, the resulting array will be $$$[1,-1,0,1]$$$. Therefore:
The sum of all $$$\text{diff}$$$'s is equal to $$$1+2+3+3+1+2+3+1+2+1=19$$$, which is the maximum possible value.
In the second testcase, regardless of how many operations we will perform, $$$a$$$ will remain equal to $$$[0,0,0]$$$.
Therefore, $$$\text{diff}([a_i,a_{i+1},...,a_j])$$$ is equal to $$$1$$$, for all $$$1 \le i \le j \le n$$$. Since there are $$$6$$$ subarrays, the answer is equal to $$$6 \cdot 1=6$$$.
A binary matrix with $$$n$$$ rows and $$$m$$$ columns is considered good if it has no $$$1 \times 3$$$ or $$$3 \times 1$$$ submatrices consisting of equal elements.
For example, $$$\begin{bmatrix}1 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 1 \end{bmatrix}$$$ and $$$\begin{bmatrix}0 & 1 & 1 \\ 1 & 1 & 0 \\ 1 & 0 & 1 \end{bmatrix}$$$ are good, while $$$\begin{bmatrix}1 & 0 & 1 \\ 1 & 0 & 1 \\ 0 & 0 & 1 \end{bmatrix} and \begin{bmatrix}1 & 0 & 1 \\ 0 & 0 & 0 \\ 1 & 1 & 0 \end{bmatrix}$$$ are not.
Given two integers $$$n$$$ and $$$m$$$, what is the maximum number of $$$1$$$'s in a good matrix with $$$n$$$ rows and $$$m$$$ columns?
Each test contains multiple testcases. The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of testcases.
The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n,m \le 10^9$$$) — the height and width of the matrix.
For each testcase, print one integer, the maximum number of $$$1$$$'s in a good matrix with $$$n$$$ rows and $$$m$$$ columns.
63 33 4126 74169 2081000000000 987654320976875237 848936273
6 8 6216 23435 658436213333333334 552869881923181134
You are given an integer $$$a$$$. Find the smallest positive integer $$$b$$$ ($$$b \ge 1$$$) such that $$$a+b$$$ is a divisor of $$$a \cdot b$$$.
It can be proven that, under the given constraints, a solution always exists.
Each test contains multiple testcases. The first line of each testcase contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.
The first line of each testcase contains one integer $$$a$$$ ($$$2 \le a \le 10^9$$$).
For each testcase, print the smallest integer $$$b \ge 1$$$ such that $$$a+b$$$ is a divisor of $$$a \cdot b$$$.
1423456789101112131415
2 6 4 20 3 42 8 18 10 110 4 156 14 10
You are given a permutation $$$a$$$ of length $$$n$$$.
On this permutation, we can perform the following operation multiple times:
Is it possible to sort the permutation?
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple testcases. The first line of each testcase contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.
The first line of each testcase contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of permutation $$$a$$$.
The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$, all $$$a_i$$$ are pairwise distinct) — the elements of permutation $$$a$$$.
It is guaranteed that the sum of $$$n$$$ across all testcases does not exceed $$$2 \cdot 10^5$$$.
For each testcase, print "YES" if $$$a$$$ can be sorted, or "NO" otherwise.
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.
61122 133 2 144 3 2 151 3 2 5 453 4 5 2 1
YES NO YES NO NO YES
In the first two testcases, no operations can be performed since $$$n \lt 3$$$. $$$[1]$$$ is already sorted, while $$$[2,1]$$$ is not.
In the third testcase, the permutation can be sorted by choosing $$$i=1$$$ and reversing $$$[a_1,a_2,a_3]$$$.
In the fourth and fifth testcases, it can be proven that $$$a$$$ cannot be sorted.
You are given a matrix with $$$n$$$ rows and $$$m$$$ columns. You will have to answer $$$q$$$ queries of the following type:
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m,n \cdot m \le 10^5$$$) — the height, and width, of the matrix, respectively.
Each of the next $$$n$$$ lines of input contain $$$m$$$ integers $$$a_{i,1},a_{i,2},\ldots,a_{i,m}$$$ ($$$1 \le a_{i,j} \le n \cdot m$$$) — the elements of the $$$i$$$-th row of the given matrix.
The next line contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.
Each of the following $$$q$$$ lines contain $$$4$$$ integers $$$i_1$$$, $$$j_1$$$, $$$i_2$$$, $$$j_2$$$ ($$$1 \le i_1 \le i_2 \le n$$$, $$$1 \le j_1 \le j_2 \le m$$$) — the submatrix's corner cells' coordinates.
For each query, print "YES", if all of the elements of the given submatrix are distinct, and "NO", otherwise.
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.
2 2 2 1 3 2 9 1 1 1 1 1 2 1 2 2 1 2 1 2 2 2 2 1 1 1 2 2 1 2 2 1 1 2 1 1 2 2 2 1 1 2 2
YES YES YES YES YES YES YES YES NO
4 6 1 4 7 1 2 3 2 5 8 6 5 4 3 6 9 7 8 9 1 2 3 4 5 6 10 1 1 3 3 1 4 3 6 1 1 4 6 1 2 4 3 2 1 3 4 4 1 4 6 1 4 4 4 2 5 4 5 2 1 2 6 4 6 4 6
YES YES NO YES NO YES YES NO NO YES
G dreamt about an unusual permutation $$$p_1, p_2, \ldots, p_n$$$.
A permutation is considered unusual if, for each element $$$p_i$$$, exactly one of the following conditions is true:
For example, $$$[1]$$$, $$$[2,1,3,4]$$$ and $$$[1,2,3,4,5]$$$ are unusual, while $$$[3,1,2]$$$ and $$$[3,2,5,4,1]$$$ are not.
Unfortunately, after waking up, G remembers only some of the elements from $$$p$$$. As such, he now wonders how many unusual permutations containing the remaining numbers exist.
Since the answer can be very large, G only wants to know its remainder modulo $$$10^9+7$$$.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$) — the number of testcases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the permutation.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0 \le a_i \le n$$$). If $$$a_i=0$$$, then G forgot the value of $$$p_i$$$. Otherwise, G knows that $$$p_i$$$ must be equal to $$$a_i$$$. It is guaranteed that there are no two indices $$$1 \le i \lt j \le n$$$ for which $$$0 \neq a_i=a_j$$$.
It is guaranteed that the sum of $$$n$$$ across all testcases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer, denoting the number of unusual permutations containing the remaining numbers. Since the answer can be very large, print its remainder modulo $$$10^9+7$$$.
442 0 0 433 1 250 0 0 4 010
2 0 4 1
The bolded numbers denote the elements that G remembered: