Question:
This temple only magnifies the mountain's power.

Badeline

This is an interactive problem.

You are given two positive integers $n$ and $m$ ($\bf{n \le m}$).

The jury has hidden from you a rectangular matrix $a$ with $n$ rows and $m$ columns, where $a_{i,j} \in \\{ -1, 0, 1 \\}$ for all $1 \le i \le n$ and $1 \le j \le m$. The jury has also selected a cell $(i_0, j_0)$. Your goal is to find $(i_0,j_0)$.

In one query, you give a cell $(i, j)$, then the jury will reply with an integer.

  * If $(i, j) = (i_0, j_0)$, the jury will reply with $0$.    * Else, let $S$ be the sum of $a_{x,y}$ over all $x$ and $y$ such that $\min(i, i_0) \le x \le \max(i, i_0)$ and $\min(j, j_0) \le y \le \max(j, j_0)$. Then, the jury will reply with $|i - i_0| + |j - j_0| + |S|$.

Find $(i_0, j_0)$ by making at most $n + 225$ queries.

Note: the grader is not adaptive: $a$ and $(i_0,j_0)$ are fixed before any queries are made.

Input Format:
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 50$) — 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 \le m \le 5000$) — the numbers of rows and the number of columns of the hidden matrix $a$ respectively.

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

Output Format:

Tags:
['interactive']

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, $\\{2,2,4\\}$ is a multiset.

You have a multiset $S$. Initially, the multiset contains only one positive integer $n$. That is, $S=\\{n\\}$. Additionally, there is a given positive integer $k$.

In one operation, you can select any positive integer $u$ in $S$ and remove one copy of $u$ from $S$. Then, insert no more than $k$ positive integers into $S$ so that the sum of all inserted integers is equal to $u$.

Find the minimum number of operations to make $S$ contain $n$ ones.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

The only line of each testcase contains two integers $n,k$ ($1\le n\le 1000,2\le k\le 1000$).

Output Format:
For each testcase, print one integer, which is the required answer.

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

Time Limit: 1000 ms
Memory Limit: 512 MB


Question:
You are given a sequence $[a_1,\ldots,a_n]$ where each element $a_i$ is either $0$ or $1$. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers $1\le l\le r\le |a|$ (where $|a|$ is the current length of $a$) and replace $[a_l,\ldots,a_r]$ with a single element $x$, where $x$ is the majority of $[a_l,\ldots,a_r]$.

Here, the majority of a sequence consisting of $0$ and $1$ is defined as follows: suppose there are $c_0$ zeros and $c_1$ ones in the sequence, respectively.

  * If $c_0\ge c_1$, the majority is $0$.    * If $c_0<c_1$, the majority is $1$.

For example, suppose $a=[1,0,0,0,1,1]$. If we select $l=1,r=2$, the resulting sequence will be $[0,0,0,1,1]$. If we select $l=4,r=6$, the resulting sequence will be $[1,0,0,1]$.

Determine if you can make $a=[1]$ with a finite number of operations.

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

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

The second line of each testcase contains a string consisting of $0$ and $1$, describing the sequence $a$.

It's guaranteed that the sum of $n$ over all testcases does not exceed $2\cdot 10^5$.

Output Format:
For each testcase, if it's possible to make $a=[1]$, print YES. Otherwise, print NO. 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:
['greedy', 'implementation']

Time Limit: 1500 ms
Memory Limit: 512 MB


Question:
You are given a positive integer $n$. Find the longest sequence of positive integers $a=[a_1,a_2,\ldots,a_k]$ that satisfies the following conditions, and print the sequence:

  * $a_i\le n$ for all $1\le i\le k$.    * $a$ is strictly increasing. That is, $a_i>a_{i-1}$ for all $2\le i\le k$.    * $a_i\,|\,a_{i-1}=n$ for all $2\le i\le k$, where $|$ denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

The only line of each test case contains one integer $n$ ($1\le n\le 10^{18}$).

It's guaranteed that the sum of lengths of the longest valid sequences does not exceed $5\cdot 10^5$.

Output Format:
For each testcase, print two lines. In the first line, print the length of your constructed sequence, $k$. In the second line, print $k$ positive integers, denoting the sequence. If there are multiple longest sequences, you can print any of them.

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

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for $10^{100}$ rounds.

In each round, the following happens in order:

  1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters.    2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future.

There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge.

If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds?

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

The first line of each test case contains an integer $n$ ($1\le n\le 3\cdot 10^5$).

The second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 10^{12}$).

The following $n-1$ lines each contain two integers $x,y$ ($1\le x,y\le n$), denoting an edge on the tree connecting vertex $x$ and $y$.

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, print one integer: the minimum possible health decrement.

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

Time Limit: 3000 ms
Memory Limit: 512 MB


Question:
For an array $[a_1,a_2,\ldots,a_n]$ of length $n$, define $f(a)$ as the sum of the minimum element over all subsegments. That is, $$f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.$$

A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. You are given a permutation $[a_1,a_2,\ldots,a_n]$. For each $i$, solve the following problem independently:

  * Erase $a_i$ from $a$, concatenating the remaining parts, resulting in $b = [a_1,a_2,\ldots,a_{i-1},\;a_{i+1},\ldots,a_{n}]$.    * Calculate $f(b)$.

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

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

The second line of each test case contains $n$ distinct integers $a_1,\ldots,a_n$ ($1\le a_i\le n$).

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

Output Format:
For each test case, print one line containing $n$ integers. The $i$-th integer should be the answer when erasing $a_i$.

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

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
For an array $u_1, u_2, \ldots, u_n$, define

  * a prefix maximum as an index $i$ such that $u_i>u_j$ for all $j<i$;    * a suffix maximum as an index $i$ such that $u_i>u_j$ for all $j>i$;    * an ascent as an index $i$ ($i>1$) such that $u_i>u_{i-1}$.

You are given three cost arrays: $[a_1, a_2, \ldots, a_n]$, $[b_1, b_2, \ldots, b_n]$, and $[c_0, c_1, \ldots, c_{n-1}]$.

Define the cost of an array that has $x$ prefix maximums, $y$ suffix maximums, and $z$ ascents as $a_x\cdot b_y\cdot c_z$.

Let the sum of costs of all permutations of $1,2,\ldots,n$ be $f(n)$. Find $f(1)$, $f(2)$, ..., $f(n)$ modulo $998\,244\,353$.

Input Format:
The first line contains an integer $n$ ($1\le n\le 700$).

The second line contains $n$ integers $a_1,\ldots,a_n$ ($0\le a_i<998\,244\,353$).

The third line contains $n$ integers $b_1,\ldots,b_n$ ($0\le b_i<998\,244\,353$).

The fourth line contains $n$ integers $c_0,\ldots,c_{n-1}$ ($0\le c_i<998\,244\,353$).

Output Format:
Print $n$ integers: the $i$-th one is $f(i)$ modulo $998\,244\,353$.

Tags:
['combinatorics', 'dp', 'fft', 'math']

Time Limit: 5000 ms
Memory Limit: 512 MB


Question:
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet.

The game is pretty simple. The screen represents a coordinate grid such that:

  * the X-axis is directed from left to right;    * the Y-axis is directed from bottom to top;    * the center of the screen has coordinates $(0, 0)$.

At the beginning of the game, the character is located in the center, and $n$ coins appear on the screen — the $i$-th coin is at coordinates $(x_i, y_i)$. The coordinates of all coins are different and not equal to $(0, 0)$.

In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $(x, y)$, then it can end up at any of the coordinates $(x, y + 1)$, $(x + 1, y + 1)$, $(x + 1, y)$, $(x + 1, y - 1)$, $(x, y - 1)$, $(x - 1, y - 1)$, $(x - 1, y)$, $(x - 1, y + 1)$.

If the character ends up at the coordinates with a coin, then Monocarp collects that coin.

After Monocarp makes a move, all coins fall down by $1$, that is, they move from $(x, y)$ to $(x, y - 1)$. You can assume that the game field is infinite in all directions.

Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it.

