2025 USACO.Guide Informatics Tournament
A. Shortest Substring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For a string $$$s$$$, let $$$f(s)$$$ be the string obtained by merging possibly several consecutive equal characters into a single character. For example, $$$f(abbbccdc) = abcdc$$$.

Given a string $$$a$$$, find the shortest substring $$$b$$$ of $$$a$$$ such that $$$f(a)$$$ is equal to $$$f(b)$$$.

Input

The first line will have $$$t$$$, the number of test cases – ($$$1 \le t \le 50$$$).

The first line of each test case will have $$$n$$$, the length of string $$$a$$$ – ($$$1 \le n \le 100$$$).

The second line of each test case will have string $$$a$$$ of length $$$n$$$ which contains only lower case characters from $$$a$$$ to $$$z$$$.

Output

For each test case, output a single line with string $$$b$$$.

Example
Input
2
8
abbbccdc
3
zzz
Output
abbbccdc
z

B. Alya and Permutation Revisited
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given integers $$$n$$$ and $$$k$$$, construct a permutation $$$p$$$ of integers $$$1 ... n$$$ that maximizes the value of $$$k$$$ after the following process.

Perform $$$n$$$ operations, on the $$$i$$$-th operation ($$$i = 1, 2, ..., n$$$)

  • It if $$$i$$$ is odd, $$$k := p_i \cdot k$$$.
  • if $$$i$$$ is even, $$$k := \lfloor \frac{k}{p_i} \rfloor$$$.
Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) – the number of test cases.

The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^5$$$).

The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output

For each test case, output the permutation $$$p_1, p_2, ..., p_n$$$ on a single line.

If there are multiple such permutations, output any.

Example
Input
2
1 3
2 4
Output
1 
2 1 
Note

In the first test case, $$$n = 1$$$, so there is only one permutation.

In the second test case, it is optimal to multiply $$$k$$$ by $$$2$$$ first and then divide it by $$$1$$$.

C. Combination Lock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given $$$3$$$ integers $$$x$$$, $$$y$$$, and $$$z$$$, you can perform three types of operations on them.

Swap the values of any $$$2$$$ of them ($$$x$$$ and $$$y$$$ or $$$x$$$ and $$$z$$$ or $$$y$$$ and $$$z$$$).

Choose one of them and increase the value of it by $$$1$$$

Choose one of them that is greater than $$$1$$$ and decrease the value of it by $$$1$$$.

Whats the minimum number of operations to make $$$x$$$ a factor of both $$$y$$$ and $$$z$$$?

Input

On the first line there will be the number of tests $$$t$$$ – ($$$1 \le t \le 100$$$).

On the first line of each test case there will be the $$$x$$$, $$$y$$$, and $$$z$$$ – ($$$1 \le x, y, z \le 10^4$$$).

Output

The answer for each test case will be a single integer on a single line.

Example
Input
4
10 25 35
10 9 11
1 10 20
3 2 1
Output
4
2
0
1
Note

In the first test case, we can decrease $$$x$$$ by $$$1$$$, increase $$$y$$$ by $$$2$$$, and increase $$$z$$$ by $$$1$$$.

D. Strictly Increasing
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a strictly increasing array $$$a$$$ of length $$$n$$$ ($$$a_1 \lt a_2 \lt \ldots \lt a_n$$$).

Determine whether it is possible to insert $$$k$$$ integers $$$b_1 \le b_2 \le \ldots \le b_k$$$ arbitrarily into $$$a$$$, such that $$$b_1 \ge a_1$$$, $$$b_k \le a_n$$$, and $$$a$$$ is strictly increasing after all $$$k$$$ integers have been inserted.

Since this problem is too easy, solve the problem for $$$q$$$ point updates on $$$a$$$.

Input

Each test contains multiple test cases. The first line 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 three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \le n \le 2\cdot 10^5, 0 \le k \le 10^9$$$, $$$1 \le q \le 2\cdot 10^5$$$) — the length of $$$a$$$, the number of integers to insert into $$$a$$$, and the number of point updates, respectively.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the strictly increasing array $$$a$$$ itself.

Each of the next $$$q$$$ lines of each test case contains two integers $$$i$$$ and $$$x$$$ ($$$1 \le i \le n, 1 \le x \le 10^9$$$) — representing a point update to set $$$a_i:=x$$$.

It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases both do not exceed $$$2\cdot 10^5$$$.

Output

Output $$$q$$$ lines, with the $$$q_i$$$-th line being "YES" if it is possible to insert $$$k$$$ integers into $$$a$$$ after the $$$i$$$-th point update, or being "NO" if it is impossible. 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.

