inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
I couldn't think of a good title for this problem, so I decided to learn from LeetCode.

— Sun Tzu, The Art of War

You are given three integers $x_c$, $y_c$, and $k$ ($-100 \leq x_c, y_c \leq 100$, $1 \leq k \leq 1000$).

You need to find $k$ distinct points ($x_1, y_1$), ($x_2, y_2$), $\ldots$, ($x_k, y_k$), having integer coordinates, on the 2D coordinate plane such that:

  * their center$^{\text{∗}}$ is ($x_c, y_c$)    * $-10^9 \leq x_i, y_i \leq 10^9$ for all $i$ from $1$ to $k$ 

It can be proven that at least one set of $k$ distinct points always exists that satisfies these conditions.

$^{\text{∗}}$The center of $k$ points ($x_1, y_1$), ($x_2, y_2$), $\ldots$, ($x_k, y_k$) is $\left( \frac{x_1 + x_2 + \ldots + x_k}{k}, \frac{y_1 + y_2 + \ldots + y_k}{k} \right)$.

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

Each test case contains three integers $x_c$, $y_c$, and $k$ ($-100 \leq x_c, y_c \leq 100$, $1 \leq k \leq 1000$) — the coordinates of the center and the number of distinct points you must output.

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

Output Format:
For each test case, output $k$ lines, the $i$-th line containing two space separated integers, $x_i$ and $y_i$, ($-10^9 \leq x_i, y_i \leq 10^9$) — denoting the position of the $i$-th point.

If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constraints.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
It is known that [Farmer John likes Permutations](https://usaco.org/index.php?page=viewproblem2&cpid=1421), but I like them too!

— Sun Tzu, The Art of Constructing Permutations

You are given a permutation$^{\text{∗}}$ $p$ of length $n$.

Find a permutation $q$ of length $n$ that minimizes the number of pairs ($i, j$) ($1 \leq i \leq j \leq n$) such that $p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$.

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

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

The following line contains $n$ space-separated integers $p_1, p_2, \ldots, p_n$ ($1 \leq p_i \leq n$) — denoting the permutation $p$ of length $n$.

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 any permutation of length $n$ (the permutation $q$) such that $q$ minimizes the number of pairs.

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

Time Limit: 1500 ms
Memory Limit: 256 MB
I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you.

— satyam343's biggest fan

You are given an array $a$ of length $n$ and an integer $k$. You are also given a binary array $b$ of length $n$.

You can perform the following operation at most $k$ times:

  * Select an index $i$ ($1 \leq i \leq n$) such that $b_i = 1$. Set $a_i = a_i + 1$ (i.e., increase $a_i$ by $1$). 

Your score is defined to be $\max\limits_{i = 1}^{n} \left( a_i + \operatorname{median}(c_i) \right)$, where $c_i$ denotes the array of length $n-1$ that you get by deleting $a_i$ from $a$. In other words, your score is the maximum value of $a_i + \operatorname{median}(c_i)$ over all $i$ from $1$ to $n$.

Find the maximum score that you can achieve if you perform the operations optimally.

For an arbitrary array $p$, $\operatorname{median}(p)$ is defined as the $\left\lfloor \frac{|p|+1}{2} \right\rfloor$-th smallest element of $p$. For example, $\operatorname{median} \left( [3,2,1,3] \right) = 2$ and $\operatorname{median} \left( [6,2,4,5,1] \right) = 4$.

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

Each test case begins with two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^9$) — the length of the $a$ and the number of operations you can perform.

The following line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — denoting the array $a$.

The following line contains $n$ space separated integers $b_1, b_2, \ldots, b_n$ ($b_i$ is $0$ or $1$) — denoting the array $b$.

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 score you can get on a new line.

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

Time Limit: 3000 ms
Memory Limit: 256 MB
MOOOOOOOOOOOOOOOOO

— Bessie the Cow, The Art of Racing on Islands

Two of Farmer John's cows, Bessie and Elsie, are planning to race on $n$ islands. There are $n - 1$ main bridges, connecting island $i$ to island $i + 1$ for all $1 \leq i \leq n - 1$. Additionally, there are $m$ alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index.