Input Format:
The first line contains a single integer $n$ ($1 \le n \le 500$) — the number of coins.

In the $i$-th of the next $n$ lines, two integers $x_i$ and $y_i$ ($-50 \le x_i, y_i \le 50$) are written — the coordinates of the $i$-th coin. The coordinates of all coins are different. No coin is located at $(0, 0)$.

Output Format:
For each coin, print "YES" if Monocarp can collect it. Otherwise, print "NO".

Tags:
['implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given two strings $a$ and $b$, both consisting of lowercase Latin letters.

A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.

For example, consider the string abac:

  * a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences;    * a, b, c, ab, ba, ac, aba, bac and abac are its substrings.

Your task is to calculate the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.

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

The first line of each test case contains a string $a$ ($1 \le |a| \le 100$), consisting of lowercase Latin letters.

The second line of each test case contains a string $b$ ($1 \le |b| \le 100$), consisting of lowercase Latin letters.

Output Format:
For each test case, print a single integer — the minimum possible length of the string that contains $a$ as a substring and $b$ as a subsequence.

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

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["5\naba\ncb\ner\ncf\nmmm\nmmm\ncontest\ntest\ncde\nabcefg"], "outputs": ["4\n4\n3\n7\n7"]}


Question:
A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.

If a person is asked to leave a review for the movie, then:

  * if that person liked the movie, they will leave a positive review, and the movie's rating will increase by $1$;    * if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by $1$;    * otherwise, they will leave a neutral review, and the movie's rating will not change.

Every person will review exactly one movie — and for every person, you can choose which movie they will review.

The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.

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

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

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-1 \le a_i \le 1$), where $a_i$ is equal to $-1$ if the first movie was disliked by the $i$-th viewer; equal to $1$ if the first movie was liked; and $0$ if the attitude is neutral.

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($-1 \le b_i \le 1$), where $b_i$ is equal to $-1$ if the second movie was disliked by the $i$-th viewer; equal to $1$ if the second movie was liked; and $0$ if the attitude is neutral.

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

Output Format:
For each test case, print a single integer — the maximum possible rating of the company, if for each person, choose which movie to leave a review on.

Tags:
['greedy', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get $1$ experience point, and every time you melt an item, you also get $1$ experience point.

There are $n$ classes of weapons you can forge and $m$ types of metal ingots.

You can create one weapon of the $i$-th class, spending $a_i$ ingots of metal of the same type. Melting a weapon of the $i$-th class (which you crafted earlier) returns you $b_i$ ingots of the type of metal it was made of.

You have $c_j$ metal ingots of the $j$-th type, and you know that you can craft a weapon of any class from any metal type. Each combination of a weapon class and a metal type can be used any number of times.

What is the maximum total amount of experience you can earn by crafting and melting weapons?

Input Format:
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) — the number of weapon classes and metal types.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the number of ingots you need to forge one weapon of the $i$-th class.

The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < a_i$), where $b_i$ is the number of ingots you return by melting one weapon of the $i$-th class you forged earlier.

The fourth line contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le 10^9$) — the number of ingots you have of the corresponding metal type.

Output Format:
Print one integer — the maximum total experience points you can gain by repeatedly forging and melting weapons.

Tags:
['brute force', 'data structures', 'dp', 'greedy', 'math', 'sortings', 'two pointers']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Consider an array $a$ of $n$ integers, where every element is from $1$ to $k$, and every integer from $1$ to $k$ appears at least once.

Let the array $b$ be constructed as follows: for the $i$-th element of $a$, $b_i$ is the distance to the closest element in $a$ which is not equal to $a_i$. In other words, $b_i = \min \limits_{j \in [1, n], a_j \ne a_i} |i - j|$.

For example, if $a = [1, 1, 2, 3, 3, 3, 3, 1]$, then $b = [2, 1, 1, 1, 2, 2, 1, 1]$.

Calculate the number of different arrays $b$ you can obtain if you consider all possible arrays $a$, and print it modulo $998244353$.

Input Format:
The only line of the input contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $2 \le k \le \min(n, 10)$).

Output Format:
Print one integer — the number of different arrays $b$ you can obtain, taken modulo $998244353$.

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

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
You are given a matrix, consisting of $n$ rows and $m$ columns.

You can perform two types of actions on it:

  * paint the entire column in blue;    * paint the entire row in red.

Note that you cannot choose which color to paint the row or column.

In one second, you can perform either one action or multiple actions at the same time. If you perform one action, it will be free. If you perform $k > 1$ actions at the same time, it will cost $k^2$ coins. When multiple actions are performed at the same time, for each cell affected by actions of both types, the color can be chosen independently.

You are asked to process $q$ queries. Before each query, all cells become colorless. Initially, there are no restrictions on the color of any cells. In the $i$-th query, a restriction of the following form is added:

  * $x_i~y_i~c_i$ — the cell in row $x_i$ in column $y_i$ should be painted in color $c_i$.

Thus, after $i$ queries, there are $i$ restrictions on the required colors of the matrix cells. After each query, output the minimum cost of painting the matrix according to the restrictions.

Input Format:
The first line contains three integers $n, m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) — the size of the matrix and the number of queries.

In the $i$-th of the next $q$ lines, two integers $x_i, y_i$ and a character $c_i$ ($1 \le x_i \le n$; $1 \le y_i \le m$; $c_i \in$ {'R', 'B'}, where 'R' means red, and 'B' means blue) — description of the $i$-th restriction. The cells in all queries are pairwise distinct.

Output Format:
Print $q$ integers — after each query, output the minimum cost of painting the matrix according to the restrictions.

Tags:
['dfs and similar', 'divide and conquer', 'graphs']

Time Limit: 6000 ms
Memory Limit: 512 MB


Question:
Alice and Bob are playing a game in an array $a$ of size $n$.

They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable $mx$ is set to $0$.

In one operation, a player can do:

  * Choose an index $i$ ($1 \le i \le n$) such that $a_{i} \geq mx$ and set $mx$ to $a_{i}$. Then, set $a_{i}$ to $0$.

Determine whether Alice has a winning strategy.

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

For each test case:

  * The first line contains an integer $n$ ($2 \leq n \leq 50$) — the size of the array.    * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array.

Output Format:
For each test case, if Alice has a winning strategy, output "YES". Otherwise, output "NO".

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:
['brute force', 'games', 'greedy', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
For an array $b$ of size $m$, we define:

  * the maximum prefix position of $b$ is the smallest index $i$ that satisfies $b_1+\ldots+b_i=\max_{j=1}^{m}(b_1+\ldots+b_j)$;   * the maximum suffix position of $b$ is the largest index $i$ that satisfies $b_i+\ldots+b_m=\max_{j=1}^{m}(b_j+\ldots+b_m)$.

You are given three integers $n$, $x$, and $y$ ($x > y$). Construct an array $a$ of size $n$ satisfying:

  * $a_i$ is either $1$ or $-1$ for all $1 \le i \le n$;   * the maximum prefix position of $a$ is $x$;   * the maximum suffix position of $a$ is $y$.

If there are multiple arrays that meet the conditions, print any. It can be proven that such an array always exists under the given conditions.

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

For each test case:

  * The only line contains three integers $n$, $x$, and $y$ ($2 \leq n \leq 10^5, 1 \le y \lt x \le n)$.

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

Output Format:
For each test case, output $n$ space-separated integers $a_1, a_2, \ldots, a_n$ in a new line.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
We define the $\operatorname{MAD}$ (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the $\operatorname{MAD}$ value is $0$.

For example, $\operatorname{MAD}([1, 2, 1]) = 1$, $\operatorname{MAD}([2, 2, 3, 3]) = 3$, $\operatorname{MAD}([1, 2, 3, 4]) = 0$.

You are given an array $a$ of size $n$. Initially, a variable $sum$ is set to $0$.

The following process will be executed in a sequential loop until all numbers in $a$ become $0$:

  1. Set $sum := sum + \sum_{i=1}^{n} a_i$;    2. Let $b$ be an array of size $n$. Set $b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i])$ for all $1 \le i \le n$, and then set $a_i := b_i$ for all $1 \le i \le n$.

