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:
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$$$.
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.
For each test case, output a single integer — the maximum possible value of $$$\sum_{i=1}^{k} \operatorname{popcount}(a_i)$$$.
62 13 16 214142 1372051000000 1001000000 1000000
1241414213221000000
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$$$.
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.
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.
For each test case, output a single integer representing the answer modulo $$$998\,244\,353$$$.
81 1 1 12 3 1 22 5 2 23 5 2 24 6 2 2100 14 52 61000000000 1000000000 1000000000 10000000001000000000 1000000000 1 1
14641285125436614251210998841
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.
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:
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$$$.
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$$$.
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$$$.
51121 251 4 5 2 61107374182331 2 3
01301
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$$$.
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:
You want to maximize the total happiness of all $$$n$$$ people combined. Find this maximum value.
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$$$.
For each test case, output a single integer representing the maximum total happiness.
53 11 2 35 11 4 5 2 66 21 1 4 5 1 410 2230 24 3 42 432 234 934 2389 333 4443 1100000000 100000000 100000000
3152685900
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.
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$$$.
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$$$:
The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
It is guaranteed that a valid permutation always exists.
For each test case, output $$$n$$$ integers representing the permutation $$$p$$$.
If there are multiple valid permutations, you can output any of them.
53p 1p 2p 33s 0s 1s 23p 1s 0p 25p 1p 4s 0p 2s 46s 0s 1s 3s 6s 10s 15
1 2 33 1 21 3 21 4 5 2 36 5 4 3 2 1
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:
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\}$$$.
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$$$.
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$$$.
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$$$.
52 13 13 24 28 3
2121821620415360
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:
In the third test case, the $$$8$$$ possible graphs are the same, but we choose subsets of size $$$m=2$$$: