HUSTPC 2022
A. Common Edges
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a connected graph with $$$n$$$ vertices and $$$m$$$ undirected edges.

You are given $$$Q$$$ queries, each containing $$$4$$$ integers $$$u,v,x,y$$$. For each query, you need to find one path from $$$u$$$ to $$$x$$$ and another from $$$v$$$ to $$$y$$$, or one path from $$$u$$$ to $$$y$$$ and another from $$$v$$$ to $$$x$$$, so that the common edges between these two paths is the least.

Input

The first line contains two integers $$$n,m\ (2\le n\le 2\cdot 10^5,1\le m\le 3\cdot 10^5)$$$ as described above.

Each of the next $$$m$$$ lines contains two integers $$$u,v\ (1\le u,v\le n)$$$, indicating an undirected edge between $$$u$$$ and $$$v$$$. It is guaranteed that there are no self-loops or multiple edges, and that the graph is connected.

The $$$(m+2)$$$-th line contains an integer $$$Q\ (1\le Q\le 10^5)$$$, the number of queries.

Each of the next $$$Q$$$ lines contains four integers $$$u,v,x,y\ (1\le u,v,x,y\le n)$$$, the starting points and the end points of the two paths.

Output

For each query output an integer in a line, indicating the minimum number of common edges.

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

In the first example, you can select path $$$(1,2)-(2,3)$$$ and $$$(1,3)$$$ for the first query so that there are no common edges. But in the second query, both paths will pass through $$$(3,4)$$$ so there is at least one common edge.

B. Contest Preparation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone is anxious to prepare for a programming contest, and he has ideas for $$$n$$$ problems now. Preparing a problem from scratch requires two jobs: making and validating. He asked $$$m$$$ people to help him with these two jobs.

All these $$$m$$$ people are skilled at making and validating problems, and each person needs exactly $$$1$$$ hour to do one of these two jobs. A job should be done by only one person without interruption (but making and validating one problem can be done by two different people), and a problem must be made before validated. Now Walk_alone wants to know the minimum hours to prepare for the contest if he arranges everyone's job properly.

Input

The input contains multiple test cases.

The first line of the input contains an integer $$$T\ (1 \leq T \leq 10^5)$$$ indicating the number of test cases.

For each testcase, there will be only one line containing two integers $$$n\ (0 \leq n \leq 10^9)$$$ and $$$m\ (1 \leq m \leq 10^9)$$$, indicating the number of problems and the number of people.

Output

For each testcase, output a single integer indicating the minimum hours Walk_alone should wait before the contest is well prepared.

Example
Input
3
0 2
2 2
4 3
Output
0
2
3

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

You are given an integer $$$n$$$ and a set $$$S\subseteq\{0,1,\dots,n-1\}$$$ with $$$m$$$ integers. A graph with $$$2^n$$$ vertices labelled from $$$0$$$ to $$$2^n-1$$$ is built in the following way: for each $$$s\in S$$$ and $$$i\in \{0,1,\dots,2^n-1\}$$$, connect vertex $$$i$$$ and $$$(i+2^s)\text{ mod }2^n$$$ with an undirected edge of length $$$1$$$.

Let $$$d_{i,j}$$$ be the distance between vertex $$$i$$$ and $$$j$$$, and you need to answer:

  • The diameter of the graph (i.e., $$$\max_{0\le i\le j \lt 2^n}d_{i,j}$$$) modulo $$$998\,244\,353$$$;
  • The number of pairs $$$(i,j)\ (0\le i\le j \lt 2^n)$$$ such that $$$d_{i,j}=\max_{0\le x\le y \lt 2^n}d_{x,y}$$$ modulo $$$998\,244\,353$$$.

It is guaranteed that the graph is connected.

Input

The first line contains two integers $$$n\ (1\le n\le 10^6)$$$ and $$$m\ (1\le m\le n)$$$ as described above.

The second line contains $$$m$$$ integers, the elements in set $$$S$$$.

Output

Output the two answers in one line separated by a space.