Find the value of $sum$ after the process.

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

For each test case:

  * The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array $a$;    * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array.

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

Output Format:
For each test case, output the value of $sum$ in a new line.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given an array $a$ of size $n$.

There is an $n \times n$ grid. In the $i$-th row, the first $a_i$ cells are black and the other cells are white. In other words, note $(i,j)$ as the cell in the $i$-th row and $j$-th column, cells $(i,1), (i,2), \ldots, (i,a_i)$ are black, and cells $(i,a_i+1), \ldots, (i,n)$ are white.

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

  * Dye a $2 \times 2$ subgrid white;    * Dye a whole row white. Note you can not dye a whole column white.

Find the minimum number of operations to dye all cells white.

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

For each test case:

  * The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the size of the array $a$.    * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq n$).

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

Output Format:
For each test case, output a single integer — the minimum number of operations to dye all cells white.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference is the limit on the number of queries.

This is an interactive problem.

You are given a tree of $n$ nodes with node $1$ as its root node.

There is a hidden mole in one of the nodes. To find its position, you can pick an integer $x$ ($1 \le x \le n$) to make an inquiry to the jury. Next, the jury will return $1$ when the mole is in subtree $x$. Otherwise, the judge will return $0$. If the judge returns $0$ and the mole is not in root node $1$, the mole will move to the parent node of the node it is currently on.

Use at most $300$ operations to find the current node where the mole is located.

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

Output Format:

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

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The only difference is the limit on the number of queries.

This is an interactive problem.

You are given a tree of $n$ nodes with node $1$ as its root node.

There is a hidden mole in one of the nodes. To find its position, you can pick an integer $x$ ($1 \le x \le n$) to make an inquiry to the jury. Next, the jury will return $1$ when the mole is in subtree $x$. Otherwise, the judge will return $0$. If the judge returns $0$ and the mole is not in root node $1$, the mole will move to the parent node of the node it is currently on.

Use at most $160$ operations to find the current node where the mole is located.

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

Output Format:

Tags:
['binary search', 'data structures', 'dfs and similar', 'divide and conquer', 'interactive', 'trees']

Time Limit: 4000 ms
Memory Limit: 256 MB


Question:
You are given an array $a$ of size $n$.

A segment $[l, r](1 \le l < r \le n)$ is called a polygonal segment only if the following conditions hold:

  * $(r-l+1) \geq 3$;    * Considering $a_l, a_{l+1}, \ldots, a_r$ as side lengths, these sides can form a polygon with $(r-l+1)$ sides.

Process $q$ queries of two types:

  * "1 l r": find the length of the longest segment among all polygonal segments $[l_0,r_0]$ satisfying $l \le l_0 \le r_0 \le r$. If there is no such polygonal segment, output $-1$ instead;    * "2 i x": assign $a_i := x$.

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

For each test case:

  * The first line of each testcase contains two integers $n$, $q$ ($4 \le n \le 2\cdot 10^5$, $1 \le q \le 10^5$);    * The second line of each testcase contains $n$ integers $a_1,a_2,\ldots, a_n$ ($1 \le a_i \le 10^{12}$);    * The following $q$ lines contain the description of queries. Each line is of one of two types:     * "1 l r" ($1 \le l < r \le n$, $r-l+1\ge 3$);      * "2 i x" ($1 \le i \le n$, $1 \le x \le 10^{12}$).

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

Output Format:
For each query, if there is no suitable segment, output $-1$ in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line.

Tags:
['brute force', 'data structures', 'divide and conquer', 'dp', 'greedy', 'two pointers']

Time Limit: 8000 ms
Memory Limit: 512 MB


Question:
You are given an array $a$ of $n$ integers, where $n$ is odd.

In one operation, you will remove two adjacent elements from the array $a$, and then concatenate the remaining parts of the array. For example, given the array $[4,7,4,2,9]$, we can obtain the arrays $[4,2,9]$ and $[4,7,9]$ by the operations $[\underline{4,7}, 4,2,9] \to [4,2,9]$ and $[4,7,\underline{4,2},9] \to [4,7,9]$ respectively. However, we cannot obtain the array $[7,2,9]$ as it requires deleting non-adjacent elements $[\underline{4},7,\underline{4},2,9]$.

You will repeatedly perform this operation until exactly one element remains in $a$.

Find the maximum possible value of the remaining element in $a$.

Input Format:
Each test contains multiple test cases. The first line 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 a single integer $n$ ($1 \le n \le 99$; $n$ is odd) — 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 100$) — the elements of the array $a$.

Note that there is no bound on the sum of $n$ over all test cases.

Output Format:
For each test case, output a single integer — the maximum possible value of the remaining element in $a$.

Tags:
['greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array $b$ of $n - 1$ integers.

An array $a$ of $n$ integers is called good if $b_i = a_i \, \& \, a_{i + 1}$ for $1 \le i \le n-1$, where $\&$ denotes the [bitwise AND operator](https://en.wikipedia.org/wiki/Bitwise_operation#AND).

Construct a good array, or report that no good arrays exist.

Input Format:
Each test contains 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 test cases follows.

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

The second line of each test case contains $n - 1$ integers $b_1, b_2, \ldots, b_{n - 1}$ ($0 \le b_i < 2^{30}$) — the elements of the array $b$.

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 $-1$ if no good arrays exist.

Otherwise, output $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i < 2^{30}$) — the elements of a good array $a$.

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

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
You are given an array $a$ of $n$ integers.

In one operation, you will perform the following two-step move:

  1. Choose an integer $x$ ($0 \le x \le 10^{9}$).    2. Replace each $a_i$ with $|a_i - x|$, where $|v|$ denotes the [absolute value](https://en.wikipedia.org/wiki/Absolute_value) of $v$.

For example, by choosing $x = 8$, the array $[5, 7, 10]$ will be changed into $[|5-8|, |7-8|, |10-8|] = [3,1,2]$.

Construct a sequence of operations to make all elements of $a$ equal to $0$ in at most $40$ operations or determine that it is impossible. You do not need to minimize the number of operations.

Input Format:
Each test contains 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 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 array $a$.

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 elements of the array $a$.

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 $-1$ if it is impossible to make all array elements equal to $0$ in at most $40$ operations.

Otherwise, output two lines. The first line of output should contain a single integer $k$ ($0 \le k \le 40$) — the number of operations. The second line of output should contain $k$ integers $x_1, x_2, \ldots, x_k$ ($0 \le x_i \le 10^{9}$) — the sequence of operations, denoting that on the $i$-th operation, you chose $x=x_i$.

If there are multiple solutions, output any of them.

You do not need to minimize the number of operations.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given an undirected graph with $n$ vertices, numbered from $1$ to $n$. There is an edge between vertices $u$ and $v$ if and only if $u \oplus v$ is a [prime number](https://en.wikipedia.org/wiki/Prime_number), where $\oplus$ denotes the [bitwise XOR operator](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

Color all vertices of the graph using the minimum number of colors, such that no two vertices directly connected by an edge have the same color.

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

The only line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of vertices in the graph.

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 two lines.

The first line should contain a single integer $k$ ($1 \le k \le n$) — the minimum number of colors required.

The second line should contain $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le k$) — the color of each vertex.

If there are multiple solutions, output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is an interactive problem.

Consider an undirected connected graph consisting of $n$ vertices and $m$ edges. Each vertex can be colored with one of three colors: $1$, $2$, or $3$. Initially, all vertices are uncolored.

Alice and Bob are playing a game consisting of $n$ rounds. In each round, the following two-step process happens:

  1. Alice chooses two different colors.    2. Bob chooses an uncolored vertex and colors it with one of the two colors chosen by Alice.

Alice wins if there exists an edge connecting two vertices of the same color. Otherwise, Bob wins.

You are given the graph. Your task is to decide which player you wish to play as and win the game.

Input Format:
Each test contains multiple test cases. The first line 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 two integers $n$, $m$ ($1 \le n \le 10^4$, $n - 1 \le m \le \min(\frac{n \cdot (n - 1)}{2}, 10^4)$) — the number of vertices and the number of edges in the graph, respectively.

Each of the next $m$ lines of each test case contains two integers $u_i$, $v_i$ ($1 \le u_i, v_i \le n$) — the edges of the graph. It is guaranteed that the graph is connected and there are no multiple edges or self-loops.

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

Output Format:

Tags:
['constructive algorithms', 'dfs and similar', 'games', 'graphs', 'greedy', 'interactive']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given $n$ sticks, numbered from $1$ to $n$. The length of the $i$-th stick is $a_i$.

You need to answer $q$ queries. In each query, you are given two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$). Determine whether it is possible to choose $6$ distinct sticks from the sticks numbered $l$ to $r$, to form $2$ non-degenerate triangles$^{\text{∗}}$.

$^{\text{∗}}$A triangle with side lengths $a$, $b$, and $c$ is called non-degenerate if:

  * $a < b + c$,    * $b < a + c$, and    * $c < a + b$.

Input Format:
The first line contains two integers $n$ and $q$ ($6 \le n \le 10^5$, $1 \le q \le 10^5$) — the number of sticks and the number of queries respectively.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — $a_i$ denotes the length of the $i$-th stick.

Each of the following $q$ lines contains two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$) — the parameters of each query.

