inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
You are given a tree with $n$ nodes numbered from $1$ to $n$, along with an array of size $n$. The value of $i$-th node is $a_{i}$. There are $q$ queries. In each query, you are given 2 nodes numbered as $x$ and $y$.

Consider the path from the node numbered as $x$ to the node numbered as $y$. Let the path be represented by $x = p_0, p_1, p_2, \ldots, p_r = y$, where $p_i$ are the intermediate nodes. Compute the sum of $a_{p_i}\oplus i$ for each $i$ such that $0 \le i \le r$ where $\oplus$ is the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) operator.

More formally, compute $$\sum_{i =0}^{r} a_{p_i}\oplus i$$.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case contains several sets of input data.

The first line of each set of input data contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of nodes.

The next $n-1$ lines of each set of input data contain $2$ integers, $u$ and $v$ representing an edge between the node numbered $u$ and the node numbered $v$. It is guaranteed that $u \ne v$ and that the edges form a tree.

The next line of each set of input data contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 5 \cdot 10^5$) — values of the nodes.

The next line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries.

The next $q$ lines describe the queries. The $i$-th query contains $2$ integers $x$ and $y$ ($1 \le x,y \le n$) denoting the starting and the ending node of the path.

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

Output Format:
For each query, output a single number — the sum from the problem statement.

Tags:
['bitmasks', 'brute force', 'dp', 'trees']

Time Limit: 3000 ms
Memory Limit: 256 MB
Define the range of a non-empty array to be the maximum value minus the minimum value. For example, the range of $[1,4,2]$ is $4-1=3$.

You are given an array $a_1, a_2, \ldots, a_n$ of length $n \geq 3$. It is guaranteed $a$ is sorted.

You have to color each element of $a$ red or blue so that:

  * the range of the red elements does not equal the range of the blue elements, and    * there is at least one element of each color. 

If there does not exist any such coloring, you should report it. If there are multiple valid colorings, you can print any of them.

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

The first line of each test case contains an integer $n$ ($3 \leq n \leq 50$) — the length of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$). It is guaranteed $a_1 \leq a_2 \leq \ldots \leq a_{n - 1} \leq a_{n}$.

Output Format:
For each test case, if it is impossible to color $a$ to satisfy all the constraints, output $\texttt{NO}$.

Otherwise, first output $\texttt{YES}$.

Then, output a string $s$ of length $n$. For $1 \leq i \leq n$, if you color $a_i$ red, $s_i$ should be $\texttt{R}$. For $1 \leq i \leq n$, if you color $a_i$ blue, $s_i$ should be $\texttt{B}$.

Tags:
['constructive algorithms']

Time Limit: 1000 ms
Memory Limit: 256 MB
A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large.

You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits?

Input Format:
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 $x$ ($10 \leq x \leq 10^{18}$).

Output Format:
For each test case, output $\texttt{YES}$ if $x$ satisfies the condition, and $\texttt{NO}$ otherwise.

You can output $\texttt{YES}$ and $\texttt{NO}$ in any case (for example, strings $\texttt{yES}$, $\texttt{yes}$, and $\texttt{Yes}$ will be recognized as a positive response).

Tags:
['implementation', 'math']

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

You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:

  * Option $1$: set $c$ to $c + a_i$.    * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$. 

Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find $k$.

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$ ($2 \leq n \leq 2 \cdot 10^5$).

The second line of each case contains $n$ integers $a_1$, $a_2$, $a_3$, $\ldots$, $a_n$ ($-10^9 \leq a_i \leq 10^9$).

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

Output Format:
For each test case, output a single integer — the value of $k$.

Tags:
['dp', 'greedy', 'math']

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

You are given an array $a$ of length $n$. Start with $c = 0$. Then, for each $i$ from $1$ to $n$ (in increasing order) do exactly one of the following:

  * Option $1$: set $c$ to $c + a_i$.    * Option $2$: set $c$ to $|c + a_i|$, where $|x|$ is the absolute value of $x$. 

Let the maximum final value of $c$ after the procedure described above be equal to $k$. Find the number of unique procedures that result in $c = k$. Two procedures are different if at any index $i$, one procedure chose option $1$ and another chose option $2$, even if the value of $c$ is equal for both procedures after that turn.

Since the answer may be large, output it modulo $998\,244\,353$.

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$ ($2 \leq n \leq 2 \cdot 10^5$).

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

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

Output Format:
For each test case, output a single integer — the number of unique procedures that result in $c = k$, modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditions:

  * each substring either equals $t$ or "$\texttt{a}$", and    * at least one substring equals $t$. 

$^{\dagger}$ A partition of a string $s$ is an ordered sequence of some $k$ strings $t_1, t_2, \ldots, t_k$ (called substrings) such that $t_1 + t_2 + \ldots + t_k = s$, where $+$ represents the concatenation operation.

Input Format:
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 string $s$ consisting of lowercase Latin characters ($2 \leq |s| \leq 2 \cdot 10^5$).

The sum of $|s|$ over all test cases does not exceed $3 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the number of nonempty strings $t \neq$ "$\texttt{a}$" that satisfy all constraints.

Tags:
['brute force', 'hashing', 'implementation', 'math', 'string suffix structures', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
Two hungry red pandas, Oscar and Lura, have a tree $T$ with $n$ nodes. They are willing to perform the following shuffle procedure on the whole tree $T$ exactly once. With this shuffle procedure, they will create a new tree out of the nodes of the old tree.

  1. Choose any node $V$ from the original tree $T$. Create a new tree $T_2$, with $V$ as the root.    2. Remove $V$ from $T$, such that the original tree is split into one or more subtrees (or zero subtrees, if $V$ is the only node in $T$).    3. Shuffle each subtree with the same procedure (again choosing any node as the root), then connect all shuffled subtrees' roots back to $V$ to finish constructing $T_2$. 

After this, Oscar and Lura are left with a new tree $T_2$. They can only eat leaves and are very hungry, so please find the maximum number of leaves over all trees that can be created in exactly one shuffle.

Note that leaves are all nodes with degree $1$. Thus, the root may be considered as a leaf if it has only one child.

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 every test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of nodes within the original tree $T$.

The next $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) — an edge within the original tree $T$. The given edges form a tree.

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

Output Format:
For each test case, output a single integer — the maximum number of leaves achievable with exactly one shuffle procedure on the whole tree.

Tags:
['dp', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive.

You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

For each $i$ from $1$ to $n$ inclusive, we must have:

  * If $s_i = \texttt{P}$, $b_i$ is the sum of $a_1$ through $a_i$.    * If $s_i = \texttt{S}$, $b_i$ is the sum of $a_i$ through $a_n$. 

Output the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ such that there exists an array $a_1, a_2, \ldots, a_n$ with elements not exceeding $m$ by absolute value satisfying the constraints given by the array $b_1, b_2, \ldots, b_n$ and the string $s$.

Since the answer may be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) — the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element $a_i$.

The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($|b_i| \leq m \cdot n$).

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

Output Format:
For each test case, output a single integer — the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
The secret behind Oscar's first magic trick has been revealed! Because he still wants to impress Lura, he comes up with a new idea: he still wants to sort a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.

This time, he chooses an integer $k$. He wants to sort the permutation in non-decreasing order using the following operation several times:

  1. Pick a continuous subarray of length $k$ and remove it from $p$.    2. Insert the continuous subarray back into $p$ at any position (perhaps, in the very front or the very back). 

To be as impressive as possible, Oscar would like to choose the maximal value of $k$ such that he can sort his permutation. Please help him find the maximal $k$ as well as a sequence of operations that will sort the permutation. You don't need to minimize the number of operations, but you are allowed to use at most $5n^2$ operations.

We have a proof that, for the maximal $k$ such that you can sort the permutation in any number of operations, you can also sort it in at most $5n^2$ operations.

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

The first line of each test case contains a single integer $n$ ($5 \leq n \leq 10^3$) — the length of the permutation.

The second line of each test case contains a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.

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

Output Format:
For each test case, first output the chosen value of $k$ on a new line ($1 \leq k \leq n$).

Then, output a single integer $m$ — the number of operations used ($0 \leq m \leq 5n^2$).

Then, on each of the next $m$ lines, output the operations denoted by two integers $i$ and $j$ ($1 \leq i, j \leq n - k + 1$), representing an operation where you remove the subarray starting from index $i$ and replace it back into $p$ at index $j$.

Tags:
['constructive algorithms', 'implementation', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
There are $n$ towers at $n$ distinct points $(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$, such that no three are collinear and no four are concyclic. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$, and you want to capture all of them. To do this, you can do the following operation any number of times:

  * Pick two towers $P$ and $Q$ you own and one tower $R$ you don't own, such that the circle through $P$, $Q$, and $R$ contains all $n$ towers inside of it.    * Afterwards, capture all towers in or on triangle $\triangle PQR$, including $R$ itself. 

An attack plan is a series of choices of $R$ ($R_1, R_2, \ldots, R_k$) using the above operations after which you capture all towers. Note that two attack plans are considered different only if they differ in their choice of $R$ in some operation; in particular, two attack plans using the same choices of $R$ but different choices of $P$ and $Q$ are considered the same.

Count the number of attack plans of minimal length. Note that it might not be possible to capture all towers, in which case the answer is $0$.

Since the answer may be large, output it modulo $998\,244\,353$.

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

The first line of each test case contains a single integer $n$ ($4 \leq n \leq 100$) — the number of towers.

The $i$-th of the next $n$ lines contains two integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$) — the location of the $i$-th tower. Initially, you own towers $(x_1, y_1)$ and $(x_2, y_2)$.

All towers are at distinct locations, no three towers are collinear, and no four towers are concyclic.

The sum of $n$ over all test cases does not exceed $1000$.

Output Format:
For each test case, output a single integer — the number of attack plans of minimal length after which you capture all towers, modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'geometry']

Time Limit: 2000 ms
Memory Limit: 256 MB
Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap.

Note that the new words may not necessarily be different.

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

The first and only line of each test case contains two space-separated strings, $a$ and $b$, both of length $3$. The strings only contain lowercase Latin letters.

Output Format:
For each test case, after the swap, output $a$ and $b$, separated by a space.

Tags:
['implementation', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Given an integer $n$, find an integer $x$ such that:

  * $2 \leq x \leq n$.    * The sum of multiples of $x$ that are less than or equal to $n$ is maximized. Formally, $x + 2x + 3x + \dots + kx$ where $kx \leq n$ is maximized over all possible values of $x$.

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

Each test case contains a single integer $n$ ($2 \leq n \leq 100$).

Output Format:
For each test case, output an integer, the optimal value of $x$. It can be shown there is only one unique answer.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Alex thinks some array is good if there exists some element that can be represented as the sum of all other elements (the sum of all other elements is $0$ if there are no other elements). For example, the array $[1,6,3,2]$ is good since $1+3+2=6$. Furthermore, the array $[0]$ is also good. However, the arrays $[1,2,3,4]$ and $[1]$ are not good.

Alex has an array $a_1,a_2,\ldots,a_n$. Help him count the number of good non-empty prefixes of the array $a$. In other words, count the number of integers $i$ ($1 \le i \le n$) such that the length $i$ prefix (i.e. $a_1,a_2,\ldots,a_i$) is good.

Input Format:
The first line of the input 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 \le n \le 2 \cdot 10^5$) — the number of elements in the array.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 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 a single integer — the number of good non-empty prefixes of the array $a$.

Tags:
['greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB
Given a $n$ by $m$ grid consisting of '.' and '#' characters, there exists a whole manhattan circle on the grid. The top left corner of the grid has coordinates $(1,1)$, and the bottom right corner has coordinates $(n, m)$.

Point ($a, b$) belongs to the manhattan circle centered at ($h, k$) if $|h - a| + |k - b| < r$, where $r$ is a positive constant.

On the grid, the set of points that are part of the manhattan circle is marked as '#'. Find the coordinates of the center of the circle.

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

The first line of each test case contains $n$ and $m$ ($1 \leq n \cdot m \leq 2 \cdot 10^5$) — the height and width of the grid, respectively.

The next $n$ lines contains $m$ characters '.' or '#'. If the character is '#', then the point is part of the manhattan circle.

It is guaranteed the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$, and there is a whole manhattan circle on the grid.

Output Format:
For each test case, output the two integers, the coordinates of the center of the circle.

Tags:
['implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
Ntarsis has a box $B$ with side lengths $x$, $y$, and $z$. It lies in the 3D coordinate plane, extending from $(0,0,0)$ to $(x,y,z)$.

Ntarsis has a secret box $S$. He wants to choose its dimensions such that all side lengths are positive integers, and the volume of $S$ is $k$. He can place $S$ somewhere within $B$ such that:

  * $S$ is parallel to all axes.    * every corner of $S$ lies on an integer coordinate. 

$S$ is magical, so when placed at an integer location inside $B$, it will not fall to the ground.

Among all possible ways to choose the dimensions of $S$, determine the maximum number of distinct locations he can choose to place his secret box $S$ inside $B$. Ntarsis does not rotate $S$ once its side lengths are selected.

Input Format:
The first line consists of an integer $t$, the number of test cases ($1 \leq t \leq 2000$). The description of the test cases follows.

The first and only line of each test case contains four integers $x, y, z$ and $k$ ($1 \leq x, y, z \leq 2000$, $1 \leq k \leq x \cdot y \cdot z$).

It is guaranteed the sum of all $x$, sum of all $y$, and sum of all $z$ do not exceed $2000$ over all test cases.

Note that $k$ may not fit in a standard 32-bit integer data type.

Output Format:
For each test case, output the answer as an integer on a new line. If there is no way to select the dimensions of $S$ so it fits in $B$, output $0$.

Tags:
['brute force', 'combinatorics', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are facing the final boss in your favorite video game. The boss enemy has $h$ health. Your character has $n$ attacks. The $i$'th attack deals $a_i$ damage to the boss but has a cooldown of $c_i$ turns, meaning the next time you can use this attack is turn $x + c_i$ if your current turn is $x$. Each turn, you can use all attacks that are not currently on cooldown, all at once. If all attacks are on cooldown, you do nothing for the turn and skip to the next turn.

Initially, all attacks are not on cooldown. How many turns will you take to beat the boss? The boss is beaten when its health is $0$ or less.

Input Format:
The first line contains $t$ ($1 \leq t \leq 10^4$)  – the number of test cases.

The first line of each test case contains two integers $h$ and $n$ ($1 \leq h, n \leq 2 \cdot 10^5$) – the health of the boss and the number of attacks you have.

The following line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ ($1 \leq a_i \leq 2 \cdot 10^5$) – the damage of your attacks.

The following line of each test case contains $n$ integers $c_1, c_2, ..., c_n$ ($1 \leq c_i \leq 2 \cdot 10^5$) – the cooldown of your attacks.

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

Output Format:
For each test case, output an integer, the minimum number of turns required to beat the boss.

Tags:
['binary search', 'data structures']

Time Limit: 2000 ms
Memory Limit: 256 MB
Let $D(n)$ represent the sum of digits of $n$. For how many integers $n$ where $10^{l} \leq n < 10^{r}$ satisfy $D(k \cdot n) = k \cdot D(n)$? Output the answer modulo $10^9+7$.

Input Format:
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) – the number of test cases.

Each test case contains three integers $l$, $r$, and $k$ ($0 \leq l < r \leq 10^9$, $1 \leq k \leq 10^9$).

Output Format:
For each test case, output an integer, the answer, modulo $10^9 + 7$.

Tags:
['combinatorics', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.

Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.

In one operation, Alex selects any row $r$ ($1 \le r \le n$) or any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ or column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \cdot m \le 10^6$) — the number of rows and columns of the grid.

The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.

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

Output Format:
For each test case, output a single integer — the maximum possible size of a connected component of '#' cells that Alex can achieve.

Tags:
['brute force', 'data structures', 'dfs and similar', 'dsu', 'graphs', 'implementation']

Time Limit: 2000 ms
Memory Limit: 512 MB