Shohag has an integer $$$n$$$. Please help him find an increasing integer sequence $$$1 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 100$$$ such that $$$a_i \bmod i \neq a_j \bmod j$$$ $$$^{\text{∗}}$$$ is satisfied over all pairs $$$1 \le i \lt j \le n$$$.
It can be shown that such a sequence always exists under the given constraints.
$$$^{\text{∗}}$$$$$$a \bmod b$$$ denotes the remainder of $$$a$$$ after division by $$$b$$$. For example, $$$7 \bmod 3 = 1, 8 \bmod 4 = 0$$$ and $$$69 \bmod 10 = 9$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases.
The first and only line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 50$$$).
For each test case, print $$$n$$$ integers — the integer sequence that satisfies the conditions mentioned in the statement. If there are multiple such sequences, output any.
236
2 7 8 2 3 32 35 69 95
In the first test case, the sequence is increasing, values are from $$$1$$$ to $$$100$$$ and each pair of indices satisfies the condition mentioned in the statement:
Note that you do not necessarily have to print the exact same sequence, you can print any other sequence as long as it satisfies the necessary conditions.
For a string $$$p$$$, let $$$f(p)$$$ be the number of distinct non-empty substrings$$$^{\text{∗}}$$$ of $$$p$$$.
Shohag has a string $$$s$$$. Help him find a non-empty string $$$p$$$ such that $$$p$$$ is a substring of $$$s$$$ and $$$f(p)$$$ is even or state that no such string exists.
$$$^{\text{∗}}$$$A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
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 a string $$$s$$$ ($$$1 \le |s| \le 10^5$$$) consisting of lowercase English letters.
It is guaranteed that the sum of the length of $$$s$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.
For each test case, print a non-empty string that satisfies the conditions mentioned in the statement, or $$$-1$$$ if no such string exists. If there are multiple solutions, output any.
5dcabaacayouknowwhocodeforcesbangladesh
abaa -1 youknowwho eforce bang
In the first test case, we can set $$$p = $$$ abaa because it is a substring of $$$s$$$ and the distinct non-empty substrings of $$$p$$$ are a, b, aa, ab, ba, aba, baa and abaa, so it has a total of $$$8$$$ distinct substrings which is even.
In the second test case, we can only set $$$p = $$$ a but it has one distinct non-empty substring but this number is odd, so not valid.
In the third test case, the whole string contains $$$52$$$ distinct non-empty substrings, so the string itself is a valid solution.
This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$\mathbf{x \neq y}$$$ and $$$x \oplus y$$$ is a divisor$$$^{\text{∗}}$$$ of either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator.
$$$^{\text{∗}}$$$The number $$$b$$$ is a divisor of the number $$$a$$$ if there exists an integer $$$c$$$ such that $$$a = b \cdot c$$$.
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 two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$).
It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$.
For each test case, print a single integer — the number of suitable $$$y$$$.
56 95 72 36 44 1
3 2 1 1 0
In the first test case, for $$$x = 6$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 9$$$, and they are $$$4$$$, $$$5$$$, and $$$7$$$.
In the second test case, for $$$x = 5$$$, there are $$$2$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 7$$$, and they are $$$4$$$ and $$$6$$$.
This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$x \oplus y$$$ is divisible$$$^{\text{∗}}$$$ by either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator.
$$$^{\text{∗}}$$$The number $$$a$$$ is divisible by the number $$$b$$$ if there exists an integer $$$c$$$ such that $$$a = b \cdot c$$$.
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 two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$).
It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$.
For each test case, print a single integer — the number of suitable $$$y$$$.
57 102 36 41 64 1
3 2 2 6 1
In the first test case, for $$$x = 7$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 10$$$, and they are $$$1$$$, $$$7$$$, and $$$9$$$.
Shohag has an integer $$$n$$$ and a set $$$S$$$ of $$$m$$$ unique integers. Help him find the lexicographically largest$$$^{\text{∗}}$$$ integer array $$$a_1, a_2, \ldots, a_n$$$ such that $$$a_i \in S$$$ for each $$$1 \le i \le n$$$ and $$$a_{\operatorname{gcd}(i, j)} \neq \operatorname{gcd}(a_i, a_j)$$$$$$^{\text{†}}$$$ is satisfied over all pairs $$$1 \le i \lt j \le n$$$, or state that no such array exists.
$$$^{\text{∗}}$$$An array $$$a$$$ is lexicographically larger than an array $$$b$$$ of the same length if $$$a \ne b$$$, and in the first position where $$$a$$$ and $$$b$$$ differ, the array $$$a$$$ has a larger element than the corresponding element in $$$b$$$.
$$$^{\text{†}}$$$$$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.
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 $$$m$$$ ($$$1 \le m \le n \le 10^5$$$).
The second line contains $$$m$$$ unique integers in increasing order, representing the elements of the set $$$S$$$ ($$$1 \le x \le n$$$ for each $$$x \in S$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, if there is no solution print $$$-1$$$, otherwise print $$$n$$$ integers — the lexicographically largest integer array that satisfies the conditions.
36 33 4 61 112 12
6 4 4 3 4 3 1 -1
In the first test case, every element in the array belongs to the given set $$$S = \{3, 4, 6\}$$$, and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair $$$(2, 3)$$$, $$$a_{\operatorname{gcd}(2, 3)} = a_1 = 6$$$ and $$$\operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4$$$, so they are not equal. There are other arrays that satisfy the conditions as well but this one is the lexicographically largest among them.
In the third test case, there is no solution possible because we are only allowed to use $$$a = [2, 2]$$$ but for this array, for pair $$$(1, 2)$$$, $$$a_{\operatorname{gcd}(1, 2)} = a_1 = 2$$$ and $$$\operatorname{gcd}(a_1, a_2) = \operatorname{gcd}(2, 2) = 2$$$, so they are equal which is not allowed!
Shohag has an array $$$a$$$ of integers. Initially $$$a = [0, 1]$$$. He can repeatedly perform the following operation any number of times:
For example, if $$$a = [4, 6, 2, 4]$$$, then the number of inversions is $$$k = 3$$$. So Shohag can obtain the following arrays after the operation: $$$[\textbf{3}, 4, 6, 2, 4]$$$, $$$[4, \textbf{3}, 6, 2, 4]$$$, $$$[4, 6, \textbf{3}, 2, 4]$$$, $$$[4, 6, 2, \textbf{3}, 4]$$$, and $$$[4, 6, 2, 4, \textbf{3}]$$$.
Given an integer $$$n$$$, help Shohag count, modulo $$$998\,244\,353$$$, the number of distinct arrays of length $$$n$$$ that can be obtained after performing the operations.
$$$^{\text{∗}}$$$The number of inversions in an array $$$a$$$ is the number of pairs of indices ($$$i$$$, $$$j$$$) such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$.
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 $$$n$$$ ($$$2 \le n \le 10^6$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output an integer — the number of possible arrays modulo $$$998\,244\,353$$$.
442769
5 1 682 325188814
In the first test case, the following $$$5$$$ arrays can be obtained (the inserted inversion count is shown in bold):
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$$$.
$$$^{\text{∗}}$$$An array $$$d$$$ is a subarray of an array $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
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$$$.
For each test case, output an integer — the number of valid arrays modulo $$$998\,244\,353$$$.
3259
4 29 165
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:
This is the hard 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$$$.
$$$^{\text{∗}}$$$An array $$$d$$$ is a subarray of an array $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 3 \cdot 10^5$$$) — the number of test cases.
The first and only line of each test case contains an integer $$$m$$$ ($$$1 \le m \le 10^6$$$).
Note that there is no limit on the sum of $$$m$$$ over all test cases.
For each test case, output an integer — the number of valid arrays modulo $$$998\,244\,353$$$.
3259
4 29 165
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:
Shohag has a tree with $$$n$$$ nodes.
Pebae has an integer $$$m$$$. She wants to assign each node a value — an integer from $$$1$$$ to $$$m$$$. So she asks Shohag to count the number, modulo $$$998\,244\,353$$$, of assignments such that following conditions are satisfied:
But this problem is too hard for Shohag to solve. As Shohag loves Pebae, he has to solve the problem. Please save Shohag!
The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 10^6$$$, $$$1 \le m \le 10^{9}$$$).
Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) indicating there is an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree.
Print a single integer — the number of valid ways to assign each vertex a value, modulo $$$998\,244\,353$$$.
6 61 22 33 44 53 6
2
2 51 2
7
12 693 51 42 34 55 68 97 34 89 101 1112 1
444144548
In the first test case, the valid assignments are $$$[1, 1, 1, 1, 1, 1]$$$ and $$$[1, 1, 1, 1, 1, 5]$$$.
In the second test case, the valid assignments are $$$[1, 1]$$$, $$$[1, 3]$$$, $$$[1, 5]$$$, $$$[3, 1]$$$, $$$[3, 5]$$$, $$$[5, 1]$$$ and $$$[5, 3]$$$.
This is the easy version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.
You are given an array $$$a$$$ of size $$$n$$$.
A cool swap walk is the following process:
You can perform at most $$$2n+4$$$ cool swap walks. Sort the array $$$a_1, a_2, \ldots, a_n$$$ in non-decreasing order. We can show that it's always possible to do so.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 500$$$) — 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 n$$$) — the elements of the array.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2.5 \cdot 10^5$$$.
For each test case, your output should consist of several lines:
321 232 1 343 2 3 4
0 2 RRDD DRDR 3 RRDRDD DRDDRR DDRRRD
In the first test case, the array $$$a$$$ is already non-decreasing, so you don't need to perform any walk.
In the second test case, $$$a=[2,1,3]$$$ initially.
In the first walk:
In the second walk:
After the two cool swap walks above, we get $$$a=[1,2,3]$$$, which is non-decreasing.
This is the hard version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.
You are given an array $$$a$$$ of size $$$n$$$.
A cool swap walk is the following process:
You can perform at most $$$n+4$$$ cool swap walks. Sort the array $$$a_1, a_2, \ldots, a_n$$$ in non-decreasing order. We can show that it's always possible to do so.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 500$$$) — 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 n$$$) — the elements of the array.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2.5 \cdot 10^5$$$.
For each test case, your output should consist of several lines:
321 232 1 343 2 3 4
0 2 RRDD DRDR 3 RRDRDD DRDDRR DDRRRD
In the first test case, the array $$$a$$$ is already non-decreasing, so you don't need to perform any walk.
In the second test case, $$$a=[2,1,3]$$$ initially.
In the first walk:
In the second walk:
After the two cool swap walks above, we get $$$a=[1,2,3]$$$, which is non-decreasing.