Output Format:
For each query, output "YES" (without quotes) if it is possible to form $2$ triangles, 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:
['brute force', 'greedy', 'implementation', 'math', 'sortings']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
You are given a grid consisting of $n$ rows and $m$ columns, where each cell is initially white. Additionally, you are given an integer $k$, where $1 \le k \le \min(n, m)$.

You will process $q$ operations of two types:

  * $\mathtt{H}$ (horizontal operation) — You choose a $1 \times k$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black.    * $\mathtt{V}$ (vertical operation) — You choose a $k \times 1$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black.

After each operation, if any rows or columns become completely black, all cells in these rows and columns are simultaneously reset to white. Specifically, if all cells in the row and column a cell is contained in become black, all cells in both the row and column will be reset to white.

Choose the rectangles in a way that you can perform all given operations, or determine that it is impossible.

Input Format:
Each test contains multiple test cases. The first line 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 four integers $n$, $m$, $k$, and $q$ ($1 \le n, m \le 100$, $1 \le k \le \min(n, m)$, $1 \le q \le 1000$) — the number of rows and columns in the grid, the size of the operation rectangle, and the number of operations, respectively.

The second line of each test case contains a string $s$ of length $q$, consisting only of characters $\mathtt{H}$ and $\mathtt{V}$ — the sequence of operation types.

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

Output Format:
For each test case, output a single integer $-1$ if it is impossible to perform all the operations.

Otherwise, output $q$ lines. Each line contains two integers $i$, $j$ ($1 \le i \le n$, $1 \le j \le m$) — the coordinates of the top-left cell of the operation rectangle.

If there are multiple solutions, output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Alice and Bob are playing a game with $n$ piles of stones, where the $i$-th pile has $a_i$ stones. Players take turns making moves, with Alice going first.

On each move, the player does the following three-step process:

  1. Choose an integer $k$ ($1 \leq k \leq \frac n 2$). Note that the value of $k$ can be different for different moves.    2. Remove $k$ piles of stones.    3. Choose another $k$ piles of stones and split each pile into two piles. The number of stones in each new pile must be a prime number.

The player who is unable to make a move loses.

Determine who will win if both players play optimally.

Input Format:
Each test contains 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 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 piles of stones.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — the number of stones in the piles.

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 "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise.

You can output each letter in any case (upper or lower). For example, the strings "alIcE", "Alice", and "alice" will all be considered identical.

Tags:
['bitmasks', 'dp', 'fft', 'games', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is an interactive problem.

You are given a grid with $n$ rows and $m$ columns. You need to fill each cell with a unique integer from $1$ to $n \cdot m$.

After filling the grid, you will play a game on this grid against the interactor. Players take turns selecting one of the previously unselected cells from the grid, with the interactor going first.

On the first turn, the interactor can choose any cell from the grid. After that, any chosen cell must be orthogonally adjacent to at least one previously selected cell. Two cells are considered orthogonally adjacent if they share an edge. The game continues until all cells have been selected.

Your goal is to let the sum of numbers in the cells selected by you be strictly less than the sum of numbers in the cells selected by the interactor.

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

The only line of each test case contains two integers $n$ and $m$ ($4 \le n, m \le 10$) — the number of rows and columns in the grid.

Output Format:

Tags:
['constructive algorithms', 'games', 'graph matchings', 'greedy', 'interactive']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
Kmes has written three integers $a$, $b$ and $c$ in order to remember that he has to give Noobish_Monk $a \times b \times c$ bananas.

Noobish_Monk has found these integers and decided to do the following at most $5$ times:

  * pick one of these integers;    * increase it by $1$.

For example, if $a = 2$, $b = 3$ and $c = 4$, then one can increase $a$ three times by one and increase $b$ two times. After that $a = 5$, $b = 5$, $c = 4$. Then the total number of bananas will be $5 \times 5 \times 4 = 100$.

What is the maximum value of $a \times b \times c$ Noobish_Monk can achieve with these operations?

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 first and only line of each test case contains three integers $a$, $b$ and $c$ ($1 \le a, b, c \le 10$) — Kmes's integers.

Output Format:
For each test case, output a single integer — the maximum amount of bananas Noobish_Monk can get.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
To celebrate his recovery, k1o0n has baked an enormous $n$ metres long potato casserole.

Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into $k$ pieces, of lengths $a_1, a_2, \dots, a_k$ meters.

k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations:

  * Pick a piece with length $a_i \ge 2$ and divide it into two pieces with lengths $1$ and $a_i - 1$. As a result, the number of pieces will increase by $1$;    * Pick a slice $a_i$ and another slice with length $a_j=1$ ($i \ne j$) and merge them into one piece with length $a_i+1$. As a result, the number of pieces will decrease by $1$.

Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length $n$.

For example, if $n=5$, $k=2$ and $a = [3, 2]$, it is optimal to do the following:

  1. Divide the piece with length $2$ into two pieces with lengths $2-1=1$ and $1$, as a result $a = [3, 1, 1]$.    2. Merge the piece with length $3$ and the piece with length $1$, as a result $a = [4, 1]$.    3. Merge the piece with length $4$ and the piece with length $1$, as a result $a = [5]$.

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

Description of each test case consists of two lines. The first line contains two integers $n$ and $k$ ($2 \le n \le 10^9$, $2 \le k \le 10^5$) — length of casserole and the number of pieces.

The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n - 1$, $\sum a_i = n$) — lengths of pieces of casserole, which Noobish_Monk has cut.

It is guaranteed that the sum of $k$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.

Output Format:
For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Gorilla and Noobish_Monk found three numbers $n$, $m$, and $k$ ($m < k$). They decided to construct a permutation$^{\dagger}$ of length $n$.

For the permutation, Noobish_Monk came up with the following function: $g(i)$ is the sum of all the numbers in the permutation on a prefix of length $i$ that are not greater than $m$. Similarly, Gorilla came up with the function $f$, where $f(i)$ is the sum of all the numbers in the permutation on a prefix of length $i$ that are not less than $k$. A prefix of length $i$ is a subarray consisting of the first $i$ elements of the original array.

For example, if $n = 5$, $m = 2$, $k = 5$, and the permutation is $[5, 3, 4, 1, 2]$, then:

  * $f(1) = 5$, because $5 \ge 5$; $g(1) = 0$, because $5 > 2$;    * $f(2) = 5$, because $3 < 5$; $g(2) = 0$, because $3 > 2$;    * $f(3) = 5$, because $4 < 5$; $g(3) = 0$, because $4 > 2$;    * $f(4) = 5$, because $1 < 5$; $g(4) = 1$, because $1 \le 2$;    * $f(5) = 5$, because $2 < 5$; $g(5) = 1 + 2 = 3$, because $2 \le 2$.