Example
Input
2
4 2 4
1 2 4 5
4 6
2 3
4 9
1 3
10 1 4
1 2 3 4 5 6 7 8 9 10
1 1
9 11
10 11
10 12
Output
YES
YES
YES
NO
NO
NO
NO
YES

E. Good Array
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ of length $$$n$$$, and an initially empty array $$$b$$$.

You may perform the following operation at most once:

  1. Pick two arbitrary indices $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$).
  2. Replace $$$a_l, a_{l + 1}, \ldots, a_r$$$ with it's cyclic shift to the left by an offset of $$$1$$$.

For instance, choosing the indices $$$l = 2$$$ and $$$r = 5$$$ for the array $$$[1, 5, 2, 3, 4, 6]$$$ will result in the new array $$$[1, 2, 3, 4, 5, 6]$$$ after the operation is performed.

After performing the operation (if you choose to do so), you will build another array $$$b$$$ with the following process: on the $$$i$$$th turn of the process, you can insert $$$a_i$$$ at either the front or back of $$$b$$$.

Determine if there exists a way to construct $$$b$$$ such that $$$b$$$ is sorted in non-decreasing order after all $$$n$$$ elements are inserted.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 4000$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 4000$$$) — the length of array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4000$$$.

Output

For each test case, print "YES" if there exists a way to construct $$$b$$$ such that it is sorted in non-decreasing order, or print "NO" if it is impossible. 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.

Example
Input
3
6
1 5 2 3 4 6
6
1 3 2 4 5 4
7
6 12 4 9 4 9 15
Output
YES
NO
YES
Note

For the first test case, choose indices $$$l = 2$$$ and $$$r = 5$$$. Then the array becomes sorted, and inserting each element to the back of $$$b$$$ will result in $$$b$$$ being sorted.

F. 7s
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

George's favorite integer is $$$7$$$. Unfortunately, $$$7$$$ is only one integer among infinitely many. Thus, George also likes integers with first digit $$$7$$$, last digit $$$7$$$, or both.

George has an array $$$a_{1...n}$$$ of $$$n$$$ integers. Count the subarrays of $$$a$$$ such that George likes the sum of the elements of the subarray. Formally, count the number of pairs $$$(i, j)$$$, where $$$1 \le i \le j \le n$$$, such that George likes $$$\displaystyle\sum_{k=i}^{j} a_k$$$.

Input

The first line contains $$$n$$$ ($$$1 \le n \le 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Output

Output one integer: the number of subarrays of $$$a$$$ such that George likes the sum of the elements of the subarray.

Example
Input
5
22 4 3 46 2
Output
3
Note

George likes the sums of $$$[22, 4, 3, 46]$$$, $$$[4, 3]$$$, and $$$[22, 4, 3, 46, 2]$$$.

G. Miyamura and Cake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Miyamura has baked a yummy cake for Hori-san. He wants to paint the top of the cake, which can be represented by an $$$n$$$ by $$$m$$$ grid. Unfortunately, his only paintbrush is a cube that is colored blue on two opposite sides and red on the remaining sides. Miyamura knows that Hori-san hates blueberries, so he wants to maximize the number of red cells.

Denote the upper left corner of the cake as $$$(1, 1)$$$ and the bottom right as $$$(n, m)$$$. Miyamura can choose the starting position and orientation.

In order to color the cake, Miyamura can roll the brush onto the adjacent cells such that one edge of the cubical brush touches the cake during the duration of the roll, coloring that cell of the cake with the color of the side of the brush that lands on it.

Miyamura cannot rotate the brush and stay in the same cell, and he cannot roll the paintbrush off the canvas. If he rolls on a cell that is already covered, he will replace the color of the cell with the new color.

Your task is to help him find a sequence of moves such that all cells are colored and the number of red cells is maximized.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). The description of the test cases follows.

The first line and only line each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$), denoting the dimensions of the grid.

Output

The first line of each test case will have the maximum number of cells that can be painted red.

The second line will have two integers $$$a$$$, $$$b$$$ and a single character $$$c$$$ denoting the starting configuration, where:

  • $$$a$$$ ($$$1 \le a \le n$$$) represents the row number of the starting location.
  • $$$b$$$ ($$$1 \le b \le m$$$) represents the column number of the starting location.
  • $$$c$$$ represents the initial orientation of the cube. If $$$c$$$ is '-', that means that the blue sides are on the left and right side. If $$$c$$$ is '|', that means that the blue sides are on the top and bottom. If $$$c$$$ is '+', that means one of the blue sides is currently touching the cake and the other is facing upwards. Note that the starting location of the brush will be colored according to the side that is touching the cake as well.