Initially, Elsie starts on island $1$, and Bessie starts on island $s$. The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island $i$. During a cow's turn, if there are any bridges connecting island $i$ to island $j$, then the cow can move to island $j$. Then, island $i$ collapses, and all bridges connecting to island $i$ also collapse. Also, note the following:

  * If there are no bridges connecting island $i$ to another island, then island $i$ collapses, and this cow is eliminated from the race.    * If the other cow is also on island $i$, then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race.    * After an island or bridge collapses, no cows may use them.    * If a cow is eliminated, their turn is skipped for the rest of the race. 

The race ends once either cow reaches island $n$. It can be shown that regardless of the cows' strategies, at least one cow reaches island $n$. Bessie wins if and only if she reaches island $n$ first.

For each $1 \leq s \leq n - 1$, determine whether Bessie wins if she starts the race on island $s$. Assume both cows follow optimal strategies to ensure their own respective victories.

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 $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq 2 \cdot 10^5$) – the number of islands and the number of alternative bridges.

The next $m$ lines of each test case contain $u$ and $v$ ($1 \leq u < v \leq n$) – the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges.

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

Output Format:
For each test case, output a binary string of length $n - 1$ on a new line. The $i$'th character is $1$ if it is possible for Bessie to win if she starts on island $i$. Otherwise, it is $0$.

Tags:
['data structures', 'dp', 'graphs', 'greedy', 'shortest paths']

Time Limit: 2000 ms
Memory Limit: 256 MB
Drink water.

— Sun Tzu, The Art of Becoming a Healthy Programmer

This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack.

You are given two integers $n$ and $x$ ($x=n$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball.

For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows:

  * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$.

  * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$.

    * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.      * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.      * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$:        * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.        * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. 

  * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times. 

For each integer $i$ from $x$ to $n$, you need to find $f(i)$.

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 $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = n$) — the number of balls and the smallest index $i$ for which you need to find $f(i)$.

The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the initial number written on each ball.

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-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.

Tags:
['binary search', 'brute force', 'data structures', 'divide and conquer', 'greedy']

Time Limit: 4000 ms
Memory Limit: 512 MB
Drink water.

— Sun Tzu, The Art of Becoming a Healthy Programmer

This is the hard version of the problem. The only difference is that $x=1$ in this version. You must solve both versions to be able to hack.

You are given two integers $n$ and $x$ ($x=1$). There are $n$ balls lined up in a row, numbered from $1$ to $n$ from left to right. Initially, there is a value $a_i$ written on the $i$-th ball.

For each integer $i$ from $1$ to $n$, we define a function $f(i)$ as follows:

  * Suppose you have a set $S = \\{1, 2, \ldots, i\\}$.

  * In each operation, you have to select an integer $l$ ($1 \leq l < i$) from $S$ such that $l$ is not the largest element of $S$. Suppose $r$ is the smallest element in $S$ which is greater than $l$.

    * If $a_l > a_r$, you set $a_l = a_l + a_r$ and remove $r$ from $S$.      * If $a_l < a_r$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.      * If $a_l = a_r$, you choose either the integer $l$ or $r$ to remove from $S$:        * If you choose to remove $l$ from $S$, you set $a_r = a_l + a_r$ and remove $l$ from $S$.        * If you choose to remove $r$ from $S$, you set $a_l = a_l + a_r$ and remove $r$ from $S$. 

  * $f(i)$ denotes the number of integers $j$ ($1 \le j \le i$) such that it is possible to obtain $S = \\{j\\}$ after performing the above operations exactly $i - 1$ times. 

For each integer $i$ from $x$ to $n$, you need to find $f(i)$.

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 $n$ and $x$ ($1 \leq n \leq 2 \cdot 10^5; x = 1$) — the number of balls and the smallest index $i$ for which you need to find $f(i)$.

The second line of each test case contains $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) — the initial number written on each ball.

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-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB
Given a two-digit positive integer $n$, find the sum of its digits.

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

The only line of each test case contains a single two-digit positive integer $n$ ($10 \leq n \leq 99$).

Output Format:
For each test case, output a single integer — the sum of the digits of $n$.

Tags:
['implementation', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Suneet and Slavic play a card game. The rules of the game are as follows:

  * Each card has an integer value between $1$ and $10$.    * Each player receives $2$ cards which are face-down (so a player doesn't know their cards).    * The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round.    * A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game. 

Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner.

For a better understanding, please check the notes section.

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

The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic has, respectively.

Output Format:
For each test case, output a single integer — the number of games Suneet would win considering all possible games.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
As a computer science student, Alex faces a hard challenge — showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes!

He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $l_i$ and $r_i$). No two tasks overlap.

Given all $n$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $s$?

![](CDN_BASE_URL/5d5195053b99e5c6936ccefadc239679)

In the first test case, Alex can shower for the first $3$ minutes of the day and not miss any of the tasks.

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$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) — the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.

Then $n$ lines follow, the $i$-th of which contains two integers $l_i$ and $r_i$ ($0 \leq l_i < r_i \leq m$) — the time interval of the $i$-th task. No two tasks overlap.

Additional constraint on the input: $l_i > r_{i-1}$ for every $i > 1$.

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

Output Format:
For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also 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:
['greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
Slavic has a very tough exam and needs your help in order to pass it. Here is the question he is struggling with:

There exists a string $s$, which consists of lowercase English letters and possibly zero or more "?".

Slavic is asked to change each "?" to a lowercase English letter such that string $t$ becomes a subsequence (not necessarily continuous) of the string $s$.

Output any such string, or say that it is impossible in case no string that respects the conditions exists.

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 string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$, and $s$ consists only of lowercase English letters and "?"-s)  – the original string you have.

The second line of each test case contains a single string $t$ ($1 \leq |t| \leq |s|$, and $t$ consists only of lowercase English letters)  – the string that should be a subsequence of string $s$.

The sum of $|s|$ over all test cases doesn't exceed $2 \cdot 10^5$, where $|x|$ denotes the length of the string $x$.

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

Otherwise, output "YES" (without quotes). Then, output one line — the string that respects all conditions.

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:
['greedy', 'implementation', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
On the board Ivy wrote down all integers from $l$ to $r$, inclusive.

In an operation, she does the following:

  * pick two numbers $x$ and $y$ on the board, erase them, and in their place write the numbers $3x$ and $\lfloor \frac{y}{3} \rfloor$. (Here $\lfloor \bullet \rfloor$ denotes rounding down to the nearest integer).

What is the minimum number of operations Ivy needs to make all numbers on the board equal $0$? We have a proof that this is always possible.

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

The only line of each test case contains two integers $l$ and $r$ ($1 \leq l < r \leq 2 \cdot 10^5$).

Output Format:
For each test case, output a single integer — the minimum number of operations needed to make all numbers on the board equal $0$.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Arul has a binary array$^{\text{∗}}$ $a$ of length $n$.

He will take all subsequences$^{\text{†}}$ of length $k$ ($k$ is odd) of this array and find their median.$^{\text{‡}}$

What is the sum of all these values?

As this sum can be very large, output it modulo $10^9 + 7$. In other words, print the remainder of this sum when divided by $10^9 + 7$.

$^{\text{∗}}$A binary array is an array consisting only of zeros and ones.

$^{\text{†}}$An array $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements. Subsequences don't have to be contiguous.

$^{\text{‡}}$The median of an array of odd length $k$ is the $\frac{k+1}{2}$-th element when sorted.

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 two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$, $k$ is odd) — the length of the array and the length of the subsequence, respectively.

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

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

Output Format:
For each test case, print the sum modulo $10^9 + 7$.

Tags:
['combinatorics', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB
This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{10}$ queries.

This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307).

We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values:

  * If $y < x$, the ruler (correctly) measures the object as having length $y$.    * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$. 

![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8)

The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$.

You need to find the value of $x$. To do that, you can make queries of the following form:

  * $\texttt{?}~a~b$ — in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you. 

Find the value of $x$. You can ask at most $\mathbf{10}$ queries.

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

Output Format:


Tags:
['binary search', 'interactive']

Time Limit: 1000 ms
Memory Limit: 256 MB
This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{7}$ queries.

This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307).

We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values:

  * If $y < x$, the ruler (correctly) measures the object as having length $y$.    * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$. 

![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8)

The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$.

You need to find the value of $x$. To do that, you can make queries of the following form:

  * $\texttt{?}~a~b$ — in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you. 

Find the value of $x$. You can ask at most $\mathbf{7}$ queries.

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

Output Format:


Tags:
['binary search', 'interactive', 'ternary search']

Time Limit: 1000 ms
Memory Limit: 256 MB
Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad.

The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' when writing the important integer. For example, instead of the integer $10^5$, he would have written $105$, and instead of $10^{19}$, he would have written $1019$.

Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.

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

The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) — the next integer from the board.

Output Format:
For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise.

You may 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:
['implementation', 'math', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB