Hyrule is entering the flu season.
Hyrule consists of $$$n$$$ cities, which are connected by $$$m$$$ directed roads. The capital (city numbered $$$1$$$) can be reached from all cities through these roads.
The flu season will last for $$$q$$$ days. On the $$$i$$$-th day at noon, the flu virus will start spreading from city $$$a_i$$$. The virus will spread along the roads to other cities. The virus spreads very quickly, so it can be assumed that all cities reachable from $$$a_i$$$ will be infected immediately. As the capital city of Hyrule, city $$$1$$$ is crucial for the whole country. If the virus that starts spreading on the $$$i$$$-th day reaches the capital, there will be an economic loss cost of $$$b_i$$$.
To prevent the virus from spreading, Auru can deploy a virus filter in a city each night (including the night before the first day). Deploying a virus filter in city $$$i$$$ has a deployment cost of $$$c_i$$$. The virus cannot spread through the city equipped with a virus filter, and that city will also remain uninfected. If the virus were to start spreading from a city equipped with a virus filter, the virus would simply disappear without infecting any cities. The deployed virus filter remains effective until Auru deploys a new filter in another city. (In other words, there can be at most one virus filter at a time.)
Auru wants to know: for each $$$i = 1, 2, \dots, q$$$, considering only the viruses in the first $$$i$$$ days, what is the minimum value of "economic loss cost + deployment cost".
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$2 \leq n \leq 10^5$$$, $$$n-1 \leq m \leq 2 \times 10^5$$$, $$$1 \leq q \leq 10^5$$$), representing the number of cities, the number of roads, and the duration of the flu season in days, respectively.
In the next $$$m$$$ lines, each contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n$$$), representing a directed road from city $$$u_i$$$ to city $$$v_i$$$. It is guaranteed that there are no self-loops, but there may be multiple edges between cities. The capital can be reached from all cities.
The next line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \leq c_i \leq 10^9$$$), representing the deployment cost of the virus filter in each city.
In the following $$$q$$$ lines, the $$$i$$$-th one contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$2 \leq a_i \leq n$$$, $$$1 \leq b_i \leq 10^9$$$), indicating the city where the virus starts to spread on the $$$i$$$-th day and the economic loss cost if the virus reaches the capital.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, the sum of $$$m$$$ over all test cases does not exceed $$$2 \times 10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a line containing $$$n$$$ integers, where the $$$i$$$-th integer represents the minimum value of the "economic loss cost + deployment cost" when only considering the viruses in the first $$$i$$$ days.
37 6 42 13 14 25 26 37 34 3 5 2 2 1 14 25 26 27 25 6 41 33 22 14 25 42 510000 10000 2 100 55 10004 10003 10004 10004 4 12 13 14 24 3100 1 1 1004 10
2 3 4 4 5 100 102 202 10
In the first test case:

Example $$$1$$$ Illustration
In the second test case:

Example $$$2$$$ Illustration
In the third test case, since only one virus filter can exist at a time, it is not possible to stop the virus from spreading by deploying virus filters in both cities $$$2$$$ and $$$3$$$. The deployment costs for cities $$$1$$$ and $$$4$$$ are higher than the economic loss cost caused by the virus, so no virus filter is deployed, and the minimum cost is $$$10$$$ (economic loss cost).

Example $$$3$$$ Illustration
Betty has a bracket sequence $$$s$$$ of length $$$n$$$, which consists of eight types of brackets: "()[]{}<>". Additionally, she has $$$m$$$ sub-bracket sequences, and the $$$i$$$-th sub-bracket sequence $$$t_i$$$ is the sequence formed by concatenating the characters from the $$$l_i$$$-th to the $$$r_i$$$-th position of $$$s$$$, i.e., $$$t_i = s_{l_i}s_{l_i+1} \cdots s_{r_i}$$$.
Betty wants to know, by taking out several pairs from these $$$m$$$ sub-bracket sequences and concatenating sequences in the same pair, how many valid bracket sequences$$$^\dagger$$$ can be formed at most? Formally, you need to find as many pairs $$$(a_i, b_i)$$$ as possible such that:
$$$^\dagger$$$ A bracket sequence $$$x$$$ is valid if and only if it satisfies one of the following conditions:
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 5 \times 10^5$$$), which represent the length of $$$s$$$ and the number of sub-bracket sequences, respectively.
The second line contains a string $$$s$$$ ($$$|s| = n$$$), which consists only of the eight brackets "()[]{}<>".
The next $$$m$$$ lines each contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq n$$$).
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \times 10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$5 \times 10^5$$$.
For each test case, output a single integer on a new line, representing the maximum number of valid bracket sequences that can be obtained by concatenating pairs of sub-bracket sequences.
48 1()[]{}<>3 62 6)(1 11 11 12 22 22 26 2([)(])1 34 622 8([{}<<<<])>>>>([]){()}3 811 141 103 419 2220 2117 2021 22
0 3 0 2
In the first test case, $$$t_1=$$$ "[]{}". Although $$$t_1$$$ is already a valid bracket sequence, there are no other sub-bracket sequences that can be paired with it, so the output is $$$0$$$.
In the second test case, there are six sub-bracket sequences containing three "(" and three ")", which can be used to form three valid bracket sequences "()". Therefore, the output should be $$$3$$$.
In the third test case, no valid bracket sequences can be formed.
In the fourth test case, one possible matching is $$$t_1t_2$$$: "{}««" + "»»", and $$$t_4t_5$$$: "{}" + "{()}".
The pirates have just seized a giant gold coin!
To determine the ownership of this gold coin, they decided to select the owner using the following method:
Let the current number of remaining pirates be $$$n$$$. The pirates line up in a queue, and the pirates at positions $$$1, 1+k, 1+2k, \dots, 1+(\lceil\frac{n}{k}\rceil - 1) k$$$ are eliminated. This operation is repeated until only one pirate remains. The final remaining pirate will receive the gold coin.
Charlie is the smartest among the pirates. He wants to know where he should stand initially to be the last pirate remaining and win the coin.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 100$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n, k \leq 10^{12}$$$), representing the initial number of pirates and the parameter used for elimination.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^{12}$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$10^{12}$$$.
For each test case, output a single integer on a new line, indicating the position of the pirate who will ultimately receive the gold coin in the initial queue.
46 28 310000 21919810 114514
4 8 8192 1919805
For the first test case in the example, the positions of the remaining pirates in the original sequence after each round are:
For the second test case in the example, the positions of the remaining pirates in the original sequence after each round are:
David has just obtained $$$n$$$ Russian nesting dolls of distinct sizes. He arranges these dolls in a row from left to right, where the $$$i$$$-th position contains a doll of size $$$a_i$$$.
Let the size of the smallest doll in the $$$i$$$-th position be $$$l_i$$$, and the size of the largest one be $$$r_i$$$. Dolls over two adjacent positions $$$i$$$ and $$$i+1$$$ can be merged if and only if $$$r_i \lt l_{i+1}$$$ or $$$r_{i+1} \lt l_i$$$. The new nesting doll will contain all the dolls from the original $$$i$$$-th and $$$i+1$$$-th positions and will be placed in the $$$i$$$-th position. All dolls in positions greater than $$$i+1$$$ will shift left by one position to fill the gap.
For example, when $$$n=4, a=[2,1,4,3]$$$, David can:
How many merge operations at most can David perform under an optimal strategy?
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$), representing the number of nesting dolls.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$, $$$\forall i \neq j, a_i \neq a_j$$$), representing the initial sizes of the dolls in each position.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a single integer on a new line, representing the maximum number of merge operations that can be performed.
842 1 4 341 4 2 343 1 4 251 3 5 2 451 4 2 5 352 5 3 1 461 3 6 5 2 462 5 1 3 6 4
3 3 2 3 3 3 4 4
This is an interactive problem.
Emily has a tree containing $$$n$$$ nodes, where the weight of the node numbered $$$i$$$ is $$$w_i$$$. Node $$$1$$$ is the root node, and the weight of the root node is $$$0$$$.
Emily wants you to guess the weight of each node. Specifically, you can make at most $$$n$$$ queries, with the $$$i$$$-th query containing two integers $$$u_i$$$ and $$$v_i$$$. If the simple path from $$$u_i$$$ to $$$v_i$$$ contains exactly $$$k$$$ edges, Emily will tell you the bitwise XOR$$$^{\dagger}$$$ of the weights of all the nodes on the simple path from node $$$u_i$$$ to node $$$v_i$$$ (including the endpoints). Otherwise, Emily will respond with "-1", indicating that she does not want to answer that question. Emily is very busy, so she will not start answering until you have asked all your questions.
Of course, you may not be able to guess the weights of all nodes with no more than $$$n$$$ queries for some cases. In such cases, you should not make any queries and instead directly tell Emily that it is impossible.
$$$^{\dagger}$$$The bitwise XOR operation refers to the addition of each bit of two binary numbers under modulo $$$2$$$. For example: $$$(0011)_2$$$ $$$\oplus$$$ $$$(0101)_2$$$ $$$=$$$ $$$(0110)_2$$$.
There is only one test case in each test file.
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 250$$$, $$$1 \leq k \leq n-1$$$), representing the number of nodes in the tree and a parameter mentioned in the query.
The next $$$n-1$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i, y_i \leq n$$$), indicating that there is an edge between nodes $$$x_i$$$ and $$$y_i$$$.
It is guaranteed that all edges form a tree, and the correct answer satisfies $$$0 \leq w_i \lt 2^{30}$$$.
First, you need to determine whether it is possible to guess the weights of all nodes within $$$n$$$ queries. If not, your program should output "No" and exit immediately. Otherwise, your program should output "Yes" and proceed to querying. 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.
To make a query, output all your $$$q$$$ queries at once in the format "? $$$q$$$ $$$u_1$$$ $$$v_1$$$ $$$u_2$$$ $$$v_2$$$ $$$\dots$$$ $$$u_q$$$ $$$v_q$$$" ($$$1 \leq q \leq n$$$, $$$1 \leq u_i, v_i \leq n$$$). After flushing your output, your program should read a line containing $$$q$$$ integers. The $$$i$$$-th integer represents the response to the $$$i$$$-th query.
To guess the weights, output your guess in the format "! $$$w_2$$$ $$$w_3$$$ $$$\dots$$$ $$$w_n$$$" ($$$0 \leq w_i \lt 2^{30}$$$). After flushing your output, your program should exit immediately.
Note that the answer for each test case is pre-determined. That is, the interactor is not adaptive. Also, note that your guess does not count as a query.
To flush your output, you can use:
4 1 1 2 2 3 2 4 1 3 2
YES ? 3 1 2 2 3 2 4 ! 1 2 3
5 2 1 2 2 3 3 4 3 5 1 2 3 4
YES ? 4 1 3 2 4 2 5 4 5 ! 4 5 3 2
6 2 1 2 2 3 3 4 4 5 4 6
NO
"Little Flower, because it's a flower."
Frank is fascinated by the beauty of numbers.
One day, Frank was watering flowers in his garden. Looking at the beautiful petals, he thought it would be wonderful if each flower could grow numbers.
So, he took out paper and pen and started sketching his ideal "number flower". An undirected connected graph is called a "number flower" if and only if it satisfies the following three conditions:
Given an integer $$$n$$$, how many different "number flowers" with $$$n$$$ nodes are there? Two graphs $$$G_1$$$ and $$$G_2$$$ are considered the same if and only if for any edge $$$(u, v)$$$ in $$$G_1$$$, a corresponding edge $$$(u, v)$$$ exists in $$$G_2$$$.
Since the answer is huge, you only need to output it modulo a prime number $$$p$$$.
There is only one test case in each test file.
The first line contains two positive integers $$$n$$$ and $$$p$$$ ($$$1 \leq n \leq 10^{10}$$$, $$$10^8 \lt p \lt 10^9$$$).
It is guaranteed that $$$p$$$ is a prime number.
Output a single integer, which is the number of different "number flowers" modulo $$$p$$$.
5 998244353
1
10 998244353
4
10000000000 998244353
889033323
Example $$$1$$$ Illustration For the first example, there is only one "number flower", which is shown in the figure.
Given two integers $$$a$$$ and $$$b$$$, you can perform one of the following two operations in each round:
Grace wants to know the minimum number of rounds needed to make both $$$a$$$ and $$$b$$$ become $$$0$$$.
$$$^{\dagger}$$$ $$$\gcd(x, y)$$$ denotes the greatest common divisor of $$$x$$$ and $$$y$$$. For example, $$$\gcd(6, 8) = 2, \gcd(7, 5) = 1$$$. The values of $$$\gcd(x, 0)$$$ and $$$\gcd(0, x)$$$ are defined as $$$x$$$.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 1000$$$). The description of the test cases follows.
Each test case consists of a single line containing two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq b, a \leq 5000, b \leq 10^{18}$$$).
For each test file, it is guaranteed that the sum of $$$a$$$ over all test cases does not exceed $$$10^4$$$.
For each test case, output a single integer representing the minimum number of rounds needed to make both $$$a$$$ and $$$b$$$ become $$$0$$$.
33 412 20114 514
3 4 6
For the first test case in the example, one possible optimal solution is:
Explanation: It's actually cold knowledge, not a cold joke. The confusion between these concepts is what makes it funny.
Hana recently needs to develop a radar system to monitor abnormal activities across the archipelago she manages.
There are $$$n$$$ islands in the ocean, and the $$$i$$$-th island is located at coordinates $$$(x_i, y_i)$$$, which can be treated as a point on the plane. Assume the radar has a scanning angle range of $$$\alpha$$$. When the radar is rotated to an angle $$$\theta$$$, it can monitor all the islands located within the angular range $$$[\theta - \frac{\alpha}{2}, \theta + \frac{\alpha}{2}]$$$.
Hana is currently low on funds, so she wants to minimize the cost of building the radar. She wants to know, when the radar is placed at the origin $$$(0,0)$$$, what the minimum scanning angle $$$\alpha$$$ should be to ensure that for any angle $$$\theta$$$, the radar can monitor at least $$$k$$$ islands.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of each test case follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2 \times 10^5$$$), representing the total number of islands and the minimum number of islands the radar must monitor at any given time.
The next $$$n$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$|x_i|, |y_i| \leq 10^9$$$), representing the position of an island. It is guaranteed that the coordinates of any two islands are different and none of them are located at the origin.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^5$$$.
For each test case, output a single decimal fraction representing the minimum radar scanning angle in radians.
Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.
Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \leq 10^{-6}$$$.
51 10 18 21 01 10 1-1 1-1 0-1 -10 -11 -14 2-1 10 10 21 14 2-1000000000 0-998244353 1998244353 11000000000 03 10 10 20 -1
6.2831853072 1.5707963268 5.4977871438 3.1415926546 3.1415926536
For the first test case in the example, there is only one island on the plane at $$$(0,1)$$$. To ensure that at least one island is always within range, the radar's monitoring range must be set to $$$360^{\circ}$$$, which is $$$2 \pi$$$ in radians.
For the second test case in the example, there are $$$8$$$ islands on the plane, with each pair of islands separated by $$$45^{\circ}$$$. If the radar's range is less than $$$90^{\circ}$$$, it would only be able to monitor one island when one of its boundaries just moves past an island (as shown on the left of the illustration). Therefore, the minimum radar range needed is $$$90^{\circ}$$$, ensuring that at least two islands are always within range.
Example $$$2$$$ Illustration
Ivan has $$$n$$$ types of items, with an infinite supply of each type. The weight of the $$$i$$$-th type of item is $$$w_i$$$.
Ivan wants to know whether it is possible to select exactly $$$n$$$ items such that the total weight of the selected items equals $$$m$$$.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 10^5, 0 \leq m \leq n^2$$$).
The second line contains $$$n$$$ integers $$$w_1, w_2, \cdots, w_n$$$ ($$$0 \leq w_i \leq n$$$).
For each test file, it is guaranteed that the sum of all $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output "Yes" if the desired selection is possible; otherwise, output "No".
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.
45 250 0 0 0 55 114 4 4 5 55 01 2 3 4 55 250 1 2 3 4
Yes No No No
In the first test case, you can select $$$5$$$ items of the $$$5$$$th type, with a total weight of $$$25$$$, so you output "Yes".
In the second test case, it can be proven that there is no selection of items that results in a total weight of $$$11$$$, so you output "No".
In the third test case, since you must select $$$5$$$ items and the weight of the lightest item is $$$1$$$, there is no way to select items with a total weight of $$$0$$$, so you output "No".
In the fourth test case, since the heaviest item weighs $$$4$$$, there is no way to select items with a total weight of $$$25$$$, so you output "No".
Jessica is a master of sorting algorithms, proficient in selection sort, insertion sort, bubble sort, and many others. Therefore, she decided to host a sorting competition.
The competition takes place on a permutation$$$^{\dagger}$$$ $$$p$$$ of length $$$n$$$, with two participants: Alice and Bob. The two players take turns performing operations, with the first player being decided by a coin toss. If the sequence is in ascending order after any player's turn, Alice wins immediately. If Alice cannot win within a finite number of turns, Bob is considered the winner.
On Alice's turn, she can choose any two positions $$$i,j$$$ ($$$i \neq j, 1 \leq i,j \leq n$$$) in the permutation and swap $$$p_i$$$ and $$$p_j$$$. On Bob's turn, he can select two adjacent positions $$$i,i+1$$$ ($$$1 \leq i \lt n$$$) and swap $$$p_i$$$ and $$$p_{i+1}$$$. Neither player is allowed to skip their turn.
Given the permutation $$$p$$$ and the name of the player who operates first, determine who will win the game if both players play optimally.
$$$^{\dagger}$$$ 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 a $$$4$$$ in the array).
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) and a string $$$s$$$ ($$$s \in \{$$$Alice, Bob$$$\}$$$), representing the length of the permutation and the name of the player who operates first.
The second line contains $$$n$$$ integers $$$p_1, p_2, \cdots, p_n$$$ ($$$1 \leq p_i \leq n$$$), representing the permutation $$$p$$$. It is guaranteed that there is at least one position $$$i$$$ such that $$$p_i \neq i$$$.
For each test file, it is guaranteed that the sum of all $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output one line containing the winner's name. If Alice wins, print "Alice"; otherwise, print "Bob".
32 Alice2 13 Bob1 3 210 Bob1 2 3 4 5 6 7 8 10 9
Alice Bob Bob
This is an interactive problem.
Kevin is dissatisfied with his MBTI, so he asked Doraemon for a magical machine: the MBTI converter.
The MBTI converter can convert the MBTI attributes of $$$8$$$ people at a time. It will generate a "random" new MBTI for all individuals based on their initial MBTIs and a $$$32$$$-bit key $$$k$$$.
Kevin carefully studied this machine and found that the "random" MBTI it generates actually follows a certain pattern. The machine mainly includes three basic structures that shuffle the MBTI: "xor", "perm", and "mix". Each of these structures takes the MBTIs of $$$8$$$ individuals as input and outputs $$$8$$$ MBTIs as a result. In each structure, the input $$$8$$$ MBTIs are denoted as $$$in_0, in_1, in_2, \cdots, in_7$$$ and the output $$$8$$$ MBTIs are denoted as $$$out_0, out_1, out_2, \cdots, out_7$$$. For convenience, Kevin replaces each MBTI with a hexadecimal number according to the following table.
| INFP | INFJ | INTP | INTJ | ISFP | ISFJ | ISTP | ISTJ |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| ENFP | ENFJ | ENTP | ENTJ | ESFP | ESFJ | ESTP | ESTJ |
| 8 | 9 | a | b | c | d | e | f |
"xor" Structure:
The xor structure flips certain attributes of the MBTI based on the key $$$k$$$.
Let $$$k_j$$$ be the $$$j$$$-th binary bit of the key $$$k$$$ ($$$0 \leq j \lt 32$$$). The functionality of the xor structure can be expressed as: $$$$$$ out_i = in_i \oplus (\overline{k_{4i+3}k_{4i+2}k_{4i+1}k_{4i}})_2 $$$$$$ where $$$\oplus$$$ denotes the bitwise XOR$$$^{1}$$$.
"perm" Structure:
The perm structure replaces the MBTI, where each MBTI is replaced by another fixed MBTI.
The functionality of the perm structure can be expressed as: $$$$$$ out_i = p_{in_i} $$$$$$ where $$$p$$$ is a fixed array of length $$$16$$$ with the following values:
| $$$p_0$$$ | $$$p_1$$$ | $$$p_2$$$ | $$$p_3$$$ | $$$p_4$$$ | $$$p_5$$$ | $$$p_6$$$ | $$$p_7$$$ | $$$p_8$$$ | $$$p_9$$$ | $$$p_{10}$$$ | $$$p_{11}$$$ | $$$p_{12}$$$ | $$$p_{13}$$$ | $$$p_{14}$$$ | $$$p_{15}$$$ |
| 7 | 0 | 5 | 6 | b | f | 4 | 3 | 1 | d | c | e | 8 | 9 | a | 2 |
"mix" Structure:
The mix structure is used to mix the MBTIs of different people, with each output determined by two inputs.
The mix structure requires an intermediate array $$$t$$$ of length $$$8$$$, defined as: $$$$$$ t_i = \begin{cases} in_i \ll 1 & 0 \leq in_i \lt 8 \\ ((in_i \oplus 8) \ll 1) \oplus 3 & 8 \leq in_i \lt 16 \\ \end{cases} $$$$$$ where $$$\oplus$$$ denotes the bitwise XOR$$$^{1}$$$ and $$$\ll$$$ denotes logical left shift of four binary digits$$$^{2}$$$.
The functionality of the mix structure can be expressed as: $$$$$$ out_i = in_{i \ggg 1} \oplus (t_{(i \ggg 1) \oplus 1}) $$$$$$ where $$$\oplus$$$ denotes the bitwise XOR$$$^{1}$$$, and $$$\ggg$$$ denotes circular right shift of three binary digits$$$^{3}$$$.
The initial $$$8$$$ MBTIs will sequentially go through $$$19$$$ structures to produce the final output of $$$8$$$ MBTIs. These structures are: xor, perm, mix, xor, perm, mix, xor, perm, mix, xor, perm, mix, xor, perm, mix, xor, perm, mix, xor, which is six consecutive rounds of "xor, perm, mix" followed by an "xor".
Users of this machine typically hope to obtain a specific MBTI. Unfortunately, when Doraemon built this machine, he encapsulated the key $$$k$$$ inside the machine and refused to tell Kevin the value of the key. Thus, Kevin turned to you, the clever one. Kevin hopes you can construct some inputs (not exceeding $$$4096$$$, or the machine may be damaged), test run to obtain the corresponding outputs, and then analyze the value of the key. This way, he can write a program to predict the output for each input, allowing everyone to get their desired MBTI.
Can you accomplish this task?
$$$^{1}$$$ Bitwise XOR refers to performing the modulo $$$2$$$ addition operation on each corresponding bit of two binary numbers. For example: $$$(0011)_2$$$ $$$\oplus$$$ $$$(0101)_2$$$ $$$=$$$ $$$(0110)_2$$$.
$$$^{2}$$$ $$$x \ll y$$$ means to logically left shift the binary number $$$x$$$ by $$$y$$$ bits, discarding the overflow and filling the vacated parts with 0. For example: $$$(1100)_2$$$ $$$\ll$$$ $$$1$$$ $$$=$$$ $$$(1000)_2$$$, $$$(1001)_2$$$ $$$\ll$$$ $$$1$$$ $$$=$$$ $$$(0010)_2$$$.
$$$^{3}$$$ $$$x \ggg y$$$ means to circularly right shift the binary number $$$x$$$ by $$$y$$$ bits. For example: $$$(101)_2$$$ $$$\ggg$$$ $$$1$$$ $$$=$$$ $$$(110)_2$$$, $$$(110)_2$$$ $$$\ggg$$$ $$$1$$$ $$$=$$$ $$$(011)_2$$$.
To run the MBTI converter, please output "? $$$in_7in_6in_5in_4in_3in_2in_1in_0$$$" ($$$in_i \in \{0-9,a-f\}$$$), where $$$in_i$$$ is the hexadecimal value corresponding to the $$$i$$$-th input MBTI. You may perform this operation at most $$$4096$$$ times. After flushing your output, please read the output of the MBTI converter in the format "$$$out_7out_6out_5out_4out_3out_2out_1out_0$$$" ($$$out_i \in \{0-9,a-f\}$$$).
To answer for the value of $$$k$$$, please output "! $$$k$$$" ($$$0 \leq k \lt 2^{32}$$$), where $$$k$$$ is the decimal representation of the value you believe the key $$$k$$$ to be. You may perform this operation at most once. After performing the operation, you should exit your program immediately.
If you run the MBTI converter more than $$$4096$$$ times, or if your output contains invalid characters, the interactor will output "-1". Upon receiving such a response, you should immediately exit your program, and you will receive a "Wrong Answer" result. Otherwise, the result is uncertain, as you will attempt to read data from a closed output stream.
To flush your output, you can use:
acf7e10b 105092e0
? 04f37255 ? 9090cfca ! 998244353
The following information might be helpful when solving the problem:
Fried-chicken is a devoted player of Hearthstone. Since the game resumed operations in the Chinese mainland, he has been obsessed with it and reached Silver 2 rank in Standard mode. Today, while ranking up using Death Knight, he encountered a formidable opponent, Stewed-chicken, and was left with just $$$1$$$ Health. To survive, Fried-chicken must eliminate all of Stewed-chicken's minions. Fortunately, he can use spell cards and his minions' attacks to achieve this goal.
Specifically, this game involves two factions: Fried-chicken and Stewed-chicken. Each faction has some minions. The $$$i$$$-th minion has $$$h_i$$$ Health. It is now Fried-chicken's turn, and each of his minions can attack any one minion from the opposing faction at most once. When one minion attacks another, both minions lose $$$1$$$ Health. If a minion's Health is reduced to $$$0$$$ or less, it dies and can no longer attack or be attacked.
To achieve his goal, Fried-chicken casts the spell "Threads of Despair," causing every minion to explode upon death, which reduces the Health of all minions by $$$1$$$. If the explosion causes the death of other minions, other minions will also explode immediately. Fried-chicken cannot have his minions attack other minions until all explosion effects have finished. After casting the spell, Fried-chicken can make his minions attack Stewed-chicken's minions in any order he chooses. He wants to know if there exists an attack order that allows Fried-chicken to eliminate all of Stewed-chicken's minions.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 5 \times 10^5$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 5 \times 10^5$$$), representing the number of Fried-chicken's minions and Stewed-chicken's minions, respectively.
The second line contains $$$n$$$ integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \leq h_i \leq 10^9$$$), where $$$h_i$$$ represents the Health of Fried-chicken's $$$i$$$-th minion.
The third line contains $$$m$$$ integers $$$h'_1, h'_2, \dots, h'_m$$$ ($$$1 \leq h'_i \leq 10^9$$$), where $$$h'_i$$$ represents the Health of Stewed-chicken's $$$i$$$-th minion.
For each test file, it is guaranteed that the sum of all $$$n$$$ across all test cases does not exceed $$$5 \times 10^5$$$, and the sum of all $$$m$$$ across all test cases does not exceed $$$5 \times 10^5$$$.
For each test case, output "Yes" if Fried-chicken can eliminate all of Stewed-chicken's minions; otherwise, output "No".
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.
33 21 1 42 63 21 1 42 72 1100 1002
Yes No Yes
37 11 1 1 1 1 1 195 23 4 5 6 71 65 33 4 5 6 71 5 7
No No Yes
In the first test case of Sample 1, one possible sequence of actions is as follows: Fried-chicken's $$$3$$$rd minion attacks Stewed-chicken's $$$2$$$nd minion, followed by Fried-chicken's $$$2$$$nd minion attacking Stewed-chicken's $$$2$$$nd minion. At this point, Fried-chicken's $$$2$$$nd minion dies, triggering an explosion. This explosion causes further deaths, leading to a chain reaction of explosions. Eventually, all minions are eliminated.
In the third test case of Sample 1, one possible sequence of actions is as follows: Fried-chicken's $$$1$$$st minion attacks Stewed-chicken's $$$1$$$st minion, followed by Fried-chicken's $$$2$$$nd minion attacking Stewed-chicken's $$$1$$$st minion. At this point, Stewed-chicken's $$$1$$$st minion dies, triggering an explosion. Ultimately, Fried-chicken is left with two minions, each having $$$98$$$ Health, while all of Stewed-chicken's minions are eliminated. Fried-chicken survives successfully.
Mary loves constructing matrices!
Today, Mary wants to fill a permutation$$$^{\dagger}$$$ of length $$$n \times m$$$ into an $$$n \times m$$$ matrix $$$A$$$, such that the sum of any two adjacent elements is unique.
In other words, for any $$$1 \leq x_1,x_2,x_3,x_4 \leq n, 1 \leq y_1,y_2,y_3,y_4 \leq m$$$, if all of the following conditions are satisfied:
For example, when $$$n=2$$$ and $$$m=3$$$, matrix $$$B$$$ is a valid solution, while matrix $$$C$$$ is not valid because $$$C_{1,1}+C_{2,1}=C_{1,2}+C_{1,3}$$$.
$$$$$$ B = \begin{bmatrix} 1 & 3 & 2 \\ 6 & 5 & 4 \end{bmatrix}, \quad C = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} $$$$$$
Given $$$n$$$ and $$$m$$$, can all the conditions above be satisfied? If so, output a valid solution.
$$$^{\dagger}$$$ 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 a $$$4$$$ in the array).
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains two integers $$$n,m$$$ ($$$1 \leq n,m \leq 1000$$$).
For each test file, it is guaranteed that the sum of $$$n \times m$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output on the first line whether a valid solution exists. If there is a valid solution, output "Yes"; otherwise, output "No". 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.
If a valid solution exists, you must also output $$$n$$$ lines, each containing $$$m$$$ integers. The $$$j$$$-th number on the $$$i$$$-th line represents the number at row $$$i$$$ and column $$$j$$$ in the matrix. You must make sure that the output numbers form a permutation of length $$$n \times m$$$. If there are multiple solutions, you may print any of them.
21 12 3
yEs 1 YES 1 3 2 6 5 4