inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$^{\text{∗}}$ which has an exit at vertex $1$, and Alice starts at some vertex $v$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.

Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$^{\text{†}}$ of the tree, Alice loses.

Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $1\le v\le n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.

Formally, let $M = 998\,244\,353$. It can be shown that the exact 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}$.

$^{\text{∗}}$A tree is a connected simple graph which has $n$ vertices and $n-1$ edges.

$^{\text{†}}$A leaf is a vertex that is connected to exactly one edge.

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

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

The $i$-th of the next $n - 1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$ and $x_i \neq y_i$) — the edges of the tree. 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 $2\cdot 10^5$.

Output Format:
For each test case, output $n$ integers on one line — the probabilities of Alice escaping starting from vertex $1, 2, \ldots, n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.

Tags:
['combinatorics', 'dfs and similar', 'dp', 'games', 'greedy', 'math', 'probabilities', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB
Note that the memory limit is unusual.

The Cheshire Cat has a riddle for Alice: given $n$ integers $a_1, a_2, \ldots, a_n$ and a target $m$, is there a way to insert $+$ and $\times$ into the circles of the expression $$a_1 \circ a_2 \circ \cdots \circ a_n = m$$ to make it true? We follow the usual order of operations: $\times$ is done before $+$.

Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!

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, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) — the number of integers and the target, respectively.

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

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 it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.

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

Tags:
['bitmasks', 'brute force', 'dp', 'implementation']

Time Limit: 3000 ms
Memory Limit: 32 MB
You are given a positive integer $k$ and a set $S$ of all integers from $l$ to $r$ (inclusive).

You can perform the following two-step operation any number of times (possibly zero):

  1. First, choose a number $x$ from the set $S$, such that there are at least $k$ multiples of $x$ in $S$ (including $x$ itself);    2. Then, remove $x$ from $S$ (note that nothing else is removed).

Find the maximum possible number of operations that can be performed.

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

The only line of each test case contains three integers $l$, $r$, and $k$ ($1\le l\le r\leq 10^9$, $1\leq k\le r-l+1$) — the minimum integer in $S$, the maximum integer in $S$, and the parameter $k$.

Output Format:
For each test case, output a single integer — the maximum possible number of operations that can be performed.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
You have a binary string$^{\text{∗}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$.

Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \le i \le n-1$):

  * First, you choose an index $k$ such that $1\le k\le |s| - 1$ and $s_{k} \neq s_{k+1}$. If it is impossible to choose such an index, you lose;    * Then, you replace $s_ks_{k+1}$ with $r_i$. Note that this decreases the length of $s$ by $1$. 

If all the $n-1$ operations are performed successfully, you win.

Determine whether it is possible for you to win this game.

$^{\text{∗}}$A binary string is a string where each character is either $\mathtt{0}$ or $\mathtt{1}$.

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

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

The second line contains the binary string $s$ of length $n$ ($s_i=\mathtt{0}$ or $\mathtt{1}$).

The third line contains the binary string $r$ of length $n-1$ ($r_i=\mathtt{0}$ or $\mathtt{1}$).

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

Output Format:
For each test case, print "YES" (without quotes) if you can win the game, and "NO" (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Tags:
['games', 'greedy', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Hello, Codeforces Forcescode!



Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode.

Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$.

Now he has hacked into the backend of Forcescode and will select an interval $[l,r]$ ($1\le l\le r\le n$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way:

  * Initially, his rating is $x=0$;    * For each $1\le i\le n$, after the $i$-th contest,      * If $l\le i\le r$, this contest will be skipped, and the rating will remain unchanged;      * Otherwise, his rating will be updated according to the following rules:        * If $a_i>x$, his rating $x$ will increase by $1$;        * If $a_i=x$, his rating $x$ will remain unchanged;        * If $a_i<x$, his rating $x$ will decrease by $1$. 

You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $[l,r]$ optimally. Note that Kevin has to skip at least one contest.

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

The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$) — the number of contests.

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$) — the performance ratings in the contests.

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

Output Format:
For each test case, output a single integer — the maximum possible rating after the recalculation if Kevin chooses the interval optimally.

Tags:
['binary search', 'data structures', 'dp', 'greedy']

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given an undirected graph with $n$ vertices and $m$ edges.

You can perform the following operation at most $2\cdot \max(n,m)$ times:

  * Choose three distinct vertices $a$, $b$, and $c$, then for each of the edges $(a,b)$, $(b,c)$, and $(c,a)$, do the following:      * If the edge does not exist, add it. On the contrary, if it exists, remove it. 

A graph is called cool if and only if one of the following holds:

  * The graph has no edges, or    * The graph is a tree. 

You have to make the graph cool by performing the above operations. Note that you can use at most $2\cdot \max(n,m)$ operations.

It can be shown that there always exists at least one solution.

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

The first line of each test case contains two integers $n$ and $m$ ($3\le n\le 10^5$, $0\le m\le \min\left(\frac{n(n-1)}{2},2\cdot 10^5\right)$) — the number of vertices and the number of edges.

Then $m$ lines follow, the $i$-th line contains two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$) — the two nodes that the $i$-th edge connects.

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

It is guaranteed that there are no self-loops or multiple-edges in the given graph.

Output Format:
For each test case, in the first line output an integer $k$ ($0\le k\le 2\cdot \max(n, m)$) — the number of operations.

Then output $k$ lines, the $i$-th line containing three distinct integers $a$, $b$, and $c$ ($1\le a,b,c\le n$) — the three integers you choose in the $i$-th operation.

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

Tags:
['constructive algorithms', 'data structures', 'dfs and similar', 'graphs', 'trees']

Time Limit: 3000 ms
Memory Limit: 512 MB
For two integers $x$ and $y$ ($x,y\ge 2$), we will say that $x$ is a generator of $y$ if and only if $x$ can be transformed to $y$ by performing the following operation some number of times (possibly zero):

  * Choose a divisor $d$ ($d\ge 2$) of $x$, then increase $x$ by $d$. 

For example,

  * $3$ is a generator of $8$ since we can perform the following operations: $3 \xrightarrow{d = 3} 6 \xrightarrow{d = 2} 8$;    * $4$ is a generator of $10$ since we can perform the following operations: $4 \xrightarrow{d = 4} 8 \xrightarrow{d = 2} 10$;    * $5$ is not a generator of $6$ since we cannot transform $5$ into $6$ with the operation above. 

Now, Kevin gives you an array $a$ consisting of $n$ pairwise distinct integers ($a_i\ge 2$).

You have to find an integer $x\ge 2$ such that for each $1\le i\le n$, $x$ is a generator of $a_i$, or determine that such an integer does not exist.

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

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

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($2\le a_i\le 4\cdot 10^5$) — the elements in the array $a$. It is guaranteed that the elements are pairwise distinct.

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

Output Format:
For each test case, output a single integer $x$ — the integer you found. Print $-1$ if there does not exist a valid $x$.

If there are multiple answers, you may output any of them.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$).

Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j\le n-1$):

  * There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p=[p_0,p_1,p_2,\ldots,p_m]$ such that:     * $p_0=i$, $p_m=j$;      * For each $0\leq x\le m-1$, either $p_{x+1}=(p_x+1)\bmod n$ or $p_{x+1}=(p_{x}-1)\bmod n$;      * For each $0\le x\le y\le m-1$ satisfying $x+y=m-1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.

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

The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) — the number of vertices in the cycle.

The second line contains a string $c$ of length $n$ ($c_i=\texttt{R}$ or $\texttt{B}$) — the color of each edge.

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

Output Format:
For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

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

Time Limit: 2000 ms
Memory Limit: 512 MB
There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$.

Kevin can perform several operations on the array. Each operation is one of the following two types:

  * Prefix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_j$ by $1$;    * Suffix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $x\le j\le n$, increases $a_j$ by $1$.

In the country of KDOI, people think that the integer $v$ is balanced. Thus, Iris gives Kevin an array $c$ consisting of $n$ integers and defines the beauty of the array $a$ as follows:

  * Initially, set $b=0$;    * For each $1\le i\le n$, if $a_i=v$, add $c_i$ to $b$;    * The beauty of $a$ is the final value of $b$.

Kevin wants to maximize the beauty of $a$ after all the operations. However, he had already performed $m$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations.

You have to help Kevin find the maximum possible beauty if he optimally performs the new operations.

However, to make sure that you are not just rolling the dice, Kevin gives you an integer $V$, and you need to solve the problem for each $1\le v\le V$.

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

The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) — the length of the array $a$, the number of initial operations, and the number that Kevin gives you.

The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1\le c_i\le 10^9$) — the elements in the array $c$.

Then $m$ lines follow, the $i$-th line containing a character $op$ and an integer $x$ ($op=\mathtt{L}$ or $\mathtt{R}$, $1\le x\le n$) — the type of the $i$-th operation and the selected index.

  * If $op=\mathtt{L}$, this operation is a prefix addition on index $x$;    * If $op=\mathtt{R}$, this operation is a suffix addition on index $x$. 

It is guaranteed that:

  * the sum of $n$ over all test cases does not exceed $2\cdot 10^5$;    * the sum of $m$ over all test cases does not exceed $2\cdot 10^5$;    * the sum of $V^2$ over all test cases does not exceed $4\cdot 10^6$.

Output Format:
For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$.

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

Time Limit: 3000 ms
Memory Limit: 1024 MB
Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day.

Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it had the message the day before. Note that each day, each edge chooses its appearance independently.

Calculate the expected number of days before all the vertices have the message, modulo $998\,244\,353$.

Input Format:
The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$).

Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) — there is an undirected edge between $u$ and $v$, and it has a probability of appearance of $\frac{p}{q}$ each day.

It is guaranteed that there are no self-loops or multiple-edges in the graph and that the graph is connected if all of the edges appear.

Additional constraint in the input: Let $g_{i,j}$ be the probability of appearance of the edge between $i$ and $j$ ($g_{i,j}=0$ if there is no edge between $i$ and $j$). It is guaranteed that for any $S\subseteq\\{1,2,\ldots,n\\}$ ($|S|\ge 1$), $$ \prod_{i\in S}\left(\prod_{j\in\\{1,2,\ldots,n\\}\setminus S}(1-g_{i,j})\right)\not\equiv1\pmod{998\,244\,353}. $$

Output Format:
Print a single integer in the only line of the output — the expected number of days, modulo $998\,244\,353$.

Formally, let $M = 998\,244\,353$. It can be shown that the exact 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}$.

Tags:
['bitmasks', 'combinatorics', 'dp']

Time Limit: 12000 ms
Memory Limit: 1024 MB
Kevin has recently learned the definition of variance. For an array $a$ of length $n$, the variance of $a$ is defined as follows:

  * Let $x=\dfrac{1}{n}\displaystyle\sum_{i=1}^n a_i$, i.e., $x$ is the mean of the array $a$;    * Then, the variance of $a$ is $$ V(a)=\frac{1}{n}\sum_{i=1}^n(a_i-x)^2. $$ 

Now, Kevin gives you an array $a$ consisting of $n$ integers, as well as an integer $k$. You can perform the following operation on $a$:

  * Select an interval $[l,r]$ ($1\le l\le r\le n$), then for each $l\le i\le r$, increase $a_i$ by $k$. 

For each $1\le p\le m$, you have to find the minimum possible variance of $a$ after exactly $p$ operations are performed, independently for each $p$.

For simplicity, you only need to output the answers multiplied by $n^2$. It can be proven that the results are always integers.

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

The first line of each test case contains three integers $n$, $m$, and $k$ ($1\le n,m\le 5000$, $\color{red}{n\cdot m\le 2\cdot 10^4}$, $1\le k\le 10^5$) — the length of the array $a$, the maximum number of operations, and the number you add to $a_i$ each time, respectively.

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

It is guaranteed that the sum of $n\cdot m$ over all tests does not exceed $2\cdot 10^4$.

Output Format:
For each test case, output $m$ integers in a single line, the $p$-th integer denoting the minimum possible variance of $a$ when exactly $p$ operations are performed, multiplied by $n^2$.

Tags:
['flows', 'graphs', 'greedy']

Time Limit: 6000 ms
Memory Limit: 256 MB
While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...

The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the following manner:

  * $b_i = \min(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$.    * $c_i = \max(a_1, a_2, \ldots, a_i)$ for each $1 \leq i \leq n$. 

Define the score of $a$ as $\sum_{i=1}^n c_i - b_i$ (i.e. the sum of $c_i - b_i$ over all $1 \leq i \leq n$). Before you calculate the score, you can shuffle the elements of $a$ however you want.

Find the maximum score that you can get if you shuffle the elements of $a$ optimally.

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

The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) — the number of elements in $a$.

The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) — the elements of the array $a$.

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

Output Format:
For each test case, output the maximum score that you can get.

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

Time Limit: 1000 ms
Memory Limit: 256 MB
For an arbitrary binary string $t$$^{\text{∗}}$, let $f(t)$ be the number of non-empty subsequences$^{\text{†}}$ of $t$ that contain only $\mathtt{0}$, and let $g(t)$ be the number of non-empty subsequences of $t$ that contain at least one $\mathtt{1}$.

Note that for $f(t)$ and for $g(t)$, each subsequence is counted as many times as it appears in $t$. E.g., $f(\mathtt{000}) = 7, g(\mathtt{100}) = 4$.

We define the oneness of the binary string $t$ to be $|f(t)-g(t)|$, where for an arbitrary integer $z$, $|z|$ represents the absolute value of $z$.

You are given a positive integer $n$. Find a binary string $s$ of length $n$ such that its oneness is as small as possible. If there are multiple strings, you can print any of them.

$^{\text{∗}}$A binary string is a string that only consists of characters $\texttt{0}$ and $\texttt{1}$.

$^{\text{†}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements. For example, subsequences of $\mathtt{1011101}$ are $\mathtt{0}$, $\mathtt{1}$, $\mathtt{11111}$, $\mathtt{0111}$, but not $\mathtt{000}$ nor $\mathtt{11100}$.

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 an integer $n$ ($1 \leq n \leq 2\cdot10^5$) — the length of $s$.

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

Output Format:
For each test case, output $s$ on a new line. If multiple answers exist, output any.

Tags:
['combinatorics', 'constructive algorithms', 'games', 'math']

Time Limit: 1500 ms
Memory Limit: 256 MB