inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position $x=0$. They will be taking turns, and Sakurako will be the one to start.

On the $i$-th move, the current player will move the dot in some direction by $2\cdot i-1$ units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction.

In other words, the following will happen:

  1. Sakurako will change the position of the dot by $-1$, $x = -1$ now    2. Kosuke will change the position of the dot by $3$, $x = 2$ now    3. Sakurako will change the position of the dot by $-5$, $x = -3$ now    4. $\cdots$ 

They will keep on playing while the absolute value of the coordinate of the dot does not exceed $n$. More formally, the game continues while $-n\le x\le n$. It can be proven that the game will always end.

Your task is to determine who will be the one who makes the last turn.

Input Format:
The first line contains one integer $t$ ($1\le t\le 100$) — the number of games that Sakurako and Kosuke played.

Each game is described by one number $n$ ($1 \le n\le 100$) — the number that defines the condition when the game ends.

Output Format:
For each of the $t$ games, output a line with the result of that game. If Sakurako makes the last turn, output "Sakurako" (without quotes); else output "Kosuke".

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

Time Limit: 1000 ms
Memory Limit: 256 MB
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there.

Kosuke is very afraid of water, so Sakurako needs to help him:

  * With her magic, she can select a square area of mountains and increase the height of each mountain on the main diagonal of that area by exactly one. 

More formally, she can choose a submatrix with the upper left corner located at $(i, j)$ and the lower right corner at $(p, q)$, such that $p-i=q-j$. She can then add one to each element at the intersection of the $(i + k)$-th row and the $(j + k)$-th column, for all $k$ such that $0 \le k \le p-i$.

Determine the minimum number of times Sakurako must use her magic so that there are no lakes.

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

Each test case is described as follows:

  * The first line of each test case consists of a single number $n$ ($1 \le n \le 500$).    * Each of the following $n$ lines consists of $n$ integers separated by spaces, which correspond to the heights of the mountains in the valley $a$ ($-10^5 \le a_{i,j} \le 10^5$). 

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

Output Format:
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Even in university, students need to relax. That is why Sakurakos teacher decided to go on a field trip. It is known that all of the students will be walking in one line. The student with index $i$ has some topic of interest which is described as $a_i$. As a teacher, you want to minimise the disturbance of the line of students.

The disturbance of the line is defined as the number of neighbouring people with the same topic of interest. In other words, disturbance is the number of indices $j$ ($1 \le j < n$) such that $a_j = a_{j + 1}$.

In order to do this, you can choose index $i$ ($1\le i\le n$) and swap students at positions $i$ and $n-i+1$. You can perform any number of swaps.

Your task is to determine the minimal amount of disturbance that you can achieve by doing the operation described above any number of times.

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

Each test case is described by two lines.

  * The first line contains one integer $n$ ($2 \le n \le 10^5$) — the length of the line of students.    * The second line contains $n$ integers $a_i$ ($1\le a_i\le n$) — the topics of interest of students in line. 

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

Output Format:
For each test case, output the minimal possible disturbance of the line that you can achieve.

