You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$ and an integer $$$k$$$.
Two players are playing a game. The first player chooses an index $$$1 \le i \le n$$$. Then the second player chooses a different index $$$1 \le j \le n, i \neq j$$$. The first player wins if $$$|a_i - a_j|$$$ is not divisible by $$$k$$$. Otherwise, the second player wins.
We play as the first player. Determine whether it is possible to win, and if so, which index $$$i$$$ should be chosen.
The absolute value of a number $$$x$$$ is denoted by $$$|x|$$$ and is equal to $$$x$$$ if $$$x \ge 0$$$, and $$$-x$$$ otherwise.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$; $$$1 \le k \le 100$$$) — the length of the array and the number $$$k$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 100$$$) — the elements of the array $$$a$$$.
For each test case, if it is impossible for the first player to win, print "NO" (without quotes).
Otherwise, print "YES" (without quotes) and on the next line the appropriate index $$$1 \le i \le n$$$. If there are multiple solutions, print any of them.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer.
73 21 2 34 21 2 4 55 310 7 3 4 55 31 31 15 55 362 117 172 217 181 36
YES 2 NO YES 3 NO NO YES 2 YES 1
In the first test case, the first player can choose $$$a_2 = 2$$$. Then:
In the second test case:
You have an array of zeros $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$.
You can perform two types of operations on it:
What is the minimum number of operations of the first type needed to make all elements of the array equal to one?
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 only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array.
Note that there is no limit on the sum of $$$n$$$ over all test cases.
For each test case, print one integer — the minimum number of needed operations of first type.
412420
1 2 2 4
In the first test case, you can perform an operation of the $$$1$$$st type with $$$i = 1$$$.
In the second test case, you can perform the following sequence of operations:
In the third test case, you can perform the following sequence of operations:
Consider a permutation$$$^{\text{∗}}$$$ $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. We can introduce the following sum for it$$$^{\text{†}}$$$:
$$$$$$S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)$$$$$$
Let us consider all permutations of length $$$n$$$ with the maximum possible value of $$$S(p)$$$. Output the $$$k$$$-th of them in lexicographical$$$^{\text{‡}}$$$order, or report that there are less than $$$k$$$ of them.
$$$^{\text{∗}}$$$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).
$$$^{\text{†}}$$$For example:
$$$^{\text{‡}}$$$An array $$$a$$$ is lexicographically smaller than an array $$$b$$$ if and only if one of the following holds:
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 only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^{12}$$$) — the length of the permutation and the index number of the desired permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
For each test case, if there are less than $$$k$$$ suitable permutations, print $$$-1$$$.
Otherwise, print the $$$k$$$-th suitable permutation.
63 23 34 114 66 397 34
1 3 2 2 3 1 -1 2 4 3 1 -1 2 3 4 5 7 6 1
Let us calculate the required sum for all permutations of length $$$3$$$ (ordered lexicographically):
| Permutation | Value of $$$S(p)$$$ |
| $$$[1, 2, 3]$$$ | $$$10$$$ |
| $$$[1, 3, 2]$$$ | $$$10$$$ |
| $$$[2, 1, 3]$$$ | $$$9$$$ |
| $$$[2, 3, 1]$$$ | $$$10$$$ |
| $$$[3, 1, 2]$$$ | $$$9$$$ |
| $$$[3, 2, 1]$$$ | $$$10$$$ |
In the first test case, you have to print the second suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[1, 3, 2]$$$.
In the second test case, you have to print the third suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[2, 3, 1]$$$.
You are given a tree with $$$n$$$ vertices.
You need to construct an array $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$, consisting of unique integers from $$$1$$$ to $$$2 \cdot n$$$, and such that for each edge $$$u_i \leftrightarrow v_i$$$ of the tree, the value $$$|a_{u_i} - a_{v_i}|$$$ is not a prime number.
Find any array that satisfies these conditions, or report that there is no such array.
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 next $$$n - 1$$$ lines contain the edges of the tree, one edge per line. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \neq v_i$$$), denoting the edge between the nodes $$$u_i$$$ and $$$v_i$$$.
It's 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$$$.
For each test case, if an array that satisfies the conditions exists, print its elements $$$a_1, a_2, \ldots, a_n$$$. Otherwise, print $$$-1$$$.
251 22 32 43 571 21 32 43 53 63 7
2 10 1 6 5 8 7 12 1 4 6 3
The possible answers are shown below. Instead of the vertex numbers, the corresponding elements of the array $$$a$$$ are written in them.
You are given a tree with $$$n$$$ vertices.
Let's place a robot in some vertex $$$v \ne 1$$$, and suppose we initially have $$$p$$$ coins. Consider the following process, where in the $$$i$$$-th step (starting from $$$i = 1$$$):
The process stops as soon as the robot reaches vertex $$$1$$$. Let $$$f(v, p)$$$ be the minimum possible expected number of steps in the process above if we spend our coins optimally.
Answer $$$q$$$ queries, in the $$$i$$$-th of which you have to find the value of $$$f(v_i, p_i)$$$, modulo$$$^{\text{∗}}$$$ $$$998\,244\,353$$$.
$$$^{\text{∗}}$$$ Formally, let $$$M = 998\,244\,353$$$. It can be shown that the 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 \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
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 two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 2 \cdot 10^3$$$; $$$1 \le q \le 2 \cdot 10^3$$$) — the number of vertices in the tree and the number of queries.
The next $$$n - 1$$$ lines contain the edges of the tree, one edge per line. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \neq v_i$$$), denoting the edge between the nodes $$$u_i$$$ and $$$v_i$$$.
The next $$$q$$$ lines contain two integers $$$v_i$$$ and $$$p_i$$$ ($$$2 \le v_i \le n$$$; $$$0 \le p_i \le n$$$).
It's 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 ^ 3$$$.
It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 3$$$.
For each test case, print $$$q$$$ integers: the values of $$$f(v_i, p_i)$$$ modulo $$$998\,244\,353$$$.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the 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 \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
24 41 22 32 42 03 04 03 112 101 22 32 41 55 66 76 86 98 1010 1110 126 09 010 011 03 17 110 112 112 211 12
1 6 6 2 4 9 8 15 2 3 6 9 5 5
The tree in the first test case:
In the first query, the expected value is equal to $$$1$$$, since the robot starts moving from vertex $$$2$$$ to vertex $$$1$$$ in the first step and the process stops.
Let's calculate the expected value in the second query ($$$x$$$ is the number of steps):
As a result, $$$f(v, p) = \sum\limits_{i=1}^{\infty}{i \cdot 2 \cdot \frac{2^{i - 1}}{3^i}} = 6$$$.
The tree in the second test case:
Consider a rectangular parallelepiped with sides $$$a$$$, $$$b$$$, and $$$c$$$, that consists of unit cubes of $$$k$$$ different colors. We can apply cyclic shifts to the parallelepiped in any of the three directions any number of times$$$^{\text{∗}}$$$.
There are $$$d_i$$$ cubes of the $$$i$$$-th color ($$$1 \le i \le k$$$). How many different parallelepipeds (with the given sides) can be formed from these cubes, no two of which can be made equal by some combination of cyclic shifts?
$$$^{\text{∗}}$$$On the image:
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 first line of each test case contains four integers: $$$a$$$, $$$b$$$, $$$c$$$, and $$$k$$$ ($$$1 \le a, b, c \le 3 \cdot 10^6$$$; $$$a \cdot b \cdot c \le 3 \cdot 10^6$$$; $$$1 \le k \le 10^6$$$) — three sides of the parallelepiped and the number of colors of unit cubes.
The second line of each test case contains $$$k$$$ integers $$$d_1, d_2, \ldots, d_k$$$ ($$$1 \le d_1 \le d_2 \le \ldots \le d_k \le 3 \cdot 10^6$$$) — the elements of the array $$$d$$$: the number of cubes of a given color.
It is guaranteed that in each test case the sum of the elements of the array $$$d$$$ is equal to $$$a \cdot b \cdot c$$$.
It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10 ^ 6$$$.
For each test case, print one integer — the number of different parallelepipeds modulo $$$998\,244\,353$$$.
61 1 1 116 1 1 31 2 312 1 1 32 4 63 3 1 23 62 3 3 26 1272 60 96 417280 86400 120960 190080
1 10 1160 12 1044 231490207
In the first test case, there is only one parallelepiped, which consists of one unit cube.