Dalton is the teacher of a class with $$$n$$$ students, numbered from $$$1$$$ to $$$n$$$. The classroom contains $$$n$$$ chairs, also numbered from $$$1$$$ to $$$n$$$. Initially student $$$i$$$ is seated on chair $$$p_i$$$. It is guaranteed that $$$p_1,p_2,\dots, p_n$$$ is a permutation of length $$$n$$$.
A student is happy if his/her number is different from the number of his/her chair. In order to make all of his students happy, Dalton can repeatedly perform the following operation: choose two distinct students and swap their chairs. What is the minimum number of moves required to make all the students happy? One can show that, under the constraints of this problem, it is possible to make all the students happy with a finite number of moves.
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).
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 contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of students.
The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — $$$p_i$$$ denotes the initial chair of student $$$i$$$. It is guaranteed that $$$p$$$ is a permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output the minimum number of moves required.
522 131 2 351 2 5 4 341 2 4 31010 2 1 3 6 5 4 7 9 8
0 2 2 1 1
In the first test case, both students are already happy, so Dalton can perform $$$0$$$ moves.
In the second test case, Dalton can swap the chairs of students $$$1$$$ and $$$2$$$ to get the array $$$[2, 1, 3]$$$. Then he can swap chairs of students $$$2$$$ and $$$3$$$ to get the array $$$[2, 3, 1]$$$. At this point all the students are happy, and he performed $$$2$$$ moves. It is impossible to perform the task with fewer moves.
In the third test case, by swapping the chairs of students $$$1$$$ and $$$2$$$ and then swapping the chairs of students $$$4$$$ and $$$5$$$, Dalton gets the array $$$[2, 1, 5, 3, 4]$$$ in $$$2$$$ moves.
Given a positive integer $$$n$$$, find the maximum size of an interval $$$[l, r]$$$ of positive integers such that, for every $$$i$$$ in the interval (i.e., $$$l \leq i \leq r$$$), $$$n$$$ is a multiple of $$$i$$$.
Given two integers $$$l\le r$$$, the size of the interval $$$[l, r]$$$ is $$$r-l+1$$$ (i.e., it coincides with the number of integers belonging to the interval).
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The only line of the description of each test case contains one integer $$$n$$$ ($$$1 \leq n \leq 10^{18}$$$).
For each test case, print a single integer: the maximum size of a valid interval.
1014099099042044745601699589137065729723659882203458280803877017195378264306201968835781298538648023412808056621000000000000000000
1 2 3 6 4 22 3 1 2 2
In the first test case, a valid interval with maximum size is $$$[1, 1]$$$ (it's valid because $$$n = 1$$$ is a multiple of $$$1$$$) and its size is $$$1$$$.
In the second test case, a valid interval with maximum size is $$$[4, 5]$$$ (it's valid because $$$n = 40$$$ is a multiple of $$$4$$$ and $$$5$$$) and its size is $$$2$$$.
In the third test case, a valid interval with maximum size is $$$[9, 11]$$$.
In the fourth test case, a valid interval with maximum size is $$$[8, 13]$$$.
In the seventh test case, a valid interval with maximum size is $$$[327869, 327871]$$$.
The only difference between the two versions of this problem is the constraint on the maximum number of operations. You can make hacks only if all versions of the problem are solved.
You are given an array $$$a_1, a_2,\dots, a_n$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations).
In one operation, you choose $$$i, j$$$ ($$$1 \leq i, j \leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$).
Make the array non-decreasing (i.e., $$$a_i \leq a_{i+1}$$$ for $$$1 \leq i \leq n-1$$$) in at most $$$50$$$ operations. You do not need to minimize the number of operations.
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 contains a single integer $$$n$$$ ($$$1 \le n \le 20$$$) — the length of the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-20 \le a_i \le 20$$$) — the array before performing the operations.
For each test case, output your operations in the following format.
The first line should contain an integer $$$k$$$ ($$$0 \le k \le 50$$$) — the number of operations.
The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$) — the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$.
After all the operations, the array $$$a_1, a_2,\dots, a_n$$$ must be non-decreasing.
1022 141 2 -10 352 1 1 1 180 0 0 0 0 0 0 051 2 -4 3 -101011 12 13 14 15 -15 -16 -17 -18 -1971 9 3 -4 -3 -2 -1310 9 8201 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 820-15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17
1 2 1 3 4 4 4 4 3 4 4 2 1 3 1 4 1 5 1 0 7 3 4 3 4 5 4 5 4 5 4 5 4 5 4 15 6 1 6 1 6 1 7 2 7 2 7 2 8 3 8 3 8 3 9 4 9 4 9 4 10 5 10 5 10 5 8 3 4 3 4 2 4 2 4 2 4 2 4 1 4 1 4 3 2 1 3 1 3 1 31 14 1 18 7 13 11 15 11 6 4 5 17 19 6 19 12 10 5 11 12 1 17 15 19 16 10 14 2 16 11 20 7 7 6 9 5 3 6 6 14 17 18 18 14 12 3 17 16 8 18 13 16 9 8 14 8 16 2 11 8 12 7 31 5 12 19 13 9 1 5 17 18 19 6 16 15 8 6 9 15 14 7 10 19 7 17 20 14 4 15 20 4 3 1 8 16 12 16 15 5 6 12 10 11 15 20 3 20 19 13 14 11 14 18 10 7 3 12 17 4 7 13 2 11 13
In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing.
In the second test case, the array changes as:
In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.
The only difference between the two versions of this problem is the constraint on the maximum number of operations. You can make hacks only if all versions of the problem are solved.
You are given an array $$$a_1, a_2,\dots, a_n$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations).
In one operation, you choose $$$i, j$$$ ($$$1 \leq i, j \leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$).
Make the array non-decreasing (i.e., $$$a_i \leq a_{i+1}$$$ for $$$1 \leq i \leq n-1$$$) in at most $$$31$$$ operations. You do not need to minimize the number of operations.
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 contains a single integer $$$n$$$ ($$$1 \le n \le 20$$$) — the length of the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-20 \le a_i \le 20$$$) — the array before performing the operations.
For each test case, output your operations in the following format.
The first line should contain an integer $$$k$$$ ($$$0 \le k \le 31$$$) — the number of operations.
The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$) — the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$.
After all the operations, the array $$$a_1, a_2,\dots, a_n$$$ must be non-decreasing.
1022 141 2 -10 352 1 1 1 180 0 0 0 0 0 0 051 2 -4 3 -101011 12 13 14 15 -15 -16 -17 -18 -1971 9 3 -4 -3 -2 -1310 9 8201 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 820-15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17
1 2 1 3 4 4 4 4 3 4 4 2 1 3 1 4 1 5 1 0 7 3 4 3 4 5 4 5 4 5 4 5 4 5 4 15 6 1 6 1 6 1 7 2 7 2 7 2 8 3 8 3 8 3 9 4 9 4 9 4 10 5 10 5 10 5 8 3 4 3 4 2 4 2 4 2 4 2 4 1 4 1 4 3 2 1 3 1 3 1 31 14 1 18 7 13 11 15 11 6 4 5 17 19 6 19 12 10 5 11 12 1 17 15 19 16 10 14 2 16 11 20 7 7 6 9 5 3 6 6 14 17 18 18 14 12 3 17 16 8 18 13 16 9 8 14 8 16 2 11 8 12 7 31 5 12 19 13 9 1 5 17 18 19 6 16 15 8 6 9 15 14 7 10 19 7 17 20 14 4 15 20 4 3 1 8 16 12 16 15 5 6 12 10 11 15 20 3 20 19 13 14 11 14 18 10 7 3 12 17 4 7 13 2 11 13
In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing.
In the second test case, the array changes as:
In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.
Andrea is playing the game Tanto Cuore.
He has a deck of $$$n$$$ cards with values $$$a_1, \ldots, a_n$$$ from top to bottom. Each card can be either locked or unlocked. Initially, only the topmost card is unlocked.
The game proceeds in turns. In each turn, Andrea chooses an unlocked card in the deck — the value written on the card is $$$v$$$ — and performs exactly one of the following two operations:
The game ends when all the cards remaining in the deck are locked, or there are no more cards in the deck.
What is the maximum number of victory points Andrea can earn?
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of cards in the deck.
The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_1, \ldots, a_n \leq n$$$) — the values of the cards in the deck.
Output a single integer — the maximum number of victory points Andrea can earn.
2 1 2
2
5 2 4 5 0 1
9
4 0 4 4 4
0
In the first sample test, the deck starts as [unlocked, locked]. Then, Andrea uses the first card to unlock the second card. Then he uses the second card to earn $$$2$$$ victory points.
In the second sample test, Andrea can use the first card to unlock the second and the third card. Then he uses the second and the third card to earn $$$4+5=9$$$ victory points.
In the third sample test, Andrea can't unlock any cards or gain any victory points with the first card.
You have a set $$$S$$$ of $$$n$$$ distinct integers between $$$1$$$ and $$$m$$$.
Each second you do the following steps:
What is the expected number of seconds until $$$S$$$ is empty?
Output the answer modulo $$$1\,000\,000\,007$$$.
Formally, let $$$P = 1\,000\,000\,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{a}{b}$$$, where $$$a$$$ and $$$b$$$ are integers and $$$b \not \equiv 0 \pmod{P}$$$. Output the integer equal to $$$a \cdot b^{-1} \bmod P$$$. In other words, output an integer $$$z$$$ such that $$$0 \le z \lt P$$$ and $$$z \cdot b \equiv a \pmod{P}$$$.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq m \leq 500$$$) — the number of elements in the set $$$S$$$ and the upper bound on the value of the elements in $$$S$$$.
The second line contains $$$n$$$ integers $$$S_1,\,S_2,\,\dots,\,S_n$$$ ($$$1 \leq S_1 \lt S_2 \lt \ldots \lt S_n \leq m$$$) — the elements of the set $$$S$$$.
Output a single integer — the expected number of seconds until $$$S$$$ is empty, modulo $$$1\,000\,000\,007$$$.
2 3 1 3
750000009
5 10 1 2 3 4 5
300277731
5 10 2 3 6 8 9
695648216
1 100 1
100
For test 1, here is a list of all the possible scenarios and their probabilities:
Adding them up, we get $$$\frac{1}{2}\cdot 4 + \frac{1}{4} \cdot 4 + \frac{1}{4} \cdot 3 = \frac{15}{4}$$$. We see that $$$750000009 \cdot 4 \equiv 15 \pmod{1\,000\,000\,007}$$$.
Michael and Brian are stuck in a hotel with $$$n$$$ rooms, numbered from $$$1$$$ to $$$n$$$, and need to find each other. But this hotel's doors are all locked and the only way of getting around is by using the teleporters in each room. Room $$$i$$$ has a teleporter that will take you to room $$$a_i$$$ (it might be that $$$a_i = i$$$). But they don't know the values of $$$a_1,a_2, \dots, a_n$$$.
Instead, they can call up the front desk to ask queries. In one query, they give a room $$$u$$$, a positive integer $$$k$$$, and a set of rooms $$$S$$$. The hotel concierge answers whether a person starting in room $$$u$$$, and using the teleporters $$$k$$$ times, ends up in a room in $$$S$$$.
Brian is in room $$$1$$$. Michael wants to know the set $$$A$$$ of rooms so that if he starts in one of those rooms they can use the teleporters to meet up. He can ask at most $$$2000$$$ queries.
The values $$$a_1, a_2, \dots, a_n$$$ are fixed before the start of the interaction and do not depend on your queries. In other words, the interactor is not adaptive.
The input contains a single integer $$$n$$$ ($$$2 \leq n \leq 500$$$).
To ask a query, print a line in the format "? u k |S| S_1 S_2 ... S_|S|" with $$$1 \leq u, S_1, \ldots, S_{|S|} \leq n$$$, all the $$$S_i$$$ distinct, and $$$1 \leq k \leq 10^9$$$.
As a response to the query you will get "1" if the answer is yes, and "0" if the answer is no.
To output an answer, you should print "! |A| A_1 A_2 ... A_|A|" with $$$1 \leq A_1, \ldots, A_{|A|} \leq n$$$ all distinct. Printing the answer doesn't count as a query.
See the interaction example for more clarity.
If you ask too many queries or you ask a malformed query, you will get Wrong answer.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
Hack format
For the hacks use the following format.
The first line should contain $$$n$$$ — the number of rooms.
The second line should contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ — the teleporter in room $$$i$$$ leads to room $$$a_i$$$.
5 0 1
? 3 5 2 2 3 ? 2 5 2 2 3 ! 3 1 3 4
In the sample test, there are $$$n=5$$$ rooms and the (hidden) array describing the behavior of the teleporters is $$$[1, 2, 1, 3, 2]$$$.