inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
378QAQ has a tree with $n$ vertices. Initially, all vertices are white.

There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order:

  1. Move $P_A$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red.    2. Move $P_B$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue. 

Initially, the vertex $a$ is painted red. If $a=b$, the vertex $a$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.

378QAQ wants to know the minimum number of steps to paint all vertices blue.

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

The first line of each test case contains one integer $n$ ($1\leq n\leq 2\cdot 10^5$).

The second line of each test case contains two integers $a$ and $b$ ($1\leq a,b\leq n$).

Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.

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 minimum number of steps to paint all vertices blue.

Tags:
['brute force', 'dfs and similar', 'dp', 'greedy', 'shortest paths', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given a tree of $n$ vertices numbered from $1$ to $n$. Initially, all vertices are colored white or black.

You are asked to perform $q$ queries:

  * "u" — toggle the color of vertex $u$ (if it was white, change it to black and vice versa). 

After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.

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

The first line of each test case contains two integers $n$ and $q$ ($1\leq n,q\leq 2\cdot 10^5$).

The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($c_i \in \\{ \mathtt{0}, \mathtt{1} \\}$) — the initial color of the vertices. $c_i$ denotes the color of vertex $i$ where $\mathtt{0}$ denotes the color white, and $\mathtt{1}$ denotes the color black.

Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.

The following $q$ lines each contain an integer $u_i$ ($1 \le u_i \le n$), indicating the color of vertex $u_i$ needs to be toggled.

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

Output Format:
For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.

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

Tags:
['binary search', 'data structures', 'dfs and similar', 'implementation', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
Define the binary encoding of a finite set of natural numbers $T \subseteq \\{0,1,2,\ldots\\}$ as $f(T) = \sum\limits_{i \in T} 2^i$. For example, $f(\\{0,2\\}) = 2^0 + 2^2 = 5$ and $f(\\{\\}) = 0$. Notice that $f$ is a bijection from all such sets to all non-negative integers. As such, $f^{-1}$ is also defined.

You are given an integer $n$ along with $2^n-1$ sets $V_1,V_2,\ldots,V_{2^n-1}$.

Find all sets $S$ that satisfy the following constraint:

  * $S \subseteq \\{0,1,\ldots,n-1\\}$. Note that $S$ can be empty.    * For all non-empty subsets $T \subseteq \\{0,1,\ldots,n-1\\}$, $|S \cap T| \in V_{f(T)}$. 

Due to the large input and output, both input and output will be given in terms of binary encodings of the sets.

Input Format:
The first line of input contains a single integer $n$ ($1 \leq n \leq 20$).

The second line of input contains $2^n-1$ integers $v_1,v_2,\ldots,v_{2^n-1}$ ($0 \leq v_i < 2^{n+1}$) — the sets $V_i$ given in their binary encoding where $V_i = f^{-1}(v_i)$.

Output Format:
The first line of output should contain an integer $k$ indicating the number of possible $S$.

In the following $k$ lines, you should output $f(S)$ for all possible $S$ in increasing order.

Tags:
['bitmasks', 'brute force', 'combinatorics', 'dfs and similar', 'divide and conquer', 'dp', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB
One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem.      You are given two strings $s$ and $t$ of length $n$ and $m$, respectively. Both strings only consist of lowercase English letters, - and *.

You need to replace all occurrences of * and -, observing the following rules:

  * For each -, you must replace it with any lowercase English letter.    * For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters. 

Note that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.

Suppose $s$ and $t$ have been transformed into $s'$ and $t'$. Now you're wondering if there's a replacement that makes $s'=t'$.

Input Format:
The first line of input contains two integers $n$ and $m$ ($1 \leq n, m \leq 2 \cdot 10^6$) — the length of the strings $s$ and $t$, respectively.

The second line contains the string $s$ of length $n$. It is guaranteed that $s$ only consists of lowercase English letters, - and *.

The third line contains the string $t$ of length $m$. It is guaranteed that $t$ only consists of lowercase English letters, - and *.

Output Format:
For each test case, output "Yes" if there is a replacement that makes $s'=t'$, and output "No" otherwise.

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

Tags:
['fft', 'greedy', 'math', 'strings']

Time Limit: 12000 ms
Memory Limit: 512 MB
378QAQ has a string $s$ of length $n$. Define the core of a string as the substring$^\dagger$ with maximum lexicographic$^\ddagger$ order.

For example, the core of "$\mathtt{bazoka}$" is "$\mathtt{zoka}$", and the core of "$\mathtt{aaa}$" is "$\mathtt{aaa}$".

378QAQ wants to rearrange the string $s$ so that the core is lexicographically minimum. Find the lexicographically minimum possible core over all rearrangements of $s$.

$^\dagger$ A substring of string $s$ is a continuous segment of letters from $s$. For example, "$\mathtt{defor}$", "$\mathtt{code}$" and "$\mathtt{o}$" are all substrings of "$\mathtt{codeforces}$" while "$\mathtt{codes}$" and "$\mathtt{aaa}$" are not.

$^\ddagger$ A string $p$ is lexicographically smaller than a string $q$ if and only if one of the following holds:

  * $p$ is a prefix of $q$, but $p \ne q$; or    * in the first position where $p$ and $q$ differ, the string $p$ has a smaller element than the corresponding element in $q$ (when compared by their ASCII code). 

For example, "$\mathtt{code}$" and "$\mathtt{coda}$" are both lexicographically smaller than "$\mathtt{codeforces}$" while "$\mathtt{codeforceston}$" and "$\mathtt{z}$" are not.

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

The first line of each test case contains a single integer $n$ ($1\leq n\leq 10^6$) — the length of string $s$.

The next line of each test case contains the string $s$ of length $n$. The string $s$ consists of lowercase English letters.

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

Output Format:
For each test case, output the lexicographically minimum possible core over all rearrangements of $s$.

Tags:
['greedy', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
This is the way it always was.

This is the way it always will be.

All will be forgotten again soon...

Jellyfish is playing a one-player card game called "Slay the Spire". There are $n$ cards in total numbered from $1$ to $n$. The $i$-th card has power $c_i$.

There is a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.

Jellyfish will repeat the following process until either her hand or the draw pile is empty.

  1. Let $x$ be the power of the card with the largest power in her hand.    2. Place a single card with power $x$ back into the draw pile.    3. Randomly draw $x$ cards from the draw pile. All subsets of $x$ cards from the draw pile have an equal chance of being drawn. If there are fewer than $x$ cards in the draw pile, Jellyfish will draw all cards. 

At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $1\,000\,000\,007$.

Formally, let $M=1\,000\,000\,007$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

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

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 120$) — the number of cards.

The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($0 \leq c_i \leq n$) — the powers of the cards. It is guaranteed that $c_1 \leq c_2 \leq \ldots \leq c_n$.

The third line of each test case contains a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.

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

Output Format:
For each test case, output the probability that Jellyfish can empty the draw pile modulo $1\,000\,000\,007$.

Tags:
['dp']

Time Limit: 5000 ms
Memory Limit: 1024 MB
Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords.

Monocarp decided that strong passwords should satisfy the following conditions:

  * password should consist only of lowercase Latin letters and digits;    * there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends);    * all digits should be sorted in the non-decreasing order;    * all letters should be sorted in the non-decreasing order. 

Note that it's allowed for the password to have only letters or only digits.

Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords?

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

The first line of each testcase contains a single integer $n$ ($1 \le n \le 20$) — the length of the password.

The second line contains a string, consisting of exactly $n$ characters. Each character is either a lowercase Latin letter or a digit.

Output Format:
For each testcase, print "YES" if the given password is strong and "NO" otherwise.

Tags:
['implementation', 'sortings', 'strings']

Time Limit: 2000 ms
Memory Limit: 256 MB
You are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$.

You can perform the following operations any number of times in any order:

  * choose any element of the array $a$ and increase it by $1$;    * choose any element of the array $a$ and decrease it by $1$;    * choose any element of the array $a$, copy it and append the copy to the end of the array $a$. 

Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $a$ into the array $b$. It can be shown that under the constraints of the problem, it is always possible.

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

Each test case consists of three lines:

  * the first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$);    * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$);    * the third line contains $n + 1$ integers $b_1, b_2, \dots, b_{n + 1}$ ($1 \le b_i \le 10^9$). 

Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, print a single integer — the minimum number of operations (possibly zero) required to transform the array $a$ into the array $b$.

Tags:
['greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB
Monocarp is opening his own IT company. He wants to hire $n$ programmers and $m$ testers.

There are $n+m+1$ candidates, numbered from $1$ to $n+m+1$ in chronological order of their arriving time. The $i$-th candidate has programming skill $a_i$ and testing skill $b_i$ (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers.

When a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position.

Your task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly $n+m$ candidates will arrive, so all $n+m$ positions in the company will be filled.

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

Each test case consists of three lines:

  * the first line contains two integers $n$ and $m$ ($0 \le n, m \le 2 \cdot 10^5$; $2 \le n + m + 1 \le 2 \cdot 10^5$) — the number of programmers and the number of testers Monocarp wants to hire, respectively;    * the second line contains $n + m + 1$ integers $a_1, a_2, \dots, a_{n+m+1}$ ($1 \le a_i \le 10^9$), where $a_i$ is the programming skill of the $i$-th candidate;    * the third line contains $n + m + 1$ integers $b_1, b_2, \dots, b_{n+m+1}$ ($1 \le b_i \le 10^9$; $b_i \ne a_i$), where $b_i$ is the testing skill of the $i$-th candidate. 

Additional constraint on the input: the sum of $(n + m + 1)$ over all test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, print $n + m + 1$ integers, where the $i$-th integer should be equal to the skill of the team if everyone except the $i$-th candidate comes to interview.

Tags:
['binary search', 'dp', 'greedy', 'implementation', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example:

  * bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");    * bracket sequences ")(", "(" and ")" are not. 

Let's define the inverse of the bracket sequence as follows: replace all brackets '(' with ')', and vice versa (all brackets ')' with '('). For example, strings "()((" and ")())" are inverses of each other.

You are given a regular bracket sequence $s$. Calculate the number of pairs of integers $(l,r)$ ($1 \le l \le r \le |s|$) such that if you replace the substring of $s$ from the $l$-th character to the $r$-th character (inclusive) with its inverse, $s$ will still be a regular bracket sequence.

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 a non-empty regular bracket sequence; it consists only of characters '(' and/or ')'.

Additional constraint on the input: the total length of the regular bracket sequences over all test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, print a single integer — the number of pairs $(l,r)$ meeting the conditions from the statement.

Tags:
['binary search', 'combinatorics', 'data structures', 'divide and conquer', 'implementation', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB
Initially, we had one array, which was a permutation of size $n$ (an array of size $n$ where each integer from $1$ to $n$ appears exactly once).

We performed $q$ operations. During the $i$-th operation, we did the following:

  * choose any array we have with at least $2$ elements;    * split it into two non-empty arrays (prefix and suffix);    * write two integers $l_i$ and $r_i$, where $l_i$ is the maximum element in the left part which we get after the split, and $r_i$ is the maximum element in the right part;    * remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool. 

For example, suppose the initial array was $[6, 3, 4, 1, 2, 5]$, and we performed the following operations:

  1. choose the array $[6, 3, 4, 1, 2, 5]$ and split it into $[6, 3]$ and $[4, 1, 2, 5]$. Then we write $l_1 = 6$ and $r_1 = 5$, and the arrays we have are $[6, 3]$ and $[4, 1, 2, 5]$;    2. choose the array $[4, 1, 2, 5]$ and split it into $[4, 1, 2]$ and $[5]$. Then we write $l_2 = 4$ and $r_2 = 5$, and the arrays we have are $[6, 3]$, $[4, 1, 2]$ and $[5]$;    3. choose the array $[4, 1, 2]$ and split it into $[4]$ and $[1, 2]$. Then we write $l_3 = 4$ and $r_3 = 2$, and the arrays we have are $[6, 3]$, $[4]$, $[1, 2]$ and $[5]$. 

You are given two integers $n$ and $q$, and two sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$. A permutation of size $n$ is called valid if we can perform $q$ operations and produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.

Calculate the number of valid permutations.

Input Format:
The first line contains two integers $n$ and $q$ ($1 \le q < n \le 3 \cdot 10^5$).

The second line contains $q$ integers $l_1, l_2, \dots, l_q$ ($1 \le l_i \le n$).

The third line contains $q$ integers $r_1, r_2, \dots, r_q$ ($1 \le r_i \le n$).

Additional constraint on the input: there exists at least one permutation which can produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.

Output Format:
Print one integer — the number of valid permutations, taken modulo $998244353$.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child.

You are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree).

Recall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges.

After $k$ edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions:

  * for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges;    * the number of bridges is as small as possible. 

Solve the task for all values of $k$ from $1$ to $n - 1$ and output the smallest number of bridges.

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

The first line of each testcase contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) — the number of vertices of the tree.

Each of the next $n - 1$ lines contain two integers $v$ and $u$ ($1 \le v, u \le n$) — the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.

Additional constraint on the input: the root (vertex $1$) has exactly one child.

The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.

Output Format:
For each testcase, print $n - 1$ integers. For each $k$ from $1$ to $n - 1$ print the smallest number of bridges that can be left after you add $k$ edges to the tree.

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

Time Limit: 2000 ms
Memory Limit: 256 MB
The little boy Nikita was given some cubes as a present. He decided to build a tower out of them.

Initially, the tower doesn't have any cubes. In one move, Nikita either puts exactly $1$ cube on top of the tower or removes exactly $1$ cube from the top of the tower. Is it possible that after $n$ moves, the resulting tower has exactly $m$ cubes?

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

The only line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 100$).

Output Format:
For each test case, output "Yes" (without quotes) if Nikita can obtain a tower with $m$ cubes, and "No" (without quotes) 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:
['math']

Time Limit: 1000 ms
Memory Limit: 256 MB
You are given a positive integer $x$. Find any array of integers $a_0, a_1, \ldots, a_{n-1}$ for which the following holds:

  * $1 \le n \le 32$,    * $a_i$ is $1$, $0$, or $-1$ for all $0 \le i \le n - 1$,    * $x = \displaystyle{\sum_{i=0}^{n - 1}{a_i \cdot 2^i}}$,    * There does not exist an index $0 \le i \le n - 2$ such that both $a_{i} \neq 0$ and $a_{i + 1} \neq 0$. 

It can be proven that under the constraints of the problem, a valid array always exists.

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 only line of each test case contains a single positive integer $x$ ($1 \le x < 2^{30}$).

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

On the first line, output an integer $n$ ($1 \le n \le 32$) — the length of the array $a_0, a_1, \ldots, a_{n-1}$.

On the second line, output the array $a_0, a_1, \ldots, a_{n-1}$.

If there are multiple valid arrays, you can output any of them.

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

Time Limit: 1000 ms
Memory Limit: 256 MB