Help them find a permutation for which the value of $\left(\sum_{i=1}^n f(i) - \sum_{i=1}^n g(i)\right)$ is maximized.

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

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 case contains three integers $n$, $m$, $k$ ($2\le n \le 10^5$; $1 \le m < k \le n$) — the size of the permutation to be constructed and two integers.

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 permutation  — a set of numbers that satisfies the conditions of the problem. If there are multiple solutions, output any of them.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
ErnKor is ready to do anything for Julen, even to swim through crocodile- infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $1$ meter and a length of $n$ meters.

The river is very cold. Therefore, in total (that is, throughout the entire swim from $0$ to $n+1$) ErnKor can swim in the water for no more than $k$ meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows:

Initially, ErnKor is on the left bank and needs to reach the right bank. They are located at the $0$ and $n+1$ meters respectively. The river can be represented as $n$ segments, each with a length of $1$ meter. Each segment contains either a log 'L', a crocodile 'C', or just water 'W'. ErnKor can move as follows:

  * If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than $m$ ($1 \le m \le 10$) meters (he can jump on the bank, on a log, or in the water).    * If he is in the water, he can only swim to the next river segment (or to the bank if he is at the $n$-th meter).    * ErnKor cannot land in a segment with a crocodile in any way.

Determine if ErnKor can reach the right bank.

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

The first line of each test case contains three numbers $n, m, k$ ($0 \le k \le 2 \cdot 10^5$, $1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10$) — the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing.

The second line of each test case contains a string $a$ of length $n$. $a_i$ denotes the object located at the $i$-th meter. ($a_i \in \\{$'W','C','L'$\\}$)

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 "YES" if ErnKor can pass the test, and output "NO" 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:
['dp', 'greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["6\n6 2 0\nLWLLLW\n6 1 1\nLWLLLL\n6 1 1\nLWLLWL\n6 2 15\nLWLLCC\n6 10 0\nCCCCCC\n6 6 1\nWCCCCW"], "outputs": ["YES\nYES\nNO\nNO\nYES\nYES"]}


Question:
One of the first programming problems by K1o0n looked like this: "Noobish_Monk has $n$ $(1 \le n \le 100)$ friends. Each of them gave him $a$ $(1 \le a \le 10000)$ apples for his birthday. Delighted with such a gift, Noobish_Monk returned $b$ $(1 \le b \le \min(10000, a \cdot n))$ apples to his friends. How many apples are left with Noobish_Monk?"

K1o0n wrote a solution, but accidentally considered the value of $n$ as a string, so the value of $n \cdot a - b$ was calculated differently. Specifically:

  * when multiplying the string $n$ by the integer $a$, he will get the string $s=\underbrace{n + n + \dots + n + n}_{a\ \text{times}}$    * when subtracting the integer $b$ from the string $s$, the last $b$ characters will be removed from it. If $b$ is greater than or equal to the length of the string $s$, it will become empty.

Learning about this, ErnKor became interested in how many pairs $(a, b)$ exist for a given $n$, satisfying the constraints of the problem, on which K1o0n's solution gives the correct answer.

"The solution gives the correct answer" means that it outputs a non-empty string, and this string, when converted to an integer, equals the correct answer, i.e., the value of $n \cdot a - b$.

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

For each test case, a single line of input contains an integer $n$ ($1 \le n \le 100$).

It is guaranteed that in all test cases, $n$ is distinct.

Output Format:
For each test case, output the answer in the following format:

In the first line, output the integer $x$ — the number of bad tests for the given $n$.

In the next $x$ lines, output two integers $a_i$ and $b_i$ — such integers that K1o0n's solution on the test "$n$ $a_i$ $b_i$" gives the correct answer.

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

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy.

Now, in order to make a purchase, Kmes needs to solve the following problem: $n$ cards with prices for different positions are laid out in front of him, on the $i$-th card there is an integer $a_i$, among these prices there is no whole positive integer $x$.

Kmes is asked to divide these cards into the minimum number of bad segments (so that each card belongs to exactly one segment). A segment is considered bad if it is impossible to select a subset of cards with a product equal to $x$. All segments, in which Kmes will divide the cards, must be bad.

Formally, the segment $(l, r)$ is bad if there are no indices $i_1 < i_2 < \ldots < i_k$ such that $l \le i_1, i_k \le r$, and $a_{i_1} \cdot a_{i_2} \ldots \cdot a_{i_k} = x$.

Help Kmes determine the minimum number of bad segments in order to enjoy his favorite dish.

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

The first line of each set of input data gives you $2$ integers $n$ and $x$ ($1 \le n \le 10^5, 2 \le x \le 10^5$) — the number of cards and the integer, respectively.

The second line of each set of input data contains $n$ integers $a_i$ ($1 \le a_i \le 2 \cdot 10^5, a_i \neq x$) — the prices on the cards.

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

Output Format:
For each set of input data, output the minimum number of bad segments.

Tags:
['brute force', 'dp', 'greedy', 'number theory', 'two pointers']

Time Limit: 4000 ms
Memory Limit: 512 MB


Question:
K1o0n gave you an array $a$ of length $n$, consisting of numbers $1, 2, \ldots, n$. Accept it? Of course! But what to do with it? Of course, calculate $\text{MEOW}(a)$.

Let $\text{MEX}(S, k)$ be the $k$-th positive (strictly greater than zero) integer in ascending order that is not present in the set $S$. Denote $\text{MEOW}(a)$ as the sum of $\text{MEX}(b, |b| + 1)$, over all distinct subsets $b$ of the array $a$.

Examples of $\text{MEX}(S, k)$ values for sets:

  * $\text{MEX}(\\{3,2\\}, 1) = 1$, because $1$ is the first positive integer not present in the set;    * $\text{MEX}(\\{4,2,1\\}, 2) = 5$, because the first two positive integers not present in the set are $3$ and $5$;    * $\text{MEX}(\\{\\}, 4) = 4$, because there are no numbers in the empty set, so the first $4$ positive integers not present in it are $1, 2, 3, 4$.

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

In a single line of each test case, an integer $n$ ($1 \le n \le 5000$) is entered, the size of the array of gifted numbers.

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

Output Format:
For each test case, output a single number — $\text{MEOW}(a)$. Since it may be very large, output it modulo $10^9 + 7$.

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

Time Limit: 2500 ms
Memory Limit: 256 MB


Question:
Tim is doing a test consisting of $4n$ questions; each question has $4$ options: 'A', 'B', 'C', and 'D'. For each option, there are exactly $n$ correct answers corresponding to that option — meaning there are $n$ questions with the answer 'A', $n$ questions with the answer 'B', $n$ questions with the answer 'C', and $n$ questions with the answer 'D'.

For each question, Tim wrote his answer on the answer sheet. If he could not figure out the answer, he would leave a question mark '?' for that question.

You are given his answer sheet of $4n$ characters. What is the maximum number of correct answers Tim can get?

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

The first line of each test case contains an integer $n$ ($1 \le n \le 100$).

The second line of each test case contains a string $s$ of $4n$ characters ($s_i \in \\{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}\\}$) — Tim's answers for the questions.

Output Format:
For each test case, print a single integer — the maximum score that Tim can achieve.

Tags:
['greedy', 'implementation']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Given an array $a$ of $n$ positive integers.

In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally:

  * If $a_i < a_j$, replace $a_i$ with $a_i + a_j$;    * Otherwise, replace $a_j$ with $a_i + a_j$.

Find the minimum number of operations needed to make all elements of the array have the same parity.

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

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

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

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 minimum number of operations required.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
There is an apartment consisting of $n$ rooms, each with its light initially turned off.

To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represented by the array $a_1, a_2, \ldots, a_n$, where $a_i$ is the time (in minutes) at which a chip is installed in the $i$-th room.

