inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
You are given two integers $x$ and $y$.

Output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.

Input Format:
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 two space-separated integers $x$ and $y$ ($0 \leq x, y \leq 9$).

Output Format:
For each test case, output two integers: the minimum of $x$ and $y$, followed by the maximum of $x$ and $y$.

Tags:
['implementation', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given a string $s$ consisting of lowercase English letters.

Rearrange the characters of $s$ to form a new string $r$ that is not equal to $s$, or report that it's impossible.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.

The only line of each test case contains a string $s$ of length at most $10$ consisting of lowercase English letters.

Output Format:
For each test case, if no such string $r$ exists as described in the statement, output "NO" (without quotes).

Otherwise, output "YES" (without quotes). Then, output one line — the string $r$, consisting of letters of string $s$.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).

If multiple answers are possible, you can output any of them.

Tags:
['implementation', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB
There is a clock labeled with the numbers $1$ through $12$ in clockwise order, as shown below.

![](CDN_BASE_URL/d18290022594db5fbc2eb2c94222b5d4)

In this example, $(a,b,c,d)=(2,9,10,6)$, and the strings intersect.

Alice and Bob have four distinct integers $a$, $b$, $c$, $d$ not more than $12$. Alice ties a red string connecting $a$ and $b$, and Bob ties a blue string connecting $c$ and $d$. Do the strings intersect? (The strings are straight line segments.)

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 5940$) — the number of test cases.

The only line of each test case contains four distinct integers $a$, $b$, $c$, $d$ ($1 \leq a, b, c, d \leq 12$).

Output Format:
For each test case, output "YES" (without quotes) if the strings intersect, 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).

Tags:
['implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given a binary string$^{\dagger}$. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

![](CDN_BASE_URL/f49d41e3510eec9591123a65ee9389ab)

Note that:

  * each character must lie in exactly one of the pieces;    * the pieces must be contiguous substrings of the original string;    * you must use all the pieces in the rearrangement. 

$^{\dagger}$ A binary string is a string consisting of characters $\texttt{0}$ and $\texttt{1}$. A sorted binary string is a binary string such that all characters $\texttt{0}$ come before all characters $\texttt{1}$.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 500$) — the number of test cases.

The only line of each test case contains a single string $s$ ($1 \leq |s| \leq 500$) consisting of characters $\texttt{0}$ and $\texttt{1}$, where $|s|$ denotes the length of the string $s$.

Output Format:
For each test case, output a single integer — the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.

