PPSC 2025
A. Reflecting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

March is visiting the beautiful city of Belobog!

The city of Belobog can be modeled through a coordinate system. March is currently at the point $$$(x_1,y_1)$$$ and she would like to get to the point $$$(x_2,y_2)$$$.

Due to technological advancements, most people prefer to teleport instead of walk. March can perform the following operation any number of times:

  • Choose two integers $$$x$$$ and $$$y$$$. Then, she will teleport to the location that is the reflection of her current location across the point $$$(x,y)$$$.

Is it possible to reach $$$(x_2,y_2)$$$ through any number of operations?

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of test cases. Descriptions of the test case follows.

The first line of each test case contains four integers $$$x_1,y_1,x_2,y_2$$$ ($$$-10^9 \leq x_1,x_2,y_1,y_2 \leq 10^9$$$).

Output

For each test case, output YES if it is possible, and NO otherwise. The letters may be outputted in uppercase or lowercase.

Example
Input
5
2 3 2 5
-15 20 -31 24
24 31 93 77
0 0 0 0
1000000000 999999999 -999999999 -1000000000
Output
YES
YES
NO
YES
NO
Note

In the first test case, March can reflect across the point $$$(2,4)$$$ to reach $$$(2,5)$$$ immediately.

In the second test case, March can first teleport through the point $$$(-20, 22)$$$ to reach $$$(-25, 24)$$$, then across the point $$$(-28, 24)$$$ to reach $$$(-31, 24)$$$.

In the third test case, we can show that March cannot reach $$$(93,77)$$$ no matter which operations she use.

B. Numbers on the Blackboard
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Note: You may want to use $$$64$$$-bit integers instead of $$$32$$$-bit integers in this task. For example, in Java, you may want to use long instead of int. In C++, you may want to use long long.

Yunli is writing numbers on the blackboard!

She first writes the number $$$1$$$ one time, then the number $$$2$$$ two times, then the number $$$3$$$ three times, and so on.

Then, Yunli gives you an integer $$$x$$$, and she wants you to tell her how many numbers she must write in this fashion until the sum of all numbers is at least $$$x$$$.

Input

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 an integer $$$x$$$ ($$$1 \leq x \leq 10^{18}$$$).

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
115The sum of $$$x$$$ does not exceed $$$2\cdot 10^5$$$ over all test cases
230$$$x \leq 10^{9}$$$
355No further constraints
Output

For each test case, output the answer on a new line.

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

The first six numbers written are $$$1$$$, $$$2$$$, $$$2$$$, $$$3$$$, $$$3$$$, $$$3$$$.

C. Leafy Distance
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sampo is hiding inside a tunnel system. The tunnel can be modeled as a tree$$$^{\text{∗}}$$$ with $$$n$$$ nodes and $$$n-1$$$ edges. There is an exit situated at every node with exactly one neighbor.

Sampo wants to decide in which node to hide in. Obviously, he should hide in a node that is the furthest distance away from any exit. Please help Sampo determine which node is the best one to hide in. If there are multiple nodes that are equally as far from an exit, output the node with the least index.

$$$^{\text{∗}}$$$A tree is a connected graph without cycles.

Input

The first line contains an integer $$$t$$$ – the number of test cases ($$$1 \leq t \leq 10^4$$$).

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$) – the number of nodes in the tree.

The following $$$n-1$$$ lines each contain two integers $$$p$$$ and $$$q$$$ ($$$1 \leq p, q \leq n, p\neq q$$$) – signifying that there is an edge from $$$p$$$ to $$$q$$$ in the tree. It is guaranteed that the given edges form a tree.

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

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
140The sum of $$$n$$$ does not exceed $$$1500$$$ over all test cases
260No further constraints
Output

For each test case, output the index of the node furthest away from any exit. If there are multiple nodes that are equally far, output the node with the least index.

Example
Input
4
3
1 3
2 3
4
1 2
2 3
4 2
4
1 2
2 3
3 4
13
1 2
2 3
3 4
4 5
4 6
2 7
7 8
2 9
6 10
5 11
11 12
10 13
Output
3
2
2
4
Note
This is the tree in the first test case. Node $$$3$$$ is distance $$$1$$$ away from exits, while nodes $$$1$$$ and $$$2$$$ are distance $$$0$$$ away from exits.

D. Counting Minimal Graphs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Note: You may want to use $$$64$$$-bit integers instead of $$$32$$$-bit integers in this task. For example, in Java, you may want to use long instead of int. In C++, you may want to use long long.

Serval has an undirected unweighted connected graph of $$$n$$$ nodes and $$$m$$$ edges.

To help you guess the structure of the graph, Serval tells you the distance from node $$$1$$$ to node $$$i$$$ for each $$$2 \leq i \leq n$$$ (distance from node $$$1$$$ to node $$$i$$$ is the minimum number of edges in any path from $$$1$$$ to $$$i$$$). Additionally, Serval tells you that the number of edges in the graph is minimized. That is, there does not exist a graph that also satisfies the distance requirements with less edges.

Despite this, there can still be a lot of possible graphs! Please count the number of graphs with minimum number of edges that satisfies the distance requirements. Since the answer can be massive, output it modulo $$$998\,244\,353$$$.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of independent test cases. Descriptions of the test cases follow.

The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2\cdot 10^5$$$) – the number of nodes in the graph.

The following line contains $$$n-1$$$ integers $$$d_2,d_3,\ldots,d_n$$$ ($$$1 \leq d_i \leq n-1$$$) – the distances from node $$$1$$$ to each other node.

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

Output

For each test case, output the number of graphs modulo $$$998\,244\,353$$$.

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

In the second test case, the two graphs are shown below

Note that we cannot have both edges 2 3 and 4 3 as the graph would have $$$4$$$ edges, while the minimal graph has $$$3$$$ edges.

In the last test case, no graph exists.

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

Note: You may want to use $$$64$$$-bit integers instead of $$$32$$$-bit integers in this task. For example, in Java, you may want to use long instead of int. In C++, you may want to use long long.

Sushang has a multiset $$$S$$$ containing $$$n$$$ elements and $$$k$$$ empty arrays $$$a_1,a_2,\ldots,a_k$$$. She will repeat the following operation until $$$S$$$ is empty:

  • Choose an element $$$x$$$ in $$$S$$$ and an integer $$$1\leq i\leq k$$$
  • Remove $$$x$$$ from $$$S$$$ and insert $$$x$$$ at the back of $$$a_i$$$

After that, Sushang calculates the score of each array as follows: if $$$a_i=[x_1,x_2,\ldots,x_m]$$$, then score$$$(a_i)=\sum_{i=1}^mx_i\cdot i=x_1\cdot1+x_2\cdot2+x_3\cdot3+\ldots+x_m\cdot m$$$. The score of an empty array is $$$0$$$.

Sushang wants to minimize the sum of scores over all arrays.

The value of $$$k$$$ is not yet known, so please find the minimum sum of scores possible for $$$k=1,2,\ldots,n$$$ independently.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of independent test cases. The description of test cases follow.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$) – the number of elements in $$$S$$$.

The second line of each test case contains $$$n$$$ integers $$$S_1,S_2,\ldots,S_n$$$ ($$$1 \leq S_i \leq 10^6$$$) – the numbers in the set $$$S$$$ initially.

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

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
150The sum of $$$n$$$ does not exceed $$$3000$$$ over all test cases
250No further constraints
Output

For each test case, output $$$n$$$ numbers on a new line: the answer for $$$k=1,2,\ldots,n$$$.

Example
Input
4
3
1 1 1
3
1 2 3
1
1
8
3 1 4 1 5 9 2 6
Output
6 4 3
10 7 6
1
94 56 44 38 35 33 32 31
Note

In the first test case, when $$$k=3$$$, it is optimal to partition into the arrays $$$[1],[1],[1]$$$. Each array has a score of $$$1$$$.

In the second test case, when $$$k=2$$$, it is optimal to partition into the arrays $$$[3,1]$$$ and $$$[2]$$$. The first array has score $$$3\cdot1+1\cdot2=5$$$ and the second array has score $$$2$$$. The total score is $$$7$$$.

F. Modular Madness
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Tingyun gives you an array $$$a$$$ of size $$$n$$$.

Given an integer $$$x$$$, define functions $$$f_0(x),f_1(x),\ldots,f_n(x)$$$ as follows:

  1. $$$f_0(x)=x$$$
  2. $$$f_k(x)=f_{k-1}(x) \text{ mod } a_k$$$ for any integer $$$k \gt 0$$$.

In other words, $$$f_n(x) = (\ldots((x \text{ mod } a_1)\text{ mod } a_2)\ldots \text{ mod } a_n)$$$.

Note for positive integers $$$a$$$ and $$$b$$$, $$$a \text{ mod } b$$$ is the unique integer $$$0\leq c \lt b$$$ such that $$$a-c$$$ is divisible by $$$b$$$. For example, $$$5 \text{ mod } 3 = 2$$$.

Tingyun then gives you $$$q$$$ values of $$$x$$$. For each $$$x$$$, please output the value of $$$f_n(x)$$$.

Input

The first line contains an integer $$$t$$$ – the number of test cases ($$$1 \leq t \leq 10^4$$$).

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 10^5$$$).

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 next $$$q$$$ lines each contain an integer $$$x$$$ ($$$1 \leq x \leq 10^9$$$).