Examples
Input
3 2
0 1
Output
2 12
Input
6 3
4 0 1
Output
6 64
Note

The graph built in the first example is as below.

The diameter of the graph is $$$2$$$, and there are $$$12$$$ pairs of vertices with distance $$$2$$$: $$$(i,i+3)$$$, $$$(i,i+4)$$$, $$$(i,i+5)$$$ for all $$$0\le i \lt 4$$$.

D. Difference
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone has a sequence $$$a$$$ of length $$$n$$$, he defines function $$$f$$$ as $$$$$$ f(l,r)=\big(\max_{l \leq i \leq r} a_i -\min_{l \leq i \leq r}a_i\big)(r-l+1) $$$$$$

He thinks that calculating the value of the function to a given interval is too boring, so he wants you to output the $$$k$$$-th largest value of the function among all $$$f(l,r)$$$ where $$$1 \leq l \leq r \leq n$$$.

Input

The first line contains $$$n\ (1\leq n\leq 5\cdot 10^5)$$$ and $$$k\ (1\le k\le n(n+1)/2)$$$, where $$$n$$$ indicates the length of sequence $$$a$$$.

The second line contains $$$n$$$ integers $$$a_1,a_2,\dots, a_n\ (|a_i|\le 10^9)$$$ as the sequence.

Output

Output a single value indicating the $$$k$$$-th largest function value among all intervals.

Example
Input
3 2
3 1 2
Output
4

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

There are $$$n$$$ points $$$P_0,P_1,\dots,P_{n-1}$$$ on a plane, and the coordinates of $$$P_{i}$$$ are $$$(x_i,y_i)\ (0\le i \lt n)$$$.

Let $$$M=(\sum_{0\le i \lt n} x_i, \sum_{0\le i \lt n} y_i)/n$$$, i.e., the mass center of the initial $$$n$$$ points. The following operations will repeat for $$$k$$$ times:

  • For each $$$0\le i \lt n$$$, extend segment $$$MP_i$$$ to $$$Q_i$$$ such that $$$|MP_i|=\cos(2\pi/n)\cdot |MQ_i|$$$.
  • Let $$$Q_{-1}=Q_{n-1}$$$ and $$$Q_n=Q_0$$$. For each $$$0\le i \lt n$$$, replace $$$P_i$$$ with $$$R_i$$$, the mass center of $$$\triangle P_iQ_{i-1}Q_{i+1}$$$.

You can see the picture below for better understanding.

Constructing $$$R_i$$$ from $$$P_{i-1}$$$, $$$P_i$$$ and $$$P_{i+1}$$$

Let $$$P'_i=\lim_{k\to\infty} P_i\ (0\le i \lt n)$$$ and $$$P'_n=P'_0$$$. It can be proved that $$$P'_i$$$ will always exist when $$$n\ge 7$$$. Now you need to output the value of

$$$$$$ \sum_{0\le i \lt n} \mathop{MP'_i}^{-\hspace{-2pt}-\hspace{-2pt}-\hspace{-2pt}\rightarrow} \times \mathop{MP'_{i+1}}^{-\hspace{-2pt}-\hspace{-2pt}-\hspace{-2pt}\rightarrow\ \ \ } $$$$$$

P.S. You can try to prove that all $$$P'_i$$$ will be on a certain ellipse though it may have nothing to do with the problem.

Input

The first line contains an integer $$$n\ (7\le n\le 10^6)$$$, the number of points.

The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i\ (|x_i|,|y_i|\le 10^5)$$$, the initial coordinates of $$$P_i$$$.

Output

Output a real number as the answer. Your answer will be considered correct if and only if the absolute or relative error of your answer to the correct answer is less than or equal to $$$10^{-6}$$$.

Examples
Input
8
1 -1
0 -2
-1 -1
-2 0
-1 1
0 2
1 1
2 0
Output
-16.485281374
Input
7
2 0
-2 1
2 -2
0 4
-3 -2
-2 -3
3 1
Output
8.192914574
Note