Tags:
['dp', 'greedy', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segments of the array $a$, such that each segment is considered beautiful.

A segment $[l,r]$ is considered beautiful if $a_l + a_{l+1} + \dots + a_{r-1} + a_r=0$.

For a fixed array $a$, your task is to compute the maximum number of non- overlapping beautiful segments.

Input Format:
The first line of input contains the number $t$ ($1 \le t \le 10^4$) — the number of test cases. Each test case consists of $2$ lines.

  * The first line contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array.   * The second line contains $n$ integers $a_i$ ($-10^5 \le a_i \le 10^5$) — the elements of the array $a$. 

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

Output Format:
For each test case, output a single integer: the maximum number of non- overlapping beautiful segments.

Tags:
['data structures', 'dp', 'dsu', 'greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $p$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple.

A permutation $p$ is considered simple if for every $i$ $(1\le i \le n)$ one of the following conditions holds:

  * $p_i=i$    * $p_{p_i}=i$ 

For example, the permutations $[1, 2, 3, 4]$, $[5, 2, 4, 3, 1]$, and $[2, 1]$ are simple, while $[2, 3, 1]$ and $[5, 2, 1, 4, 3]$ are not.

In one operation, Kosuke can choose indices $i,j$ $(1\le i,j\le n)$ and swap the elements $p_i$ and $p_j$.

Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple.

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

Each test case is described by two lines.

  * The first line contains one integer $n$ ($1\le n \le 10^6$) — the length of the permutation $p$.    * The second line contains $n$ integers $p_i$ ($1\le p_i\le n$) — the elements of the permutation $p$. 

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

It is guaranteed that $p$ is a permutation.

Output Format:
For each test case, output the minimum number of operations that Kosuke needs to perform to make the permutation simple.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Kosuke is too lazy. He will not give you any legend, just the task:

Fibonacci numbers are defined as follows:

  * $f(1)=f(2)=1$.    * $f(n)=f(n-1)+f(n-2)$ $(3\le n)$ 

We denote $G(n,k)$ as an index of the $n$-th Fibonacci number that is divisible by $k$. For given $n$ and $k$, compute $G(n,k)$.

As this number can be too big, output it by modulo $10^9+7$.

For example: $G(3,2)=9$ because the $3$-rd Fibonacci number that is divisible by $2$ is $34$. $[1,1,\textbf{2},3,5,\textbf{8},13,21,\textbf{34}]$.

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

The first and only line contains two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^5$).

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

Output Format:
For each test case, output the only number: the value $G(n,k)$ taken by modulo $10^9+7$.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away.

To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina.

Also, for each guess, Kosuke assumes that Chefir could move along the edges an arbitrary number of times:

  * from vertex $a$ to vertex $b$, if $a$ is an ancestor$^{\text{∗}}$ of $b$, the stamina will not change;    * from vertex $a$ to vertex $b$, if $a$ is not an ancestor of $b$, then Chefir's stamina decreases by $1$. 

If Chefir's stamina is $0$, he cannot make a move of the second type.

For each assumption, your task is to find the distance to the farthest vertex that Chefir could reach from vertex $v_i$, having $k_i$ stamina.

$^{\text{∗}}$Vertex $a$ is an ancestor of vertex $b$ if the shortest path from $b$ to the root passes through $a$.

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

Each test case is described as follows:

  * The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.   * The next $n-1$ lines contain the edges of the tree. It is guaranteed that the given edges form a tree.   * The next line consists of a single integer $q$ $(1\le q\le 2 \cdot 10^5)$, which denotes the number of guesses made by Kosuke.   * The next $q$ lines describe the guesses made by Kosuke, with two integers $v_i$, $k_i$ $(1\le v_i \le n, 0 \le k_i\le n)$. 

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

Output Format:
For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina.

Tags:
['data structures', 'dfs and similar', 'dp', 'greedy', 'trees']

Time Limit: 4000 ms
Memory Limit: 256 MB
Red was ejected. They were not the imposter.

There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\cdot m+c$ is initially at $(r,c)$.

The person at $(r, c)$ decides to leave. To fill the gap, let the person who left be numbered $i$. Each person numbered $j>i$ will move to the position where the person numbered $j-1$ is initially at. The following diagram illustrates the case where $n=2$, $m=3$, $r=1$, and $c=2$.

![](CDN_BASE_URL/9b0b8e601446e3410296d7c9b1ff8763)

Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at $(r_0, c_0)$ and then moved to $(r_1, c_1)$, the Manhattan distance is $|r_0-r_1|+|c_0-c_1|$.

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 testcase contains $4$ integers $n$, $m$, $r$, and $c$ ($1\le r\le n\le 10^6$, $1 \le c \le m \le 10^6$), where $n$ is the number of rows, $m$ is the number of columns, and $(r,c)$ is the position where the person who left is initially at.

Output Format:
For each test case, output a single integer denoting the sum of the Manhattan distances.

Tags:
['implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
There are 3 heroes and 3 villains, so 6 people in total.

Given a positive integer $n$. Find the smallest integer whose decimal representation has length $n$ and consists only of $3$s and $6$s such that it is divisible by both $33$ and $66$. If no such integer exists, print $-1$.

Input Format:
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 a single integer $n$ ($1\le n\le 500$) — the length of the decimal representation.

Output Format:
For each test case, output the smallest required integer if such an integer exists and $-1$ otherwise.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Alya has been given a hard problem. Unfortunately, she is too busy running for student council. Please solve this problem for her.

Given an integer $n$, construct a permutation $p$ of integers $1, 2, \ldots, n$ that maximizes the value of $k$ (which is initially $0$) after the following process.

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

  * If $i$ is odd, $k=k\,\&\,p_i$, where $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).    * If $i$ is even, $k=k\,|\,p_i$, where $|$ denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

Input Format:
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 a single integer $n$ ($5\le n\le 2 \cdot 10^5$) — the length of the permutation.

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 the maximum value of $k$ in the first line and output the permutation $p_1, p_2,\ldots, p_n$ in the second line.

If there are multiple such permutations, output any.

Tags:
['bitmasks', 'constructive algorithms', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
Three r there are's in strawberry.

You are given an array $b$ of length $m$. You can perform the following operation any number of times (possibly zero):

  * Choose two distinct indices $i$ and $j$ where $\bf{1\le i < j\le m}$ and $b_i$ is even, divide $b_i$ by $2$ and multiply $b_j$ by $2$. 

Your task is to maximize the sum of the array after performing any number of such operations. Since it could be large, output this sum modulo $10^9+7$.

Since this problem is too easy, you are given an array $a$ of length $n$ and need to solve the problem for each prefix of $a$.

In other words, denoting the maximum sum of $b$ after performing any number of such operations as $f(b)$, you need to output $f([a_1])$, $f([a_1,a_2])$, $\ldots$, $f([a_1,a_2,\ldots,a_n])$ modulo $10^9+7$ respectively.

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$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$.

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

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

Output Format:
For each test case, output $n$ integers representing the answer for each prefix of $a$ modulo $10^9+7$.

Tags:
['binary search', 'data structures', 'divide and conquer', 'greedy', 'implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB
Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me...

You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations.

  * Increase $d$ — the damage of your weapon by $1$, costing $x$ coins.    * Attack the monster, dealing $d$ damage and costing $y$ coins. 

You cannot perform the first operation for more than $k$ times in a row.

Find the minimum number of coins needed to defeat the monster by dealing at least $z$ damage.

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 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) — the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.

Output Format:
For each test case, output the minimum number of coins needed to defeat the monster.

Tags:
['binary search', 'brute force', 'constructive algorithms', 'greedy', 'implementation', 'math', 'ternary search']

Time Limit: 2000 ms
Memory Limit: 256 MB
This really says a lot about our society.

One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$.

You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $u$ as choosing a single node in $u$'s subtree$^{\text{∗}}$ and incrementing or decrementing its value by $1$. The order in which operations are performed on nodes is as follows:

  * For $1 \le i \le n$, the $i$-th operation will be performed on node $i$.    * For $i > n$, the $i$-th operation will be performed on the same node as operation $i - n$. 

More formally, the $i$-th operation will be performed on the $(((i - 1) \bmod n) + 1)$-th node.$^{\text{†}}$

Note that you cannot skip over operations; that is, you cannot perform the $i$-th operation without first performing operations $1, 2, \ldots, i - 1$.

Find the minimum number of operations you must perform before you can make the values of all nodes equal to $0$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $0$ after finite operations, output $-1$.

$^{\text{∗}}$The subtree of a node $u$ is the set of nodes for which $u$ lies on the shortest path from this node to the root, including $u$ itself.

$^{\text{†}}$Here, $a \bmod b$ denotes the remainder from dividing $a$ by $b$.

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

The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) — the number of nodes and the root of the tree.

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 starting value of each node.

Each of the next $n - 1$ lines of each test case contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) representing an undirected edge from $u$ to $v$. It is guaranteed that the given edges form a tree.

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