It is guaranteed that neither the sum of $$$n$$$ nor the sum of $$$q$$$ exceeds $$$10^5$$$ over all test cases.

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
120$$$q=1$$$
240$$$a_i, x \leq 50$$$, sum of $$$q\leq 3\cdot 10^4$$$
340No further constraints
Output

Output the answer for each $$$x$$$ on a new line.

Example
Input
2
3 3
15 13 11
2145
2146
25
5 6
14182 5527 2319 958 724
819248982
912849157
8172857
871267639
126471891
918273752
Output
0
1
10
516
380
24
486
108
157

G. A Counting Problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Note: You may want to use $$$64$$$-bit integers instead of $$$32$$$-bit integers in this task. For example, in Java, you may want to use long instead of int. In C++, you may want to use long long.

Given an array $$$a$$$ of size $$$m$$$, let $$$f(a)$$$ be the number of numbers $$$1\leq i\leq m$$$ where $$$\min(a_1,a_2,\ldots,a_i)=a_i$$$.

For example, if $$$a=[2,4,3,2,1]$$$, $$$f(a)=3$$$ because $$$a_1=\min(a_1)$$$, $$$a_4=\min(a_1,a_2,a_3,a_4)$$$, and $$$a_5=\min(a_1,a_2,a_3,a_4,a_5)$$$.

Seele gives you two integers $$$n$$$ and $$$k$$$.

There are $$$k^n$$$ length-$$$n$$$ arrays such that each element is a positive integer from $$$1$$$ to $$$k$$$, inclusive. Over all such arrays, Seele asks you to output the sum of $$$f(a)$$$ over all such arrays $$$a$$$.

Since the answer may be huge, output the answer modulo $$$998\,244\,353$$$.

Input

The first test case contains an integer $$$t$$$ – the number of independent test cases ($$$1 \leq t \leq 10^4$$$).

The only line of each test case contains two integers $$$n,k$$$ ($$$1 \leq n \leq 10^9, 1 \leq k \leq 3\cdot 10^5$$$).

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

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
110$$$n\leq 7$$$, the sum of $$$k^n$$$ does not exceed $$$1.5\cdot 10^5$$$ over all test cases.
215$$$k=2$$$, sum of $$$n$$$ does not exceed $$$3000$$$ over all test cases.
320$$$k=2$$$, sum of $$$n$$$ does not exceed $$$3\cdot 10^5$$$ over all test cases.
425The sum of $$$n\cdot k$$$ does not exceed $$$10^6$$$ over all test cases.
530No further constraints
Output

For each test case, output the answer modulo $$$998\,244\,353$$$.

Examples
Input
5
2 2
3 2
3 3
5 5
888 999
Output
7
19
59
8479
667852386
Input
1
1000000000 300000
Output
732088305
Note

The first examples satisfies subtask $$$4$$$ and the second example satisfies subtask $$$5$$$.

In the first test case of the first example, we need to consider the following four arrays:

  • $$$f([1,1])=2$$$.
  • $$$f([1,2])=1$$$.
  • $$$f([2,1])=2$$$.
  • $$$f([2,2])=2$$$.

The sum over all arrays is $$$7$$$.

H. Maximizing Pairs
time limit per test
10 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You may want to use C++ for this problem if you want full credit.

Bronya has $$$n$$$ cards ($$$n$$$ is even), with the $$$i$$$th card having the number $$$a_i$$$ written on it. Given an integer $$$k$$$, she plays the following game:

  1. Initially, Bronya's score is $$$0$$$.
  2. While Bronya has at least two cards in her hand, she picks two cards
  3. She discards the two cards and adds $$$1$$$ to her score if the sum of the numbers on the card is exactly $$$k$$$.

Naturally, Bronya wants to maximize her score for any given $$$k$$$.

Bronya has not yet determined what value of $$$k$$$ she will use. Therefore, your task is to find out what her score will be for each value of $$$k$$$ from $$$1$$$ to $$$2\cdot n$$$.

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of independent test cases. The descriptions of test cases follow.

The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$). It is guaranteed that $$$n$$$ is even.

The following line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq n$$$).

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

Scoring

Partial credits will be given to programs who pass tests with smaller constraints outlined below.

GroupPointsConstraints
150The sum of $$$n$$$ does not exceed $$$5000$$$ over all test cases
250No further constraints.
Output

Output $$$2\cdot n$$$ lines on a new line: the answer for $$$k=1,2,3,\ldots,2\cdot n$$$.

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

In the first test case, for $$$k=5$$$, Bronya can earn $$$1$$$ point by doing the following:

  • Pick $$$1$$$ and $$$2$$$ from her hand. Since $$$1+2\neq 5$$$, she does not earn any points.
  • Pick $$$2$$$ and $$$3$$$ from her hand. Since $$$2+3=5$$$, she earns a point.