Codeforces Round 1105 (Div. 2)
A. Another Popcount Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two integers $$$n$$$ and $$$k$$$.

Your task is to construct a sequence $$$a$$$ consisting of $$$k$$$ non-negative integers $$$a_1, a_2, \ldots, a_k$$$ such that:

  • $$$\sum_{i=1}^{k} a_i \le n$$$
  • The total number of set bits, i.e., $$$\sum_{i=1}^{k} \operatorname{popcount}(a_i)$$$, is as large as possible.

You only need to output the maximum possible value of $$$\sum_{i=1}^{k} \operatorname{popcount}(a_i)$$$.

Here, $$$\operatorname{popcount}(x)$$$ denotes the number of $$$1$$$ bits in the binary representation of $$$x$$$. For example, $$$\operatorname{popcount}(6) = \operatorname{popcount}((110)_2) = 2$$$, and $$$\operatorname{popcount}(0) = 0$$$.

Input

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

Each of the next $$$t$$$ lines contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 10^6$$$) — the maximum allowed sum of the sequence and the length of the sequence, respectively.

Output

For each test case, output a single integer — the maximum possible value of $$$\sum_{i=1}^{k} \operatorname{popcount}(a_i)$$$.

Example
Input
6
2 1
3 1
6 2
14142 137205
1000000 100
1000000 1000000
Output
1
2
4
14142
1322
1000000
Note

In the first test case, $$$n=2$$$ and $$$k=1$$$. We can choose $$$a = [1]$$$ or $$$a = [2]$$$. In both cases, the sum of popcounts is $$$1$$$.

In the second test case, $$$n=3$$$ and $$$k=1$$$. We can choose $$$a = [3]$$$, since $$$(3)_2 = (11)_2$$$, $$$\operatorname{popcount}(3) = 2$$$.

In the third test case, $$$n=6$$$ and $$$k=2$$$. We can choose $$$a = [3, 3]$$$. The sum is $$$3 + 3 = 6 \le 6$$$, and the total popcount is $$$\operatorname{popcount}(3) + \operatorname{popcount}(3) = 2 + 2 = 4$$$.

B. AI Finds Nothing Here
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Milkcat2009 has a matrix $$$a$$$ with $$$n$$$ rows and $$$m$$$ columns consisting of integers $$$0$$$ and $$$1$$$. The rows are numbered from $$$1$$$ to $$$n$$$, and the columns are numbered from $$$1$$$ to $$$m$$$. The element in the $$$i$$$-th row and $$$j$$$-th column is denoted as $$$a_{i,j}$$$.

Milkcat2009's AI scans every single submatrix$$$^{\text{∗}}$$$ of $$$a$$$ consisting of $$$r$$$ rows and $$$c$$$ columns. The AI considers the matrix clean if the bitwise XOR sum of elements in each such submatrix is equal to $$$0$$$.

Formally, the matrix $$$a$$$ is valid if and only if for all $$$1 \le i \le n - r + 1$$$ and $$$1 \le j \le m - c + 1$$$: $$$$$$ \bigoplus_{x=i}^{i+r-1} \bigoplus_{y=j}^{j+c-1} a_{x,y} = 0 $$$$$$

where $$$\oplus$$$ denotes summation with the bitwise XOR operation.

Your task is, given various sets of integers $$$n, m, r, c$$$, to count the number of clean matrices of $$$n$$$ rows and $$$m$$$ columns with respect to scanning dimensions $$$r$$$ and $$$c$$$. Since the answer can be very large, calculate it modulo $$$998\,244\,353$$$.

$$$^{\text{∗}}$$$A submatrix of a matrix is obtained by removing some rows (from the beginning and/or end) and/or columns (from the beginning and/or end) from the original matrix.

Input

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

Each test case consists of a single line containing four integers $$$n, m, r, c$$$ ($$$1 \le r \le n \le 10^9$$$, $$$1 \le c \le m \le 10^9$$$), representing the dimensions of the matrix and the dimensions of the scanning area, respectively.

Output

For each test case, output a single integer representing the answer modulo $$$998\,244\,353$$$.

Example
Input
8
1 1 1 1
2 3 1 2
2 5 2 2
3 5 2 2
4 6 2 2
100 14 52 6
1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1 1
Output
1
4
64
128
512
543661425
121099884
1
Note

For the first test case. The only element $$$a_{1,1}$$$ must satisfy $$$a_{1,1} = 0$$$. Since $$$a_{1,1}$$$ must be $$$0$$$, there is only $$$1$$$ valid matrix: $$$[0]$$$.

For the second test case.The condition requires the XOR sum of every $$$1 \times 2$$$ submatrix to be $$$0$$$. This implies $$$a_{i,j} \oplus a_{i,j+1} = 0$$$, so $$$a_{i,j} = a_{i,j+1}$$$ for all valid $$$i,j$$$. For each row, all elements must be equal. Thus, each row can be either $$$[0, 0, 0]$$$ or $$$[1, 1, 1]$$$. With $$$2$$$ rows, there are $$$2^2 =4$$$ valid matrices.

C. Nim Game Is XOR Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob are playing a game with an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Alice goes first.

In each turn, the current player must choose an array of $$$n$$$ non-negative integers $$$b = [b_1, b_2, \ldots, b_n]$$$ that satisfies the following conditions:

  1. $$$0 \le b_i \le a_i$$$ for all $$$1 \le i \le n$$$;
  2. $$$\sum_{i=1}^n b_i \gt 0$$$ (i.e. the array $$$b$$$ does not consist entirely of zeros);
  3. $$$b_1 \oplus b_2 \oplus \ldots \oplus b_n = 0$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.

After choosing the array $$$b$$$, the player updates the array $$$a$$$ by performing $$$a_i \leftarrow a_i - b_i$$$ for all $$$1 \le i \le n$$$.

The player who cannot perform such an operation loses the game.

Determine the number of valid choices for the array $$$b$$$ that Alice can make on her first turn to guarantee a win, assuming both players play optimally. Since this number may be large, output the answer modulo $$$998\,244\,353$$$.

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — 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 \lt 2^{30}$$$) — the contents of the array $$$a$$$

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

Output

For each test case, output the number of valid choices for the array $$$b$$$ that Alice can make on her first turn to guarantee a win modulo $$$998\,244\,353$$$.

Example
Input
5
1
1
2
1 2
5
1 4 5 2 6
1
1073741823
3
1 2 3
Output
0
1
3
0
1
Note

In the first test case, Alice must choose an array $$$b$$$ of length $$$1$$$. The conditions require $$$b_1 \le a_1$$$, $$$b_1 \gt 0$$$, and $$$b_1 = 0$$$. It is impossible to satisfy $$$b_1 \gt 0$$$ and $$$b_1 = 0$$$ simultaneously. Thus, Alice has no valid moves and loses immediately. The answer is $$$0$$$.

In the second test case, $$$a = [1, 2]$$$. Alice must choose $$$b = [b_1, b_2]$$$. The condition $$$b_1 \oplus b_2 = 0$$$ implies that $$$b_1 = b_2$$$. Since $$$0 \le b_1 \le 1$$$ and $$$0 \le b_2 \le 2$$$, and the array $$$b$$$ cannot consist entirely of zeros, the only valid choice is $$$b = [1, 1]$$$. If Alice chooses $$$b = [1, 1]$$$, the array updates to $$$a = [1-1, 2-1] = [0, 1]$$$. Now it is Bob's turn. Similar to Alice's situation, Bob must choose $$$b'$$$ such that $$$b'_1 = b'_2$$$. Since $$$a_1 = 0$$$, he is forced to pick $$$b'_1 = 0$$$, which means $$$b'_2 = 0$$$. Since a valid move must have $$$\sum b'_i \gt 0$$$, Bob has no valid moves and loses. Therefore, $$$b = [1, 1]$$$ is a winning move for Alice, and the answer is $$$1$$$.

D. Decidophobia
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$n$$$ people attending a round-table party, numbered $$$1, 2, 3, \ldots, n$$$ in clockwise order. You have prepared some gifts to distribute among them.

Each person $$$i$$$ has a weight $$$a_i$$$ and a common field of view $$$d$$$. The field of view for person $$$i$$$ consists of the $$$d$$$ people sitting clockwise and the $$$d$$$ people sitting counter-clockwise from them (a total of $$$2d$$$ people excluding person $$$i$$$).

The happiness gained by person $$$i$$$ is determined by the following rules:

  • If person $$$i$$$ receives a gift, and there are $$$x$$$ people within their field of view who did not receive a gift, they gain $$$x \cdot a_i$$$ happiness.
  • If person $$$i$$$ does not receive a gift, and there are $$$x$$$ people within their field of view who received a gift, they incur $$$-x \cdot a_i$$$ happiness.

You want to maximize the total happiness of all $$$n$$$ people combined. Find this maximum value.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$d$$$ ($$$3 \le n \le 2 \cdot 10^5$$$, $$$1 \le d \lt \frac{n}{2}$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^8$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th person.

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

Output

For each test case, output a single integer representing the maximum total happiness.

Example
Input
5
3 1
1 2 3
5 1
1 4 5 2 6
6 2
1 1 4 5 1 4
10 2
230 24 3 42 432 234 934 2389 333 444
3 1
100000000 100000000 100000000
Output
3
15
26
8590
0
Note

In the first test case, there are 3 people sitting in a circle. For each person $$$i$$$, the field of view is $$$d=1$$$, which includes the 1 person sitting clockwise and the 1 person sitting counter-clockwise from them. If person 2 receives the gift, they gain happiness from 2 neighbors who did not receive it, resulting in $$$2 \cdot a_2 = 2 \cdot 2 = 4$$$. However, person 1 and person 3 incur loss because they did not receive a gift but have a neighbor who did. After calculating all possibilities, the maximum total happiness is 3.

In the second test case, $$$n=5, d=1$$$. The best solution is to give gifts to persons 2, 3, and 5, which can achieve the maximum total happiness value of 15.

E. Revival
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider a permutation $$$p$$$ of $$$1, 2, \ldots, n$$$. Let $$$s_i$$$ denote the number of inversions in the entire prefix $$$p_1, p_2, \ldots, p_i$$$, defined as:

$$$$$$ s_i = \sum_{1 \le x \lt y \le i} [p_x \gt p_y], $$$$$$

where the square parantheses denote the Iverson bracket notation.

For each position $$$i$$$ ($$$1 \le i \le n$$$), you are given a condition in the form of either $$$p_i = x$$$ or $$$s_i = x$$$. Your task is to reconstruct the original permutation $$$p$$$.

Input

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

For each test case: The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$). Each of the following $$$n$$$ lines contains a character $$$c$$$ ($$$c \in \{\text{'p'}, \text{'s'}\}$$$) and an integer $$$x$$$:

  • If $$$c = \text{'p'}$$$, it denotes that $$$p_i = x$$$, where $$$1 \le x \le n$$$.
  • If $$$c = \text{'s'}$$$, it denotes that $$$s_i = x$$$, where $$$0 \le x \le \frac{i(i-1)}{2}$$$.

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

It is guaranteed that a valid permutation always exists.

Output

For each test case, output $$$n$$$ integers representing the permutation $$$p$$$.

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

Example
Input
5
3
p 1
p 2
p 3
3
s 0
s 1
s 2
3
p 1
s 0
p 2
5
p 1
p 4
s 0
p 2
s 4
6
s 0
s 1
s 3
s 6
s 10
s 15
Output
1 2 3
3 1 2
1 3 2
1 4 5 2 3
6 5 4 3 2 1
Note

In the first test case, the values of all elements are explicitly given, so the unique valid permutation is $$$\{1, 2, 3\}$$$.

In the second test case, we need to reconstruct the permutation from the prefix inversion counts:

  • For position $$$2$$$, the number of inversions increases by $$$s_2 - s_1 = 1 - 0 = 1$$$. This means $$$p_2$$$ must be smaller than exactly one element before it, so $$$p_1 \gt p_2$$$.
  • For position $$$3$$$, the number of inversions increases by $$$s_3 - s_2 = 2 - 1 = 1$$$. This means $$$p_3$$$ is smaller than exactly one element before it.
The only permutation satisfying these conditions is $$$\{3, 1, 2\}$$$.

In the third test case, we are given $$$p_1 = 1$$$ and $$$p_3 = 2$$$. The only remaining available value for $$$p_2$$$ is $$$3$$$. We can verify that the prefix $$$p_1, p_2$$$ (which is $$$1, 3$$$) has $$$0$$$ inversions, perfectly satisfying the condition $$$s_2 = 0$$$. Thus, the answer is $$$\{1, 3, 2\}$$$.

In the fifth test case, the given $$$s_i$$$ values perfectly match $$$\frac{i(i-1)}{2}$$$, which is the maximum possible number of inversions for a prefix of length $$$i$$$. This implies that every element must be smaller than all elements before it, meaning the permutation is strictly decreasing. Hence, the answer is $$$\{6, 5, 4, 3, 2, 1\}$$$.

F. Hunting the Beast
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In Zhuji, a city in central Zhejiang Province, local folklore tells of a wild beast known as the Modeiyon. Dwelling deep in the mountains, it is said to sneak into villages at night to devour livestock and prey on lone travelers. Though many elders claim to have seen it, no photograph of the creature has ever been taken.

A brave group of $$$m$$$ people decides to head up the mountain to hunt the beast. The mountain's locations and trails can be modeled as a functional graph $$$G$$$ with $$$n$$$ vertices (numbered $$$1$$$ to $$$n$$$). A functional graph is a directed graph with $$$n$$$ vertices and $$$n$$$ edges, where every vertex has an out-degree of exactly $$$1$$$. Additionally, it is known that the mountain's trails do not ever form self-loops.

The group will choose exactly $$$m$$$ distinct vertices to form their initial starting set $$$S$$$. A starting set $$$S$$$ is defined as successful if every vertex $$$u$$$ in the graph is reachable from at least one vertex $$$v \in S$$$ (a vertex is always reachable from itself).

There are $$$\binom{n}{m}$$$ possible ways to choose the starting set of size $$$m$$$. They define the value of a graph $$$G$$$ as the number of successful starting sets it has.

However, the exact layout of the mountain's trails is unknown. If the destination of the single outgoing edge from each vertex is chosen arbitrarily from the remaining $$$n-1$$$ vertices (excluding the vertex itself), there are exactly $$$(n-1)^n$$$ possible functional graphs. Given $$$n$$$ and $$$m$$$, your task is to calculate the sum of the values of all $$$(n-1)^n$$$ possible functional graphs. Since the answer can be very large, print it modulo $$$998\,244\,353$$$.

Input

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

The only line of each test case contains two integers $$$n,m$$$ ($$$1\le m \le n\le 10^6$$$) — the number of vertices in the graph and the number of starting vertices.

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

Output

For each test case, output a single integer — the sum of the values of all $$$(n-1)^n$$$ possible functional graphs, modulo $$$998\,244\,353$$$.

Example
Input
5
2 1
3 1
3 2
4 2
8 3
Output
2
12
18
216
20415360
Note

In the first test case, there is exactly $$$(2-1)^2=1$$$ valid functional graph: $$$1 \to 2$$$ and $$$2 \to 1$$$. Both starting sets of size $$$1$$$ ($$$\{1\}$$$ and $$$\{2\}$$$) can reach all vertices. Thus, the total value is $$$2$$$.

In the second test case, there are $$$(3-1)^3=8$$$ valid functional graphs. They can be categorized as follows:

  • $$$2$$$ graphs form a single cycle of length $$$3$$$ (e.g., $$$1 \to 2 \to 3 \to 1$$$). Starting at any of the $$$3$$$ vertices can reach all vertices. This contributes $$$2 \cdot 3 = 6$$$ to the total value.
  • $$$6$$$ graphs consist of a $$$2$$$-cycle and a single leaf pointing to it (e.g., $$$1 \leftrightarrow 2$$$ and $$$3 \to 1$$$). To reach all vertices, the starting set must be exactly the leaf vertex. This contributes $$$6 \cdot 1 = 6$$$ to the total value.
The total sum of values is $$$6 + 6 = 12$$$.

In the third test case, the $$$8$$$ possible graphs are the same, but we choose subsets of size $$$m=2$$$:

  • For the $$$2$$$ cycle graphs, any subset of size $$$2$$$ is successful. This contributes $$$2 \cdot \binom{3}{2} = 6$$$.
  • For the $$$6$$$ leaf graphs, the subset must contain the leaf vertex. There are exactly $$$\binom{2}{1}=2$$$ such subsets for each graph. This contributes $$$6 \cdot 2 = 12$$$.
The total sum of values is $$$6 + 12 = 18$$$.