Tags:
['dp', 'greedy', 'implementation', 'sortings', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Timur is in a car traveling on the number line from point $0$ to point $n$. The car starts moving from point $0$ at minute $0$.

There are $k+1$ signs on the line at points $0, a_1, a_2, \dots, a_k$, and Timur knows that the car will arrive there at minutes $0, b_1, b_2, \dots, b_k$, respectively. The sequences $a$ and $b$ are strictly increasing with $a_k = n$.

![](CDN_BASE_URL/bacc58f5d5013f5922e96a1311487b97)

Between any two adjacent signs, the car travels with a constant speed. Timur has $q$ queries: each query will be an integer $d$, and Timur wants you to output how many minutes it takes the car to reach point $d$, rounded down to the nearest integer.

Input Format:
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 three integers $n$, $k$, and $q$, ($k \leq n \leq 10^9$; $1 \leq k, q \leq 10^5$) — the final destination, the number of points Timur knows the time for, and the number of queries respectively.

The second line of each test case contains $k$ integers $a_i$ ($1 \leq a_i \leq n$; $a_i < a_{i+1}$ for every $1 \leq i \leq k-1$; $a_k = n$).

The third line of each test case contains $k$ integers $b_i$ ($1 \leq b_i \leq 10^9$; $b_i < b_{i+1}$ for every $1 \leq i \leq k-1$).

Each of the following $q$ lines contains a single integer $d$ ($0 \leq d \leq n$) — the distance that Timur asks the minutes passed for.

The sum of $k$ over all test cases doesn't exceed $10^5$, and the sum of $q$ over all test cases doesn't exceed $10^5$.

Output Format:
For each query, output a single integer — the number of minutes passed until the car reaches the point $d$, rounded down.

Tags:
['binary search', 'math', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB
Given an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$.

A lattice point is a point with integer coordinates. The Euclidean distance from $(0, 0)$ to the point $(x,y)$ is $\sqrt{x^2 + y^2}$.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.

The only line of each test case contains a single integer $r$ ($1 \leq r \leq 10^5$).

The sum of $r$ over all test cases does not exceed $10^5$.

Output Format:
For each test case, output a single integer — the number of lattice points that have an Euclidean distance $d$ from $(0, 0)$ such that $r \leq d < r+1$.

Tags:
['binary search', 'brute force', 'dfs and similar', 'geometry', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given an array $a$ consisting of $n$ nonnegative integers.

You can swap the elements at positions $i$ and $j$ if $a_i~\mathsf{XOR}~a_j < 4$, where $\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

Find the lexicographically smallest array that can be made with any number of swaps.

An array $x$ is lexicographically smaller than an array $y$ if in the first position where $x$ and $y$ differ, $x_i < y_i$.

Input Format:
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\cdot10^5$) — the length of the array.

The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 10^9$) — the elements of the array.

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

Output Format:
For each test case, output $n$ integers — the lexicographically smallest array that can be made with any number of swaps.

Tags:
['data structures', 'dsu', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Bob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \leq i \leq n$. For example, one possible grid for $n=4$ is shown below:

$$\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\ -a_4 & a_4 & -a_1 & -a_3 \\\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}$$

Alice and Bob play a game as follows:

  * Bob shows Alice his grid.    * Alice gives Bob an array $a_1, a_2, \dots, a_n$ of her choosing, whose elements are all $\mathbf{-1}$ or $\mathbf{1}$.    * Bob substitutes these values into his grid to make a grid of $-1$s and $1$s.    * Bob sorts the elements of each column in non-decreasing order.    * Alice wins if all the elements in the middle row are $1$; otherwise, Bob wins. 

For example, suppose Alice gives Bob the array $[1, -1, -1, 1]$ for the grid above. Then the following will happen (colors are added for clarity):

$$\begin{bmatrix} \color{red}{a_1} & \color{green}{-a_2} & \color{blue}{-a_3} & \color{green}{-a_2} \\\ -a_4 & a_4 & \color{red}{-a_1} & \color{blue}{-a_3} \\\ \color{red}{a_1} & \color{green}{a_2} & \color{green}{-a_2} & a_4 \end{bmatrix} \xrightarrow{[\color{red}{1},\color{green}{-1},\color{blue}{-1},1]} \begin{bmatrix} \color{red}{1} & \color{green}{1} & \color{blue}{1} & \color{green}{1} \\\ -1 & 1 & \color{red}{-1} & \color{blue}{1} \\\ \color{red}{1} & \color{green}{-1} & \color{green}{1} & 1 \end{bmatrix} \xrightarrow{\text{sort each column}} \begin{bmatrix} -1 & -1 & -1 & 1 \\\ \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} \\\ 1 & 1 & 1 & 1 \\\ \end{bmatrix}\,. $$ Since the middle row is all $1$, Alice wins.

Given Bob's grid, determine whether or not Alice can choose the array $a$ to win the game.

Input Format:
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.

The first line of each test case contains a single integer $n$ ($2 \leq n \leq 500$) — the number of columns of Bob's grid.

The next three lines each contain $n$ integers, the $i$-th of which contains $g_{i,1}, g_{i,2}, \dots, g_{i,n}$ ($-n \leq g_{i,j} \leq n$, $g_{i,j} \neq 0$), representing Bob's grid.

If cell $x > 0$ is in the input, that cell in Bob's grid should contain $a_x$; if $x < 0$ is in the input, that cell in Bob's grid should contain $-a_{-x}$. See the sample input and notes for a better understanding.

Output Format:
For each test case, output "YES" (without quotes) if Alice can win, 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).

Tags:
['2-sat', 'dfs and similar', 'graphs']

Time Limit: 2000 ms
Memory Limit: 256 MB
A contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$ are sorted in non-decreasing order.

Some of the problems may be more difficult than expected, so the writers must propose more problems. When a new problem with difficulty $w$ is proposed, the most difficult problem will be deleted from the contest, and the problems will be sorted in a way that the difficulties are non-decreasing.

In other words, in each operation, you choose an integer $w$, insert it into the array $a$, sort array $a$ in non-decreasing order, and remove the last element from it.

Find the minimum number of new problems to make $a_i\le b_i$ for all $i$.

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

The first line of each test case contains only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of problems.

The second line of each test case contains an array $a$ of length $n$ ($1\le a_1\le a_2\le\cdots\le a_n\le 10^9$).

The third line of each test case contains an array $b$ of length $n$ ($1\le b_1\le b_2\le\cdots\le b_n\le 10^9$).

Output Format:
For each test case, print an integer as your answer in a new line.

Tags:
['brute force', 'greedy', 'two pointers']

Time Limit: 1000 ms
Memory Limit: 256 MB
There are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first.

In each operation, the player chooses a facing-up coin, removes the coin, and flips the two coins that are adjacent to it. If (before the operation) there are only two coins left, then one will be removed and the other won't be flipped (as it would be flipped twice). If (before the operation) there is only one coin left, no coins will be flipped. If (before the operation) there are no facing-up coins, the player loses.

Decide who will win the game if they both play optimally. It can be proved that the game will end in a finite number of operations, and one of them will win.

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

The first line of each test case contains only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of the coins.

A string $s$ of length $n$ follows on the second line of each test case, containing only "U" and "D", representing that each coin is facing up or facing down.

Output Format:
For each test case, print "YES" if Alice will win the game, and "NO" otherwise.

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.

Tags:
['games']

Time Limit: 1000 ms
Memory Limit: 256 MB
You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them.

There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy $k$ new cards in total, and the cards you buy can contain any integer between $1$ and $n$.

After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length $n$ which are a permutation of $[1, 2, \ldots, n]$. What's the maximum score you can get?

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

The first line of each test case contains two integers $n$, $k$ ($1\le n \le 2 \cdot 10^5$, $0\le k \le 10^{12}$) — the number of distinct types of cards and the number of coins.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^{12}$) — the number of cards of type $i$ you have at the beginning.

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

Output Format:
For each test case, output a single line containing an integer: the maximum score you can get.

