Codeforces Round 865 (Div. 2)
A. Ian Visits Mary
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ian and Mary are frogs living on lattice points of the Cartesian coordinate plane, with Ian living on $$$(0,0)$$$ and Mary living on $$$(a,b)$$$.

Ian would like to visit Mary by jumping around the Cartesian coordinate plane. Every second, he jumps from his current position $$$(x_p, y_p)$$$ to another lattice point $$$(x_q, y_q)$$$, such that no lattice point other than $$$(x_p, y_p)$$$ and $$$(x_q, y_q)$$$ lies on the segment between point $$$(x_p, y_p)$$$ and point $$$(x_q, y_q)$$$.

As Ian wants to meet Mary as soon as possible, he wants to jump towards point $$$(a,b)$$$ using at most $$$2$$$ jumps. Unfortunately, Ian is not good at maths. Can you help him?

A lattice point is defined as a point with both the $$$x$$$-coordinate and $$$y$$$-coordinate being integers.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of test cases follows.

The first and only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1\le a,b\le 10^9$$$) — the coordinates of the lattice point where Mary lives.

Output

For each test case, print an integer $$$n$$$ ($$$1 \le n \le 2$$$) on the first line, denoting the number of jumps Ian uses in order to meet Mary. Note that you do not need to minimize the number of jumps.

On the $$$i$$$-th line of the next $$$n$$$ lines, print two integers $$$0 \le x_i,y_i \le 10^9$$$ separated by a space, denoting Ian's location $$$(x_i,y_i)$$$ after the $$$i$$$-th jump. $$$x_n = a$$$, $$$y_n = b$$$ must hold.

Ian's initial location and his locations after each of the $$$n$$$ jumps need not be distinct.

If there are multiple solutions, output any.

Example
Input
8
3 4
4 4
3 6
2 2
1 1
7 3
2022 2023
1000000000 1000000000
Output
1
3 4
2
3 2
4 4
2
5 3
3 6
2
1 0
2 2
1
1 1
1
7 3
1
2022 2023
2
69420420 469696969
1000000000 1000000000
Note

In the first test case:

$$$(0,0) \to (3,4)$$$

In the second test case:

$$$(0,0) \to (3,2) \to (4,4)$$$

In the third test case:

$$$(0,0) \to (5,3) \to (3,6)$$$

B. Grid Reconstruction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Consider a $$$2 \times n$$$ grid, where $$$n$$$ is an even integer. You may place the integers $$$1, 2, \ldots, 2n$$$ on the grid, using each integer exactly once.

A path is a sequence of cells achieved by starting at $$$(1, 1)$$$, then repeatedly walking either downwards or to the right, and stopping when $$$(2, n)$$$ is reached. The path should not extend beyond the grid.

The cost of a path is the alternating sum of the numbers written on the cells in a path. That is, let the numbers written on the cells be $$$a_1, a_2, \ldots, a_k$$$ (in the order that it is visited), the cost of the path is $$$a_1 - a_2 + a_3 - a_4 + \ldots = \sum_{i=1}^k a_i \cdot (-1)^{i+1}$$$.

Construct a way to place the integers $$$1, 2, \ldots, 2n$$$ on the grid, such that the minimum cost over all paths from $$$(1, 1)$$$ to $$$(2, n)$$$ is maximized. If there are multiple such grids that result in the maximum value, output any of them.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of test cases follows.

The first and the only line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$, $$$n$$$ is even) — the number of the columns in the grid.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output

For each test case, output $$$2$$$ lines, each containing $$$n$$$ integers — the desired grid. If there are multiple solutions, output any of them.

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

In the first test case, there are only two paths from cell $$$(1, 1)$$$ to cell $$$(2, 2)$$$. Their costs are $$$3-1+4=6$$$ and $$$3-2+4=5$$$. Then the minimum cost is $$$5$$$, which is the maximum possible value.

In the second test case, there are four paths from cell $$$(1, 1)$$$ to cell $$$(2, 4)$$$. Their costs are $$$8-1+5-3+7=16$$$, $$$8-2+5-3+7=15$$$, $$$8-2+6-3+7=16$$$, and $$$8-2+6-4+7=15$$$. Then the minimum value is $$$15$$$, which is the maximum possible value.

