Busy Beaver has $$$N$$$ bins and a large amount of balls. The $$$i$$$-th bin can hold up to $$$s_i$$$ balls and currently contains $$$a_i$$$ balls. In a move, he first chooses a bin $$$i$$$ that is currently full (i.e., $$$a_i = s_i$$$). Then, for each ball in the chosen bin, he chooses to either discard it or move it to a different bin with enough space. (Within the same move, it is allowed to move different balls to different bins.)
Using only these moves, Busy Beaver is trying to remove all of the balls from all of the bins. Determine whether or not it is possible to do so.
Each test contains multiple test cases. The first line of input contains a single integer $$$T$$$ $$$(1\le T\le 10^5)$$$, the number of test cases. The description of each test case follows.
The first line of each test case contains a single positive integer $$$N$$$ $$$(1\le N\le 5\cdot 10^5)$$$.
The second line contains $$$N$$$ integers $$$a_1,a_2,\dots, a_N$$$ $$$(1\le a_i\le 10^9)$$$ — the number of balls currently in each bin.
The third line contains $$$N$$$ integers $$$s_1,s_2,\dots, s_N$$$ $$$(1\le s_i\le 10^9,a_i\le s_i)$$$ — the number of balls each bin can hold.
It is guaranteed that the sum of $$$N$$$ across all test cases does not exceed $$$5\cdot 10^5$$$.
For each test case, output "YES" (without quotes) if all bins may be emptied, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
232 1 52 3 832 3 52 5 11
YES NO
In the first test case, Busy Beaver can move all the balls in the first bin to the second bin. Then he will have exactly $$$1 + 2 = 3$$$ balls in the second bin, making the second bin full.
Then he can move all the balls from the second bin to the third bin. Then he will have exactly $$$5 + 3 = 8$$$ balls in the third bin, making the third bin full.
Finally, he can throw all the balls in the third bin away.
In the second test case, it can be shown that it is impossible to discard all the balls.
Busy Beaver has just learned about the Median of Medians algorithm! To better understand the algorithm, he has chosen an odd positive integer $$$N$$$ and wants to experiment with permutations of $$$\{1,2,\dots,3N\}$$$.$$$^{\text{∗}}$$$
For any odd number $$$k$$$ and a sequence of distinct numbers $$$A = (a_1,a_2,\dots,a_k)$$$, let $$$B = (b_1,b_2,\dots,b_k)$$$ be $$$A$$$ sorted in increasing order. Then, $$$\text{median}(a_1,a_2,\dots,a_k)$$$ is $$$b_{\frac{k+1}{2}}$$$, the $$$\left(\frac{k+1}{2}\right)$$$-th element of $$$B$$$.
Busy Beaver thinks that a permutation of $$$(p_1,p_2,\dots,p_{3N})$$$ of $$$\{1,2,\dots,3N\}$$$ is nice if and only if
$$$$$$ \text{median}\Big(\text{median}(p_1, p_2, \dots, p_N),\text{median}(p_{N+1}, p_{N+2},\dots,p_{2N}),\text{median}(p_{2N+1},\dots,p_{3N})\Big) = \frac{3N+1}{2}. $$$$$$
Busy Beaver is extra picky with his permutations; he likes having certain numbers in certain positions. He has $$$M$$$ pairs of numbers $$$(a_i,b_i)$$$. A nice permutation $$$(p_1,p_2,\dots,p_{3N})$$$ is fitting if $$$p_{a_i} = b_i$$$ for all $$$1 \leq i \leq M$$$.
Help Busy Beaver determine the number of fitting permutations! Since the number of such permutations may be very large, compute the number of such permutations modulo $$$10^9 + 7$$$.
$$$^{\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).
Each test contains multiple test cases. The first line of input contains a single positive integer $$$T$$$, the number of test cases $$$(1 \leq T \leq 10^5)$$$. The description of each test case follows.
The first line of each test case contains two spaced positive integers $$$N$$$ and $$$M$$$ ($$$1 \leq N \leq 3 \cdot 10^5$$$, $$$0 \leq M \leq 3N$$$, and $$$N$$$ is odd) — the size of the permutation, and the number of pairs $$$(a_i, b_i)$$$, respectively.
The next $$$M$$$ lines contain two positive integers $$$a_i, b_i$$$ ($$$1 \leq a_i, b_i \leq 3N$$$) — specifying that $$$p_{a_i} = b_i$$$. It is guaranteed that for all $$$1 \leq i \lt j \leq M$$$, $$$a_i \neq a_j$$$ and $$$b_i \neq b_j$$$.
It is guaranteed that the sum of $$$M$$$ across all test cases is no more than $$$10^6$$$.
Note that there is no additional guarantee on the sum of $$$N$$$ across all test cases.
For each test case, output one line with a single integer, indicating the number of fitting permutations modulo $$$10^9 + 7$$$.
There are four subtasks for this problem.
41 03 91 12 23 34 45 56 67 78 89 93 64 45 56 67 78 89 93 31 12 23 5
6 1 6 0
In the first test case, $$$N=1$$$, so we are working with permutations of length $$$3N = 3$$$. Since $$$M=0$$$, we have no additional constraints on the permutation. One can check that for all permutations of length $$$3$$$, computing the median of the medians gives the correct median of $$$2$$$, so there are $$$6$$$ fitting permutations.
In the second test case, $$$N=3$$$, so we are working with permutations of length $$$3N = 9$$$. Since $$$M=9$$$, we have $$$9$$$ constraints on the permutation, which have fixed all $$$9$$$ elements of the permutation to $$$(1,2,3,4,5,6,7,8,9)$$$.
In the third test case, $$$N=3$$$, so we are working with permutations of length $$$3N = 9$$$. Since $$$M=6$$$, we have $$$6$$$ constraints on the permutation, which have fixed the last $$$6$$$ elements of the permutation to $$$(4,5,6,7,8,9)$$$. We are free to permute $$$(1,2,3)$$$ among the first $$$3$$$ elements. Using a similar analysis as the second test case, we see that the following $$$6$$$ permutations
In the fourth test case, $$$N=3$$$, so we are working with permutations of length $$$3N = 9$$$. Since $$$M=3$$$, we have $$$3$$$ constraints on the permutation, which have fixed the first $$$3$$$ elements of the permutation to $$$(1,2,5)$$$. It can be checked that there are no fitting permutations that satisfy these constraints.
Busy Beaver is taking a walk on a path of colorful tiles.
The path consists of a line of $$$N$$$ tiles, each colored either red (r), green (g), blue (b), or yellow (y).
Busy Beaver's walk will follow these rules:
Busy Beaver will record the sequence of tile colors he visits on the walk in order. Busy Beaver is confident that he can recreate any sequence of $$$N$$$ colors on his walk.
Prove him wrong by providing any sequence of $$$N$$$ colors he can't recreate.
It can be shown that an answer always exists.
Each test 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$$$ ($$$3 \leq N \leq 3000$$$) — the number of tiles.
The second line of each test case contains a length $$$N$$$ string of characters in ('r', 'g', 'b', 'y'), the $$$i$$$'th character denoting the color of the $$$i$$$'th tile.
It is guaranteed that the sum of $$$N$$$ across all test cases is no more than $$$3 \cdot 10^5$$$.
For each test case, output the answer-sequence as a string of characters in ('r', 'g', 'b', 'y').
There are three subtasks for this problem.
3 3 rgb 3 gby 7 rgbybgr
yyy rrr yryryry
In the first test case, yellow never appears, so a sequence of $$$3$$$ yellows is not possible.
In the second test case, red never appears, so a sequence of $$$3$$$ reds is not possible.
In the third test case, every red tile is at least $$$3$$$ positions away from the yellow tile. So, any walk transitioning from a yellow to a red tile is impossible.
While studying for his algorithms midterm, Busy Beaver came up with the following problem and wants your help to solve it.
You are given an integer $$$N$$$ and a permutation $$$p_1,p_2,\ldots,p_N$$$ of length $$$N$$$.$$$^{\text{∗}}$$$ Find the length of the longest contiguous subarray ($$$l,r$$$) ($$$1 \le l \le r \le N$$$) of $$$p$$$ such that the number of inversions$$$^{\text{†}}$$$ of $$$p_l,p_{l+1},\ldots,p_r$$$ is not divisible by $$$K$$$, or determine if such a subarray does not exist.
Help Busy Beaver find the answer to this problem!
$$$^{\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{†}}$$$An inversion in a permutation p is a pair of indices $$$(i, j)$$$ such that $$$i \gt j$$$ and $$$p_i \lt p_j$$$. For example, a permutation $$$[4, 1, 3, 2]$$$ contains 4 inversions: $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(4, 1)$$$, $$$(4, 3)$$$.
Each test contains multiple test cases. The first line of input contains a single positive integer $$$T$$$, the number of test cases $$$(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$$$ ($$$2 \le K \le N \le 2 \cdot 10^5$$$).
The second line of each test case contains $$$N$$$ distinct positive integers $$$p_1,p_2,\ldots,p_N$$$ ($$$1 \le p_i \le N$$$).
It is guaranteed that the sum of $$$N$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one line with a single integer, indicating the length of the longest contiguous subarray of $$$p$$$ such that the number of inversions of the subarray is not divisible by $$$K$$$. If such a subarray does not exist, output $$$-1$$$.
There are two subtasks for this problem.
44 31 3 2 45 51 2 3 4 57 23 1 4 2 6 5 77 27 5 1 4 6 2 3
4 -1 5 5
In the first test case, the number of inversions of $$$[1,3,2,4]$$$ is $$$1$$$ (the second and third elements form the only inversion pair). Since $$$1$$$ is not divisible by $$$3$$$, the longest contiguous subarray whose number of inversions isn't divisible by $$$5$$$ is the entire array itself. Thus, the answer to the test case is the length of the entire array, which is $$$4$$$.
In the second test case, the number of inversions of each contiguous subarray is $$$0$$$ because the array is sorted. Since no contiguous subarray has an inversion count not divisible by $$$5$$$, the answer is $$$-1$$$.
In the third test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by $$$2$$$ is $$$[3, 1, 4, 2, 6]$$$, which has $$$3$$$ inversions.
In the fourth test case, it can be shown that the longest contiguous subarray whose number of inversions is not divisible by $$$2$$$ is $$$[5, 1, 4, 6, 2]$$$, which has $$$5$$$ inversions.
Let $$$K$$$, $$$P$$$, and $$$X$$$ be integers, where $$$P$$$ is prime and $$$0 \le K \le \min(P-1, 8)$$$.
Define a sequence $$$a$$$ of rational numbers as follows:
Output $$$a_{P-K}$$$ modulo $$$P$$$ (note the unusual modulo). Formally, let $$$a_{P-K} = \frac{x}{y}$$$ in lowest terms, and output an integer $$$0 \le b \lt P$$$ such that $$$by \equiv x \pmod{P}$$$. We can show that such a $$$b$$$ exists and is unique under the constraints of this problem.
We recommend that C++ users use the following code, from KACTL, to perform modulo operations faster. Note that creating FastMod instances is a relatively slow operation, so avoid repeatedly doing so for the same modulo.
typedef unsigned long long ull;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(-1ULL / b) {}
ull reduce(ull a) {
ull q = (ull)((__uint128_t(m) * a) >> 64), r = a - q * b;
return r - (r >= b) * b;
}
};
Each test contains multiple test cases. The first line of input contains a single integer $$$T$$$ $$$(1 \leq T \leq 200)$$$, the number of test cases. The description of each test case follows.
Each test case consists of one line of input with three integers $$$K$$$, $$$P$$$, and $$$X$$$ ($$$\mathbf{0 \le K \le \min(P-1, 8)}$$$, $$$3 \le P \lt 2 \cdot 10^7$$$, $$$1 \le X \le 10^9$$$, $$$P$$$ is prime).
It is guaranteed that the sum of $$$P$$$ over all test cases does not exceed $$$2 \cdot 10^7$$$.
For each test case, output a line with a single integer: $$$a_{P-K}$$$ modulo $$$P$$$.
22 5 38 199999 123456789
3 42180
In the first test case, we have $$$K = 2$$$, $$$P = 5$$$, $$$X = 3$$$, and we want to find $$$a_{P-K} = a_3$$$.
We may evaluate the initial elements of $$$a$$$ as follows:
Since $$$2 \cdot 3 \equiv 21 \pmod{5}$$$, the answer is $$$3 \pmod 5$$$.