MITIT Spring 2025 Qualification Round 2
A. Balls and Bins
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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$$$.

Output

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).

Example
Input
2
3
2 1 5
2 3 8
3
2 3 5
2 5 11
Output
YES
NO
Note

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.

B. Median of Medians
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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.

Output

For each test case, output one line with a single integer, indicating the number of fitting permutations modulo $$$10^9 + 7$$$.

Scoring

There are four subtasks for this problem.

  • ($$$10$$$ points): $$$N \leq 3, T \leq 10$$$.
  • ($$$25$$$ points): $$$N \leq 10^3$$$.
  • ($$$20$$$ points): $$$M = 0$$$.
  • ($$$45$$$ points): No additional constraints.
Example
Input
4
1 0
3 9
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
3 6
4 4
5 5
6 6
7 7
8 8
9 9
3 3
1 1
2 2
3 5
Output
6
1
6
0
Note

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)$$$.

  • The median of the first $$$3$$$ elements $$$(1,2,3)$$$ is $$$2$$$.
  • The median of the middle $$$3$$$ elements $$$(4,5,6)$$$ is $$$5$$$.
  • The median of the last $$$3$$$ elements $$$(7,8,9)$$$ is $$$8$$$.
The median of $$$(2,5,8)$$$ is $$$5$$$, which is the correct median. Thus, there is exactly $$$1$$$ fitting permutation, which is $$$(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

  • $$$(1,2,3,4,5,6,7,8,9)$$$
  • $$$(1,3,2,4,5,6,7,8,9)$$$
  • $$$(2,1,3,4,5,6,7,8,9)$$$
  • $$$(2,3,1,4,5,6,7,8,9)$$$
  • $$$(3,1,2,4,5,6,7,8,9)$$$
  • $$$(3,2,1,4,5,6,7,8,9)$$$
are all fitting 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.

C. Busy Beaver's Colorful Walk
time limit per test
2 с
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • The walk visits $$$N$$$ tiles, starting from any initial tile.
  • Each move must be to a tile at most two positions away from the current tile (possibly revisiting a previously visited tile, going backwards, or staying at the same tile).

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.

Input

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$$$.

Output

For each test case, output the answer-sequence as a string of characters in ('r', 'g', 'b', 'y').

Scoring

There are three subtasks for this problem.

  • ($$$10$$$ points): $$$N \leq 10, T \leq 10^3$$$.
  • ($$$10$$$ points): $$$N \leq 100, T \leq 10^3$$$.
  • ($$$80$$$ points): No additional constraints.
Example
Input
3
3
rgb
3
gby
7
rgbybgr
Output
yyy
rrr
yryryry
Note

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.

D. Indivisible Inversions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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)$$$.

Input

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$$$.

Output

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$$$.

Scoring

There are two subtasks for this problem.

  • ($$$10$$$ points): The sum of $$$N$$$ across all test cases is no more than $$$2 \cdot 10^3$$$.
  • ($$$90$$$ points): No additional constraints.
Example
Input
4
4 3
1 3 2 4
5 5
1 2 3 4 5
7 2
3 1 4 2 6 5 7
7 2
7 5 1 4 6 2 3
Output
4
-1
5
5
Note

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.

E. Sequence Evaluation
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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:

  • $$$a_1 = 1$$$.
  • For all $$$n \ge 2$$$, $$$a_n = X \sum_{i=1}^{n-1} \frac{a_i}{n-i}$$$.

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;
}
};
Input

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$$$.

Output

For each test case, output a line with a single integer: $$$a_{P-K}$$$ modulo $$$P$$$.

Scoring
  • ($$$25$$$ points): The sum of $$$P$$$ over all test cases does not exceed $$$10^5$$$.
  • ($$$75$$$ points): No additional constraints.
Example
Input
2
2 5 3
8 199999 123456789
Output
3
42180
Note

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:

  • $$$a_1 = 1$$$.
  • $$$a_2 = Xa_1 = 3$$$.
  • $$$a_3 = X(a_1/2 + a_2) = 3(7/2) = 21/2$$$.

Since $$$2 \cdot 3 \equiv 21 \pmod{5}$$$, the answer is $$$3 \pmod 5$$$.