C. Ian and Array Sorting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

To thank Ian, Mary gifted an array $$$a$$$ of length $$$n$$$ to Ian. To make himself look smart, he wants to make the array in non-decreasing order by doing the following finitely many times: he chooses two adjacent elements $$$a_i$$$ and $$$a_{i+1}$$$ ($$$1\le i\le n-1$$$), and increases both of them by $$$1$$$ or decreases both of them by $$$1$$$. Note that, the elements of the array can become negative.

As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows.

The first line of each test case consists of a single integer $$$n$$$ ($$$2\le n\le 3\cdot10^5$$$) — the number of elements in 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$$$) — the elements of the array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot10^5$$$.

Output

For each test case, output "YES" if there exists a sequence of operations which make the array non-decreasing, else output "NO".

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

Example
Input
5
3
1 3 2
2
2 1
4
1 3 5 7
4
2 1 4 3
5
5 4 3 2 1
Output
YES
NO
YES
NO
YES
Note

For the first test case, we can increase $$$a_2$$$ and $$$a_3$$$ both by $$$1$$$. The array is now $$$[1, 4, 3]$$$.

Then we can decrease $$$a_1$$$ and $$$a_2$$$ both by $$$1$$$. The array is now $$$[0, 3, 3]$$$, which is sorted in non-decreasing order. So the answer is "YES".

For the second test case, no matter how Ian perform the operations, $$$a_1$$$ will always be larger than $$$a_2$$$. So the answer is "NO" and Ian cannot pretend to be smart.

For the third test case, the array is already in non-decreasing order, so Ian does not need to do anything.

D. Sum Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

There is a hidden permutation $$$p_1, p_2, \dots, p_n$$$.

Consider an undirected graph with $$$n$$$ nodes only with no edges. You can make two types of queries:

  1. Specify an integer $$$x$$$ satisfying $$$2 \le x \le 2n$$$. For all integers $$$i$$$ ($$$1 \le i \le n$$$) such that $$$1 \le x-i \le n$$$, an edge between node $$$i$$$ and node $$$x-i$$$ will be added.
  2. Query the number of edges in the shortest path between node $$$p_i$$$ and node $$$p_j$$$. As the answer to this question you will get the number of edges in the shortest path if such a path exists, or $$$-1$$$ if there is no such path.

Note that you can make both types of queries in any order.

Within $$$2n$$$ queries (including type $$$1$$$ and type $$$2$$$), guess two possible permutations, at least one of which is $$$p_1, p_2, \dots, p_n$$$. You get accepted if at least one of the permutations is correct. You are allowed to guess the same permutation twice.

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 a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^3$$$) — the length of the permutation.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

Interaction

The interaction for each test case begins by reading the integer $$$n$$$.

Then, make at most $$$2n$$$ queries:

  • If you want to make a type $$$1$$$ query, output "+ x". $$$x$$$ must be an integer between $$$2$$$ and $$$2n$$$ inclusive. After doing that read $$$1$$$ or $$$-2$$$. If you read $$$1$$$ your query was valid, otherwise it was invalid or you exceed the limit of queries, and your program must terminate immediately to receive a Wrong Answer verdict.
  • If you want to make a type $$$2$$$ query, output "? i j". $$$i$$$ and $$$j$$$ must be integers between $$$1$$$ and $$$n$$$ inclusive. After that, read in a single integer $$$r$$$ ($$$-1 \le r \le n$$$) — the answer to your query. If you receive the integer $$$−2$$$ instead of an answer, it means your program has made an invalid query, or has exceeded the limit of queries. Your program must terminate immediately to receive a Wrong Answer verdict.

At any point of the interaction, if you want to guess two permutations, output "! $$$p_{1,1}$$$ $$$p_{1,2}$$$ $$$\dots$$$ $$$p_{1,n}$$$ $$$p_{2,1}$$$ $$$p_{2,2}$$$ $$$\dots$$$ $$$p_{2,n}$$$". Note that you should output the two permutations on the same line, and no exclamation mark is needed to separate the two permutations. After doing that read $$$1$$$ or $$$-2$$$. If you read $$$1$$$ your answer was correct, otherwise it was incorrect and your program must terminate immediately to receive a Wrong Answer verdict. After that, move on to the next test case, or terminate the program if there are none. Note that reporting the answer does not count as a query.

