inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.

Suppose Nikita has an array of integers $a$ of length $n$. He will call a subsequence$^\dagger$ of the array special if its [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple) is not contained in $a$. The LCM of an empty subsequence is equal to $0$.

Nikita wonders: what is the length of the longest special subsequence of $a$? Help him answer this question!

$^\dagger$ A sequence $b$ is a subsequence of $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements, without changing the order of the remaining elements. For example, $[5,2,3]$ is a subsequence of $[1,5,7,8,2,4,3]$.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 2000$) — 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 \le n \le 2000$) — the length of the array $a$.

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

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 — the length of the longest special subsequence of $a$.

Tags:
['brute force', 'data structures', 'dp', 'greedy', 'math', 'number theory', 'sortings']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a binary (consisting only of 0s and 1s) $n \times m$ matrix. You are also given a XORificator, using which you can invert all the values in a chosen row (i.e. replace 0 with 1 and 1 with 0).

A column in the matrix is considered special if it contains exactly one 1. Your task is to find the maximum number of columns that can be made special at the same time, and the set of rows the XORificator should be used on to achieve that.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 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, m \leq 3 \cdot 10^5$, $n \cdot m \leq 3 \cdot 10^5$).

Each of the following $n$ lines of the test case contains a binary string of length $m$.

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

Output Format:
For each test case, output two lines.

In the first line, output the maximum number of special columns that is possible to get simultaneously.

In the second line, output a binary string of length $n$, where the $i$-th character is 0, if you don't use the XORificator on the $i$-th row, and 1, if you use the XORificator on the $i$-th row.

If there are multiple valid XORificator configurations that achieve the optimal answer, you can output any of them.

Tags:
['bitmasks', 'brute force', 'greedy', 'hashing']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is an interactive problem.

You are given an integer $n$.

The jury has hidden from you a directed graph with $n$ vertices (numbered from $1$ to $n$) and some number of edges. You additionally know that:

  * The graph only contains edges of the form $i \leftarrow j$, where $1 \le i < j \le n$.    * For any three vertices $1 \le i < j < k \le n$, at least one of the following holds$^\dagger$:      * Vertex $i$ is reachable from vertex $j$, or      * Vertex $i$ is reachable from vertex $k$, or      * Vertex $j$ is reachable from vertex $k$. 

You want to color each vertex in either black or white such that for any two vertices $i$ and $j$ ($1 \le i < j \le n$) of the same color, vertex $i$ is reachable from vertex $j$.

To do that, you can ask queries of the following type:

  * ? i j — is vertex $i$ reachable from vertex $j$ ($1 \le i < j \le n$)? 

Find any valid vertex coloring of the hidden graph in at most $2 \cdot n$ queries. It can be proven that such a coloring always exists.

Note that the grader is not adaptive: the graph is fixed before any queries are made.

$^\dagger$ Vertex $a$ is reachable from vertex $b$ if there exists a [path](https://en.wikipedia.org/wiki/Path_\(graph_theory\)) from vertex $b$ to vertex $a$ in the graph.

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

The only line of each test case contains a single integer $n$ ($3 \le n \le 100$) — the number of vertices in the hidden graph.

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

Output Format:


Tags:
['constructive algorithms', 'graphs', 'interactive']

Time Limit: 3000 ms
Memory Limit: 256 MB
Alice has $n$ books. The $1$-st book contains $a_1$ pages, the $2$-nd book contains $a_2$ pages, $\ldots$, the $n$-th book contains $a_n$ pages. Alice does the following:

  * She divides all the books into two non-empty piles. Thus, each book ends up in exactly one of the two piles.   * Alice reads one book with the highest number in each pile.

Alice loves reading very much. Help her find the maximum total number of pages she can read by dividing the books into two piles.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) — 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 \le n \le 100$) — the number of books Alice has.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the number of pages in each book.

Output Format:
For each test case, output a single integer — the maximum number of pages Alice can read.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
Bob decided to open a bakery. On the opening day, he baked $n$ buns that he can sell. The usual price of a bun is $a$ coins, but to attract customers, Bob organized the following promotion:

  * Bob chooses some integer $k$ ($0 \le k \le \min(n, b)$).   * Bob sells the first $k$ buns at a modified price. In this case, the price of the $i$-th ($1 \le i \le k$) sold bun is $(b - i + 1)$ coins.   * The remaining $(n - k)$ buns are sold at $a$ coins each.

Note that $k$ can be equal to $0$. In this case, Bob will sell all the buns at $a$ coins each.

Help Bob determine the maximum profit he can obtain by selling all $n$ buns.

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

The only line of each test case contains three integers $n$, $a$, and $b$ ($1 \le n, a, b \le 10^9$) — the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.

Output Format:
For each test case, output a single integer — the maximum profit that Bob can obtain.

Tags:
['binary search', 'greedy', 'math', 'ternary search']

Time Limit: 1000 ms
Memory Limit: 256 MB
Let's call the Manhattan value of a permutation$^{\dagger}$ $p$ the value of the expression $|p_1 - 1| + |p_2 - 2| + \ldots + |p_n - n|$.

For example, for the permutation $[1, 2, 3]$, the Manhattan value is $|1 - 1| + |2 - 2| + |3 - 3| = 0$, and for the permutation $[3, 1, 2]$, the Manhattan value is $|3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4$.

You are given integers $n$ and $k$. Find a permutation $p$ of length $n$ such that its Manhattan value is equal to $k$, or determine that no such permutation exists.

$^{\dagger}$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:
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 only line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^{5}, 0 \le k \le 10^{12}$) — the length of the permutation and the required Manhattan value.

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, if there is no suitable permutation, output "No". Otherwise, in the first line, output "Yes", and in the second line, output $n$ distinct integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$) — a suitable permutation.

If there are multiple solutions, output any of them.

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

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Elections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote for the candidate with the lowest number.

The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.

You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number $i$ to participate in the elections, all $a_i$ of his fans will become undecided, and will vote for the candidate with the lowest number.

You are curious to find, for each $i$ from $1$ to $n$, the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win the elections.

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 $c$ ($1 \le n \le 2 \cdot 10^5$, $0 \le c \le 10^9$) — the number of candidates in the elections and the number of undecided people.

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 number of fans for each candidate.

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

Output Format:
For each test case, output $n$ integers, the $i$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
Sasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1.

There is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$:

  1. If $a_{i} = a_{i + 2} =$ 0, then you can assign $b_{i + 1} :=$ 1 ($1 \le i \le k - 2$).    2. If $b_{i} = b_{i + 2} =$ 1, then you can assign $a_{i + 1} :=$ 1 ($1 \le i \le k - 2$). 

Sasha became interested in the following: if we consider the string $a=s_ls_{l+1}\ldots s_r$ and the string $b=t_lt_{l+1}\ldots t_r$, what is the maximum number of 1 characters in the string $a$ that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs $(l_i, r_i)$ that interest him.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 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 \le n \le 2 \cdot 10^5$) — the length of the strings $s$ and $t$.

The second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.

The third line of each test case contains a binary string $t$ of length $n$, consisting of the characters 0 and 1.

The fourth line of each test case contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries.

The $i$-th of the following lines contains two integers $l_{i}$ and $r_{i}$ ($1 \le l_{i} \le r_{i} \le n$) — the boundaries of the $i$-th pair of substrings that interest Sasha.

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

Output Format:
For each test case, output $q$ integers — the answers to all queries.

Tags:
['brute force', 'data structures', 'dp', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
Given an array $a$ of length $n$. Let's construct a square matrix $b$ of size $n \times n$, in which the $i$-th row contains the array $a$ cyclically shifted to the right by $(i - 1)$. For example, for the array $a = [3, 4, 5]$, the obtained matrix is

$$b = \begin{bmatrix} 3 & 4 & 5 \\\ 5 & 3 & 4 \\\ 4 & 5 & 3 \end{bmatrix}$$

Let's construct the following graph:

  * The graph contains $n^2$ vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element $b_{i, j}$ as $(i, j)$.   * We will draw an edge between vertices $(i_1, j_1)$ and $(i_2, j_2)$ if $|i_1 - i_2| + |j_1 - j_2| \le k$ and $\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1$, where $\gcd(x, y)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$. 

Your task is to calculate the number of connected components$^{\dagger}$ in the obtained graph.

$^{\dagger}$A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.

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

The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^6$, $2 \le k \le 2 \cdot 10^6$) — the length of the array and the parameter $k$.

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

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

Output Format:
For each test case, output a single integer — the number of connected components in the obtained graph.

Tags:
['data structures', 'dfs and similar', 'dsu', 'graphs', 'number theory', 'two pointers']

Time Limit: 4000 ms
Memory Limit: 512 MB
Alice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens:

  * Bob chooses two integers $i$ and $j$ ($1 \le i < j \le n$), and then finds the maximum among the integers $a_i, a_{i + 1},\ldots, a_j$;    * If the obtained maximum is strictly greater than $k$, Alice wins, otherwise Bob wins. 

Help Alice find the maximum $k$ at which she is guaranteed to win.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 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 \le n \le 5 \cdot 10^4$) — the number of elements in the array.