Output Format:
For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.

Tags:
['binary search', 'brute force', 'dfs and similar', 'dp', 'trees']

Time Limit: 4000 ms
Memory Limit: 256 MB
The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved.

Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trenches! Fortunately, she is applying for a job at Moogle to resolve this issue. Moogle interviews require intensive knowledge of obscure algorithms and complex data structures, but Bessie received a tip-off from an LGM on exactly what she has to go learn.

Bessie wrote the following code to binary search for a certain element $k$ in a possibly unsorted array $[a_1, a_2,\ldots,a_n]$ with $n$ elements.

              let l = 1     let h = n          while l < h:       let m = floor((l + h) / 2)            if a[m] < k:         l = m + 1       else:         h = m          return l     

Bessie submitted her code to Farmer John's problem with $m$ ($1 \leq m \leq n$) tests. The $i$-th test is of the form $(x_i, k_i)$ ($1 \leq x, k \leq n$). It is guaranteed all the $x_i$ are distinct and all the $k_i$ are distinct.

Test $i$ is correct if the following hold:

  1. The $x_i$-th element in the array is $k_i$.    2. If Bessie calls the binary search as shown in the above code for $k_i$, it will return $x_i$. 

It might not be possible for all $m$ tests to be correct on the same array, so Farmer John will remove some of them so Bessie can AC. Let $r$ be the minimum of tests removed so that there exists an array $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ so that all remaining tests are correct.

In addition to finding $r$, Farmer John wants you to count the number of arrays $[a_1, a_2,\ldots,a_n]$ with $1 \leq a_i \leq n$ such that there exists a way to remove exactly $r$ tests so that all the remaining tests are correct. Since this number may be very large, please find it modulo $998\,244\,353$.

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 two integers $n$ and $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests.

The following $m$ lines each contain two integers, describing the tests. The $i$-th line contains two integers $x_i$ and $k_i$ ($1 \leq x_i, k_i \leq n$) denoting the index and value of the test. It is guaranteed all $x_i$ are distinct and all $k_i$ are distinct.

It is guaranteed the sum of $n$ across all test cases does not exceed $10^4$.

Output Format:
For each test case, output two integers, $r$ — the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.

Tags:
['dp', 'trees']

Time Limit: 5000 ms
Memory Limit: 256 MB