Codeforces Round 889 (Div. 2)
A. Dalton the Teacher
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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$$$.

Output

For each test case, output the minimum number of moves required.

Example
Input
5
2
2 1
3
1 2 3
5
1 2 5 4 3
4
1 2 4 3
10
10 2 1 3 6 5 4 7 9 8
Output
0
2
2
1
1
Note

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.

B. Longest Divisors Interval
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

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}$$$).

Output

For each test case, print a single integer: the maximum size of a valid interval.

Example
Input
10
1
40
990990
4204474560
169958913706572972
365988220345828080
387701719537826430
620196883578129853
864802341280805662
1000000000000000000
Output
1
2
3
6
4
22
3
1
2
2
Note

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]$$$.

C1. Dual (Easy Version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Example
Input
10
2
2 1
4
1 2 -10 3
5
2 1 1 1 1
8
0 0 0 0 0 0 0 0
5
1 2 -4 3 -10
10
11 12 13 14 15 -15 -16 -17 -18 -19
7
1 9 3 -4 -3 -2 -1
3
10 9 8
20
1 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 8
20
-15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17
Output
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
Note

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:

  • $$$[1, 2, -10, 3]$$$
  • $$$[1, 2, -10, 6]$$$
  • $$$[1, 2, -10, 12]$$$
  • $$$[1, 2, 2, 12]$$$

In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.

C2. Dual (Hard Version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

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.

Example
Input
10
2
2 1
4
1 2 -10 3
5
2 1 1 1 1
8
0 0 0 0 0 0 0 0
5
1 2 -4 3 -10
10
11 12 13 14 15 -15 -16 -17 -18 -19
7
1 9 3 -4 -3 -2 -1
3
10 9 8
20
1 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 8
20
-15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17
Output
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
Note

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:

  • $$$[1, 2, -10, 3]$$$
  • $$$[1, 2, -10, 6]$$$
  • $$$[1, 2, -10, 12]$$$
  • $$$[1, 2, 2, 12]$$$

In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.

D. Earn or Unlock
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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:

  1. Unlock the first $$$v$$$ locked cards in the deck from the top. If there are less than $$$v$$$ locked cards in the deck, then unlock all the locked cards.
  2. Earn $$$v$$$ victory points.
In both cases, after performing the operation, he removes the card from the deck.

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?

Input

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

Output a single integer — the maximum number of victory points Andrea can earn.

Examples
Input
2
1 2
Output
2
Input
5
2 4 5 0 1
Output
9
Input
4
0 4 4 4
Output
0
Note

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.

E. Expected Destruction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a set $$$S$$$ of $$$n$$$ distinct integers between $$$1$$$ and $$$m$$$.

Each second you do the following steps:

  1. Pick an element $$$x$$$ in $$$S$$$ uniformly at random.
  2. Remove $$$x$$$ from $$$S$$$.
  3. If $$$x+1 \leq m$$$ and $$$x+1$$$ is not in $$$S$$$, add $$$x+1$$$ to $$$S$$$.

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}$$$.

Input

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

Output a single integer — the expected number of seconds until $$$S$$$ is empty, modulo $$$1\,000\,000\,007$$$.

Examples
Input
2 3
1 3
Output
750000009
Input
5 10
1 2 3 4 5
Output
300277731
Input
5 10
2 3 6 8 9
Output
695648216
Input
1 100
1
Output
100
Note

For test 1, here is a list of all the possible scenarios and their probabilities:

  1. $$$[1, 3]$$$ (50% chance) $$$\to$$$ $$$[1]$$$ $$$\to$$$ $$$[2]$$$ $$$\to$$$ $$$[3]$$$ $$$\to$$$ $$$[]$$$
  2. $$$[1, 3]$$$ (50% chance) $$$\to$$$ $$$[2, 3]$$$ (50% chance) $$$\to$$$ $$$[2]$$$ $$$\to$$$ $$$[3]$$$ $$$\to$$$ $$$[]$$$
  3. $$$[1, 3]$$$ (50% chance) $$$\to$$$ $$$[2, 3]$$$ (50% chance) $$$\to$$$ $$$[3]$$$ $$$\to$$$ $$$[]$$$

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}$$$.

F. Michael and Hotel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The input contains a single integer $$$n$$$ ($$$2 \leq n \leq 500$$$).

Interaction

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:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

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$$$.

Example
Input
5

0

1
Output
? 3 5 2 2 3

? 2 5 2 2 3

! 3 1 3 4
Note

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]$$$.

  • The first query asks whether starting from room number $$$a=3$$$, and using the teleporters $$$5$$$ times, one ends up in one of the two rooms $$$S=\{2, 3\}$$$. This action results in ending up in the room $$$1$$$, so the answer is $$$0$$$.
  • The second query asks whether starting from room number $$$a=2$$$, and using the teleporters $$$5$$$ times, one ends up in one of the two rooms $$$S=\{2, 3\}$$$. This action results in ending up in the room $$$2$$$, so the answer is $$$1$$$.