The second line of each test case contains $n$ integers $a_1, a_2,\ldots, a_n$ ($1 \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 $5 \cdot 10^4$.

Output Format:
For each test case, output one integer — the maximum integer $k$ at which Alice is guaranteed to win.

Tags:
['brute force', 'greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given two distinct non-negative integers $x$ and $y$. Consider two infinite sequences $a_1, a_2, a_3, \ldots$ and $b_1, b_2, b_3, \ldots$, where

  * $a_n = n \oplus x$;    * $b_n = n \oplus y$. 

Here, $x \oplus y$ denotes the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation of integers $x$ and $y$.

For example, with $x = 6$, the first $8$ elements of sequence $a$ will look as follows: $[7, 4, 5, 2, 3, 0, 1, 14, \ldots]$. Note that the indices of elements start with $1$.

Your task is to find the length of the longest common subsegment$^\dagger$ of sequences $a$ and $b$. In other words, find the maximum integer $m$ such that $a_i = b_j, a_{i + 1} = b_{j + 1}, \ldots, a_{i + m - 1} = b_{j + m - 1}$ for some $i, j \ge 1$.

$^\dagger$A subsegment of sequence $p$ is a sequence $p_l,p_{l+1},\ldots,p_r$, where $1 \le l \le r$.

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

The only line of each test case contains two integers $x$ and $y$ ($0 \le x, y \le 10^9, x \neq y$) — the parameters of the sequences.

Output Format:
For each test case, output a single integer — the length of the longest common subsegment.

Tags:
['bitmasks', 'greedy']

Time Limit: 1000 ms
Memory Limit: 256 MB
You have been offered to play a game. In this game, there are $n$ possible outcomes, and for each of them, you must bet a certain integer amount of coins. In the event that the $i$-th outcome turns out to be winning, you will receive back the amount of coins equal to your bet on that outcome, multiplied by $k_i$. Note that exactly one of the $n$ outcomes will be winning.

Your task is to determine how to distribute the coins in such a way that you will come out ahead in the event of any winning outcome. More formally, the total amount of coins you bet on all outcomes must be strictly less than the number of coins received back for each possible winning outcome.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 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 \le n \le 50$) — the number of outcomes.

The second line of each test case contains $n$ integers $k_1,k_2,\ldots,k_n$ ($2 \le k_i \le 20$) — the multiplier for the amount of coins if the $i$-th outcome turns out to be winning.

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 $-1$ if there is no way to distribute the coins as required. Otherwise, output $n$ integers $x_1, x_2,\ldots, x_n$ ($1 \le x_i \le 10^{9}$) — your bets on the outcomes.

It can be shown that if a solution exists, there is always a solution that satisfies these constraints.

If there are multiple suitable solutions, output any of them.

Tags:
['binary search', 'combinatorics', 'constructive algorithms', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a binary string $s$ of length $n$, consisting of zeros and ones. You can perform the following operation exactly once:

  1. Choose an integer $p$ ($1 \le p \le n$).    2. Reverse the substring $s_1 s_2 \ldots s_p$. After this step, the string $s_1 s_2 \ldots s_n$ will become $s_p s_{p-1} \ldots s_1 s_{p+1} s_{p+2} \ldots s_n$.    3. Then, perform a cyclic shift of the string $s$ to the left $p$ times. After this step, the initial string $s_1s_2 \ldots s_n$ will become $s_{p+1}s_{p+2} \ldots s_n s_p s_{p-1} \ldots s_1$. 

For example, if you apply the operation to the string 110001100110 with $p=3$, after the second step, the string will become 011001100110, and after the third step, it will become 001100110011.

A string $s$ is called $k$-proper if two conditions are met:

  * $s_1=s_2=\ldots=s_k$;    * $s_{i+k} \neq s_i$ for any $i$ ($1 \le i \le n - k$). 

For example, with $k=3$, the strings 000, 111000111, and 111000 are $k$-proper, while the strings 000000, 001100, and 1110000 are not.

You are given an integer $k$, which is a divisor of $n$. Find an integer $p$ ($1 \le p \le n$) such that after performing the operation, the string $s$ becomes $k$-proper, or determine that it is impossible. Note that if the string is initially $k$-proper, you still need to apply exactly one operation to it.

Input Format:
Each test consists of multiple test cases. The first line contains one integer $t$ ($1 \le t \le 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 $k$ ($1 \le k \le n$, $2 \le n \le 10^5$) — the length of the string $s$ and the value of $k$. It is guaranteed that $k$ is a divisor of $n$.

The second line of each test case contains a binary string $s$ of length $n$, consisting of the characters 0 and 1.

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 value of $p$ to make the string $k$-proper, or $-1$ if it is impossible.

If there are multiple solutions, output any of them.

Tags:
['bitmasks', 'brute force', 'constructive algorithms', 'dp', 'greedy', 'hashing', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
The Manhattan distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is defined as: $$|x_1 - x_2| + |y_1 - y_2|.$$

We call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal.

You are given a set of pairwise distinct points and an even integer $d$. Your task is to find any Manhattan triangle, composed of three distinct points from the given set, where the Manhattan distance between any pair of vertices is equal to $d$.

Input Format:
Each test consists of multiple test cases. The first line contains one integer $t$ ($1 \le t \le 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 $d$ ($3 \le n \le 2 \cdot 10^5$, $2 \le d \le 4 \cdot 10^5$, $d$ is even) — the number of points and the required Manhattan distance between the vertices of the triangle.

The $(i + 1)$-th line of each test case contains two integers $x_i$ and $y_i$ ($-10^5 \le x_i, y_i \le 10^5$) — the coordinates of the $i$-th point. It is guaranteed that all points are pairwise distinct.

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 three distinct integers $i$, $j$, and $k$ ($1 \le i,j,k \le n$) — the indices of the points forming the Manhattan triangle. If there is no solution, output "$0\ 0\ 0$" (without quotes).

If there are multiple solutions, output any of them.

Tags:
['binary search', 'constructive algorithms', 'data structures', 'geometry', 'implementation', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 256 MB