As soon as a chip is installed, it changes the room's light status every $k$ minutes — it turns on the light for $k$ minutes, then turns it off for the next $k$ minutes, then turns it back on for the next $k$ minutes, and so on. In other words, the light status is changed by the chip at minute $a_i$, $a_i + k$, $a_i + 2k$, $a_i + 3k$, $\ldots$ for the $i$-th room.

What is the earliest moment when all rooms in the apartment have their lights turned on?

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

The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of rooms in the apartment and the period of the chips.

The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the moments when the chips are installed.

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, print a single integer — the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print $-1$ instead.

Tags:
['implementation', 'math']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given two positive integers $n$ and $k$, and another array $a$ of $n$ integers.

In one operation, you can select any subarray of size $k$ of $a$, then remove it from the array without changing the order of other elements. More formally, let $(l, r)$ be an operation on subarray $a_l, a_{l+1}, \ldots, a_r$ such that $r-l+1=k$, then performing this operation means replacing $a$ with $[a_1, \ldots, a_{l-1}, a_{r+1}, \ldots, a_n]$.

For example, if $a=[1,2,3,4,5]$ and we perform operation $(3,5)$ on this array, it will become $a=[1,2]$. Moreover, operation $(2, 4)$ results in $a=[1,5]$, and operation $(1,3)$ results in $a=[4,5]$.

You have to repeat the operation while the length of $a$ is greater than $k$ (which means $|a| \gt k$). What is the largest possible median$^\dagger$ of all remaining elements of the array $a$ after the process?

$^\dagger$The median of an array of length $n$ is the element whose index is $\left \lfloor (n+1)/2 \right \rfloor$ after we sort the elements in non-decreasing order. For example: $median([2,1,5,4,3]) = 3$, $median([5]) = 5$, and $median([6,8,2,4]) = 4$.

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

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

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

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

Output Format:
For each test case, print a single integer — the largest median possible after performing the operations.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Given a matrix $a$ of size $n \times m$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $i$-th row and the $j$-th column of the matrix is called $a_{i,j}$.

Let's define $f(i)$ and $g(j)$ as the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) of all integers in the $i$-th row and the $j$-th column, respectively. In one operation, you can either:

  * Select any row $i$, then assign $a_{i,j} := g(j)$ for each $1 \le j \le m$; or    * Select any column $j$, then assign $a_{i,j} := f(i)$ for each $1 \le i \le n$.

![](CDN_BASE_URL/3bba110245afb461968dbb618fc60828) An example of applying an operation on column $2$ of the matrix.

In this example, as we apply an operation on column $2$, all elements in this column are changed:

  * $a_{1,2} := f(1) = a_{1,1} \oplus a_{1,2} \oplus a_{1,3} \oplus a_{1,4} = 1 \oplus 1 \oplus 1 \oplus 1 = 0$    * $a_{2,2} := f(2) = a_{2,1} \oplus a_{2,2} \oplus a_{2,3} \oplus a_{2,4} = 2 \oplus 3 \oplus 5 \oplus 7 = 3$    * $a_{3,2} := f(3) = a_{3,1} \oplus a_{3,2} \oplus a_{3,3} \oplus a_{3,4} = 2 \oplus 0 \oplus 3 \oplus 0 = 1$    * $a_{4,2} := f(4) = a_{4,1} \oplus a_{4,2} \oplus a_{4,3} \oplus a_{4,4} = 10 \oplus 11 \oplus 12 \oplus 16 = 29$

You can apply the operations any number of times. Then, we calculate the $\textit{beauty}$ of the final matrix by summing the absolute differences between all pairs of its adjacent cells.

More formally, $\textit{beauty}(a) = \sum|a_{x,y} - a_{r,c}|$ for all cells $(x, y)$ and $(r, c)$ if they are adjacent. Two cells are considered adjacent if they share a side.

Find the minimum $\textit{beauty}$ among all obtainable matrices.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 15$) — the number of rows and columns of $a$, respectively.

The next $n$ lines, each containing $m$ integers $a_{i,1}, a_{i,2}, \ldots, a_{i,m}$ ($0 \le a_{i,j} < 2^{20}$) — description of the matrix $a$.

It is guaranteed that the sum of $(n^2 + m^2)$ over all test cases does not exceed $500$.

Output Format:
For each test case, print a single integer $b$ — the smallest possible $\textit{beauty}$ of the matrix.

Tags:
['bitmasks', 'constructive algorithms', 'dp', 'implementation']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference is that in this version $k \le n$. You can make hacks only if both versions of the problem are solved.      Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.

You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.

The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:

  * If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's).    * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).

Then, it will execute the changed script starting from the character which it couldn't execute.

![](CDN_BASE_URL/ff5ae9758c965c2d8398c936e9581dab) An example of the robot's movement process, $s = \texttt{"ULULURD"}$

The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.

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

The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le n$).

The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) — the script to be executed.

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

Output Format:
For each test case, print a single integer — the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.

Tags:
['brute force', 'chinese remainder theorem', 'constructive algorithms', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
This is the hard version of the problem. The only difference is that in this version $k \le 10^{12}$. You can make hacks only if both versions of the problem are solved.

Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.

You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.

The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:

  * If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's).    * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).

Then, it will execute the changed script starting from the character which it couldn't execute.

![](CDN_BASE_URL/ff5ae9758c965c2d8398c936e9581dab) An example of the robot's movement process, $s = \texttt{"ULULURD"}$

The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.

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

The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le 10^{12}$).

The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) — the script to be executed.

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

Output Format:
For each test case, print a single integer — the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.

Tags:
['chinese remainder theorem', 'math', 'number theory']

Time Limit: 3000 ms
Memory Limit: 256 MB


Question:
Petr, watching Sergey's stream, came up with a matrix $a$, consisting of $n$ rows and $m$ columns (the number in the $i$-th row and $j$-th column is denoted as $a_{i, j}$), which contains all integers from $1$ to $n \cdot m$. But he didn't like the arrangement of the numbers, and now he wants to come up with a new matrix $b$, consisting of $n$ rows and $m$ columns, which will also contain all integers from $1$ to $n \cdot m$, such that for any $1 \leq i \leq n, 1 \leq j \leq m$ it holds that $a_{i, j} \ne b_{i, j}$.

You are given the matrix $a$, construct any matrix $b$ that meets Petr's requirements, or determine that it is impossible.

Hurry up! Otherwise, he will donate all his money to the stream in search of an answer to his question.

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

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n, m \leq 10$) — the number of rows and columns of matrix $a$.

The next $n$ lines contain $m$ integers each, describing matrix $a$. The $i$-th of these lines contains the elements of matrix $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$.

It is guaranteed that all numbers in matrix $a$ are distinct and $1 \leq a_{i, j} \leq n \cdot m$.

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

Output Format:
For each test case, output $n \cdot m$ integers — any suitable matrix $b$, or $-1$ if such a matrix does not exist.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Vova really loves the [XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation (denoted as $\oplus$). Recently, when he was going to sleep, he came up with a fun game.

At the beginning of the game, Vova chooses two binary sequences $s$ and $t$ of length $n$ and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers $0$ and $1$. Vanya can choose integers $l, r$ such that $1 \leq l \leq r \leq n$, and for all $l \leq i \leq r$ simultaneously replace $s_i$ with $s_i \oplus s_{i - l + 1}$, where $s_i$ is the $i$-th element of the sequence $s$.

In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence $t$ from the sequence $s$. Determine if the game will be interesting for the sequences $s$ and $t$.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $q$ ($1 \le q \le 10^{4}$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the sequences $s$ and $t$.

The second line of each test case contains a binary sequence $s$ of length $n$.

The third line of each test case contains a binary sequence $t$ 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 "Yes" if the game will be interesting, otherwise output "No".

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', 'constructive algorithms', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Yaroslav is playing a computer game, and at one of the levels, he encountered $n$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $i$-th mushroom from the beginning has a toxicity level of $a_i$. Yaroslav can choose two integers $1 \le l \le r \le n$, and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers $l, l+1, l+2, \ldots, r$.

The character has a toxicity level $g$, initially equal to $0$. The computer game is defined by the number $x$ — the maximum toxicity level at any given time. When eating a mushroom with toxicity level $k$, the following happens:

  1. The toxicity level of the character is increased by $k$.    2. If $g \leq x$, the process continues; otherwise, $g$ becomes zero and the process continues.

Yaroslav became interested in how many ways there are to choose the values of $l$ and $r$ such that the final value of $g$ is not zero. Help Yaroslav find this number!

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

The first line of each test case contains two integers $n$, $x$ ($1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9$) — the number of mushrooms and the maximum toxicity level.

The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).

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 number — the number of subsegments such that the final value of $g$ will not be zero.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Vanya has a graph with $n$ vertices (numbered from $1$ to $n$) and an array $a$ of $n$ integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform $n - 1$ operations.

Operation number $x$ (operations are numbered in order starting from $1$) is as follows:

  * Choose $2$ different numbers $1 \leq u,v \leq n$, such that $|a_u - a_v|$ is divisible by $x$.    * Add an undirected edge between vertices $u$ and $v$ to the graph.

Help Vanya get a connected$^{\text{∗}}$ graph using the $n - 1$ operations, or determine that it is impossible.

$^{\text{∗}}$A graph is called connected if it is possible to reach any vertex from any other by moving along the edges.

Input Format:
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{3}$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains the number $n$ ($1 \leq n \leq 2000$) — the number of vertices in the graph.

The second line of each test case contains $n$ numbers $a_1, a_2, \cdots a_n$ ($1 \leq a_i \leq 10^9$).

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

Output Format:
For each test case, if there is no solution, then output "No" (without quotes).

Otherwise, output "Yes" (without quotes), and then output $n - 1$ lines, where in the $i$-th line, output the numbers $u$ and $v$ that need to be chosen for operation $i$.

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:
['constructive algorithms', 'dsu', 'graphs', 'greedy', 'math', 'number theory', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given a forest of $k$ rooted trees$^{\text{∗}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation:

  * Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree.

Timofey loves bitwise operations, so he wants the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain.

$^{\text{∗}}$ A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees.

$^{\text{†}}$ The subtree of a vertex $v$ is the set of vertices for which $v$ lies on the shortest path from this vertex to the root, including $v$ itself.

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

The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) — the number of trees in the forest.

This is followed by a description of each of the $k$ trees:

The first line contains a single integer $n$ ($1 \leq n \leq 10^6$) — the size of the tree. The vertices of the tree are numbered with integers from $1$ to $n$. The root of the tree is vertex number $1$.

The second line contains $n - 1$ integers $p_2, p_3, \ldots p_n$ ($1 \leq p_i < i$), where $p_i$ — the parent of vertex $i$.

It is guaranteed that the sum of $k$ and $n$ for all sets of input data does not exceed $10^6$.

Output Format:
For each test case, output a single integer — the maximum result that can be obtained.

Tags:
['bitmasks', 'greedy', 'math', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Pelican Town represents $n$ houses connected by $m$ bidirectional roads. Some roads have NPCs standing on them. Farmer Buba needs to walk on each road with an NPC and talk to them.

Help the farmer find a route satisfying the following properties:

  * The route starts at some house, follows the roads, and ends at the same house.    * The route does not follow any road more than once (in both directions together).    * The route follows each road with an NPC exactly once.

Note that the route can follow roads without NPCs, and you do not need to minimize the length of the route.

It is guaranteed that you can reach any house from any other by walking on the roads with NPCs only.

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

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 5 \cdot 10^5, 1 \leq m \leq 5 \cdot 10^5$) — the number of houses and roads in Pelican Town respectively.

In each of the next $m$ lines, three integers $u$, $v$, and $c$ ($1 \leq u, v \leq n, c = 0/1$) are given — the ends of the road and whether an NPC is on this road. If $c = 1$, then the road has an NPC. If $c = 0$, then the road has no NPC.

The graph may contain multiple edges and loops, and if there are multiple edges with NPCs standing on them, the route must follow each of these roads.

It is guaranteed that you can reach any house from any other by walking on the roads with NPCs only.

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

Output Format:
For each test case, if there is no solution, then output "No" (without quotes).

Otherwise, output "Yes" (without quotes), and then output $k$ — the number of roads in the route. In the next line, output $k + 1$ numbers — the houses of the route in the order of traversal. Note that the first house should match the last one, as the route is cyclic.

If there are multiple answers, you can print any of them.

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:
['constructive algorithms', 'dfs and similar', 'graphs', 'trees']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
After winning another Bed Wars game, Masha and Olya wanted to relax and decided to play a new game. Masha gives Olya an array $a$ of length $n$ and a number $s$. Now Olya's task is to find a non-negative number $x$ such that $\displaystyle\sum_{i=1}^{n} a_i \oplus x = s$. But she is very tired after a tight round, so please help her with this.

But this task seemed too simple to them, so they decided to make the numbers larger (up to $2^k$) and provide you with their binary representation.

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

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

The second line contains a string of length $k$, consisting of zeros and ones — the binary representation of the number $s$, starting from the most significant bits.

The next $n$ lines also contain strings of length $k$, consisting of zeros and ones, the $i$-th of these strings contains the binary representation of the number $a_i$, starting from the most significant bits.

It is guaranteed that the sum of the values $n \cdot k$ for all test cases does not exceed $2 \cdot 10^6$.

Output Format:
For each test case, output a string of length $k$ on a separate line, consisting of zeros or ones — the binary representation of any suitable number $x$ ($x \ge 0$), starting from the most significant bits, or $-1$ if such $x$ does not exist.

Tags:
['bitmasks', 'brute force', 'dp', 'graphs', 'math']

Time Limit: 2000 ms
Memory Limit: 512 MB


Question:
This is an interactive problem!

Timofey is writing a competition called Capture the Flag (or CTF for short). He has one task left, which involves hacking a security system. The entire system is based on polynomial hashes$^{\text{∗}}$.

Timofey can input a string consisting of lowercase Latin letters into the system, and the system will return its polynomial hash. To hack the system, Timofey needs to find the polynomial hash parameters ($p$ and $m$) that the system uses.

Timofey doesn't have much time left, so he will only be able to make $3$ queries. Help him solve the task.

$^{\text{∗}}$ The polynomial hash of a string $s$, consisting of lowercase Latin letters of length $n$, based on $p$ and modulo $m$ is $(\mathrm{ord}(s_1) \cdot p ^ 0 + \mathrm{ord}(s_2) \cdot p ^ 1 + \mathrm{ord}(s_3) \cdot p ^ 2 + \ldots + \mathrm{ord}(s_n) \cdot p ^ {n - 1}) \bmod m$. Where $s_i$ denotes the $i$-th character of the string $s$, $\mathrm{ord}(\mathrm{chr})$ denotes the ordinal number of the character $\mathrm{chr}$ in the English alphabet, and $x \bmod m$ is the remainder of $x$ when divided by $m$.

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

It is guaranteed that the $p$ and $m$ used by the system satisfy the conditions: $26 < p \leq 50$ and $p + 1 < m \leq 2 \cdot 10^9$.

Output Format:

Tags:
['combinatorics', 'constructive algorithms', 'games', 'greedy', 'hashing', 'interactive', 'math', 'number theory', 'strings']

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
Vitaly503 is given a checkered board with a side of $n$ and $k$ chips. He realized that all these $k$ chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).

Let's denote the cell in the $i$-th row and $j$-th column as $(i ,j)$. A diagonal is the set of cells for which the value $i + j$ is the same. For example, cells $(3, 1)$, $(2, 2)$, and $(1, 3)$ lie on the same diagonal, but $(1, 2)$ and $(2, 3)$ do not. A diagonal is called occupied if it contains at least one chip.

Determine what is the minimum possible number of occupied diagonals among all placements of $k$ chips.

Input Format:
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \le t \le 500$) — the number of sets of input data. Then follow the descriptions of the sets of input data.

The only line of each set of input data contains two integers $n$, $k$ ($1 \le n \le 100, 0 \le k \le n^2$) — the side of the checkered board and the number of available chips, respectively.

Output Format:
For each set of input data, output a single integer — the minimum number of occupied diagonals with at least one chip that he can get after placing all $k$ chips.

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

Time Limit: 1000 ms
Memory Limit: 256 MB


Question:
This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration.

A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ flowers in the store, each of which is characterized by the number of petals, and a flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use in her bouquet should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. This is followed by descriptions of the test cases.

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^{18}$) — the number of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower in the store.

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 maximum possible number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.

Tags:
['binary search', 'brute force', 'greedy', 'sortings', 'two pointers']

Time Limit: 1500 ms
Memory Limit: 512 MB


Question:
This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.

A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. This is followed by descriptions of the test cases.

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^{18}$) — the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ different integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower type in the store (for different indexes $i \neq j$, it must be $a_i \neq a_j$). The third line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^9$), where $c_i$ is the quantity of the $i$-th flower type in the store.

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

Output Format:
For each test case, print one integer — the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.

Tags:
['binary search', 'data structures', 'greedy', 'math', 'sortings', 'two pointers']

Time Limit: 1500 ms
Memory Limit: 256 MB


Question:
ikrpprpp found an array $a$ consisting of integers. He likes justice, so he wants to make $a$ fair — that is, make it non-decreasing. To do that, he can perform an act of justice on an index $1 \le i \le n$ of the array, which will replace $a_i$ with $a_i ^ 2$ (the element at position $i$ with its square). For example, if $a = [2,4,3,3,5,3]$ and ikrpprpp chooses to perform an act of justice on $i = 4$, $a$ becomes $[2,4,3,9,5,3]$.

What is the minimum number of acts of justice needed to make the array non- decreasing?

Input Format:
First line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. It is followed by the description of test cases.

For each test case, the first line contains an integer $n$ — size of the array $a$. The second line contains $n$ ($1 \le n \le 2 \cdot 10 ^5$) integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i \le 10 ^ 6$).

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

Output Format:
For each testcase, print an integer — minimum number of acts of justice required to make the array $a$ non-decreasing. If it is impossible to do that, print $-1$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You're a linguist studying a mysterious ancient language. You know that

  1. Its words consist only of the first $c$ letters of the Latin alphabet.    2. Each word has a case which can be unambiguously determined by its last letter (different letters correspond to different cases). For example, words "ABACABA" and "ABA" (if they exist) have the same case in this language because they both have the same ending 'A', whereas "ALICE" and "BOB" have different cases. If the language does not have a case corresponding to some letter, it means that the word cannot end with this letter.    3. The length of each word is $k$ or less.

You have a single text written in this language. Unfortunately, as the language is really ancient, spaces between words are missing and all letters are uppercase. You wonder what is the minimum number of cases the language can have. Can you find this out?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. It is followed by descriptions of the test cases.

The first line of each test case contains three integers $n$, $c$, $k$ ($1 \le k \le n \le 2^{18}$, $1 \le c \le 18$) — the length of the text, the number of letters in the language, and the maximum length of the word.

The second line contains a string of $n$ characters — the text itself. Each character is one of the first $c$ uppercase letters of the Latin alphabet.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2^{18}$ and the sum of $2^c$ over all test cases does not exceed $2^{18}$.

Output Format:
For each test case, output a single line consisting of a single integer — the minimum number of cases in the language.

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

Time Limit: 2000 ms
Memory Limit: 256 MB

{"inputs": ["7\n5 5 1\nABCDE\n3 1 2\nAAA\n3 2 2\nAAB\n10 2 2\nABABABABAB\n4 4 4\nDCBA\n1 17 1\nQ\n9 3 2\nABCABCABC"], "outputs": ["5\n1\n2\n1\n1\n1\n2"]}


Question:
This is the easy version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.

Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.

Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.

However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. It is followed by descriptions of the test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) — the number of desks.

The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) — the intelligence values of the knights.

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 line containing one integer — the minimal difference Arthur can achieve.

Tags:
['2-sat', 'data structures', 'dp', 'matrices', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
This is the hard version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.

Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.

Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.

However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?

Input Format:
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) — the number of test cases. It is followed by descriptions of the test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 100\,000$) — the number of desks.

The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) — the intelligence values of the knights.

It is guaranteed that the sum of $n$ over all test cases does not exceed $100\,000$.

Output Format:
For each test case, output a single line containing one integer — the minimal difference Arthur can achieve.

Tags:
['data structures', 'dp', 'matrices', 'two pointers']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
It's another beautiful day on Farmer John's farm.

After Farmer John arrived at his farm, he counted $n$ legs. It is known only chickens and cows live on the farm, and a chicken has $2$ legs while a cow has $4$.

What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?

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

Each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^3$, $n$ is even).

Output Format:
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.

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

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$.

Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of cells such that every cell belongs to exactly one block.

Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value.

For example, the following demonstration shows a grid being reduced by factor of $3$.

Original grid $0$| $0$| $0$| $1$| $1$| $1$   ---|---|---|---|---|---   $0$| $0$| $0$| $1$| $1$| $1$   $0$| $0$| $0$| $1$| $1$| $1$   $1$| $1$| $1$| $0$| $0$| $0$   $1$| $1$| $1$| $0$| $0$| $0$   $1$| $1$| $1$| $0$| $0$| $0$   Reduced grid $0$| $1$   ---|---   $1$| $0$      Help Tina reduce the grid by a factor of $k$.

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 two integers $n$ and $k$ ($1 \leq n \leq 1000$, $1 \le k \le n$, $k$ is a divisor of $n$) — the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by.

Each of the following $n$ lines contain $n$ characters describing the cells of the grid. Each character is either $0$ or $1$. It is guaranteed every $k$ by $k$ block has the same value.

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

Output Format:
For each test case, output the grid reduced by a factor of $k$ on a new line.

Tags:
['greedy', 'implementation']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries.

For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and set $a_i = x$ where $x$ is any character you desire. Output the minimum number of operations you must perform such that $\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}$. The operations you perform on one query does not affect other queries.

For an arbitrary string $c$, $\texttt{sorted(c[l..r])}$ denotes the substring consisting of characters $c_l, c_{l+1}, ... , c_r$ sorted in lexicographical order.

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

The first line of each test case contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^5$) – the length of both strings and the number of queries.

The following line contains $a$ of length $n$. It is guaranteed $a$ only contains lowercase latin letters.

The following line contains $b$ of length $n$. It is guaranteed $b$ only contains lowercase latin letters.

The following $q$ lines contain two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) – the range of the query.

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

Output Format:
For each query, output an integer, the minimum number of operations you need to perform in a new line.

Tags:
['dp', 'greedy', 'sortings', 'strings']

Time Limit: 5000 ms
Memory Limit: 256 MB


Question:
Counting is Fun!

— satyam343

Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.

Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as different) and $a$, $b$, $c$ must be strictly greater than $0$.

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

Each test case contains two integers $n$ and $x$ ($1 \leq n,x \leq 10^6$).

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

Output Format:
Output a single integer — the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.

Tags:
['binary search', 'brute force', 'combinatorics', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB


Question:
In a desperate attempt to obtain your waifu favorite character, you have hacked into the source code of the game. After days of struggling, you finally find the binary string that encodes the gacha system of the game. In order to decode it, you must first solve the following problem.

You are given a binary string $s$ of length $n$. For each pair of integers $(l, r)$ $(1 \leq l \leq r \leq n)$, count the number of pairs $(x, y)$ $(l \leq x \leq y \leq r)$ such that the amount of $\mathtt{0}$ equals the amount of $\mathtt{1}$ in the substring $s_xs_{x+1}...s_y$.

Output the sum of counts over all possible $(l, r)$ modulo $10^9+7$.

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

Each test case contains a binary string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$). It is guaranteed $s$ only contains characters $\mathtt{0}$ and $\mathtt{1}$.

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

Output Format:
For each test case, output an integer, the answer modulo $10^9+7$.

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

Time Limit: 2000 ms
Memory Limit: 256 MB