Note that even if you output a correct permutation, the second permutation should be a permutation and not an arbitrary array.

At any point, if you continue interaction after reading in the integer $$$-2$$$, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query or the answer 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.

Interactor is non-adaptive. This means that all permutations are fixed before the interaction starts.

Hacks

To make a hack, use the following format.

The first line should contain a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.

The first line of each test case should contain a single integer $$$n$$$ ($$$2 \le n \le 10^3$$$) — the length of the permutation.

The second line of each test case should contain $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the hidden permutation.

The sum of $$$n$$$ over all test cases should not exceed $$$10^3$$$.

Example
Input
2
6

1

1

1

1

1

2

-1

1
2

1
Output


+ 12

+ 2

+ 3

? 1 3

+ 5

? 1 5

? 4 5

! 1 4 2 5 3 6 1 2 3 4 5 6


! 1 2 2 1

Note

In the first test case, $$$n=6$$$ and the hidden permutation $$$p = [1,4,2,5,3,6]$$$.

Firstly, make a type $$$1$$$ query on $$$x=12, 2, 3$$$ respectively. This adds four edges to the graph in total:

  • An edge that connects node $$$6$$$ and node $$$6$$$.
  • An edge that connects node $$$1$$$ and node $$$1$$$.
  • An edge that connects node $$$1$$$ and node $$$2$$$.
  • An edge that connects node $$$2$$$ and node $$$1$$$.

Since all of these queries are valid, the interactor returns $$$1$$$ after each of them.

Then, query the number of edges in the shortest path between node $$$p_1 = 1$$$ and $$$p_3 = 2$$$, which is equal to $$$1$$$.

Then, make a type $$$1$$$ query on $$$x=5$$$. This adds four edges to the graph in total:

  • An edge that connects node $$$1$$$ and node $$$4$$$.
  • An edge that connects node $$$2$$$ and node $$$3$$$.
  • An edge that connects node $$$3$$$ and node $$$2$$$.
  • An edge that connects node $$$4$$$ and node $$$1$$$.

Since this query is valid, the interactor returns $$$1$$$.

Then, query the number of edges in the shortest path between node $$$p_1 = 1$$$ and $$$p_5 = 3$$$, which is equal to $$$2$$$.

Then, query the number of edges in the shortest path between node $$$p_4 = 5$$$ and $$$p_5 = 3$$$. Such a path doesn't exist, therefore the interactor returns $$$-1$$$.

Afterwards, due to some magic, two possible permutations that can be $$$p$$$ are determined: the first permutation is $$$[1,4,2,5,3,6]$$$ and the second permutation is $$$[1,2,3,4,5,6]$$$. Since the first permutation is equal to the hidden permutation, this test case is solved correctly. In total, $$$7$$$ queries are used, which is within the limit of $$$2 \cdot 6 = 12$$$ queries.

Since the answer is correct, the interactor returns $$$1$$$.

In the second test case, $$$n=2$$$ and the hidden permutation is $$$p = [2,1]$$$.

Since there are only $$$2! = 2$$$ possible permutations, no queries are needed. It is sufficient to just output the two permutations, $$$[1,2]$$$ and $$$[2,1]$$$. In total, $$$0$$$ queries are used, which is within the limit of $$$2 \cdot 2 = 4$$$ queries.

Since the answer is correct, the interactor returns $$$1$$$.

E. Between
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer $$$n$$$, as well as $$$m$$$ pairs of integers $$$(a_i,b_i)$$$, where $$$1\leq a_i , b_i \leq n$$$, $$$a_i \ne b_i$$$.

You want to construct a sequence satisfying the following requirements:

  • All elements in the sequence are integers between $$$1$$$ and $$$n$$$.
  • There is exactly one element with value $$$1$$$ in the sequence.
  • For each $$$i$$$ ($$$1 \le i \le m$$$), between any two elements (on different positions) in the sequence with value $$$a_i$$$, there is at least one element with value $$$b_i$$$.
  • The sequence constructed has the maximum length among all possible sequences satisfying the above properties.

Sometimes, it is possible that such a sequence can be arbitrarily long, in which case you should output "INFINITE". Otherwise, you should output "FINITE" and the sequence itself. If there are multiple possible constructions that yield the maximum length, output any.

Input

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 300$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 1500$$$, $$$0 \le m \le 5000$$$) — the maximum possible value of the element of the sequence and the number of pairs.

The $$$i$$$-th of the next $$$m$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i , b_i \le n$$$, $$$a_i \ne b_i$$$).

$$$(a_i, b_i) \ne (a_j, b_j)$$$ for all $$$1 \le i \lt j \le m$$$.

It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$1500$$$ and the sum of $$$m$$$ over all test cases doesn't exceed $$$5000$$$.

Output

For each test case, on the first line output "INFINITE" if the sequence can be arbitrarily long, and "FINITE" otherwise.

If you have outputted "FINITE", then your output should be followed by $$$2$$$ lines.

The first line contains an integer $$$s$$$, the maximum length of the sequence.

The second line contains $$$s$$$ integers, each are between $$$1$$$ and $$$n$$$ inclusive, representing the elements of the sequence.

If there are multiple sequences with the maximum length, output any of them.

It can be shown that, for all test cases with answer "FINITE", then under the constraints, the maximum possible sum of sequence lengths of those test cases does not exceed $$$2\cdot10^6$$$.

Example
Input
5
3 2
3 1
2 1
1 0
2 0
2 2
1 2
2 1
5 5
2 1
3 1
4 2
4 5
5 1
Output
FINITE
5
2 3 1 2 3 
FINITE
1
1 
INFINITE
FINITE
3
2 1 2 
FINITE
10
4 2 3 5 4 1 3 2 5 4 
Note

In the first test case, there is an element $$$1$$$ between two elements with value $$$3$$$ and an element $$$1$$$ between two elements with value $$$2$$$. It can be shown that there is no suitable sequences of length more than $$$5$$$.

In the second case, $$$[1]$$$ is the only possible sequence because there should be exactly one element with value $$$1$$$ in the sequence.

In the third case, we can get an arbitrary long sequence like $$$1, 2, 2, 2, \ldots$$$.

F. XOR Counting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given two positive integers $$$n$$$ and $$$m$$$. Find the sum of all possible values of $$$a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m$$$, where $$$a_1,a_2,\ldots,a_m$$$ are non-negative integers such that $$$a_1+a_2+\ldots+a_m=n$$$.

Note that all possible values $$$a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m$$$ should be counted in the sum exactly once.

As the answer may be too large, output your answer modulo $$$998244353$$$.

Here, $$$\bigoplus$$$ denotes the bitwise XOR operation.

Input

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows.

The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$0\le n\le 10^{18}, 1\le m\le 10^5$$$) — the sum and the number of integers in the set, respectively.

Output

For each test case, output the sum of all possible values of $$$a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_m$$$ among all non-negative integers $$$a_1,a_2,\ldots,a_m$$$ with $$$a_1+a_2+\ldots+a_m=n$$$. As the answer may be too large, output your answer modulo $$$998244353$$$.

Example
Input
7
69 1
5 2
0 10
420 69
12 26
73 34
1000000000000000000 10
Output
69
6
0
44310
42
1369
216734648
Note

For the first test case, we must have $$$a_1=69$$$, so it's the only possible value of $$$a_1$$$, therefore our answer is $$$69$$$.

For the second test case, $$$(a_1,a_2)$$$ can be $$$(0,5), (1,4), (2,3), (3,2), (4,1)$$$ or $$$(5,0)$$$, in which $$$a_1\bigoplus a_2$$$ are $$$5,5,1,1,5,5$$$ respectively. So $$$a_1\bigoplus a_2$$$ can be $$$1$$$ or $$$5$$$, therefore our answer is $$$1+5=6$$$.

For the third test case, $$$a_1,a_2,\ldots,a_{10}$$$ must be all $$$0$$$, so $$$a_1\bigoplus a_2\bigoplus\ldots\bigoplus a_{10}=0$$$. Therefore our answer is $$$0$$$.