Tags:
['binary search', 'constructive algorithms', 'greedy', 'implementation', 'math', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.

You are given two positive integers $n$, $m$.

Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:

  * $1\le a\le n$, $1\le b\le m$;    * $a+b$ is a multiple of $b \cdot \gcd(a,b)$.

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

The first line of each test case contains two integers $n$, $m$ ($1\le n,m\le 2 \cdot 10^6$).

It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.

Output Format:
For each test case, print a single integer: the number of valid pairs.

Tags:
['brute force', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.

You are given two positive integers $n$, $m$.

Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:

  * $1\le a\le n$, $1\le b\le m$;    * $b \cdot \gcd(a,b)$ is a multiple of $a+b$.

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

The first line of each test case contains two integers $n$, $m$ ($1\le n,m\le 2 \cdot 10^6$).

It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.

Output Format:
For each test case, print a single integer: the number of valid pairs.

Tags:
['brute force', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
Let $\operatorname{lowbit}(x)$ denote the value of the lowest binary bit of $x$, e.g. $\operatorname{lowbit}(12)=4$, $\operatorname{lowbit}(8)=8$.

For an array $a$ of length $n$, if an array $s$ of length $n$ satisfies $s_k=\left(\sum\limits_{i=k-\operatorname{lowbit}(k)+1}^{k}a_i\right)\bmod 998\,244\,353$ for all $k$, then $s$ is called the Fenwick Tree of $a$. Let's denote it as $s=f(a)$.

For a positive integer $k$ and an array $a$, $f^k(a)$ is defined as follows:

$$ f^k(a)= \begin{cases} f(a)&\textrm{if }k=1\\\ f(f^{k-1}(a))&\textrm{otherwise.}\\\ \end{cases} $$

You are given an array $b$ of length $n$ and a positive integer $k$. Find an array $a$ that satisfies $0\le a_i < 998\,244\,353$ and $f^k(a)=b$. It can be proved that an answer always exists. If there are multiple possible answers, you may print any of them.

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

The first line of each test case contains two positive integers $n$ ($1 \leq n \leq 2\cdot 10^5$) and $k$ ($1\le k\le 10^9$), representing the length of the array and the number of times the function $f$ is performed.

The second line of each test case contains an array $b_1, b_2, \ldots, b_n$ ($0\le b_i < 998\,244\,353$).

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

Output Format:
For each test case, print a single line, containing a valid array $a$ of length $n$.

Tags:
['combinatorics', 'data structures', 'math', 'matrices']

Time Limit: 3000 ms
Memory Limit: 256 MB
Little R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \leq a_2 \leq \ldots \leq a_n$.

To do this, she can perform several magic tricks. Little R has a fixed array $b_1\ldots b_m$ of length $m$. Formally, let's define a trick as a procedure that does the following things in order:

  * Choose a set $S \subseteq \\{1, 2, \ldots, n\\}$.    * For each $u \in S$, assign $a_u$ with $b_{a_u}$. 

Little R wonders how many tricks are needed at least to make the initial array non-decreasing. If it is not possible with any amount of tricks, print $-1$ instead.

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

The first line of each test case contains two integers $n$ and $m$ ($1\leq n \leq 10^6$, $1 \leq m \leq 10^6$) — the length of the initial array and the range of the elements in the array.

The second line of each test case contains $n$ integers $a_1, \ldots, a_n$ ($1 \leq a_i \leq m$) — the initial array.

The third line of each test case contains $m$ integers $b_1, \ldots, b_m$ ($1 \leq b_i \leq m$) — the fixed magic array.

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

Output Format:
For each test case, output a single integer: the minimum number of tricks needed, or $-1$ if it is impossible to make $a_1, \ldots, a_n$ non- decreasing.

Tags:
['binary search', 'dfs and similar', 'graphs', 'implementation']

Time Limit: 4000 ms
Memory Limit: 512 MB
Three friends gathered to play a few games of chess together.

In every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.

You've been told that their scores after all the games were played were $p_1$, $p_2$ and $p_3$. Additionally, it is guaranteed that $p_1 \leq p_2 \leq p_3$ holds.

Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain $p_1$, $p_2$ and $p_3$ as a result of a non-negative number of games between the three players, print $-1$ instead.

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

The first line of each test case contains three integers $p_1$, $p_2$ and $p_3$ ($0 \leq p_1 \leq p_2 \leq p_3 \leq 30$) — the scores of the three players, sorted non-decreasingly.

Output Format:
For each testcase, print one number — the maximum possible number of draws that could've happened, or $-1$ if the scores aren't consistent with any valid set of games and results.

Tags:
['brute force', 'dp', 'implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Today, Cat and Fox found an array $a$ consisting of $n$ non-negative integers.

Define the loneliness of $a$ as the smallest positive integer $k$ ($1 \le k \le n$) such that for any two positive integers $i$ and $j$ ($1 \leq i, j \leq n - k +1$), the following holds: $$a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1} | \ldots | a_{j+k-1},$$ where $x | y$ denotes the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of $x$ and $y$. In other words, for every $k$ consecutive elements, their bitwise OR should be the same. Note that the loneliness of $a$ is well-defined, because for $k = n$ the condition is satisfied.

Cat and Fox want to know how lonely the array $a$ is. Help them calculate the loneliness of the found array.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4 $) — the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer $n$ ($1 \leq n \leq 10^5$) — the length of the array $a$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < 2^{20}$) — the elements of the array.

It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.

Output Format:
For each test case, print one integer  — the loneliness of the given array.

Tags:
['binary search', 'bitmasks', 'data structures', 'greedy', 'math', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB