inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode!

Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $a$ consisting of $n$ elements, and she needs to calculate the product of each two adjacent elements (that is, $a_1 \cdot a_2$, $a_2 \cdot a_3$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $a$ in such a way that $a_i \cdot a_{i + 1} \leq k$ holds for every $1 \leq i < n$. There are two types of operations she can perform:

  1. She can rearrange the elements of the array $a$ in an arbitrary way.   2. She can select an arbitrary element of the array $a$ and change its value to an arbitrary integer from $1$ to $k$. 

Iris wants to minimize the number of operations of type $2$ that she uses.

However, that's completely not the end of the summer holiday! Summer holiday lasts for $q$ days, and on the $i$-th day, Iris is asked to solve the Math homework for the subarray $b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}$. Help Iris and tell her the minimum number of type $2$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $b$ is not changed.

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

The first line of each test case contains three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \leq 10^6$) — the length of array $b$, the number of days, and the upper bound for the multiplication calculation.

The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq k$) — the elements of the array $b$.

Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$) — the boundaries of the subarray on the $i$-th day.

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

Output Format:
For each test, output a single line containing $q$ integers — the minimum number of operations of type $2$ needed for each day.

Tags:
['data structures', 'greedy', 'implementation', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB
Iris likes full binary trees.

Let's define the depth of a rooted tree as the maximum number of vertices on the simple paths from some vertex to the root. A full binary tree of depth $d$ is a binary tree of depth $d$ with exactly $2^d - 1$ vertices.

Iris calls a tree a $d$-binary tree if some vertices and edges can be added to it to make it a full binary tree of depth $d$. Note that any vertex can be chosen as the root of a full binary tree.

Since performing operations on large trees is difficult, she defines the binary depth of a tree as the minimum $d$ satisfying that the tree is $d$-binary. Specifically, if there is no integer $d \ge 1$ such that the tree is $d$-binary, the binary depth of the tree is $-1$.

Iris now has a tree consisting of only vertex $1$. She wants to add $n - 1$ more vertices to form a larger tree. She will add the vertices one by one. When she adds vertex $i$ ($2 \leq i \leq n$), she'll give you an integer $p_i$ ($1 \leq p_i < i$), and add a new edge connecting vertices $i$ and $p_i$.

Iris wants to ask you the binary depth of the tree formed by the first $i$ vertices for each $1 \le i \le n$. Can you tell her the answer?

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 a single integer $n$ ($2 \leq n \leq 5 \cdot 10^5$) — the final size of the tree.

The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) — descriptions of all edges of the tree.

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 $n$ integers, $i$-th of them representing the binary depth of the tree formed by the first $i$ vertices.

Tags:
['brute force', 'data structures', 'dfs and similar', 'trees']

Time Limit: 4000 ms
Memory Limit: 1024 MB
Sadly, Dora poured the paint when painting the class mural. Dora considers the mural as the matrix $b$ of size $n \times n$. Initially, $b_{i,j} = 0$ for all $1 \le i, j \le n$.

Dora has only two brushes which have two different colors. In one operation, she can paint the matrix with one of two brushes:

  * The first brush has color $1$ on it and can paint one column of the matrix. That is, Dora chooses $1 \leq j \leq n$ and makes $b_{i,j} := 1$ for all $1 \leq i \leq n$;    * The second brush has color $2$ on it and can paint one row of the matrix. That is, Dora chooses $1 \leq i \leq n$ and makes $b_{i,j} := 2$ for all $1 \leq j \leq n$. 

Dora paints the matrix so that the resulting matrix $b$ contains only $1$ and $2$.

For a matrix $b$, let $f(b)$ denote the minimum number of operations needed to turn the initial matrix (containing only $0$) into $b$. The beauty of a matrix $b$ is the number of ways to paint the initial matrix in exactly $f(b)$ operations to turn it into $b$. If there's no way to turn the initial matrix into $b$, the beauty of $b$ is $0$.

However, Dora made a uniformly random mistake; there's exactly one element different in the matrix $a$ given to you from the real matrix $b$. That is, there is exactly one pair $(i, j)$ such that $a_{i, j} = 3 - b_{i, j}$.

Please help Dora compute the expected beauty of the real matrix $b$ modulo $998\,244\,353$ (all possible $n^2$ mistakes have equal probability).

Since the size of the matrix is too large, Dora will only tell you the positions of $m$ elements of color $1$, and the remaining $n^2-m$ elements have color $2$.

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 two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq \min(10^6, n^2)$) — the size of the matrix and the number of elements of color $1$.

Then $m$ lines follow, each containing two positive integers $x_i$ and $y_i$ ($1 \leq x_i, y_i \leq n$) — denoting that $a_{x_i, y_i} = 1$.

It is guaranteed that if $i \neq j$, then $(x_i, y_i) \neq (x_j, y_j)$.

It is also guaranteed that the sum of $n$ over all test cases does not exceed $4\cdot10^5$, 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 expected beauty of the real matrix $b$, modulo $998\,244\,353$.

Tags:
['brute force', 'combinatorics', 'constructive algorithms', 'graphs', 'implementation']

Time Limit: 3000 ms
Memory Limit: 512 MB
Dora has a set $s$ containing integers. In the beginning, she will put all integers in $[l, r]$ into the set $s$. That is, an integer $x$ is initially contained in the set if and only if $l \leq x \leq r$. Then she allows you to perform the following operations:

  * Select three distinct integers $a$, $b$, and $c$ from the set $s$, such that $\gcd(a, b) = \gcd(b, c) = \gcd(a, c) = 1^\dagger$.    * Then, remove these three integers from the set $s$. 

What is the maximum number of operations you can perform?

$^\dagger$Recall that $\gcd(x, y)$ means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$.

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

The only line of each test case contains two integers $l$ and $r$ ($1 \leq l \leq r \leq 1000$) — the range of integers in the initial set.

Output Format:
For each test case, output a single integer — the maximum number of operations you can perform.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it.

Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types:

  * $\texttt{+ l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i + 1$.    * $\texttt{- l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i - 1$. 

For example, if the initial array $a = [7, 1, 3, 4, 3]$, after performing the operation $\texttt{+} \space 2 \space 4$, the array $a = [7, 1, 4, 5, 4]$. Then, after performing the operation $\texttt{-} \space 1 \space 10$, the array $a = [6, 0, 3, 4, 3]$.

Index is curious about the maximum value in the array $a$. Please help her find it after each of the $m$ operations.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. 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^5$, $1 \leq m \leq 10^5$) — the length of the array and the number of operations.

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

Then $m$ lines follow, each line corresponds to the operation, in the following format: $\texttt{c l r}$ ($c \in \\{\texttt +, \texttt -\\}$, $l$ and $r$ are integers, $1 \leq l \leq r \leq 10^9$) — the description of the operation.

Note that the elements $a_i$ may not satisfy $1\le a_i\le 10^9$ after some operations, as it is shown in the example.

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

Output Format:
For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.

Tags:
['data structures', 'greedy']

Time Limit: 1000 ms
Memory Limit: 256 MB
Dora has just learned the programming language C++!

However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array $c$ with $n$ elements. Dora has two integers $a$ and $b$. In one operation, she can choose one of the following things to do.

  * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $a$.    * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $b$. 

Note that $a$ and $b$ are constants, and they can be the same.

Let's define a range of array $d$ as $\max(d_i) - \min(d_i)$. For instance, the range of the array $[1, 2, 3, 4]$ is $4 - 1 = 3$, the range of the array $[5, 2, 8, 2, 2, 1]$ is $8 - 1 = 7$, and the range of the array $[3, 3, 3]$ is $3 - 3 = 0$.

After any number of operations (possibly, $0$), Dora calculates the range of the new array. You need to help Dora minimize this value, but since Dora loves exploring all by herself, you only need to tell her the minimized value.

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 test cases follows.

The first line of each test case contains three integers $n$, $a$, and $b$ ($1 \leq n \leq 10^5$, $1 \leq a, b \leq 10^9$) — the length of the array $c$ and the constant values, respectively.

The second line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$) — the initial elements of the array $c$.

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

Output Format:
For each test case, output a single integer — the minimum possible range of the array after any number of operations.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$.

Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it.

Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$.

![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5)

  * Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$.   * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$.

The score of a tree is defined as the number of leaves with non-zero weight in the tree.

But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.

Assuming that both girls play optimally, please determine the final score of the tree.

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

The first line of each test case contains a single integer $n$ ($2 \leq n \leq 10^5$) — the number of vertices in the tree.

The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$) — denoting an edge between vertices $u$ and $v$.

It's guaranteed that the given edges form a tree.

The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$.

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

Output Format:
For each test case, output a single integer — the final score of the tree.

Tags:
['games', 'graphs', 'greedy', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$.

Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer.

The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search.

![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers.

We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree.

Next, there will be $n - 1$ events:

  * Iris is given integers $x$ and $y$, indicating that $t_x = y$. 

After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers.

Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.

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 two integers $n$ and $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) — the number of vertices in the tree and the sum of the edge weights.

The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) — the description of the edges of the tree.

Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$.

It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$.

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 one line containing $n-1$ integers, each representing the answer after each event.

Tags:
['brute force', 'data structures', 'dfs and similar', 'math', 'trees']

Time Limit: 3000 ms
Memory Limit: 256 MB
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set.

Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not.

Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant.

Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.

Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.

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 a single integer $n$ ($1 \leq n \leq 4 \cdot 10^5$) — length of the array $a$.

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

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

Output Format:
For each test case, output a single integer — the number of brilliant subarrays.

Tags:
['binary search', 'data structures', 'number theory', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 512 MB
Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos.

In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$.

Sakurako is not sure if it is possible to solve this problem, so determine whether there is a way to assign signs such that the sum of all elements in the array equals $0$.

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

The only line of each test case contains two integers $a$ and $b$ ($0\le a,b<10$)  — the number of '1's and the number of '2's in the array.

Output Format:
For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, and "No" otherwise.

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Tags:
['brute force', 'constructive algorithms', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
A beautiful binary matrix is a matrix that has ones on its edges and zeros inside.

![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices.

Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows of the matrix, starting from the first and ending with the $r$-th. More formally, the element from the matrix in the $i$-th row and $j$-th column corresponds to the $((i-1)*c+j)$-th element of the string.

You need to check whether the beautiful matrix from which the string $s$ was obtained could be squared. In other words, you need to check whether the string $s$ could have been build from a square beautiful binary matrix (i.e., one where $r=c$).

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

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the string.

The second line of each test case contains the string $s$ of length $n$. The string is always the result of writing out the strings of a beautiful matrix.

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

Output Format:
Print "Yes", if the original matrix could have been square, and "No" otherwise.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if:

  * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$;    * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$. 

Sakurako has come up with boundaries $l$ and $r$ and wants to construct a good array of maximum length, where $l \le a_i \le r$ for all $a_i$.

Help Sakurako find the maximum length of a good array for the given $l$ and $r$.

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

The only line of each test case contains two integers $l$ and $r$ ($1\le l\le r\le 10^9$).

Output Format:
For each test case, output a single integer  — the length of the longest good array Sakurako can form given $l$ and $r$.

Tags:
['binary search', 'brute force', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB