ReasonFLux-Coder
Collection
Coding LLMs excel at both writing code and generating unit tests.
•
9 items
•
Updated
•
6
dataset
stringclasses 1
value | solutions
null | task_id
int64 0
467
| exe_method
stringclasses 1
value | example_input
sequencelengths 1
5
| example_output
sequencelengths 1
5
| test_input
sequencelengths 1
20
| test_output
sequencelengths 1
20
| question
stringlengths 474
5.27k
| test_time_limit
int64 1
1
|
---|---|---|---|---|---|---|---|---|---|
Codeforces_test | null | 0 | stdin | [
"8\n1\n0\n2\n01\n10\n3\n010\n000\n101\n4\n0110\n1001\n1001\n0110\n11\n11111110111\n10000010010\n10111010011\n10111010011\n10111010001\n10000010000\n11111110101\n00000000111\n11011010011\n10010101100\n11101010100\n11\n11011111110\n10010000010\n00010111010\n10010111010\n01010111010\n11010000010\n01011111110\n11000000000\n01010000010\n01000111100\n00000001010\n11\n11010101001\n11001010100\n00000000110\n11111110010\n10000010010\n10111010110\n10111010111\n10111010010\n10000010110\n11111110100\n00000000000\n3\n111\n100\n111\n"
] | [
"0\n4\n9\n355\n593092633\n438667113\n922743932\n155\n"
] | [
"2\n13\n1011111101110\n1011101111111\n1111011111110\n1111111111111\n1111101001111\n1011111011111\n1111101111110\n1101001111111\n1110111010111\n1101111011111\n0111111011011\n1111111111011\n1110111111000\n13\n1010010110000\n0010010010110\n1010100011010\n1011000110011\n0001110100011\n1100000010000\n1111111000111\n0101110111011\n0101100110000\n0100001011110\n1111001001011\n1111110100010\n1100101101101\n",
"1\n14\n00000000000000\n00000001001000\n00000000000000\n00000000000000\n00000010000100\n00100000000001\n00000000000001\n10000000000010\n00100000000000\n00000000000000\n00001000000000\n00000000000000\n00000000000000\n00000000000000\n",
"1\n14\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n",
"1\n14\n00000000000000\n00000000000000\n00000000000000\n00000100000010\n00000000000000\n10000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000001\n00000000000000\n00000000000000\n00000000000100\n",
"1\n14\n00000000000100\n00000000000000\n00000000000000\n00000000000000\n00000000000000\n00000000000001\n00000000110000\n00000000000000\n00000000000000\n00100010000000\n10000000000000\n00000100000000\n00000100000000\n00000000001000\n",
"1\n14\n00000010000000\n00000000000000\n00001000000000\n01000001000000\n00000100100000\n10000000000000\n00001000000000\n00101000000000\n00000010000001\n00000000000000\n01000000000000\n00000000000100\n00000000000000\n00000001000000\n",
"1\n14\n00000000000000\n00000000000001\n00000000000000\n10100000010010\n00000000000000\n10010100000000\n00001010000010\n10000000000000\n00000000000000\n00011000000000\n00000000000001\n00000000000000\n00000010100000\n10000001001000\n",
"1\n14\n01100010000001\n11000010000000\n00000000000001\n00000010000001\n00010000000100\n00001000000000\n00100000000000\n01000000110000\n01000001000000\n00000000000000\n00000100000001\n00000101000001\n00000000001000\n00000000000000\n",
"1\n14\n00000110000000\n00010000000000\n10000000001001\n00000100000000\n00000100000010\n00000010101000\n00000000010000\n00010010000100\n00000010100000\n00000001000000\n11100001100000\n00000001010001\n01000000000000\n00101000000000\n",
"1\n14\n00001000000000\n00000101010100\n10100010000000\n01000100100000\n10000010000000\n01000001100000\n00000000000000\n11100000100000\n00110100101000\n01000010010100\n00000000000010\n00000101000010\n00000000100000\n10000000000000\n",
"1\n14\n01101010000000\n00010000000001\n00000000110100\n10000010000000\n00000100000100\n00000001100010\n01000000110100\n00000001010001\n00010000000011\n01000000100100\n00000000010000\n00000100000000\n00100100000000\n11011100100010\n",
"1\n14\n01010100000110\n11100110001010\n00001101000000\n00000000010000\n00000000100000\n00100010000010\n10100000000010\n10001000001010\n00001000011000\n00100010000011\n10010000100001\n00000100100000\n01000000000010\n01100000000100\n",
"1\n14\n00000100001000\n00101001011001\n00000000101000\n00000000001001\n01000010000000\n01011100011001\n00110010001000\n00010101100000\n10000011111000\n00010001000000\n10010000010000\n00000101000000\n00010000100000\n01010110101000\n",
"1\n14\n00000010100100\n10000001010100\n00001100010000\n00011100000100\n10001001000000\n00000000111000\n00001010000000\n00001101100011\n01100001100001\n01001100000111\n00010001011000\n10111011010010\n00000000010000\n10000010000001\n",
"1\n14\n00100010100000\n11100000011010\n01110000000001\n01000110001000\n10011000000011\n11101000011111\n00010000001000\n10100110000110\n10011000100001\n00000001001001\n00000001000000\n00110000011001\n00000000100001\n11001110000000\n",
"1\n14\n01100001010010\n00010011100000\n10100011110100\n00100110000001\n00010000011101\n01000011011100\n00101100000000\n00011010111000\n00011000010001\n00001001010011\n01000110000000\n10100001100010\n00000011000001\n01100100001001\n",
"1\n14\n11001110010010\n10101100000000\n01000010000010\n00000011110100\n00000000100000\n01110011100001\n00011001010001\n10100101001010\n11100111100101\n00100010010000\n00100110010100\n00000001101001\n01110010000010\n00011110100100\n",
"1\n14\n10000000010111\n00011101000111\n10000111000100\n11001110000101\n01101101000000\n01010011001010\n10000001100110\n00100000011100\n11010000100110\n00111101011001\n00100110001010\n10010001110000\n00001000010000\n00100010110100\n",
"1\n14\n10100110011000\n11110011000001\n00010110010111\n10010010100010\n10000011000010\n00011001011100\n11100101011001\n01001001110000\n10100011010101\n00100100000100\n01100100000001\n11110010100010\n01000001100101\n00110010010110\n",
"1\n14\n10101110001001\n10001000001100\n10110110001001\n00001000011000\n00001000010000\n01111010011111\n00011100111110\n01000101101100\n00001010110010\n00100111010010\n01000010101100\n01110110101110\n00111000100001\n00010100111111\n"
] | [
"283846468\n932959163\n",
"4876\n",
"0\n",
"64\n",
"4064\n",
"65822\n",
"1565933\n",
"37486795\n",
"220548424\n",
"814456507\n",
"504219283\n",
"563273889\n",
"890361504\n",
"6370940\n",
"500253357\n",
"981554370\n",
"418313361\n",
"420066028\n",
"816404576\n",
"282047794\n"
] | In a two-dimensional universe, a star can be represented by a point $$$(x,y)$$$ on a two-dimensional plane. Two stars are directly connected if and only if their $$$x$$$ or $$$y$$$ coordinates are the same, and there are no other stars on the line segment between them. Define a galaxy as a connected component composed of stars connected directly or indirectly (through other stars).
For a set of stars, its value is defined as the minimum number of galaxies that can be obtained after performing the following operation for any (possibly, zero) times: in each operation, you can select a point $$$(x,y)$$$ without stars. If a star can be directly connected to at least $$$3$$$ stars after creating it here, then you create a star here.
You are given a $$$n\times n$$$ matrix $$$a$$$ consisting of $$$0$$$ and $$$1$$$ describing a set $$$S$$$ of stars. There is a star at $$$(x,y)$$$ if and only if $$$a_{x,y}=1$$$. Calculate the sum, modulo $$$10^9 + 7$$$, of the values of all non-empty subsets of $$$S$$$.
Input format:
The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
For each test case, the first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 14$$$) — the size of matrix $$$a$$$.
Then $$$n$$$ lines follow; the $$$i$$$-th line contains a string $$$a_i$$$ of length $$$n$$$ — the $$$i$$$-th row of matrix $$$a$$$.
It is guaranteed that the sum of $$$2^n$$$ over all test cases does not exceed $$$2^{14}$$$.
Output format:
For each test case, output the sum, modulo $$$10^9 + 7$$$, of the values of all non-empty subsets of $$$S$$$.
Example Input 0:
8
1
0
2
01
10
3
010
000
101
4
0110
1001
1001
0110
11
11111110111
10000010010
10111010011
10111010011
10111010001
10000010000
11111110101
00000000111
11011010011
10010101100
11101010100
11
11011111110
10010000010
00010111010
10010111010
01010111010
11010000010
01011111110
11000000000
01010000010
01000111100
00000001010
11
11010101001
11001010100
00000000110
11111110010
10000010010
10111010110
10111010111
10111010010
10000010110
11111110100
00000000000
3
111
100
111
Example Output 0:
0
4
9
355
593092633
438667113
922743932
155
Notes:
In the first test case, $$$S$$$ is empty. $$$S$$$ has no non-empty subsets. So the answer is $$$0$$$.
In the second test case, $$$S = \{(1,2),(2,1)\}$$$. $$$S$$$ has $$$3$$$ non-empty subsets.
- $$$\{(1,2)\}$$$ and $$$\{(2,1)\}$$$ — there is only one star in the set, forming $$$1$$$ galaxy.
- $$$\{(1,2),(2,1)\}$$$ — two stars in the set are not connected, forming $$$2$$$ galaxies.
So the answer is $$$1+1+2=4$$$.
In the third test case, $$$S = \{(1,2),(3,1),(3,3)\}$$$. $$$S$$$ has $$$7$$$ non-empty subsets.
- $$$\{(1,2)\}$$$, $$$\{(3,1)\}$$$, and $$$\{(3,3)\}$$$ — there is only one star in the set, forming $$$1$$$ galaxy.
- $$$\{(1,2),(3,1)\}$$$ and $$$\{(1,2),(3,3)\}$$$ — two stars in the set are not connected, forming $$$2$$$ galaxies.
- $$$\{(3,1),(3,3)\}$$$ — two stars in the set are connected, forming $$$1$$$ galaxy.
- $$$\{(1,2),(3,1),(3,3)\}$$$ — initially, star $$$(1,2)$$$ is not in the galaxy formed by $$$(3,1)$$$ and $$$(3,3)$$$. You can make an operation creating a star at $$$(3,2)$$$ connecting to these three stars, forming $$$1$$$ galaxy.
So the answer is $$$1+1+1+2+2+1+1=9$$$. | 1 |
Codeforces_test | null | 1 | stdin | [
"5\n1\n0 0\n1\n0 1\n1\n1 1\n3\n0 0 1 0 1 0\n3\n0 1 1 1 0 0\n"
] | [
"0 0\n1 1\n0 0\n0 2\n1 3\n"
] | [
"5\n1\n0 0\n1\n0 1\n1\n1 1\n3\n0 0 1 0 1 0\n3\n0 1 1 1 0 0\n"
] | [
"0 0\n1 1\n0 0\n0 2\n1 3\n"
] | Alice has just crafted a circuit with $$$n$$$ lights and $$$2n$$$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:
- Each light is connected to exactly two switches.
- Each switch is connected to exactly one light. It's unknown which light each switch is connected to.
- When all switches are off, all lights are also off.
- If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.
Alice brings the circuit, which shows only the states of the $$$2n$$$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?
Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?
Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of lights in the circuit.
The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$0 \le a_i \le 1$$$) — the states of the switches in the circuit. $$$a_i = 0$$$ means the $$$i$$$-th switch is off, and $$$a_i = 1$$$ means the $$$i$$$-th switch is on.
Output format:
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.
Example Input 0:
5
1
0 0
1
0 1
1
1 1
3
0 0 1 0 1 0
3
0 1 1 1 0 0
Example Output 0:
0 0
1 1
0 0
0 2
1 3
Notes:
In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off.
In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on.
In the third test case, there is only one light in the circuit, and both switches are on, so the light is off as it was toggled twice.
In the fourth test case, to have no lights on, the switches can be arranged in this way:
- Switch $$$1$$$ and switch $$$4$$$ are connected to light $$$1$$$. Since both switches are off, light $$$1$$$ is also off.
- Switch $$$2$$$ and switch $$$6$$$ are connected to light $$$2$$$. Since both switches are off, light $$$2$$$ is also off.
- Switch $$$3$$$ and switch $$$5$$$ are connected to light $$$3$$$. Both switches are on, so light $$$3$$$ is toggled twice from its initial off state, and thus also stays off.
And to have $$$2$$$ lights on, the switches can be arranged in this way:
- Switch $$$1$$$ and switch $$$2$$$ are connected to light $$$1$$$. Since both switches are off, light $$$1$$$ is also off.
- Switch $$$3$$$ and switch $$$4$$$ are connected to light $$$2$$$. Since switch $$$3$$$ is on and switch $$$4$$$ is off, light $$$2$$$ is toggled once from its initial off state, so it is on.
- Switch $$$5$$$ and switch $$$6$$$ are connected to light $$$3$$$. Since switch $$$5$$$ is on and switch $$$6$$$ is off, light $$$3$$$ is toggled once from its initial off state, so it is on. | 1 |
Codeforces_test | null | 2 | stdin | [
"2\n6\n\n3 4\n\n3 4\n\n2 3\n\n10\n\n3 4\n\n6 7\n"
] | [
"? 6 1 2 3 4 5 6\n\n? 4 3 6 1 5\n\n? 4 3 6 2 5\n\n! 3 6\n\n? 6 1 3 7 8 9 10\n\n? 8 1 2 3 4 5 6 7 8\n\n! 6 5\n"
] | [
"2\n6\n6 2 3 5 1 4\n10\n10 9 8 7 6 5 4 3 2 1\n"
] | [
"5\n3\n"
] | This is an interactive problem.
Returning from a restful vacation on Australia's Gold Coast, Penchick forgot to bring home gifts for his pet duck Duong Canh! But perhaps a beautiful problem crafted through deep thought on the scenic beaches could be the perfect souvenir.
There is a hidden permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$, where $$$n$$$ is even. You are allowed to make the following query:
- Choose a subsequence$$$^{\text{†}}$$$ of the permutation $$$p$$$ with even length $$$4\le k\le n$$$. The interactor will return the value of the two medians$$$^{\text{‡}}$$$ in the chosen subsequence.
Find the index of the two medians in permutation $$$p$$$ using at most $$$80$$$ queries.
Note that the interactor is non-adaptive. This means that the permutation $$$p$$$ is fixed at the beginning and will not change based on your queries.
Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The only line of each test case contains a single integer $$$n$$$ ($$$6 \le n \le 100$$$, $$$n$$$ is even) — the length of the hidden permutation $$$p$$$.
For each test case, after reading the integer $$$n$$$, you should begin the interaction and find the answer before reading $$$n$$$ for the next test case.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
Example Input 0:
2
6
3 4
3 4
2 3
10
3 4
6 7
Example Output 0:
? 6 1 2 3 4 5 6
? 4 3 6 1 5
? 4 3 6 2 5
! 3 6
? 6 1 3 7 8 9 10
? 8 1 2 3 4 5 6 7 8
! 6 5
Notes:
In the first test case, the hidden permutation is $$$p = [6, 2, 3, 5, 1, 4]$$$.
1. The entire permutation was chosen for the first query. The two medians of the entire permutation $$$p$$$ are $$$3$$$ and $$$4$$$.
2. The indices of the chosen subsequence in the second query are $$$3$$$, $$$6$$$, $$$1$$$, and $$$5$$$. The interactor returns the two medians of the subsequence $$$[p_3, p_6, p_1, p_5] = [3, 4, 6, 1]$$$, which are $$$3$$$ and $$$4$$$.
3. The indices of the chosen subsequence in the second query are $$$3$$$, $$$6$$$, $$$2$$$, and $$$5$$$. The interactor returns the two medians of the subsequence $$$[p_3, p_6, p_2, p_5] = [3, 4, 2, 1]$$$, which are $$$2$$$ and $$$3$$$.
The answer "! 3 6" is valid as $$$p_3 = 3$$$ and $$$p_6 = 4$$$.
In the second test case, the hidden permutation is $$$p = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]$$$.
1. The indices of the chosen subsequence in the second query are $$$1$$$, $$$3$$$, $$$7$$$, $$$8$$$, $$$9$$$, and $$$10$$$. The interactor returns the two medians of the subsequence $$$[p_1, p_3, p_7, p_8, p_9, p_{10}] = [10, 8, 4, 3, 2, 1]$$$, which are $$$3$$$ and $$$4$$$.
2. The indices of the chosen subsequence in the second query are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, and $$$8$$$. The interactor returns the two medians of the subsequence $$$[p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8] = [10, 9, 8, 7, 6, 5, 4, 3]$$$, which are $$$6$$$ and $$$7$$$.
The answer "! 5 6" is valid as $$$p_5 = 6$$$ and $$$p_6 = 5$$$. | 1 |
Codeforces_test | null | 3 | stdin | [
"5\n4\n1 2\n2 3\n3 4\n2\n1 2\n4\n1 2\n1 3\n1 4\n8\n3 7\n2 4\n1 2\n2 5\n3 6\n1 3\n3 8\n6\n2 3\n1 2\n3 6\n1 5\n1 4\n"
] | [
"1\n0\n3\n7\n3\n"
] | [
"5\n4\n1 2\n2 3\n3 4\n2\n1 2\n4\n1 2\n1 3\n1 4\n8\n3 7\n2 4\n1 2\n2 5\n3 6\n1 3\n3 8\n6\n2 3\n1 2\n3 6\n1 5\n1 4\n"
] | [
"1\n0\n3\n7\n3\n"
] | In the heart of an ancient kingdom grows the legendary Tree of Life — the only one of its kind and the source of magical power for the entire world. The tree consists of $$$n$$$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $$$n-1$$$ channels in the tree, with the $$$i$$$-th channel connecting nodes $$$v_i$$$ and $$$u_i$$$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree.
However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $$$v_1, v_2, \ldots, v_k$$$, where each pair of adjacent nodes $$$v_i$$$ and $$$v_{i+1}$$$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $$$(v_i, v_{i+1})$$$ and $$$(v_{i+1}, v_{i+2})$$$ is blocked for each $$$1 \leq i \leq k - 2$$$.
The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.
Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!
Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 4 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 5 \cdot 10^5$$$) — the number of nodes in the Tree of Life.
The $$$i$$$-th of the following $$$n - 1$$$ lines of each test case contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 \leq v_i < u_i \leq n$$$) — the channel connecting nodes $$$v_i$$$ and $$$u_i$$$.
It is guaranteed that there exists a unique simple path through the channels between any two nodes.
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, output a single integer — the minimum number of paths that the sages need to select to prevent a catastrophe.
Example Input 0:
5
4
1 2
2 3
3 4
2
1 2
4
1 2
1 3
1 4
8
3 7
2 4
1 2
2 5
3 6
1 3
3 8
6
2 3
1 2
3 6
1 5
1 4
Example Output 0:
1
0
3
7
3
Notes:
In the first test case, there are two pairs of channels emanating from a single node: $$$(1, 2)$$$ and $$$(2, 3)$$$, $$$(2, 3)$$$ and $$$(3, 4)$$$. It is sufficient to perform the ritual along the path $$$1-2-3-4$$$. Thus, the answer is $$$1$$$.
In the second test case, there are no pairs of channels emanating from a single node, so the answer is $$$0$$$.
In the third test case, rituals can be performed along the paths $$$2-1-3$$$, $$$2-1-4$$$, and $$$3-1-4$$$. | 1 |
Codeforces_test | null | 4 | stdin | [
"4\n4 4\n1 2 3 2\n8 15\n1 2 3 4 5 6 7 8\n6 1\n1 1 1 1 1 1\n16 9\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3\n"
] | [
"2\n1\n0\n4\n"
] | [
"4\n4 4\n1 2 3 2\n8 15\n1 2 3 4 5 6 7 8\n6 1\n1 1 1 1 1 1\n16 9\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3\n"
] | [
"2\n1\n0\n4\n"
] | Alice and Bob are playing a game. There are $$$n$$$ ($$$n$$$ is even) integers written on a blackboard, represented by $$$x_1, x_2, \ldots, x_n$$$. There is also a given integer $$$k$$$ and an integer score that is initially $$$0$$$. The game lasts for $$$\frac{n}{2}$$$ turns, in which the following events happen sequentially:
- Alice selects an integer from the blackboard and erases it. Let's call Alice's chosen integer $$$a$$$.
- Bob selects an integer from the blackboard and erases it. Let's call Bob's chosen integer $$$b$$$.
- If $$$a+b=k$$$, add $$$1$$$ to score.
Alice is playing to minimize the score while Bob is playing to maximize the score. Assuming both players use optimal strategies, what is the score after the game ends?
Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2\cdot 10^5, 1 \leq k \leq 2\cdot n$$$, $$$n$$$ is even).
The second line of each test case contains $$$n$$$ integers $$$x_1,x_2,\ldots,x_n$$$ ($$$1 \leq x_i \leq n$$$) — the integers on the blackboard.
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 score if both players play optimally.
Example Input 0:
4
4 4
1 2 3 2
8 15
1 2 3 4 5 6 7 8
6 1
1 1 1 1 1 1
16 9
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
Example Output 0:
2
1
0
4
Notes:
In the first test case, one way the game may go is as follows:
- Alice selects $$$1$$$ and Bob selects $$$3$$$. The score increases as $$$1+3=4$$$. Now the two integers remaining on the blackboard are $$$2$$$ and $$$2$$$.
- Alice and Bob both select $$$2$$$. The score increases as $$$2+2=4$$$.
- The game ends as the blackboard now has no integers.
In the third test case, it is impossible for the sum of Alice and Bob's selected integers to be $$$1$$$, so we answer $$$0$$$.
Note that this is just an example of how the game may proceed for demonstration purposes. This may not be Alice or Bob's most optimal strategies. | 1 |
Codeforces_test | null | 5 | stdin | [
"4 4\n.//.\n.\\\\.\n.\\/.\n....\n",
"4 6\n./..\\.\n.\\...\\\n./../\\\n......\n",
"4 4\n....\n./\\.\n.\\/.\n....\n"
] | [
"2\nN3 W2\n",
"2\nE3 S2\n",
"0\n"
] | [
"4 4\n.//.\n.\\\\.\n.\\/.\n....\n",
"4 6\n./..\\.\n.\\...\\\n./../\\\n......\n",
"4 4\n....\n./\\.\n.\\/.\n....\n",
"1 1\n/\n",
"1 1\n\\\n",
"2 2\n\\/\n..\n",
"2 2\n./\n.\\\n",
"3 6\n......\n./....\n......\n",
"3 3\n.//\n//.\n/..\n",
"3 3\n///\n///\n///\n",
"3 3\n/\\.\n\\\\\\\n\\/\\\n"
] | [
"2\nN3 W2\n",
"2\nE3 S2\n",
"0\n",
"4\nE1 N1 S1 W1\n",
"4\nE1 N1 S1 W1\n",
"2\nN1 N2\n",
"2\nE1 E2\n",
"4\nE2 N2 S2 W2\n",
"2\nN3 W3\n",
"0\n",
"2\nE3 W2\n"
] | You are given a grid of $$$R$$$ rows (numbered from $$$1$$$ to $$$R$$$ from north to south) and $$$C$$$ columns (numbered from $$$1$$$ to $$$C$$$ from west to east). Every cell in this grid is a square of the same size. The cell located at row $$$r$$$ and column $$$c$$$ is denoted as $$$(r, c)$$$. Each cell can either be empty or have a mirror in one of the cell's diagonals. Each mirror is represented by a line segment. The mirror is type $$$1$$$ if it is positioned diagonally from the southwest corner to the northeast corner of the cell, or type $$$2$$$ for the other diagonal.
These mirrors follow the law of reflection, that is, the angle of reflection equals the angle of incidence. Formally, for type $$$1$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the west, east, north, and south of the cell, respectively. Similarly, for type $$$2$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the east, west, south, and north of the cell, respectively.
You want to put a laser from outside the grid such that all mirrors are hit by the laser beam. There are $$$2 \cdot (R+C)$$$ possible locations to put the laser:
- from the north side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the south;
- from the south side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the north;
- from the east side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the west; and
- from the west side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the east.
Determine all possible locations for the laser such that all mirrors are hit by the laser beam.
Input format:
The first line consists of two integers $$$R$$$ $$$C$$$ ($$$1 \leq R, C \leq 200$$$).
Each of the next $$$R$$$ lines consists of a string $$$S_r$$$ of length $$$C$$$. The $$$c$$$-th character of string $$$S_r$$$ represents cell $$$(r, c)$$$. Each character can either be . if the cell is empty, / if the cell has type $$$1$$$ mirror, or \ if the cell has type $$$2$$$ mirror. There is at least one mirror in the grid.
Output format:
Output a single integer representing the number of possible locations for the laser such that all mirrors are hit by the laser beam. Denote this number as $$$k$$$.
If $$$k > 0$$$, then output $$$k$$$ space-separated strings representing the location of the laser. Each string consists of a character followed without any space by an integer. The character represents the side of the grid, which could be N, S, E, or W if you put the laser on the north, south, east, or west side of the grid, respectively. The integer represents the row/column number. You can output the strings in any order.
Example Input 0:
4 4
.//.
.\\.
.\/.
....
Example Output 0:
2
N3 W2
Example Input 1:
4 6
./..\.
.\...\
./../\
......
Example Output 1:
2
E3 S2
Example Input 2:
4 4
....
./\.
.\/.
....
Example Output 2:
0
Notes:
Explanation for the sample input/output #1
The following illustration shows one of the solutions of this sample.
Explanation for the sample input/output #2
The following illustration shows one of the solutions of this sample. | 1 |
Codeforces_test | null | 6 | stdin | [
"7\nSS\nR\nRPS\nRPPP\nSPPRSP\nPPP\nPR\n"
] | [
"3\n1\n3\n6\n7\n5\n3\n"
] | [
"7\nSS\nR\nRPS\nRPPP\nSPPRSP\nPPP\nPR\n"
] | [
"3\n1\n3\n6\n7\n5\n3\n"
] | Rock-Paper-Scissors is a game for two players. It is played in rounds. During each round, every player chooses one of three moves: Rock, Paper, or Scissors. Depending on the chosen moves, the following happens:
- if one player chooses Rock and the other player chooses Paper, the player who chooses Paper wins and gets a point;
- if one player chooses Scissors and the other player chooses Paper, the player who chooses Scissors wins and gets a point;
- if one player chooses Scissors and the other player chooses Rock, the player who chooses Rock wins and gets a point;
- and if both players choose the same move, nobody wins and nobody gets a point.
Monocarp decided to play against a bot. During the game, Monocarp noticed that the bot's behavior is very predictable:
- in the first round, it chooses Rock;
- in every round except the first, it chooses the move that beats the opponent's move in the previous round (for example, if in the previous round its opponent played Scissors, then the bot chooses Rock now).
Monocarp has a favorite string $$$s$$$, consisting of the characters R, P, and/or S. Monocarp decided to play a series of rounds against the bot. However, he wants both of the following conditions to be met:
- the final score is in favor of Monocarp (i. e., the number of rounds he won is strictly greater than the number of rounds the bot won);
- the string $$$s$$$ appears as a contiguous substring in the sequence of the bot's moves (where R denotes Rock, P denotes Paper, and S denotes Scissors).
Help Monocarp and calculate the minimum number of rounds he needs to play against the bot to satisfy both of the aforementioned conditions.
Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The only line of each test case contains a string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$), consisting of the characters R, P, and/or S.
Additional constraint on the input: the sum of the lengths of the strings $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output format:
For each test case, print a single integer — the minimum number of rounds Monocarp needs to play against the bot to satisfy both of the aforementioned conditions.
Example Input 0:
7
SS
R
RPS
RPPP
SPPRSP
PPP
PR
Example Output 0:
3
1
3
6
7
5
3
Notes:
In the first example, Monocarp can play PPR, then the bot's moves are RSS, and the score is $$$2:1$$$ in favor of Monocarp.
In the second example, Monocarp can play P, then the bot's moves are R, and the score is $$$1:0$$$ in favor of Monocarp.
In the third example, Monocarp can play RPR, then the bot's moves are RPS, and the score is $$$1:0$$$ in favor of Monocarp.
In the fourth example, Monocarp can play RRRSPR, then the bot's moves are RPPPRS, and the score is $$$3:2$$$ in favor of Monocarp.
In the fifth example, Monocarp can play PRRSPRS, then the bot's moves are RSPPRSP, and the score is $$$6:1$$$ in favor of Monocarp.
In the sixth example, Monocarp can play PRRRS, then the bot's moves are RSPPP, and the score is $$$3:2$$$ in favor of Monocarp.
In the seventh example, Monocarp can play RSR, then the bot's moves are RPR, and the score is $$$1:0$$$ in favor of Monocarp. | 1 |
Codeforces_test | null | 7 | stdin | [
"7\n3\n1 2 3\n3 2 2\n1\n13\n45\n5\n5 4 7 8 6\n4 4 5 5 5\n4\n6 4 8 8\n12 13 14 12\n3\n92856133 46637598 12345678\n29384774 73775896 87654321\n2\n65 12\n110 31\n4\n677810235 275091182 428565855 720629731\n74522416 889934149 3394714 230851724\n"
] | [
"4\n4\n0\n6552\n722019507\n541\n665443265\n"
] | [
"7\n3\n1 2 3\n3 2 2\n1\n13\n45\n5\n5 4 7 8 6\n4 4 5 5 5\n4\n6 4 8 8\n12 13 14 12\n3\n92856133 46637598 12345678\n29384774 73775896 87654321\n2\n65 12\n110 31\n4\n677810235 275091182 428565855 720629731\n74522416 889934149 3394714 230851724\n"
] | [
"4\n4\n0\n6552\n722019507\n541\n665443265\n"
] | This is the hard version of this problem. The only difference is that you need to output the number of choices of games where Bob wins in this version, where the number of stones in each pile are not fixed. You must solve both versions to be able to hack.
Alice and Bob are playing a familiar game where they take turns removing stones from $$$n$$$ piles. Initially, there are $$$x_i$$$ stones in the $$$i$$$-th pile, and it has an associated value $$$a_i$$$. A player can take $$$d$$$ stones away from the $$$i$$$-th pile if and only if both of the following conditions are met:
- $$$1 \le d \le a_i$$$, and
- $$$x \, \& \, d = d$$$, where $$$x$$$ is the current number of stones in the $$$i$$$-th pile and $$$\&$$$ denotes the bitwise AND operation.
The player who cannot make a move loses, and Alice goes first.
You're given the $$$a_i$$$ values of each pile, but the number of stones in the $$$i$$$-th pile has not been determined yet. For the $$$i$$$-th pile, $$$x_i$$$ can be any integer between $$$1$$$ and $$$b_i$$$, inclusive. That is, you can choose an array $$$x_1, x_2, \ldots, x_n$$$ such that the condition $$$1 \le x_i \le b_i$$$ is satisfied for all piles.
Your task is to count the number of games where Bob wins if both players play optimally. Two games are considered different if the number of stones in any pile is different, i.e., the arrays of $$$x$$$ differ in at least one position.
Since the answer can be very large, please output the result modulo $$$10^9 + 7$$$.
Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains $$$n$$$ ($$$1 \le n \le 10^4$$$) — the number of piles.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i < 2^{30}$$$).
The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i < 2^{30}$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
Output format:
Output a single integer, the number of games where Bob wins, modulo $$$10^9 + 7$$$.
Example Input 0:
7
3
1 2 3
3 2 2
1
13
45
5
5 4 7 8 6
4 4 5 5 5
4
6 4 8 8
12 13 14 12
3
92856133 46637598 12345678
29384774 73775896 87654321
2
65 12
110 31
4
677810235 275091182 428565855 720629731
74522416 889934149 3394714 230851724
Example Output 0:
4
4
0
6552
722019507
541
665443265
Notes:
In the first test case, no matter which values of $$$x_2$$$ and $$$x_3$$$ we choose, the second and third piles will always be chosen exactly once before no more stones can be taken from them. If $$$x_1 = 2$$$, then no stones can be taken from it, so Bob will make the last move. If $$$x_1 = 1$$$ or $$$x_1 = 3$$$, then exactly one move can be made on that pile, so Alice will make the last move. So Bob wins when $$$x = [2, 1, 1]$$$ or $$$x = [2, 1, 2]$$$ or $$$x = [2, 2, 1]$$$ or $$$x = [2, 2, 2]$$$.
In the second test case, Bob wins when $$$x_1 = 14$$$ or $$$x_1 = 30$$$ by removing $$$14 - k$$$ stones, where $$$k$$$ is the number of stones Alice removes on her turn. Bob also wins when $$$x_1 = 16$$$ or $$$x_1 = 32$$$ since Alice does not have any moves to begin with. | 1 |
Codeforces_test | null | 8 | stdin | [
"3\n2\n5\n9\n"
] | [
"4\n29\n165\n"
] | [
"3\n2\n5\n9\n",
"9\n10000\n10001\n10002\n10003\n10004\n10005\n10006\n10007\n10008\n",
"1\n99999\n",
"1\n100000\n",
"1\n66320\n",
"1\n83160\n",
"1\n98280\n",
"20\n4810\n4811\n4812\n4813\n4814\n4815\n4816\n4817\n4818\n4819\n4820\n4821\n4822\n4823\n4824\n4825\n4826\n4827\n4828\n4829\n",
"19\n5092\n5093\n5094\n5095\n5096\n5097\n5098\n5099\n5100\n5101\n5102\n5103\n5104\n5105\n5106\n5107\n5108\n5109\n5110\n",
"15\n6561\n6562\n6563\n6564\n6565\n6566\n6567\n6568\n6569\n6570\n6571\n6572\n6573\n6574\n6575\n",
"10\n9955\n9956\n9957\n9958\n9959\n9960\n9961\n9962\n9963\n9964\n",
"9\n11075\n11076\n11077\n11078\n11079\n11080\n11081\n11082\n11083\n",
"8\n12472\n12473\n12474\n12475\n12476\n12477\n12478\n12479\n",
"7\n14264\n14265\n14266\n14267\n14268\n14269\n14270\n",
"5\n19990\n19991\n19992\n19993\n19994\n",
"4\n24994\n24995\n24996\n24997\n",
"3\n33330\n33331\n33332\n",
"2\n49999\n50000\n",
"1\n50000\n",
"1\n88496\n"
] | [
"4\n29\n165\n",
"926353610\n930486251\n910514688\n935141989\n320198968\n685185061\n735275092\n735295105\n425719120\n",
"527066026\n",
"668533589\n",
"823050396\n",
"601463362\n",
"565515786\n",
"939583113\n942299934\n39026730\n39036355\n918112810\n14110953\n98095962\n98105595\n365228036\n366548073\n711397042\n721749323\n733389626\n85910694\n86215253\n873293990\n49118431\n59496468\n866726333\n870677990\n",
"884478784\n888868729\n615946605\n624296250\n40494035\n52064212\n65074303\n65084500\n658963389\n658973590\n672004093\n687989418\n55515599\n63897968\n49384706\n49394919\n627691141\n877169693\n405739934\n",
"435369340\n584034204\n584047329\n141055619\n458397323\n463887552\n545627770\n258997708\n259010845\n271382433\n271395574\n815196103\n818149259\n691899215\n673142346\n",
"41325096\n919072819\n963182316\n868797565\n877485130\n762130981\n786552422\n528979462\n121075524\n235120402\n",
"114760090\n412127977\n875650721\n98723862\n83062013\n621563247\n651769968\n940666439\n940688604\n",
"813889691\n813914636\n508185761\n917476137\n71163782\n140411119\n39977489\n40002446\n",
"760574889\n337588549\n85940755\n119872648\n303284133\n324102670\n314280120\n",
"422869618\n422909599\n146309872\n146349857\n81557069\n",
"935140087\n137025659\n843792423\n997123936\n",
"531400558\n531467219\n667814106\n",
"201856150\n111623803\n",
"111623803\n",
"597431212\n"
] | This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on $$$t$$$, $$$m$$$, and the sum of $$$m$$$. You can only make hacks if both versions of the problem are solved.
For an integer array $$$a$$$ of length $$$n$$$, define $$$f(k)$$$ as the greatest common divisor (GCD) of the maximum values of all subarrays$$$^{\text{∗}}$$$ of length $$$k$$$. For example, if the array is $$$[2, 1, 4, 6, 2]$$$, then $$$f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4]), \operatorname{max}([1, 4, 6]), \operatorname{max}([4, 6, 2])) = \operatorname{gcd}(4, 6, 6) = 2$$$.
An array is good if $$$f(i) \neq f(j)$$$ is satisfied over all pairs $$$1 \le i \lt j \le n$$$.
Shohag has an integer $$$m$$$. Help him count the number, modulo $$$998\,244\,353$$$, of non-empty good arrays of arbitrary length such that each element of the array is an integer from $$$1$$$ to $$$m$$$.
Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first and only line of each test case contains an integer $$$m$$$ ($$$1 \le m \le 10^5$$$).
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.
Output format:
For each test case, output an integer — the number of valid arrays modulo $$$998\,244\,353$$$.
Example Input 0:
3
2
5
9
Example Output 0:
4
29
165
Notes:
In the first test case, the valid arrays are $$$[1]$$$, $$$[1, 2]$$$, $$$[2]$$$, and $$$[2, 1]$$$.
In the second test case, there are a total of $$$29$$$ valid arrays. In particular, the array $$$[2, 1, 4]$$$ with length $$$n = 3$$$ is valid because all elements are from $$$1$$$ to $$$m = 5$$$ and $$$f(1)$$$, $$$f(2)$$$ and $$$f(n = 3)$$$ all are distinct:
- $$$f(1) = \operatorname{gcd}(\operatorname{max}([2]), \operatorname{max}([1]), \operatorname{max}([4])) = \operatorname{gcd}(2, 1, 4) = 1.$$$
- $$$f(2) = \operatorname{gcd}(\operatorname{max}([2, 1]), \operatorname{max}([1, 4])) = \operatorname{gcd}(2, 4) = 2.$$$
- $$$f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4])) = \operatorname{gcd}(4) = 4.$$$ | 1 |
Codeforces_test | null | 9 | stdin | [
"3\n5\n1 3 1 3 4\n3\n1 2 3\n4\n4 4 4 4\n"
] | [
"4\n4\n1\n"
] | [
"3\n5\n1 3 1 3 4\n3\n1 2 3\n4\n4 4 4 4\n"
] | [
"4\n4\n1\n"
] | Alice and Bob play a game. There are $$$n$$$ marbles, the $$$i$$$-th of them has color $$$c_i$$$. The players take turns; Alice goes first, then Bob, then Alice again, then Bob again, and so on.
During their turn, a player must take one of the remaining marbles and remove it from the game. If there are no marbles left (all $$$n$$$ marbles have been taken), the game ends.
Alice's score at the end of the game is calculated as follows:
- she receives $$$1$$$ point for every color $$$x$$$ such that she has taken at least one marble of that color;
- additionally, she receives $$$1$$$ point for every color $$$x$$$ such that she has taken all marbles of that color (of course, only colors present in the game are considered).
For example, suppose there are $$$5$$$ marbles, their colors are $$$[1, 3, 1, 3, 4]$$$, and the game goes as follows: Alice takes the $$$1$$$-st marble, then Bob takes the $$$3$$$-rd marble, then Alice takes the $$$5$$$-th marble, then Bob takes the $$$2$$$-nd marble, and finally, Alice takes the $$$4$$$-th marble. Then, Alice receives $$$4$$$ points: $$$3$$$ points for having at least one marble for colors $$$1$$$, $$$3$$$ and $$$4$$$, and $$$1$$$ point for having all marbles of color $$$4$$$. Note that this strategy is not necessarily optimal for both players.
Alice wants to maximize her score at the end of the game. Bob wants to minimize it. Both players play optimally (i. e. Alice chooses a strategy which allows her to get as many points as possible, and Bob chooses a strategy which minimizes the amount of points Alice can get).
Calculate Alice's score at the end of the game.
Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.
Each test case consists of two lines:
- the first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of marbles;
- the second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le n$$$) — the colors of the marbles.
Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.
Output format:
For each test case, print one integer — Alice's score at the end of the game, assuming that both players play optimally.
Example Input 0:
3
5
1 3 1 3 4
3
1 2 3
4
4 4 4 4
Example Output 0:
4
4
1
Notes:
In the second test case of the example, the colors of all marbles are distinct, so, no matter how the players act, Alice receives $$$4$$$ points for having all marbles of two colors, and no marbles of the third color.
In the third test case of the example, the colors of all marbles are the same, so, no matter how the players act, Alice receives $$$1$$$ point for having at least one (but not all) marble of color $$$4$$$. | 1 |
Codeforces_test | null | 10 | stdin | [
"3\n3 3 3\n",
"4\n2 2 4 4\n",
"9\n4 2 6 9 7 7 7 3 3\n"
] | [
"36\n",
"44\n",
"10858\n"
] | [
"3\n3 3 3\n",
"4\n2 2 4 4\n",
"9\n4 2 6 9 7 7 7 3 3\n",
"2\n1 1\n",
"2\n1 2\n",
"2\n2 2\n",
"20\n6 11 8 18 18 9 9 17 13 11 2 6 8 20 10 16 17 13 17 13\n",
"20\n20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20\n",
"20\n20 11 17 18 13 17 13 16 14 14 17 12 16 13 16 17 10 11 11 19\n",
"20\n2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 2 1 2 1 2\n",
"20\n20 20 19 18 20 19 19 20 18 19 20 20 18 20 19 18 18 19 20 18\n",
"20\n3 6 12 2 20 15 4 1 14 16 18 9 19 5 8 17 13 7 10 11\n",
"20\n6 20 6 20 18 8 18 18 12 14 14 14 10 4 18 16 18 8 10 10\n",
"20\n3 12 18 12 3 15 6 12 12 9 3 12 6 6 9 9 12 12 18 3\n",
"20\n12 12 20 16 20 4 20 12 20 4 8 4 20 4 20 12 16 4 20 16\n",
"20\n5 10 5 5 5 10 5 15 5 10 20 20 10 20 10 5 15 20 20 15\n",
"20\n19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19\n",
"20\n19 11 13 19 11 17 13 13 13 17 17 19 11 17 19 17 11 13 11 17\n",
"20\n11 2 17 17 19 2 17 5 7 3 13 5 17 11 2 5 5 17 13 11\n",
"100\n64 5 56 22 41 91 50 59 13 93 99 21 14 88 48 88 35 46 43 21 56 99 15 68 27 19 50 50 80 88 2 76 95 42 49 59 85 10 7 75 52 36 29 30 67 49 4 71 11 23 2 1 75 98 1 99 64 20 9 85 95 87 79 24 17 77 49 15 97 30 52 74 30 83 98 57 73 25 79 13 37 84 75 95 34 62 79 29 19 22 59 85 71 61 72 97 78 47 19 7\n"
] | [
"36\n",
"44\n",
"10858\n",
"2\n",
"0\n",
"4\n",
"345471941\n",
"814884643\n",
"391972057\n",
"371292043\n",
"870560372\n",
"343901199\n",
"779046866\n",
"627430749\n",
"758557443\n",
"833255082\n",
"324930452\n",
"432017879\n",
"451884715\n",
"501309126\n"
] | You are playing the Greatest Common Divisor Deck-Building Card Game (GCDDCG). There are $$$N$$$ cards (numbered from $$$1$$$ to $$$N$$$). Card $$$i$$$ has the value of $$$A_i$$$, which is an integer between $$$1$$$ and $$$N$$$ (inclusive).
The game consists of $$$N$$$ rounds (numbered from $$$1$$$ to $$$N$$$). Within each round, you need to build two non-empty decks, deck $$$1$$$ and deck $$$2$$$. A card cannot be inside both decks, and it is allowed to not use all $$$N$$$ cards. In round $$$i$$$, the greatest common divisor (GCD) of the card values in each deck must equal $$$i$$$.
Your creativity point during round $$$i$$$ is the product of $$$i$$$ and the number of ways to build two valid decks. Two ways are considered different if one of the decks contains different cards.
Find the sum of creativity points across all $$$N$$$ rounds. Since the sum can be very large, calculate the sum modulo $$$998\,244\,353$$$.
Input format:
The first line consists of an integer $$$N$$$ ($$$2 \leq N \leq 200\,000)$$$.
The second line consists of $$$N$$$ integers $$$A_i$$$ ($$$1 \leq A_i \leq N$$$).
Output format:
Output a single integer representing the sum of creativity points across all $$$N$$$ rounds modulo $$$998\,244\,353$$$.
Example Input 0:
3
3 3 3
Example Output 0:
36
Example Input 1:
4
2 2 4 4
Example Output 1:
44
Example Input 2:
9
4 2 6 9 7 7 7 3 3
Example Output 2:
10858
Notes:
Explanation for the sample input/output #1
The creativity point during each of rounds $$$1$$$ and $$$2$$$ is $$$0$$$.
During round $$$3$$$, there are $$$12$$$ ways to build both decks. Denote $$$B$$$ and $$$C$$$ as the set of card numbers within deck $$$1$$$ and deck $$$2$$$, respectively. The $$$12$$$ ways to build both decks are:
- $$$B = \{ 1 \}, C = \{ 2 \}$$$;
- $$$B = \{ 1 \}, C = \{ 3 \}$$$;
- $$$B = \{ 1 \}, C = \{ 2, 3 \}$$$;
- $$$B = \{ 2 \}, C = \{ 1 \}$$$;
- $$$B = \{ 2 \}, C = \{ 3 \}$$$;
- $$$B = \{ 2 \}, C = \{ 1, 3 \}$$$;
- $$$B = \{ 3 \}, C = \{ 1 \}$$$;
- $$$B = \{ 3 \}, C = \{ 2 \}$$$;
- $$$B = \{ 3 \}, C = \{ 1, 2 \}$$$;
- $$$B = \{ 1, 2 \}, C = \{ 3 \}$$$;
- $$$B = \{ 2, 3 \}, C = \{ 1 \}$$$; and
- $$$B = \{ 1, 3 \}, C = \{ 2 \}$$$.
Explanation for the sample input/output #2
For rounds $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$, there are $$$0$$$, $$$18$$$, $$$0$$$, and $$$2$$$ ways to build both decks, respectively. | 1 |
Codeforces_test | null | 11 | stdin | [
"4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc\n"
] | [
"No\nnO\nYes\nYES\n"
] | [
"4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc\n",
"1\n7\nhonglan\n"
] | [
"No\nNo\nYes\nYes\n",
"Yes\n"
] | Turtle thinks a string $$$s$$$ is a good string if there exists a sequence of strings $$$t_1, t_2, \ldots, t_k$$$ ($$$k$$$ is an arbitrary integer) such that:
- $$$k \ge 2$$$.
- $$$s = t_1 + t_2 + \ldots + t_k$$$, where $$$+$$$ represents the concatenation operation. For example, $$$\texttt{abc} = \texttt{a} + \texttt{bc}$$$.
- For all $$$1 \le i < j \le k$$$, the first character of $$$t_i$$$ isn't equal to the last character of $$$t_j$$$.
Turtle is given a string $$$s$$$ consisting of lowercase Latin letters. Please tell him whether the string $$$s$$$ is a good string!
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 the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the string.
The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.
Output format:
For each test case, output "YES" if the string $$$s$$$ is a good string, and "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.
Example Input 0:
4
2
aa
3
aba
4
abcb
12
abcabcabcabc
Example Output 0:
No
nO
Yes
YES
Notes:
In the first test case, the sequence of strings $$$\texttt{a}, \texttt{a}$$$ satisfies the condition $$$s = t_1 + t_2 + \ldots + t_k$$$, but the first character of $$$t_1$$$ is equal to the last character of $$$t_2$$$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer is "NO".
In the third test case, the sequence of strings $$$\texttt{ab}, \texttt{cb}$$$ satisfies all of the conditions.
In the fourth test case, the sequence of strings $$$\texttt{abca}, \texttt{bcab}, \texttt{cabc}$$$ satisfies all of the conditions. | 1 |
Codeforces_test | null | 12 | stdin | [
"9\n4 4\n2 3 4 5\n4 3\n2 3 4 5\n4 3\n3 4 8 9\n3 3\n6 2 1\n1 1\n1\n7 5\n14 34 6 25 46 7 17\n6 5\n40 80 99 60 90 50\n6 5\n64 40 50 68 70 10\n2 1\n1 1000000000\n"
] | [
"5\n-1\n10\n8\n1\n47\n100\n-1\n-1\n"
] | [
"9\n4 4\n2 3 4 5\n4 3\n2 3 4 5\n4 3\n3 4 8 9\n3 3\n6 2 1\n1 1\n1\n7 5\n14 34 6 25 46 7 17\n6 5\n40 80 99 60 90 50\n6 5\n64 40 50 68 70 10\n2 1\n1 1000000000\n"
] | [
"5\n-1\n10\n8\n1\n47\n100\n-1\n-1\n"
] | 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.
Example Input 0:
9
4 4
2 3 4 5
4 3
2 3 4 5
4 3
3 4 8 9
3 3
6 2 1
1 1
1
7 5
14 34 6 25 46 7 17
6 5
40 80 99 60 90 50
6 5
64 40 50 68 70 10
2 1
1 1000000000
Example Output 0:
5
-1
10
8
1
47
100
-1
-1
Notes:
In the first test case, all lights will be on by the minute $$$5$$$ without any of them being turned off by the chips. The answer is $$$5$$$.
In the second test case, due to $$$k=3$$$, the $$$1$$$-st light will be on at minutes $$$2, 3, 4, 8, 9, 10, 14, \ldots$$$; meanwhile, the $$$4$$$-th light will be on at minutes $$$5, 6, 7, 11, 12, 13, 17, \ldots$$$. These two sequences don't have any number in common, so they will never be on at the same time.
In the third test case, it can be seen that the $$$1$$$-st and $$$2$$$-nd lights will be turned off at minutes $$$6$$$ and $$$7$$$, but the chips will turn them back on at minutes $$$9$$$ and $$$10$$$. The $$$3$$$-rd and $$$4$$$-th lights will also be on at minute $$$10$$$, so the answer is $$$10$$$. | 1 |
Codeforces_test | null | 13 | stdin | [
"4\n2 3 7\nS.\n..\n2 3 7\nS.\n.W\n2 7 3\nS.\n.W\n4 999999999 1000000000\nW.S.\nW..W\n",
"2\n6 6 7\nW....W\nW.S..W\n6 6 7\nW...WW\n..S..W\n"
] | [
"0\n3\n6\n2999999997\n",
"21\n20\n"
] | [
"4\n2 3 7\nS.\n..\n2 3 7\nS.\n.W\n2 7 3\nS.\n.W\n4 999999999 1000000000\nW.S.\nW..W\n",
"2\n6 6 7\nW....W\nW.S..W\n6 6 7\nW...WW\n..S..W\n"
] | [
"0\n3\n6\n2999999997\n",
"21\n20\n"
] | In a game you started playing recently, there is a field that can be represented as a rectangular grid. The field has $$$2$$$ rows and $$$n$$$ columns — $$$2n$$$ cells in total.
Some cells are empty, but some are occupied by wolves. In the beginning of the game, you have one sheep in some cell, and you'd like to save it from wolves.
Wolves will attack you at night, so you still have some time for preparation. You have two ways to deal with wolves:
1. You pay $$$h$$$ coins to hunters and choose a cell occupied by a wolf. Hunters will clear the cell, eradicating the wolf in it.
2. You pay $$$b$$$ coins to builders and choose an empty cell. Builders will dig a trench in the chosen cell that wolves can't cross.
Let's say that a wolf can reach the sheep if there is a path that starts at the wolf's cell and finishes at the sheep's cell. This path shouldn't contain any cells with trenches, and each two consecutive cells in the path should be neighbors (share a side).
What is the minimum total amount of money you should pay to ensure that none of the wolves can reach the sheep?
Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1200$$$) — the number of test cases. Next, $$$t$$$ independent cases follow.
The first line of each test case contains three integers $$$n$$$, $$$h$$$, and $$$b$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le h, b \le 10^9$$$) — the size of the grid and corresponding costs.
The next two lines contain a description of the grid. The $$$j$$$-th character in the $$$i$$$-th line is either '.', 'S' or 'W':
- '.' means that the cell is empty;
- 'S' means that the cell is occupied by the sheep; there is exactly one such cell on the grid;
- 'W' means that the cell is occupied by a wolf.
Additional constraints:
- there are no wolves in cells neighboring the sheep cell;
- the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
Output format:
For each test case, print a single integer — the minimum total amount of money you should pay to save your sheep.
Example Input 0:
4
2 3 7
S.
..
2 3 7
S.
.W
2 7 3
S.
.W
4 999999999 1000000000
W.S.
W..W
Example Output 0:
0
3
6
2999999997
Example Input 1:
2
6 6 7
W....W
W.S..W
6 6 7
W...WW
..S..W
Example Output 1:
21
20
Notes:
One of the optimal strategies in the first test case of the second test can be shown like this:
W....W$$$\Rightarrow$$$W.#..WW.S..WW#S#.W
W...WW$$$\Rightarrow$$$W..#WW..S..W..S#.W | 1 |
Codeforces_test | null | 14 | stdin | [
"6\n7\n3 6 4 9 2 5 2\n5\n5 4 4 2 2\n8\n2 2 4 4 6 6 10 10\n1\n1000\n9\n6 8 9 10 12 9 7 5 4\n7\n300000000 600000000 400000000 900000000 200000000 400000000 200000000\n"
] | [
"2\n0\n6\n0\n4\n2\n"
] | [
"6\n7\n3 6 4 9 2 5 2\n5\n5 4 4 2 2\n8\n2 2 4 4 6 6 10 10\n1\n1000\n9\n6 8 9 10 12 9 7 5 4\n7\n300000000 600000000 400000000 900000000 200000000 400000000 200000000\n"
] | [
"2\n0\n6\n0\n4\n2\n"
] | Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an $$$\mathcal{O}(n)$$$ time complexity.
It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array $$$[1, 4, 2, 3, 6, 5, 5, 7, 7]$$$ becomes $$$[1, 4, 6, 7, 7]$$$ after a Stalin Sort.
We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays$$$^{\text{∗}}$$$, as many times as is needed.
Given an array $$$a$$$ of $$$n$$$ integers, determine the minimum number of integers which must be removed from the array to make it vulnerable.
Input format:
Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. This 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 size of the array.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
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 integer — the minimum number of integers which must be removed from the array to make it vulnerable.
Example Input 0:
6
7
3 6 4 9 2 5 2
5
5 4 4 2 2
8
2 2 4 4 6 6 10 10
1
1000
9
6 8 9 10 12 9 7 5 4
7
300000000 600000000 400000000 900000000 200000000 400000000 200000000
Example Output 0:
2
0
6
0
4
2
Notes:
In the first test case, the optimal answer is to remove the numbers $$$3$$$ and $$$9$$$. Then we are left with $$$a = [6, 4, 2, 5, 2]$$$. To show this array is vulnerable, we can first apply a Stalin Sort on the subarray $$$[4, 2, 5]$$$ to get $$$a = [6, 4, 5, 2]$$$ and then apply a Stalin Sort on the subarray $$$[6, 4, 5]$$$ to get $$$a = [6, 2]$$$, which is non-increasing.
In the second test case, the array is already non-increasing, so we don't have to remove any integers. | 1 |
Codeforces_test | null | 15 | stdin | [
"3\n3\n1 2 3\n5 4 6\n5\n1 2 3 6 8\n5 4 7 9 10\n5\n3 1 4 1 5\n7 2 6 5 10\n"
] | [
"3\n4\n4\n"
] | [
"3\n3\n1 2 3\n5 4 6\n5\n1 2 3 6 8\n5 4 7 9 10\n5\n3 1 4 1 5\n7 2 6 5 10\n"
] | [
"3\n4\n4\n"
] | This is the hard version of the problem. In this version, the constraints on $$$n$$$ and the time limit are higher. You can make hacks only if both versions of the problem are solved.
A set of (closed) segments is complex if it can be partitioned into some subsets such that
- all the subsets have the same size; and
- a pair of segments intersects if and only if the two segments are in the same subset.
You are given $$$n$$$ segments $$$[l_1, r_1], [l_2, r_2], \ldots, [l_n, r_n]$$$. Find the maximum size of a complex subset of these segments.
Input format:
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.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of segments.
The second line of each test case contains $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$1 \le l_i \le 2n$$$) — the left endpoints of the segments.
The third line of each test case contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$l_i \leq r_i \le 2n$$$) — the right endpoints of the segments.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
Output format:
For each test case, output a single integer: the maximum size of a complex subset of the given segments.
Example Input 0:
3
3
1 2 3
5 4 6
5
1 2 3 6 8
5 4 7 9 10
5
3 1 4 1 5
7 2 6 5 10
Example Output 0:
3
4
4
Notes:
In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments.
In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: $$$\{\{ [1, 5], [2, 4] \}, \{ [6, 9], [8, 10] \}\}$$$.
In the third test case, it is optimal to make a single group containing all the segments except the second. | 1 |
Codeforces_test | null | 16 | stdin | [
"5\n5\n1 -1 10 1 1\n5\n-1 -1 -1 -1 -1\n2\n-1 2\n2\n7 1\n3\n1 4 -1\n"
] | [
"8\n-1 0 1 2 9 10 11 12 \n6\n-5 -4 -3 -2 -1 0 \n4\n-1 0 1 2 \n4\n0 1 7 8 \n6\n-1 0 1 3 4 5\n"
] | [
"5\n5\n1 -1 10 1 1\n5\n-1 -1 -1 -1 -1\n2\n-1 2\n2\n7 1\n3\n1 4 -1\n",
"1\n4\n-1 1 1 1\n",
"1\n19\n1 -1 1 1 -1 1 1 1 1 1 1 1 1 -1 1 -1 -1 -1 1\n",
"1\n3\n1 1 -1\n"
] | [
"8\n-1 0 1 2 9 10 11 12 \n6\n-5 -4 -3 -2 -1 0 \n4\n-1 0 1 2 \n4\n0 1 7 8 \n6\n-1 0 1 3 4 5 \n",
"5\n-1 0 1 2 3 \n",
"13\n-3 -2 -1 0 1 2 3 4 5 6 7 8 9 \n",
"4\n-1 0 1 2 \n"
] | You are given an array $$$a$$$ of $$$n$$$ integers, where all elements except for at most one are equal to $$$-1$$$ or $$$1$$$. The remaining element $$$x$$$ satisfies $$$-10^9 \le x \le 10^9$$$.
Find all possible sums of subarrays of $$$a$$$, including the empty subarray, whose sum is defined as $$$0$$$. In other words, find all integers $$$x$$$ such that the array $$$a$$$ has at least one subarray (possibly empty) with sum equal to $$$x$$$. A subarray is a contiguous subsegment of an array.
Output these sums in ascending order. Each sum should be printed only once, even if it is achieved by multiple subarrays.
Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then, $$$t$$$ test cases follow.
Each test case consists of two lines:
- The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array.
- The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$. In the array $$$a$$$, there is at most one element that is neither $$$1$$$ nor $$$-1$$$.
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, output two lines:
- In the first line, print a single integer — the number of distinct subarray sums.
- In the second line, print these sums in ascending order.
Each sum should be printed only once, even if it is produced by multiple subarrays.
Example Input 0:
5
5
1 -1 10 1 1
5
-1 -1 -1 -1 -1
2
-1 2
2
7 1
3
1 4 -1
Example Output 0:
8
-1 0 1 2 9 10 11 12
6
-5 -4 -3 -2 -1 0
4
-1 0 1 2
4
0 1 7 8
6
-1 0 1 3 4 5
Notes:
Let's define $$$a[i,j]$$$ as the subarray of $$$a$$$ from position $$$i$$$ to position $$$j$$$.
Consider the first test case of the example:
- $$$-1$$$ is produced by $$$a[2,2]$$$;
- $$$0$$$ is produced by the empty subarray;
- $$$1$$$ is produced by $$$a[4,4]$$$;
- $$$2$$$ is produced by $$$a[4,5]$$$;
- $$$9$$$ is produced by $$$a[2,3]$$$;
- $$$10$$$ is produced by $$$a[1,3]$$$;
- $$$11$$$ is produced by $$$a[3,4]$$$;
- $$$12$$$ is produced by $$$a[3,5]$$$. | 1 |
Codeforces_test | null | 17 | stdin | [
"4\n2\n2 1\n2 1\n3\n1 2 3\n3 2 1\n4\n2 1 4 3\n4 2 3 1\n5\n1 4 3 2 5\n5 2 3 4 1\n"
] | [
"0\n1\n1 3\n3\n1 4\n2 4\n1 3\n4\n1 2\n4 5\n2 5\n1 4\n"
] | [
"4\n2\n2 1\n2 1\n3\n1 2 3\n3 2 1\n4\n2 1 4 3\n4 2 3 1\n5\n1 4 3 2 5\n5 2 3 4 1\n"
] | [
"0\n1\n1 3\n3\n1 4\n2 4\n1 3\n4\n1 2\n4 5\n2 5\n1 4\n"
] | You are given two permutations $$$p_1,p_2,\ldots,p_n$$$ and $$$q_1,q_2,\ldots,q_n$$$ of length $$$n$$$. In one operation, you can select two integers $$$1\leq i,j\leq n,i\neq j$$$ and swap $$$p_i$$$ and $$$p_j$$$. The cost of the operation is $$$\min (|i-j|,|p_i-p_j|)$$$.
Find the minimum cost to make $$$p_i = q_i$$$ hold for all $$$1\leq i\leq n$$$ and output a sequence of operations to achieve the minimum cost.
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Input format:
The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of input test cases.
The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of permutations $$$p$$$ and $$$q$$$.
The second line contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1\leq p_i\leq n$$$) — the permutation $$$p$$$. It is guaranteed that $$$p_1,p_2,\ldots,p_n$$$ is a permutation of $$$1,2,\ldots,n$$$.
The third line contains $$$n$$$ integers $$$q_1,q_2,\ldots,q_n$$$ ($$$1\leq q_i\leq n$$$) — the permutation $$$q$$$. It is guaranteed that $$$q_1,q_2,\ldots,q_n$$$ is a permutation of $$$1,2,\ldots,n$$$.
It is guaranteed that the sum of $$$n^3$$$ over all test cases does not exceed $$$10^6$$$.
Output format:
For each test case, output the total number of operations $$$k$$$ ($$$0\le k\le n^2$$$) on the first line. Then output $$$k$$$ lines, each containing two integers $$$i,j$$$ ($$$1\le i,j\le n$$$, $$$i\neq j$$$) representing an operation to swap $$$p_i$$$ and $$$p_j$$$ in order.
It can be shown that no optimal operation sequence has a length greater than $$$n^2$$$.
Example Input 0:
4
2
2 1
2 1
3
1 2 3
3 2 1
4
2 1 4 3
4 2 3 1
5
1 4 3 2 5
5 2 3 4 1
Example Output 0:
0
1
1 3
3
1 4
2 4
1 3
4
1 2
4 5
2 5
1 4
Notes:
In the second test case, you can swap $$$p_1,p_3$$$ costing $$$\min(|1-3|,|1-3|)=2$$$. Then $$$p$$$ equals $$$q$$$ with a cost of $$$2$$$.
In the third test case, you can perform the following operations:
Initially, $$$p=[2,1,4,3]$$$.
1. Swap $$$p_1,p_4$$$ costing $$$\min(|1-4|,|2-3|)=1$$$, resulting in $$$p=[3,1,4,2]$$$.
2. Swap $$$p_2,p_4$$$ costing $$$\min(|2-4|,|1-2|)=1$$$, resulting in $$$p=[3,2,4,1]$$$.
3. Swap $$$p_1,p_3$$$ costing $$$\min(|1-3|,|3-4|)=1$$$. Then $$$p$$$ equals $$$q$$$ with a cost of $$$3$$$. | 1 |
Codeforces_test | null | 18 | stdin | [
"9\n2\n1 1\n2\n2 1\n4\n1 1 4 4\n4\n3 4 3 3\n4\n2 3 4 4\n6\n3 3 4 5 3 3\n6\n2 2 2 4 4 4\n8\n1 1 1 1 1 1 1 4\n10\n9 9 9 10 10 10 10 10 10 10\n"
] | [
"Yes\nNo\nYes\nYes\nNo\nYes\nNo\nYes\nYes\n"
] | [
"9\n2\n1 1\n2\n2 1\n4\n1 1 4 4\n4\n3 4 3 3\n4\n2 3 4 4\n6\n3 3 4 5 3 3\n6\n2 2 2 4 4 4\n8\n1 1 1 1 1 1 1 4\n10\n9 9 9 10 10 10 10 10 10 10\n"
] | [
"Yes\nNo\nYes\nYes\nNo\nYes\nNo\nYes\nYes\n"
] | You have two large bags of numbers. Initially, the first bag contains $$$n$$$ numbers: $$$a_1, a_2, \ldots, a_n$$$, while the second bag is empty. You are allowed to perform the following operations:
- Choose any number from the first bag and move it to the second bag.
- Choose a number from the first bag that is also present in the second bag and increase it by one.
You can perform an unlimited number of operations of both types, in any order. Is it possible to make the contents of the first and second bags identical?
Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the length of the array $$$a$$$. It is guaranteed that $$$n$$$ is an even number.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$).
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$.
Output format:
For each test case, print "YES" if it is possible to equalize the contents of the bags. Otherwise, output "NO".
You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer).
Example Input 0:
9
2
1 1
2
2 1
4
1 1 4 4
4
3 4 3 3
4
2 3 4 4
6
3 3 4 5 3 3
6
2 2 2 4 4 4
8
1 1 1 1 1 1 1 4
10
9 9 9 10 10 10 10 10 10 10
Example Output 0:
Yes
No
Yes
Yes
No
Yes
No
Yes
Yes
Notes:
Let's analyze the sixth test case: we will show the sequence of operations that leads to the equality of the bags. Initially, the first bag consists of the numbers $$$(3, 3, 4, 5, 3, 3)$$$, and the second bag is empty.
1. In the first operation, move the number $$$3$$$ from the first bag to the second. State: $$$(3, 4, 5, 3, 3)$$$ and $$$(3)$$$.
2. In the second operation, increase the number $$$3$$$ from the first bag by one. This operation is possible because the second bag contains the number $$$3$$$. State: $$$(4, 4, 5, 3, 3)$$$ and $$$(3)$$$.
3. In the third operation, move the number $$$4$$$ from the first bag to the second. State: $$$(4, 5, 3, 3)$$$ and $$$(3, 4)$$$.
4. In the fourth operation, increase the number $$$4$$$ from the first bag by one. State: $$$(5, 5, 3, 3)$$$ and $$$(3, 4)$$$.
5. In the fifth operation, move the number $$$5$$$ from the first bag to the second. State: $$$(5, 3, 3)$$$ and $$$(3, 4, 5)$$$.
6. In the sixth operation, increase the number $$$3$$$ from the first bag by one. State: $$$(5, 3, 4)$$$ and $$$(3, 4, 5)$$$.
As we can see, as a result of these operations, it is possible to make the contents of the bags equal, so the answer exists. | 1 |
Codeforces_test | null | 19 | stdin | [
"5\n5\n1 5\n2 4\n3 3\n4 2\n5 1\n3\n2 2\n1 1\n1 2\n1\n3 2\n3\n100 100\n100 100\n100 100\n4\n1 4\n2 3\n1 5\n3 2\n"
] | [
"20\n8\n10\n400\n16\n"
] | [
"5\n5\n1 5\n2 4\n3 3\n4 2\n5 1\n3\n2 2\n1 1\n1 2\n1\n3 2\n3\n100 100\n100 100\n100 100\n4\n1 4\n2 3\n1 5\n3 2\n"
] | [
"20\n8\n10\n400\n16\n"
] | You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $$$n$$$ stamps. Each stamp is a rectangle of width $$$w_i$$$ and height $$$h_i$$$.
You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, and for each cell, the stamp must either cover it fully or not cover it at all. You can use the stamp at any position on the grid, even if some or all of the cells covered by the stamping area are already black.
What is the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used?
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 the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$).
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \le w_i, h_i \le 100$$$).
Output format:
For each test case, output a single integer — the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.
Example Input 0:
5
5
1 5
2 4
3 3
4 2
5 1
3
2 2
1 1
1 2
1
3 2
3
100 100
100 100
100 100
4
1 4
2 3
1 5
3 2
Example Output 0:
20
8
10
400
16
Notes:
In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity.
After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $$$20$$$. It can be shown that there is no way of using the stamps that yields a lower total perimeter.
In the second test case, the second and third stamps can be used entirely inside the first one, so the minimum perimeter is equal to $$$8$$$. | 1 |
Codeforces_test | null | 20 | stdin | [
"6\n1\n2\n2\n2 19\n3\n1 3 20\n4\n1 2 3 4\n5\n1 2 3 4 5\n6\n1 2 1 1 1 25\n"
] | [
"-1\n-1\n0\n15\n16\n0\n"
] | [
"6\n1\n2\n2\n2 19\n3\n1 3 20\n4\n1 2 3 4\n5\n1 2 3 4 5\n6\n1 2 1 1 1 25\n"
] | [
"-1\n-1\n0\n15\n16\n0\n"
] | Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood!
There are $$$n$$$ people living in the town. Just now, the wealth of the $$$i$$$-th person was $$$a_i$$$ gold. But guess what? The richest person has found an extra pot of gold!
More formally, find an $$$a_j=max(a_1, a_2, \dots, a_n)$$$, change $$$a_j$$$ to $$$a_j+x$$$, where $$$x$$$ is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.
A person is unhappy if their wealth is strictly less than half of the average wealth$$$^{\text{∗}}$$$.
If strictly more than half of the total population $$$n$$$ are unhappy, Robin Hood will appear by popular demand.
Determine the minimum value of $$$x$$$ for Robin Hood to appear, or output $$$-1$$$ if it is impossible.
Input format:
The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2\cdot10^5$$$) — the total population.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the wealth of each person.
It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.
Output format:
For each test case, output one integer — the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output $$$-1$$$ instead.
Example Input 0:
6
1
2
2
2 19
3
1 3 20
4
1 2 3 4
5
1 2 3 4 5
6
1 2 1 1 1 25
Example Output 0:
-1
-1
0
15
16
0
Notes:
In the first test case, it is impossible for a single person to be unhappy.
In the second test case, there is always $$$1$$$ happy person (the richest).
In the third test case, no additional gold are required, so the answer is $$$0$$$.
In the fourth test case, after adding $$$15$$$ gold, the average wealth becomes $$$\frac{25}{4}$$$, and half of this average is $$$\frac{25}{8}$$$, resulting in $$$3$$$ people being unhappy.
In the fifth test case, after adding $$$16$$$ gold, the average wealth becomes $$$\frac{31}{5}$$$, resulting in $$$3$$$ people being unhappy. | 1 |
Codeforces_test | null | 21 | stdin | [
"4\n4 4 3\n1 2 3 4\n1 3 4\n5 4 3\n1 2 3 4\n1 3 4\n4 4 4\n1 2 3 4\n1 2 3 4\n2 2 1\n1 2\n2\n"
] | [
"0100\n0000\n1111\n10\n"
] | [
"4\n4 4 3\n1 2 3 4\n1 3 4\n5 4 3\n1 2 3 4\n1 3 4\n4 4 4\n1 2 3 4\n1 2 3 4\n2 2 1\n1 2\n2\n"
] | [
"0100\n0000\n1111\n10\n"
] | Monocarp is preparing for his first exam at the university. There are $$$n$$$ different questions which can be asked during the exam, numbered from $$$1$$$ to $$$n$$$. There are $$$m$$$ different lists of questions; each list consists of exactly $$$n-1$$$ different questions. Each list $$$i$$$ is characterized by one integer $$$a_i$$$, which is the index of the only question which is not present in the $$$i$$$-th list. For example, if $$$n = 4$$$ and $$$a_i = 3$$$, the $$$i$$$-th list contains questions $$$[1, 2, 4]$$$.
During the exam, Monocarp will receive one of these $$$m$$$ lists of questions. Then, the professor will make Monocarp answer all questions from the list. So, Monocarp will pass only if he knows all questions from the list.
Monocarp knows the answers for $$$k$$$ questions $$$q_1, q_2, \dots, q_k$$$. For each list, determine if Monocarp will pass the exam if he receives that list.
Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
Each test case consists of three lines:
- the first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 3 \cdot 10^5$$$; $$$1 \le m, k \le n$$$);
- the second line contains $$$m$$$ distinct integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_i \le n$$$; $$$a_i < a_{i+1}$$$);
- the third line contains $$$k$$$ distinct integers $$$q_1, q_2, \dots, q_k$$$ ($$$1 \le q_i \le n$$$; $$$q_i < q_{i+1}$$$).
Additional constraints on the input:
- the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
Output format:
For each test case, print a string of $$$m$$$ characters. The $$$i$$$-th character should be 1 if Monocarp passes the exam if he receives the $$$i$$$-th question list, 0 if Monocarp won't pass.
Example Input 0:
4
4 4 3
1 2 3 4
1 3 4
5 4 3
1 2 3 4
1 3 4
4 4 4
1 2 3 4
1 2 3 4
2 2 1
1 2
2
Example Output 0:
0100
0000
1111
10
Notes:
In the first test case, Monocarp knows the questions $$$[1, 3, 4]$$$. Let's consider all the question lists:
- the first list consists of questions $$$[2, 3, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass;
- the second list consists of questions $$$[1, 3, 4]$$$. Monocarp knows all these questions, so he will pass;
- the third list consists of questions $$$[1, 2, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass;
- the fourth list consists of questions $$$[1, 2, 3]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass. | 1 |
Codeforces_test | null | 22 | stdin | [
"6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193\n"
] | [
"3\n18\n5\n1\n18\n53\n"
] | [
"6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193\n"
] | [
"3\n18\n5\n1\n18\n53\n"
] | Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $$$x\neq y$$$ from the set such that their average $$$\frac{x+y}2$$$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $$$x$$$ and $$$y$$$ remain in the set.
Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $$$1$$$. For example, sets $$$\{2\}$$$, $$$\{2, 5, 4, 3\}$$$, $$$\{5, 6, 8, 7\}$$$ are consecutive, while $$$\{2, 4, 5, 6\}$$$, $$$\{9, 7\}$$$ are not.
Eri likes consecutive sets. Suppose there is an array $$$b$$$, then Eri puts all elements in $$$b$$$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $$$b$$$ will be called brilliant.
Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.
Eri has an array $$$a$$$ of $$$n$$$ positive integers. Please help him to count the number of pairs of integers $$$(l,r)$$$ such that $$$1 \leq l \leq r \leq n$$$ and the subarray $$$a_l, a_{l+1}, \ldots, a_r$$$ is brilliant.
Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 4 \cdot 10^5$$$) — length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$4 \cdot 10^5$$$.
Output format:
For each test case, output a single integer — the number of brilliant subarrays.
Example Input 0:
6
2
2 2
6
1 3 6 10 15 21
5
6 30 18 36 9
1
1000000000
6
1 1 4 5 1 4
12
70 130 90 90 90 108 612 500 451 171 193 193
Example Output 0:
3
18
5
1
18
53
Notes:
In the first test case, the array $$$a = [2, 2]$$$ has $$$3$$$ subarrays: $$$[2]$$$, $$$[2]$$$, and $$$[2, 2]$$$. For all of them, the set only contains a single integer $$$2$$$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $$$3$$$.
In the second test case, let's consider the subarray $$$[3, 6, 10]$$$. We can do operations as follows:
$$$$$$\{3,6,10\} \xrightarrow{x=6,y=10} \{3,6,8,10\} \xrightarrow{x=6,y=8} \{3,6,7,8,10\} \xrightarrow{x=3,y=7} \{3,5,6,7,8,10\}$$$$$$ $$$$$$\xrightarrow{x=3,y=5} \{3,4,5,6,7,8,10\} \xrightarrow{x=8,y=10} \{3,4,5,6,7,8,9,10\}$$$$$$
$$$\{3,4,5,6,7,8,9,10\}$$$ is a consecutive set, so the subarray $$$[3, 6, 10]$$$ is brilliant. | 1 |
Codeforces_test | null | 23 | stdin | [
"5\nqwq\nppppp\npppwwwqqq\nwqpqwpqwwqp\npqpqpqpq\n"
] | [
"pwp\nqqqqq\npppwwwqqq\nqpwwpqwpqpw\npqpqpqpq\n"
] | [
"5\nqwq\nppppp\npppwwwqqq\nwqpqwpqwwqp\npqpqpqpq\n",
"1\npwwwqppppwppwwwpwwwppqpwwqppqpppwwwwpwwwwpwwwwqwqqwwww\n"
] | [
"pwp\nqqqqq\npppwwwqqq\nqpwwpqwpqpw\npqpqpqpq\n",
"wwwwppwpwwwwqwwwwqwwwwqqqpqqpwwqpqqwwwqwwwqqwqqqqpwwwq\n"
] | A string consisting of only characters 'p', 'q', and 'w' is painted on a glass window of a store. Ship walks past the store, standing directly in front of the glass window, and observes string $$$a$$$. Ship then heads inside the store, looks directly at the same glass window, and observes string $$$b$$$.
Ship gives you string $$$a$$$. Your job is to find and output $$$b$$$.
Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
The only line of each test case contains a string $$$a$$$ ($$$1 \leq |a| \leq 100$$$) — the string Ship observes from outside the store. It is guaranteed that $$$a$$$ only contains characters 'p', 'q', and 'w'.
Output format:
For each test case, output string $$$b$$$, the string Ship observes from inside the store, on a new line.
Example Input 0:
5
qwq
ppppp
pppwwwqqq
wqpqwpqwwqp
pqpqpqpq
Example Output 0:
pwp
qqqqq
pppwwwqqq
qpwwpqwpqpw
pqpqpqpq
| 1 |
Codeforces_test | null | 24 | stdin | [
"3\n10\n1 2 3 4 5 6 7 8 9 10\n11\n1 6 9 4 7 4 4 10 3 2 3\n4\n527792568 502211460 850237282 374773208\n"
] | [
"1 3 8 13 46 59 126 149 1174 1311 \n1 7 22 26 70 74 150 1303 1306 1308 1568 \n527792568 83665723 399119771 773892979\n"
] | [
"3\n10\n1 2 3 4 5 6 7 8 9 10\n11\n1 6 9 4 7 4 4 10 3 2 3\n4\n527792568 502211460 850237282 374773208\n",
"1\n3\n999999998 2097152 1048576\n"
] | [
"1 3 8 13 46 59 126 149 1174 1311 \n1 7 22 26 70 74 150 1303 1306 1308 1568 \n527792568 83665723 399119771 773892979 \n",
"999999998 2097143 546480318 \n"
] | You are given an array $$$b$$$ of length $$$m$$$. You can perform the following operation any number of times (possibly zero):
- Choose two distinct indices $$$i$$$ and $$$j$$$ where $$$\bf{1\le i < j\le m}$$$ and $$$b_i$$$ is even, divide $$$b_i$$$ by $$$2$$$ and multiply $$$b_j$$$ by $$$2$$$.
Since this problem is too easy, you are given an array $$$a$$$ of length $$$n$$$ and need to solve the problem for each prefix of $$$a$$$.
In other words, denoting the maximum sum of $$$b$$$ after performing any number of such operations as $$$f(b)$$$, you need to output $$$f([a_1])$$$, $$$f([a_1,a_2])$$$, $$$\ldots$$$, $$$f([a_1,a_2,\ldots,a_n])$$$ modulo $$$10^9+7$$$ respectively.
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 length of $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the starting values of array $$$a$$$.
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 $$$n$$$ integers representing the answer for each prefix of $$$a$$$ modulo $$$10^9+7$$$.
Example Input 0:
3
10
1 2 3 4 5 6 7 8 9 10
11
1 6 9 4 7 4 4 10 3 2 3
4
527792568 502211460 850237282 374773208
Example Output 0:
1 3 8 13 46 59 126 149 1174 1311
1 7 22 26 70 74 150 1303 1306 1308 1568
527792568 83665723 399119771 773892979
Notes:
For each prefix in the first example, a possible array after operations is:
- $$$[1]$$$ and the sum is $$$1$$$;
- $$$[1, 2]$$$ and the sum is $$$3$$$;
- $$$[1, 1, 6]$$$ and the sum is $$$8$$$;
- $$$[1, 1, 3, 8]$$$ and the sum is $$$13$$$;
- $$$[1, 1, 3, 1, 40]$$$ and the sum is $$$46$$$;
- $$$[1, 1, 3, 1, 5, 48]$$$ and the sum is $$$59$$$;
- $$$[1, 1, 3, 1, 5, 3, 112]$$$ and the sum is $$$126$$$;
- $$$[1, 1, 3, 1, 5, 3, 7, 128]$$$ and the sum is $$$149$$$;
- $$$[1, 1, 3, 1, 5, 3, 7, 1, 1152]$$$ and the sum is $$$1174$$$;
- $$$[1, 1, 3, 1, 5, 3, 7, 1, 9, 1280]$$$ and the sum is $$$1311$$$. | 1 |
Codeforces_test | null | 25 | stdin | [
"6\n2 3\n10\n2 3\n11\n5 1\n11101\n7 9\n1101011\n17 34\n11001010001010010\n1 1000000000\n1\n"
] | [
"3\n2\n0\n8\n32\n0\n"
] | [
"6\n2 3\n10\n2 3\n11\n5 1\n11101\n7 9\n1101011\n17 34\n11001010001010010\n1 1000000000\n1\n"
] | [
"3\n2\n0\n8\n32\n0\n"
] | This is the hard version of the problem. The difference between the versions is that in this version, the constraints on $$$t$$$, $$$k$$$, and $$$m$$$ are higher. You can hack only if you solved all versions of this problem.
A sequence $$$a$$$ of $$$n$$$ integers is called good if the following condition holds:
- Let $$$\text{cnt}_x$$$ be the number of occurrences of $$$x$$$ in sequence $$$a$$$. For all pairs $$$0 \le i < j < m$$$, at least one of the following has to be true: $$$\text{cnt}_i = 0$$$, $$$\text{cnt}_j = 0$$$, or $$$\text{cnt}_i \le \text{cnt}_j$$$. In other words, if both $$$i$$$ and $$$j$$$ are present in sequence $$$a$$$, then the number of occurrences of $$$i$$$ in $$$a$$$ is less than or equal to the number of occurrences of $$$j$$$ in $$$a$$$.
You are given integers $$$n$$$ and $$$m$$$. Calculate the value of the bitwise XOR of the median$$$^{\text{∗}}$$$ of all good sequences $$$a$$$ of length $$$n$$$ with $$$0\le a_i < m$$$.
Note that the value of $$$n$$$ can be very large, so you are given its binary representation instead.
Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$k$$$ and $$$m$$$ ($$$1 \le k \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the number of bits in $$$n$$$ and the upper bound on the elements in sequence $$$a$$$.
The second line of each test case contains a binary string of length $$$k$$$ — the binary representation of $$$n$$$ with no leading zeros.
It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
Output format:
For each test case, output a single integer representing the bitwise XOR of the median of all good sequences $$$a$$$ of length $$$n$$$ where $$$0\le a_i < m$$$.
Example Input 0:
6
2 3
10
2 3
11
5 1
11101
7 9
1101011
17 34
11001010001010010
1 1000000000
1
Example Output 0:
3
2
0
8
32
0
Notes:
In the first example, $$$n = 10_2 = 2$$$ and $$$m = 3$$$. All possible sequences with elements less than $$$m$$$ are: $$$[0, 0]$$$, $$$[0, 1]$$$, $$$[0, 2]$$$, $$$[1, 0]$$$, $$$[1, 1]$$$, $$$[1, 2]$$$, $$$[2, 0]$$$, $$$[2, 1]$$$, $$$[2, 2]$$$. All of them are good, so the answer is: $$$0 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 2 = 3$$$.
In the second example, $$$n = 11_2 = 3$$$ and $$$m = 3$$$. Some good sequences are $$$[2, 2, 2]$$$, $$$[1, 0, 1]$$$, and $$$[2, 0, 1]$$$. However, a sequence $$$[2, 0, 0]$$$ is not good, because $$$\text{cnt}_0 = 2$$$, $$$\text{cnt}_2 = 1$$$. Therefore, if we set $$$i = 0$$$ and $$$j = 2$$$, $$$i < j$$$ holds, but $$$\text{cnt}_i \le \text{cnt}_j$$$ does not. | 1 |
We use Stdio input/output format here. For example, for the task to calculate the sum of a list, the input and output are in the following format:
input = "5\n1 2 3 4 5\n"
output = "15"
CodeContests and CodeForces are using this format, however, MBPP and part of LiveCodeBench are using functional input/output format, such like
assert sum_function([1, 2, 3, 4, 5]) == 15
In this project, we have converted the the functional format to the Stdio format to achieve consistency.
@article{wang2025cure,
title={Co-Evolving LLM Coder and Unit Tester via Reinforcement Learning},
author={Wang, Yinjie and Yang, Ling and Tian, Ye and Shen, Ke and Wang, Mengdi},
journal={arXiv preprint arXiv:2506.03136},
year={2025}
}
@misc{penedo2025codeforces,
title={CodeForces},
author={Guilherme Penedo and Anton Lozhkov and Hynek Kydlíček and Loubna Ben Allal and Edward Beeching and Agustín Piqueres Lajarín and Quentin Gallouédec and Nathan Habib and Lewis Tunstall and Leandro von Werra},
year={2025},
publisher = {Hugging Face},
journal = {Hugging Face repository},
howpublished = {\url{https://huggingface.co/datasets/open-r1/codeforces}}
}