Kaori wants to spend the day with Shizuku! However, the zoo is closed, so they are visiting Farmer John's farm instead.
At Farmer John's farm, Shizuku counts $$$n$$$ legs. It is known that only chickens and cows live on the farm; a chicken has $$$2$$$ legs, while a cow has $$$4$$$.
Count how many different configurations of Farmer John's farm are possible. Two configurations are considered different if they contain either a different number of chickens, a different number of cows, or both.
Note that Farmer John's farm may contain zero chickens or zero cows.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$1\leq n \leq 100$$$).
For each test case, output a single integer, the number of different configurations of Farmer John's farm that are possible.
52346100
102226
For $$$n=4$$$, there are two possible configurations of Farmer John's farm:
For $$$n=3$$$, it can be shown that there are no possible configurations of Farmer John's farm.
Yuu is trying out the student council! Unfortunately, she is being forced to do clerical work... Touko wants her to fill out the blanks in various student council documents.
You are given a partially filled array of nonnegative integers $$$a_1, a_2, \dots, a_n$$$, where blank elements are denoted with $$$-1$$$. You would like to fill in the blank elements with nonnegative integers, such that the absolute value of the sum of the elements in its difference array is minimized.
More formally, let $$$b$$$ be the array of length $$$n-1$$$ such that $$$b_i = a_{i+1} - a_i$$$ for all $$$1\leq i\leq n-1$$$. Find the minimum possible value of $$$|b_1 + b_2 + \dots + b_{n-1}|$$$, across all possible ways to fill in the blank elements of $$$a$$$.
Additionally, output the array that achieves this minimum. If there are multiple such arrays, output the one that is lexicographically smallest$$$^{\text{∗}}$$$.
$$$^{\text{∗}}$$$For two arbitrary arrays $$$c$$$ and $$$d$$$ of length $$$n$$$, we say that $$$c$$$ is lexicographically smaller than $$$d$$$ if there exists an index $$$i$$$ ($$$1\leq i\leq n$$$) such that $$$c_j = d_j$$$ for all $$$j \lt i$$$, and $$$c_i \lt d_i$$$. In other words, $$$c$$$ and $$$d$$$ differ in at least one index, and at the first index at which they differ, $$$c_i$$$ is smaller than $$$d_i$$$.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$a_1, a_2, \dots, a_n$$$ ($$$-1\leq a_i \leq 10^6$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, on the first line, output the minimum possible value of $$$|b_1 + b_2 + \dots + b_{n-1}|$$$. Then, on the second line, output $$$n$$$ integers, the values of $$$a_1, a_2, \dots, a_n$$$ in the lexicographically smallest array achieving this minimum.
642 -1 7 14-1 2 4 -182 -1 1 5 11 12 1 -13-1 -1 -132 5 42-1 5
12 0 7 100 2 4 002 0 1 5 11 12 1 200 0 022 5 405 5
In the first example, we fill in the array $$$a = [2, 0, 7, 1]$$$, which yields the difference array $$$b = [-2, 7, -6]$$$.
The absolute value of the sum of the elements in $$$b$$$ is $$$1$$$. It can be proven that this is the minimum possible. Furthermore, it can be proven that this is the lexicographically smallest array $$$a$$$ that achieves this minimum.
This is the easy version of the problem. The only difference between the easy and hard versions is that in the easy version, $$$a_i, b_i \leq 1$$$.
Renako is stuck between a rock and a hard place... and by that, of course, I mean Ajisai and Mai! Both of them want to hang out with her, and she just can't decide! So Ajisai and Mai have decided to play the XOR game.
Ajisai and Mai are given arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ ($$$0\leq a_i, b_i\leq {\color{red}{1}}$$$). They will play a game that lasts for $$$n$$$ turns, where Ajisai moves on odd-numbered turns and Mai moves on even-numbered turns. On the $$$i$$$-th turn, the player to move may choose to swap $$$a_i$$$ and $$$b_i$$$, or pass.
Note that if a swap occurs, the index that is being swapped must match the turn number. For example, on the first turn, Ajisai may choose to swap $$$a_1$$$ and $$$b_1$$$, or pass. On the second turn, Mai may choose to swap $$$a_2$$$ and $$$b_2$$$, or pass. This continues for $$$n$$$ turns. Thus, only Ajisai can swap odd indices, and only Mai can swap even indices.
At the end of the game, Ajisai achieves a score of $$$a_1 \oplus a_2 \oplus \dots \oplus a_n$$$, and Mai achieves a score of $$$b_1 \oplus b_2 \oplus \dots \oplus b_n$$$$$$^{\text{∗}}$$$. The player with the higher score wins. If the players have the same score, the game ends in a tie.
Determine the outcome of the game with optimal play. More formally, one player is considered to win with optimal play if there exists a strategy for them such that they always win, regardless of their opponent's choices. The game is considered a tie with optimal play if neither player has such a strategy.
$$$^{\text{∗}}$$$$$$\oplus$$$ denotes the bitwise XOR operation
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$a_1, a_2, \dots, a_n$$$ ($$$0\leq a_i\leq 1$$$).
The third line of each test case contains $$$n$$$ integers, $$$b_1, b_2, \dots, b_n$$$ ($$$0\leq b_i\leq 1$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output on a single line "Ajisai" if Ajisai wins with optimal play, "Mai" if Mai wins with optimal play, or "Tie" if the game ends in a tie with optimal play.
You may output the answer in any case (upper or lower). For example, the strings "mAi", "mai", "MAI", and "maI" will be recognized as "Mai".
641 0 0 11 0 1 160 1 1 1 1 00 0 1 0 1 140 0 1 01 0 1 151 0 1 1 10 1 1 1 061 1 1 1 1 11 1 1 1 1 150 1 0 0 11 0 0 1 1
AjisaiMaiTieAjisaiTieMai
In the first example, one way the game might play out is as follows:
On turn 1, Ajisai chooses to swap $$$a_1$$$ and $$$b_1$$$. Now the arrays are $$$a = [1, 0, 0, 1]$$$ and $$$b = [1, 0, 1, 1]$$$.
On turn 2, Mai chooses to pass.
On turn 3, Ajisai chooses to swap $$$a_3$$$ and $$$b_3$$$. Now the arrays are $$$a = [1, 0, 1, 1]$$$ and $$$b = [1, 0, 0, 1]$$$.
On turn 4, Mai chooses to swap $$$a_4$$$ and $$$b_4$$$. Now the arrays are $$$a = [1, 0, 1, 1]$$$ and $$$b = [1, 0, 0, 1]$$$.
Now, Ajisai's final score is $$$1\oplus 0\oplus 1\oplus 1 = 1$$$ and Mai's final score is $$$1\oplus 0\oplus 0\oplus 1 = 0$$$. Therefore, Ajisai wins the game.
It is not guaranteed that the above description is representative of optimal play.
This is the hard version of the problem. The only difference between the easy and hard versions is that in the hard version, $$$a_i, b_i \leq 10^6$$$.
Renako is stuck between a rock and a hard place... and by that, of course, I mean Ajisai and Mai! Both of them want to hang out with her, and she just can't decide! So Ajisai and Mai have decided to play the XOR game.
Ajisai and Mai are given arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ ($$$0\leq a_i, b_i\leq {\color{red}{10^6}}$$$). They will play a game that lasts for $$$n$$$ turns, where Ajisai moves on odd-numbered turns and Mai moves on even-numbered turns. On the $$$i$$$-th turn, the player to move may choose to swap $$$a_i$$$ and $$$b_i$$$, or pass.
Note that if a swap occurs, the index that is being swapped must match the turn number. For example, on the first turn, Ajisai may choose to swap $$$a_1$$$ and $$$b_1$$$, or pass. On the second turn, Mai may choose to swap $$$a_2$$$ and $$$b_2$$$, or pass. This continues for $$$n$$$ turns. Thus, only Ajisai can swap odd indices, and only Mai can swap even indices.
At the end of the game, Ajisai achieves a score of $$$a_1 \oplus a_2 \oplus \dots \oplus a_n$$$, and Mai achieves a score of $$$b_1 \oplus b_2 \oplus \dots \oplus b_n$$$$$$^{\text{∗}}$$$. The player with the higher score wins. If the players have the same score, the game ends in a tie.
Determine the outcome of the game with optimal play. More formally, one player is considered to win with optimal play if there exists a strategy for them such that they always win, regardless of their opponent's choices. The game is considered a tie with optimal play if neither player has such a strategy.
$$$^{\text{∗}}$$$$$$\oplus$$$ denotes the bitwise XOR operation
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$a_1, a_2, \dots, a_n$$$ ($$$0\leq a_i\leq 10^6$$$).
The third line of each test case contains $$$n$$$ integers, $$$b_1, b_2, \dots, b_n$$$ ($$$0\leq b_i\leq 10^6$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output on a single line "Ajisai" if Ajisai wins with optimal play, "Mai" if Mai wins with optimal play, or "Tie" if the game ends in a tie with optimal play.
You may output the answer in any case (upper or lower). For example, the strings "mAi", "mai", "MAI", and "maI" will be recognized as "Mai".
641 4 6 13 2 3 7620 11 1 7 7 014 8 3 6 17 642 6 3 63 4 7 151 4 5 5 36 7 1 2 1369 5 9 17 17 61 13 6 13 1 1552 3 8 1 53 1 6 14 7
MaiAjisaiTieAjisaiMaiTie
In the first example, one way the game might play out is as follows:
On turn 1, Ajisai chooses to swap $$$a_1$$$ and $$$b_1$$$. Now the arrays are $$$a = [3, 4, 6, 1]$$$ and $$$b = [1, 2, 3, 7]$$$.
On turn 2, Mai chooses to swap $$$a_2$$$ and $$$b_2$$$. Now the arrays are $$$a = [3, 2, 6, 1]$$$ and $$$b = [1, 4, 3, 7]$$$.
On turn 3, Ajisai chooses to pass.
On turn 4, Mai chooses to swap $$$a_4$$$ and $$$b_4$$$. Now the arrays are $$$a = [3, 2, 6, 7]$$$ and $$$b = [1, 4, 3, 1]$$$.
Now, Ajisai's final score is $$$3\oplus 2\oplus 6\oplus 7 = 0$$$ and Mai's final score is $$$1\oplus 4\oplus 3\oplus 1 = 7$$$. Therefore, Mai wins the game.
It is not guaranteed that the above description is representative of optimal play.
For additional examples, see the examples of the easy version.
This is the easy version of the problem. The only difference between the easy and hard versions is that the hard version asks you to construct an example of a satisfactory tree.
As an Earth mage, Rae has mastered the spell of growing trees! But Manaria brags that she can grow a more impressive species of trees. Rae remembers that the most rare type of tree can be grown using a formula represented by a certain permutation — please help her construct it!
You are given a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$.
Determine if there exists an undirected tree with $$$n$$$ vertices labeled $$$1, 2, \dots, n$$$, satisfying the following condition:
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array that contains every integer from $$$1$$$ to $$$n$$$ exactly once, in any order.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$p_1, p_2, \dots, p_n$$$ ($$$1\leq p_i\leq n$$$). It is guaranteed that all $$$p_i$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.
You may output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "YES", and "yeS" will be recognized as "Yes".
961 3 4 5 2 643 4 1 254 3 5 1 241 2 3 474 3 5 7 6 2 162 4 6 1 3 532 1 342 4 1 364 2 6 5 1 3
YesNoNoYesNoYesYesYesYes
In the first example, we can construct the tree with the following edges:
Then we have that
In the second example, it can be shown that there does not exist a tree satisfying the given constraints.
Anis and her new assistant Euphie are improving the Witch's Broom! Magicology requires great precision and care — in order to fly, the construction of the broom must have sufficiently few imperfections.
For an arbitrary array $$$a$$$ of length $$$m$$$, call an index $$$i$$$ ($$$1\leq i\leq m-2$$$) bad if $$$a_i$$$, $$$a_{i+1}$$$, and $$$a_{i+2}$$$ are all pairwise coprime. More formally, $$$i$$$ is a bad index if and only if $$$\gcd(a_i, a_{i+1}) = \gcd(a_i, a_{i+2}) = \gcd(a_{i+1}, a_{i+2}) = 1$$$$$$^{\text{∗}}$$$. Furthermore, call $$$a$$$ good if it has at most $$$6$$$ bad indices.
You are given an integer $$$n$$$. Construct a good permutation$$$^{\text{†}}$$$ $$$p$$$ of length $$$n$$$. It can be shown that such a permutation always exists.
Note that you do not have to minimize the number of bad indices.
$$$^{\text{∗}}$$$$$$\gcd(x, y)$$$ denotes the greatest common divisor of $$$x$$$ and $$$y$$$
$$$^{\text{†}}$$$ A permutation of length $$$n$$$ is an array that contains every integer from $$$1$$$ to $$$n$$$ exactly once, in any order.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$3\leq n \leq 2\cdot 10^5$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output on a single line $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$, an example of a good permutation of length $$$n$$$. If there are multiple good permutations, you may output any of them.
43689
2 1 3 4 1 6 3 5 2 4 1 6 3 5 2 8 7 5 4 8 1 9 3 6 2 7
For $$$n=9$$$, we have
| $$$i$$$ | $$$p_i$$$ | $$$p_{i+1}$$$ | $$$p_{i+2}$$$ | $$$\gcd(p_i, p_{i+1})$$$ | $$$\gcd(p_i, p_{i+2})$$$ | $$$\gcd(p_{i+1}, p_{i+2})$$$ |
| $$$1$$$ | $$$5$$$ | $$$4$$$ | $$$8$$$ | $$$1$$$ | $$$1$$$ | $$$4$$$ |
| $$$2$$$ | $$$4$$$ | $$$8$$$ | $$$1$$$ | $$$4$$$ | $$$1$$$ | $$$1$$$ |
| $$$3$$$ | $$$8$$$ | $$$1$$$ | $$$9$$$ | $$$1$$$ | $$$1$$$ | $$$1$$$ |
| $$$4$$$ | $$$1$$$ | $$$9$$$ | $$$3$$$ | $$$1$$$ | $$$1$$$ | $$$3$$$ |
| $$$5$$$ | $$$9$$$ | $$$3$$$ | $$$6$$$ | $$$3$$$ | $$$3$$$ | $$$3$$$ |
| $$$6$$$ | $$$3$$$ | $$$6$$$ | $$$2$$$ | $$$3$$$ | $$$1$$$ | $$$2$$$ |
| $$$7$$$ | $$$6$$$ | $$$2$$$ | $$$7$$$ | $$$2$$$ | $$$1$$$ | $$$1$$$ |
The only bad index is $$$3$$$. Since $$$1\leq 6$$$, $$$p$$$ is a good permutation.
This is the hard version of the problem. The only difference between the easy and hard versions is that the hard version asks you to construct an example of a satisfactory tree.
As an Earth mage, Rae has mastered the spell of growing trees! But Manaria brags that she can grow a more impressive species of trees. Rae remembers that the most rare type of tree can be grown using a formula represented by a certain permutation — please help her construct it!
You are given a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$.
Determine if there exists an undirected tree with $$$n$$$ vertices labeled $$$1, 2, \dots, n$$$, satisfying the following condition:
Additionally, if there exists such a tree, output any of them.
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array that contains every integer from $$$1$$$ to $$$n$$$ exactly once, in any order.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$p_1, p_2, \dots, p_n$$$ ($$$1\leq p_i\leq n$$$). It is guaranteed that all $$$p_i$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output on a single line "Yes" if there exists a tree satisfying the given condition, and "No" otherwise.
Then, if the answer is "Yes", output $$$n-1$$$ lines. The $$$i$$$-th of these lines should contain two integers $$$u$$$ and $$$v$$$, denoting an edge connecting vertices $$$u$$$ and $$$v$$$.
You may output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "YES", and "yeS" will be recognized as "Yes".
961 3 4 5 2 643 4 1 254 3 5 1 241 2 3 474 3 5 7 6 2 162 4 6 1 3 532 1 342 4 1 364 2 6 5 1 3
Yes 3 1 4 1 6 5 6 2 6 1 No No Yes 2 1 4 3 4 1 No Yes 4 2 6 2 3 1 5 1 5 2 Yes 3 2 3 1 Yes 4 2 3 1 3 2 Yes 6 4 6 2 3 1 5 4 2 3
In the first example, we can construct the tree given in the sample output. We have that
In the second example, it can be shown that there does not exist a tree satisfying the given constraints.
Adachi is in the middle of a generational crashout... over, uh, this problem! Yep, that's definitely it... anyways, please help her solve it!
You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ ($$$1\leq a_i\leq b_i$$$).
In one operation, you may either:
Let $$$x$$$ denote the minimum number of operations needed to make $$$a = b$$$. Two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ are considered equal if $$$a_i = b_i$$$ for all $$$1\leq i\leq n$$$.
Find the value of $$$x$$$. Additionally, count the number of sequences of operations that make $$$a = b$$$ using exactly $$$x$$$ operations. Two such sequences of operations are considered different if, for any $$$1\leq j\leq x$$$, the $$$j$$$-th operation of each sequence differs (either in the type of operation selected or the index chosen, if applicable).
Since the number of sequences may be large, output it modulo $$${\color{red}{10^6+3}}$$$. Note that $$$10^6+3$$$ is a prime number.
The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers, $$$a_1, a_2, \dots, a_n$$$ ($$$1\leq a_i\leq 10^6$$$).
The third line of each test case contains $$$n$$$ integers, $$$b_1, b_2, \dots, b_n$$$ ($$$a_i\leq b_i\leq 10^6$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output two integers, the value of $$$x$$$, and the number of sequences of operations that make $$$a = b$$$ using exactly $$$x$$$ operations, modulo $$$10^6+3$$$. The value of $$$x$$$ should be printed exactly; that is, it should not be taken modulo $$$10^6 + 3$$$.
861 3 6 4 3 23 7 10 4 4 821 14 352 3 2 5 118 13 10 30 755 4 3 6 2100 125 231 113 10742 2 2 22 2 2 241 1 1 12 2 2 271 1 1 1 1 1 200000200000 200000 200000 200000 200000 200000 2000003542264 174876 441510641112 325241 995342
17 8271163 112 28835 5678120 11 11199994 0803045 366998
In the second sample, it is possible to convert $$$a$$$ into $$$b$$$ using only three operations. There is only one way to do so, namely:
It can be shown that it is impossible to convert $$$a$$$ into $$$b$$$ using fewer than three operations.
For the cost of 5000 yen, Miyagi can ask Sendai to do whatever she wants! Today, Miyagi demands that Sendai make her an array... specifically, Miyagi only wants a very particular type of array.
For arbitrary integers $$$b\geq 2$$$ and $$$x\geq 1$$$, define $$$v(b, x)$$$ to be the maximal $$$k$$$ satisfying $$$b^k \mid x$$$; that is, the maximal $$$k$$$ such that $$$x$$$ is a multiple of $$$b^k$$$. It can be shown that this is always a well-defined, nonnegative integer.
You are given integers $$$n$$$ and $$$m$$$ satisfying $$$n\leq m$$$. Find the maximum value of $$$\sum_{i=2}^n v(i, a_i)$$$ across all arrays $$$a$$$ of length $$$n$$$ satisfying the following conditions:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2\leq n\leq m \leq 2\cdot 10^5$$$).
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output a single integer, the maximum value of $$$\sum_{i=2}^n v(i, a_i)$$$ across all arrays $$$a$$$ of length $$$n$$$ satisfying the given conditions.
64 206 66 2163 5002 85 29
75191339
In the first example, one possible array is $$$a = [6, 8, 9, 16]$$$ which yields a value of $$$3 + 2 + 2 = 7$$$.
It can be shown that this is the maximum value of $$$\sum_{i=2}^n v(i, a_i)$$$ across all arrays $$$a$$$ of length $$$n$$$ satisfying the given conditions.