The third line will have an integer $$$q$$$ ($$$0 \le q \le 25000$$$) denoting the number of rolls your solution performs. It is guaranteed that a sequence of operations of at most $$$25000$$$ exists to color the cake optimally.

The fourth line will contain a string of length $$$q$$$. Each character of the string is either $$$L$$$, $$$R$$$, $$$D$$$, or $$$U$$$, indicating that at that step, the cube rolled left, right, down, or up respectively.

Note that you do not need to minimize $$$q$$$. If there are multiple valid solutions, print any of them.

Example
Input
3
1 1
2 2
1 2
Output
1
1 1 |
0

3
1 1 |
5
RDLRU
2
1 1 |
1
R
Note

We use 'B' to denote the blue cells, 'R' to denote the red cells and '.' to denote the uncolored cells.

For the first testcase, the number of red cells is $$$1$$$, and the operation process can be represented as follows.


|

For the second testcase, the number of red cells is $$$3$$$, and the operation process can be represented as follows.


|.
..

R|
..

RR
.+

RR
-B

RR
R+

R|
RB

For the third testcase, the number of red cells is $$$2$$$, and the operation process can be represented as follows.


|

R|

H. Shortest Subarray
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For an array $$$x$$$, let $$$f(x)$$$ be the array obtained by deleting all $$$x_i$$$ such that there exists some $$$j \lt i$$$, and $$$x_j$$$ is equal to $$$x_i$$$, and concatenating the rest together. In other words, it keeps the first occurrence of each value and concatenates them in the order they appear in the $$$x$$$. For example, $$$f([1, 2, 1, 3]) = [1, 2, 3]$$$.

Given an array $$$a$$$, find the length of the shortest subarray $$$b$$$ of $$$a$$$ such that $$$f(a)$$$ is equal to $$$f(b)$$$.

A subarray is an array obtained by deleting several (possibly zero) elements from the beginning and end of the original array.

Input

The first line has $$$t$$$, the number of testcases. — ($$$1 \le t \le 3 \cdot 10^5$$$)

The first line of each test case has $$$n$$$, the length of $$$a$$$. — ($$$1 \le n \le 3 \cdot 10^5$$$)

The second line of each test case has $$$n$$$ integers, which is the array $$$a$$$. — ($$$1 \le a_i \le n$$$)

The sum of $$$n$$$ over all test cases will not exceed $$$3 \cdot 10^5$$$.

Output

The output of each test case has a single integer, the length of the shortest subarray.

Example
Input
3
9
1 1 2 2 3 3 1 2 3
12
1 2 2 2 2 2 2 3 1 2 1 3
13
1 2 2 2 2 2 3 4 1 4 2 3 4
Output
3
4
8
Note

In the first test case, the optimal subarray is the last 3 elements.

I. GCD Splicing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An array $$$c$$$ of length $$$n$$$ is good if it satisfies the following property:

  • For all $$$1 \lt i \le n$$$, there exists some $$$j \lt i$$$ such that $$$\gcd(c_i, c_j) \gt 1$$$.

We can measure the cost of some array $$$b$$$ of $$$n$$$ integers. In one operation, you can remove any good subsequence from the array. The cost of $$$b$$$ is the minimum number of operations to make the array empty.

You're given an array $$$a$$$ of length $$$n$$$. You want to minimise the cost of $$$a$$$ by performing the following action exactly once:

  • Choose positions $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$.
  • Perform a cyclic shift to the right on the subarray $$$a_{l \ldots r}$$$.

Please output the minimum cost after performing the following action exactly once on $$$a$$$.

Input

Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. This is followed by descriptions of the test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the size of the array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 3 \cdot 10^5$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

Output

For each test case, output a single integer — the minimum cost of $$$a$$$ after performing the given action exactly once.

Example
Input
4
10
10 9 8 7 6 5 4 3 2 1
8
28 14 7 27 9 3 5 15
4
5 10 3 6
4
3 5 6 6
Output
3
2
1
2
Note

In the first sample, one optimal selection is to perform the action with $$$l = 2$$$ and $$$r = 5$$$. After performing the action, $$$a = [10, 6, 9, 8, 7, 5, 4, 3, 2, 1]$$$. We can show that the cost of this $$$a$$$ is equal to $$$3$$$:

  • In the first operation, remove the subsequence $$$[10, 6, 9, 8, 5, 4, 3, 2]$$$. Now $$$a = [7, 1]$$$.
  • In the second operation, remove the subsequence $$$[7]$$$.
  • In the third operation, remove the subsequence $$$[1]$$$.