In the first example, the $$$8$$$ points will be on a circle of radius $$$1.70709$$$ centered at $$$(0,0)$$$ after infinite operations.

F. K-th Power
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone hates numbers with large exponents, and he considers an integer $$$x$$$ as good if and only if there does not exist a prime number $$$p$$$, such that $$$x$$$ is a multiple of $$$p^k$$$. He hopes to find the number of all good integers in range $$$[l,r]$$$.

Input

The only line of input contains three integers $$$l,r\ (1 \leq l \leq r \leq 10^{14})$$$ and $$$k\ (2 \leq k \leq 10^9)$$$, indicating the lower range, upper range of the querying interval and the asked $$$k$$$ mentioned above.

Output

Output a single integer indicating the number of good integers in range of $$$[l,r]$$$.

Example
Input
1 10 2
Output
7
Note

In the example above, $$$4$$$ and $$$8$$$ are multiples of $$$2^2$$$, and $$$9$$$ is a multiple of $$$3^2$$$.

G. Nerdle
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Walk_alone is going to primary school! To train his computing ability, Weierstras gives him a game called Nerdle.

In this game, he should guess an equation of length $$$8$$$, and an equation is valid if and only if it satisfies the following rules:

  • The length of the equation is $$$8$$$, and an equation must contain exactly one equal sign ('='). The two sides of the equation (called AddExprs) must be equal;
  • An AddExpr consists of one or more MulExprs connected by plus signs ('+') or minus signs ('-');
  • A MulExpr consists of one or more Numbers connected by multiple signs ('*') or division signs ('/');
  • A Number consists of at most one negative sign ('-') and one or more Digits (leading 0s are allowed in this game, and negative signs can connect directly with minus signs);
  • The result of each division must be an integer (for example, expression 5/4*4 is not allowed, even if the final result is an integer).

For example, 5*4/4=05, 1--2=1+2 and -1*-2=02 are all valid equations, but 5/4*4=05, 5/4=10/8, 2+---1=1, 1+2+3=07, +12=10+2 or 1=3-2=01 are not.

Formally speaking, all valid equations must satisfy the following Extended Backus-Naur Form:

Equation := AddExpr '=' AddExpr
AddExpr := MulExpr | AddExpr ('+'|'-') MulExpr
MulExpr := Number | MulExpr ('*'|'/') Number
Number := ['-'] Digit {Digit}
Digit := '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'

Now Walk_alone has a pattern of length $$$8$$$, and he asks you to tell him the number of valid equations satisfying the pattern. The pattern consists only of digits, '+', '-', '*', '/', '=' and '?'. If the $$$i$$$-th character in the pattern is '?', it means the $$$i$$$-th character in the equation is not restricted, otherwise the character must be the same as the pattern.

Input

The only line of the input contains a string of length $$$8$$$ as the pattern.

Output

Output an integer indicating the number of equations.

Examples
Input
?-?+1=?9
Output
2
Input
???*????
Output
37305
Note

All possible equations in the first example are 8-0+1=09 and 9-1+1=09.

H. Permutation Counting
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given an integer $$$n$$$, and $$$m$$$ pairs of integers $$$(x_i,y_i)\ (1\le i\le m)$$$ with distinct $$$x_i$$$, i.e. $$$x_i\neq x_j$$$ for all $$$i\neq j$$$. You need to find the number of permutation $$$p$$$ of length $$$n$$$ satisfying $$$p_{x_i} \lt p_{y_i}$$$ for all $$$i$$$. Since the number might be too large, you only need to output the answer modulo $$$998\,244\,353$$$.

Recall that a permutation of length $$$n$$$ is a sequence of length $$$n$$$ and contains $$$1,2,\dots, n$$$ exactly once. For instance, $$$\{1,2,3\}$$$ and $$$\{3,1,2\}$$$ are permutations while $$$\{1,2,4\}$$$ and $$$\{1,2,2\}$$$ are not.

Input

The first line contains two integers $$$n\ (1 \leq n \leq 2\cdot 10^6)$$$ and $$$m\ (0 \leq m \leq n)$$$, indicating the length of the permutation and the number of constraints.

The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$x_i\ (1\le x_i\le n)$$$ and $$$y_i\ (1\le y_i\le n, x_i\neq y_i)$$$ indicating the constraint that $$$p_{x_i} \lt p_{y_i}$$$.

Output

Output an integer indicating the number of permutations satisfying the conditions modulo $$$998\,244\,353$$$.

Examples
Input
3 1
1 2
Output
3 
Input
3 2
1 2
2 3
Output
1 
Input
3 2
1 2
2 1
Output
0 
Input
10 7
1 2
3 2
5 4
7 8
2 6
4 6
8 6
Output
37800 
Note

In the first sample, all valid permutations are $$$\{1,2,3\},\{1,3,2\}$$$ and $$$\{2,3,1\}$$$, so the answer is $$$3$$$.

I. Repetition
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone loves repetition! He has a lot to say, and he may repeat some information in every sentence (represented as $$$n$$$ strings $$$S_1,S_2,\dots, S_n$$$) he says. Since such information could be important, his teammates want to extract a substring which appears in every sentence for at least $$$k$$$ times. However, some information (represented as string $$$T$$$) is so sensitive that they would not allow it to appear in the information. It is hard to find out the longest information they can extract, so they ask you for help.

Formally, you are given $$$n$$$ strings $$$S_1,S_2,\dots,S_n$$$ and a pattern $$$T$$$. Your task is to find the length of the longest string which appears in every $$${S}_i\ (1\le i\le n)$$$ for at least $$$k$$$ times, and does not have $$$T$$$ as its continuous substring.

Input

The first line contains two integers $$$n\ (1\le n \le 10^5)$$$ and $$$k\ (1 \leq k \leq 10^5)$$$.

The $$$i$$$-th of the following $$$n$$$ lines contains a string $$${S}_i\ (|S_i|\ge 1,\, \sum_{i=1}^n|{S}_i|\le 10^6)$$$.

The last line contains a string $$$T\ (1\le |T|\le 10^5)$$$.

It is guaranteed that all strings contain only lowercase English letters.

Output

Output an integer representing the answer. If such string does not exist, output $$$-1$$$ instead.

Examples
Input
2 2
aaaaaababab
ababaaaaaabab
aa
Output
4
Input
2 3
aaaaaababab
ababaaaaaabab
ab
Output
4
Input
2 5
aaaaa
aaaa
b
Output
-1
Note

In the first example, 'abab' appears in both strings for $$$2$$$ times, so the answer is $$$4$$$. Note that though 'aaaaa' is longer, it has 'aa' as its substring.

J. Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone loves sequences. For a sequence $$$a_1,a_2,\dots,a_n$$$ of length $$$n$$$, he defines $$$A_i\ (1\le i \lt n)$$$ as

$$$$$$A_i=\bigoplus_{j=1}^i \bigoplus_{k=i+1}^n a_j \,\&\, a_k$$$$$$

where $$$\&$$$ represents the bitwise AND, and $$$\oplus$$$ means bitwise XOR.

Now Walk_alone has an initially empty sequence. He will do $$$q$$$ operations to this sequence, and there will be two types of operations:

  • 1 x: add an element $$$x\ (0\le x \lt 2^{20})$$$ to the back of the sequence.
  • 2: output $$$\max_{1\le i \lt n}A_i$$$ where $$$n$$$ is the length of sequence $$$a$$$. The answer is $$$0$$$ if $$$n \lt 2$$$.
Input

The first line of the input contains one integer $$$q\ (1 \leq q \leq 10^5)$$$, indicating the number of operations.

Each of the next $$$q$$$ lines will contain one kind of operations described above. It is guaranteed that $$$0\le x \lt 2^{20}$$$ for every operation of the first type.

Output

For each operation of the second type, output the answer in a line.

Example
Input
11
1 6
2
1 7
2
1 13
1 4
1 14
2
1 14
1 10
2
Output
0
6
8
12

K. Triangles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone has a $$$500\times 500$$$ square grid in a plane. The lower-left corner of the grid locates at $$$(0,0)$$$, and all squares in it are of size $$$1\times 1$$$.

There are $$$n$$$ segments in the grid, the $$$i$$$-th of which connecting point $$$(x_i,y_i)$$$ and $$$(x_i-1,y_i-1)$$$. Now you are required to find out the number of triangles in the grid.

Input

The first line contains an integer $$$n\ (1\le n\le 250\,000)$$$, the number of segments in the grid.

The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i\ (1\le x_i,y_i\le 500)$$$ as described above. It is guaranteed that $$$(x_i,y_i)\neq (x_j,y_j)$$$ for all $$$i\neq j$$$.

Output

Output an integer representing the number of triangles.

Examples
Input
3
1 1
2 2
3 1
Output
8
Input
6
1 1
2 1
3 2
4 3
1 3
2 4
Output
20
Note

The figure below illustrates the first example.

L. WA Sorting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On December 4th, ICPC2021 Nanjing was held. During the contest, Walk_alone (abbr. WA) did nothing but be stuck by Problem D, which was an easy data structure problem with a background of an adapted bubble sort. So he has being scolded by his teammates for a long time for losing a gold medal.

Today, his teammates gave him another adjusted sorting algorithm to help him overcome his psychological shadow. But his dread and fear stops him from even thinking about this problem. Help him solve the problem below to avoid being scolded by his teammates again!

Its pseudo-code is shown below.

Obviously not every sequence can be sorted by this algorithm, but it does not matter, and here comes the question:

Given a permutation $$$A=\{a_1,a_2,\cdots, a_n\}$$$. Let $$$A_k=\{a_1,a_2,\cdots, a_k\}$$$. Your task is to count the total number of times $$$m$$$ is assigned if we call $$${\rm SORT}(A_k)$$$ for every $$$k \ (1 \le k \le n)$$$.

Input

The first line contains an integer $$$n\ (1\le n\le 10^6)$$$ indicating the length of the sequence.

The second line contains $$$n$$$ integers $$$a_1,a_2,\cdots, a_n\ (1\le a_i\le n)$$$ indicating the given permutation.

Output

Output one line containing $$$n$$$ integers $$$s_1,s_2,\cdots, s_n$$$ separated by a space, where $$$s_i$$$ indicates the total number of times $$$m$$$ is assigned if we call $$${\rm SORT}(A_i)$$$.

Examples
Input
6
5 4 2 6 3 1
Output
1 4 8 11 13 19 
Input
6
4 5 2 3 6 1
Output
1 3 6 8 13 17 

M. XOR Almost Everything
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Walk_alone has a sequence $$$a$$$ of length $$$n$$$. He can do the following operations for arbitrary times (possibly zero):

  • Select an index $$$x$$$ and an integer $$$y$$$ ($$$1 \leq x \leq n, 0 \leq y \lt 2^{30}$$$);
  • For all index $$$i$$$ such that $$$i \neq x$$$, change $$$a_i$$$ to $$$a_i \oplus y$$$, where $$$\oplus$$$ denotes bitwise XOR.

Note that Walk_alone can select different $$$y$$$ in different operations.

Now he wants to know if he can change all numbers in the sequence to $$$0$$$.

Input

The first line contains a integers $$$n\ (1 \leq n \leq 10^5)$$$, the length of sequence $$$a$$$.

The next line contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n \ (0 \leq a_i \lt 2^{30})$$$, indicating the elements in $$$a$$$.

Output

Print "$$$\mathtt{YES}$$$" (without quotes) if Walk_alone can change all numbers in the sequence to $$$0$$$, otherwise print "$$$\mathtt{NO}$$$".

Examples
Input
5
5 4 2 1 2
Output
YES
Input
5
9 3 5 6 1
Output
NO