J. Making 0s
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an array $$$a$$$ with $$$n$$$ elements, $$$n = 4^p, 0 \le p \le 10, -10^9 \le a_i \le 10^9$$$, you can perform the following operation.

1. Choose an integer $$$k, 0 \le k \le p$$$.

2. Divide $$$a$$$ into $$$4^k$$$ consecutive segments of length $$$4^{p - k}$$$.

3. Choose one of these segments and either add $$$1$$$ or subtract $$$1$$$ from all the elements in that segment.

Find the minimum number of operations to make the entire array $$$0$$$.

Input

The first line has a single integer $$$t$$$, the number of tests. ($$$1 \le t \le 4^{9}$$$)

The first line of each test case has a single integer $$$n$$$. ($$$1 \le n \le 4^{10}$$$)

The second line of each test case has $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$. ($$$-10^9 \le a_i \le 10^9$$$)

The sum of $$$n$$$ over all test cases doesn't exceed $$$4^{10}$$$.

Output

For each test case output a single integer, the minimum number of operations.

Example
Input
3
1
3
4
1 4 3 4
16
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1
Output
3
7
3

K. Hydra
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two best friends — Alice and Bob — are playing a Minecraft modpack when they reach the final boss. The final boss is based on the Hydra, a mythical beast which is said to grow back two heads after you chop off one of them. Luckily, the mod developers made the boss possible to beat, so it works slightly differently than that.

The hydra is initially made of $$$n$$$ heads, the $$$i$$$-th head having a strength level of $$$a_i$$$, and there is an additional parameter $$$k$$$. If Alice or Bob attacks one of the heads having $$$a_i \gt k$$$ with integer power $$$p$$$ ($$$1 \le p \le a_i$$$) — which you can choose per-attack — the strength level of the head decreases by $$$p$$$, and another head grows with $$$m$$$ strength such that $$$m$$$ is the smallest integer with $$$m^2 \ge p$$$. If any head with $$$a_i \le k$$$ is attacked, it is instantly severed and no heads regrow. Otherwise, if $$$a_i$$$ falls to zero after the attack, the head is severed, and the Hydra perishes if all heads are severed and no new ones are about to grow.

Whoever gets the final hit on the Hydra gets an achievement. Alice asks you to decide whether she should hit first or second if they both play optimally to try to get the achievement.

Input

The first line of input contains $$$t$$$, the number of testcases ($$$1 \le t \le 10^4$$$).

The first line of each testcase contains $$$n$$$ and $$$k$$$, the number of heads that the Hydra begins with and the value of the parameter respectively ($$$1 \le n \le 10^6$$$, $$$2 \le k \le 10^9$$$).

The second line of each testcase contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), the strength level of the heads which the Hydra begins with.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.

Output

Output the word 'First' if Alice should make the first strike in order to get the achievement, or 'Second' if she should let Bob go for it.

Example
Input
5
1 10
6
4 5
25 5 6 3
4 3
85 30 3 7
3 4
4 5 6
4 2
1 2 5 9
Output
First
Second
Second
First
Second

L. Quicksort
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an array of positive integers, define one quicksort operation as the following:

  • Choose any integer in the array as the pivot.
  • Move all items strictly less than the pivot to its left, maintaining their original order.
  • Move all items strictly greater than the pivot to its right, maintaining their original order.
  • Any items equal to the pivot will be grouped together with it.

For example, if we have the array $$$\{5, 4, 6, 5, 3, 1, 4, 2\}$$$ and we choose the pivot to be $$$4$$$, the new array becomes $$$\{3, 1, 2, 4, 4, 5, 6, 5\}$$$.

Given an array of $$$n$$$ integers and $$$q$$$ modification queries in the form $$$a_i = v$$$, find the minimum number of quicksort operations required to sort the array into ascending order after each operation. The updates are permanent.

Input

The first line contains $$$n$$$ ($$$1 \le n \le 10^5$$$).

The second line contains $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

The third line contains $$$q$$$ ($$$1 \le q \le 10^5$$$).

Each of the next $$$q$$$ lines contains $$$i$$$ and $$$v$$$ ($$$1 \le i \le n$$$, $$$1 \le v \le 10^9$$$), indicating a modification query in the form $$$a_i = v$$$.

Output

After each modification query, print the minimum number of quicksort operations required to sort the array.

Example
Input
8
8 5 4 6 1 7 3 5
8
7 8
7 4
6 6
4 1
4 7
2 8
5 4
5 7
Output
3 3 2 2 3 3 2 2