OCPC 2026 Winter, Day 1: KIT-∞ Contest
A. Another Annoying Math Problem
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given an integer $$$k$$$ and a polynomial $$$\operatorname{p}$$$.

We call polynomials $$$\operatorname{p}$$$ and $$$\operatorname{q}$$$ different modulo $$$k$$$ iff there exists some $$$x$$$ such that: $$$\operatorname{p}(x) \not= \operatorname{q}(x) \mod k$$$.

Output the number of different polynomials modulo $$$k$$$ in $$$\{x,\operatorname{p}(x),\operatorname{p}(\operatorname{p}(x)),\operatorname{p}(\operatorname{p}(\operatorname{p}(x))), \dots\}$$$.

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^3$$$), the number of tests.

Each test consists of two lines. The first line contains two integers $$$d, k$$$ ($$$0 \leq d \leq 10^{4}$$$, $$$1 \leq k \leq 10^{4}$$$), the degree of the polynomial and the modulo. The second line contains $$$d+1$$$ integers $$$a_0, \dots, a_d$$$ ($$$-10^9 \leq a_i \leq 10^9$$$), the coefficients of the polynomial: $$$$$$\operatorname{p}(x) = \sum_{i=0}^d a_i \cdot x^i$$$$$$

It is guaranteed that the sum of $$$d$$$ as well as the sum of $$$k$$$ over all testcases does not exceed $$$2 \cdot 10^{4}$$$.

Output

Output the size of the set. As the answer may be large, output it modulo $$$998\,244\,353$$$.

Example
Input
2
2 4
-2 0 2
5 6
2 8 -1 -1 -2 8
Output
3
2

B. Bored Brother
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

Towers of Hanoi is a game where you have $$$n$$$ disks of different sizes and three rods. To start out, all disks are stacked on the first rod, such that the largest disk is at the bottom and going up the pile, each disk is smaller than the one below it. The goal is to move all disks to the last rod. In any given move, you are only allowed to move one disk from the top of one pile to the top of another. At any time, it has to hold that for two disks on top of each other, the upper one has to be smaller than the lower one. If this condition is fulfilled, we call the current state a valid game state.

This weekend you are in charge of babysitting your little brother. As he has a tendency to wreak havoc on your house once he is bored, you want to keep him occupied. In order to do so, you are playing Towers of Hanoi with him, but he has not quite understood the rules yet. In order to help him, you will at any time cover one of the three piles with your hand. Your brother will then randomly pick one of the two remaining piles and move the uppermost piece to the other pile (if one of the piles is empty, he cannot pick from this pile). You can only move your hand to a different pile when you are in a valid game state. If you optimally decide which pile to cover at any time, calculate is the expected number of moves your brother will make until he has moved all $$$n$$$ pieces from the first to the last pile such that they are stacked in increasing size from bottom to top.

If this process is not clear, the sample explanation may help.

Input

The first line contains one number $$$t$$$ ($$$1 \leq t \leq 10^5$$$), the number of test cases. Each test consists of one integer $$$n$$$ ($$$1 \leq n \leq 10^{12}$$$), the number of disks.

Output

For each test, output one number, the expected number of moves modulo $$$M = 998\,244\,353$$$.

It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$ where $$$q \not \equiv 0 \ \operatorname{mod} M$$$. Output $$$p \cdot q^{-1}\ \operatorname{mod} M$$$. In other words output $$$0 \leq x \lt M$$$ such that, $$$x \cdot q \equiv p \ \operatorname{mod} M$$$.

Example
Input
4
1
2
4
2759
Output
1
5
41
823845074
Note

In the following, we will show the optimal strategy for $$$n = 2$$$. It can be proven that covering another pile at any time will lead to a larger expected number of moves.

Initially, both disks are on the first pile, with the smaller one on top.

We will cover the third pile with our hand.

Thus, the two uncovered piles are one and two. As there are no disks on the second pile, the little brother will choose to move the upper most disk from the first pile to the second pile. This is his first move.

After this, we are in a valid game-state and are thus allowed to move our hand. We will do so, now covering the second pile.

Once again, the little brother will move the disk from pile one to pile three, as he has no other move. This is his second move.

Now we are in a valid game state once again. This time we will move our hand to cover the first pile.

This leaves the brother with two possible moves: he either moves the disk from the second to the third pile or the disk from the third to the second pile.

In the first case, we are done in three moves.

   

In the second case, we cannot move our hand, as we are in an invalid gamestate, and can thus only wait for our brothers fourth move, returning us to the previous state.

Even though we are once again in a valid state, we will not move our hand. It can be shown that it is optimal to not move our hand from this point onward. In expectation, resolving this state will take our brother $$$3$$$ moves, giving us the final answer of $$$2+3 = 5$$$ moves.

C. Cyclefree Complement Chromatic Calculation
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given a forest$$$^{\text{∗}}$$$. Output the chromatic number$$$^{\text{†}}$$$ of the forest's complement$$$^{\text{‡}}$$$.

$$$^{\text{∗}}$$$A forest is an acyclic graph

$$$^{\text{†}}$$$The chromatic number of a graph is the smallest number of colors needed to color the vertices of the graph, such that no two vertices connected by an edge get the same color

$$$^{\text{‡}}$$$The complement of a graph is a graph on the same vertex set, where a pair of vertices is connected by an edge iff there was no edge between these vertices in the original graph

Input

The first line contains two integers $$$n, m$$$ ($$$1 \leq n \leq 2 \cdot 10^{5}$$$, $$$0 \leq m \leq n-1$$$).

The following m lines contain integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i,v_i \leq n$$$), the edges of the graph. It is guaranteed that the given edges form a forest.

Output

The chromatic number of the complement.

Examples
Input
5 4
2 3
4 2
2 1
2 5
Output
4
Input
5 4
5 3
3 1
2 3
5 4
Output
3
Input
5 3
1 4
4 5
3 1
Output
3
Note

For the first example, vertices $$$1$$$ and $$$2$$$ can get the same color while all other vertices get distinct colors. It can be shown that no coloring in fewer colors exists.

For the second example, vertices $$$1$$$ and $$$3$$$ can get the same color, $$$2$$$ gets a different color, and $$$4$$$ and $$$5$$$ get a common color distinct from the others. It can be shown that no coloring in fewer colors exists.

D. Drunk Walk Home
time limit per test
2 seconds
memory limit per test
16 megabytes
input
standard input
output
standard output

Note the low memory limit.

After a night at the pub, John needs to walk home. Luckily, the route is very simple: He can walk in a straight line from the pub door until he bumps into his own door, $$$x$$$ meters away. Miraculously, he can still accurately steer the course despite his intoxicated state; however, he has lost control over the length of his steps. The length of each step is uniformly distributed between $$$0$$$ and $$$1$$$ meter, and the lengths of different steps are independent.

Compute the expected number of steps John needs to take until he bumps into his door. He bumps into his door in a step if the length of the step is strictly larger than the remaining distance. The number of steps includes this final step.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^5$$$). The description of the test cases follows.

Each test case consists of one real number $$$x$$$ ($$$0 \leq x \leq 10^{18}$$$), given with exactly $$$12$$$ digits after the decimal point.

Output

For each test case, output one floating-point number, the expected number of steps.

Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$.

Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

Example
Input
5
0.000000000000
0.500000000000
1.000000000000
1.523891059731
2.000000000000
Output
1.000000000000
1.648721270700
2.718281828459
3.705415927655
4.670774270472

E. Edit Set
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

You were gifted a set containing many different integers. As you find large sets too intimidating, you want to make it smaller. In order to achieve this, you are allowed to apply the following operations to it:

  • choose one element from the set and add $$$1$$$ to it
  • choose one element from the set and subtract $$$1$$$ from it
Note that as this is a set, when after an operation two elements would have the same value, only one of them stays in the set, decreasing its size by one.
Input

The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 \leq k \leq n \leq 2\cdot10^5$$$), the initial size of the set and the size you want to reach. The next line contains $$$n$$$ distinct integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^9$$$), the numbers in the initial set.

Output

Output the minimal number of operations required to reduce the size of the set to $$$k$$$.

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

For the first sample, we can choose $$$4$$$ from the set and increase it by one. Then the set becomes $$$\{8,5,6,1,10\}$$$. Then we can choose $$$6$$$ and decrease it by one. As 5 is already present in the set, the new set will look like $$$\{8,5,1,10\}$$$ and we are done. It can be shown that no sequence of operations with fewer than $$$2$$$ moves exists, which decreases the size of the set to $$$4$$$.

For the second sample, we can decrease $$$3$$$ two times in a row and increase $$$8$$$ once, ending up with the set $$$\{1,9\}$$$. No shorter move sequence exists.

For the third sample, we can increase $$$4$$$ to $$$7$$$, then decrease $$$12$$$ to $$$11$$$ and then increase $$$7$$$ to $$$11$$$ taking us $$$8$$$ moves in total. This can be proven to be optimal.

F. Failing Robots
time limit per test
1.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You control a swarm of $$$n$$$ robots, operating in an environment with $$$n$$$ locations. Initially, all robots are at different locations. You can program the robots, but due to a malfunction, all robots will execute the same program. To fix them, you want to gather all of them in the same location. Write a program which achieves this.

Your program consists of a list of subroutines, numbered $$$1 \dots m$$$. Subroutine $$$1$$$ is the main program. Each subroutine is a sequence of primitive instructions and calls to other subroutines.

There are $$$k$$$ primitive instructions, numbered $$$1 \dots k$$$. The effect of a primitive instruction depends on the current location of the robot. You are given, for each primitive instruction and each location, the new location after executing the instruction.

Input

The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 \leq n \leq 300$$$, $$$1 \leq k \leq 200$$$).

The following description of a primitive instruction is repeated on the next $$$k$$$ lines. One line with $$$n$$$ integers $$$s_i$$$ ($$$1 \leq s_i \leq n$$$), the new location if the primitive instruction is executed at location $$$i$$$.

Output

If no such program exists, output one line containing NO.

Otherwise, on the first line, output YES.

On the next line, output one integer $$$m$$$ ($$$1 \leq m \leq 2 \cdot 10^5$$$), the number of subroutines you will use.

Following this output $$$m$$$ lines, the definitions of your subroutines: Output one integer $$$l_i$$$ ($$$0 \leq l_i \leq 2 \cdot 10^5$$$), followed by $$$l_i$$$ integers $$$v_1, \dots v_{l_i}$$$ ($$$-k \leq v_i \leq m, v_i \neq 0$$$). These are the commands of the subroutine:

  • if $$$v_i \lt 0$$$: Execute primitive instruction $$$-v_i$$$
  • if $$$v_i \gt 0$$$: Call subroutine $$$v_i$$$

You have to guarantee that $$$\sum l_i \leq 2 \cdot 10^5$$$.

Subroutine $$$1$$$ will then be invoked on all robots. You have to guarantee that this will terminate and execute no more than $$$10^8$$$ primitive instructions.

It can be shown that if a solution exists, there is also a solution satisfying these constraints.

Examples
Input
6 1
1 1 2 1 4 4
Output
YES
1
2 -1 -1
Input
3 2
2 3 1
3 1 2
Output
NO
Input
4 2
2 1 1 3
1 4 4 3
Output
YES
3
3 2 3 2
2 -1 -1
1 -2
Note

We will explain the third sample. The two primitive instructions given are:

It can be checked that the sequence $$$(1,1,2,1,1)$$$ corresponding to first taking two moves in the first graph, then one move in the second graph, and then once again two moves in the first graph, is a valid solution. This is also the sequence of instructions executed by the program in the solution.

G. Graph Matching
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

You are given an infinite graph. Determine whether it has a perfect matching$$$^{\text{∗}}$$$.

The infinite graph is given in the following format:

We consider a family of graphs $$$G_1, \cdots, G_k$$$. Each edge $$$(u,v)$$$ has a label l, $$$0 \le l \le k$$$.

We start with a copy of the graph $$$G_1$$$. Every edge $$$(u,v)$$$ with label $$$l \ne 0$$$ is replaced by a copy of the graph $$$G_l$$$, with $$$G_l$$$'s vertex $$$1$$$ merged with $$$u$$$ and $$$G_l$$$'s vertex $$$2$$$ merged with $$$v$$$. This replacing is repeated to generate a potentially infinite graph.

Formally, let $$$H_0 = G_1$$$. $$$H_{i+1}$$$ is produced from $$$H_i$$$ by the replacement procedure described above. $$$H_i'$$$ is produced from $$$H_i$$$ by removing all edges with with labels $$$l \ne 0$$$. The resulting graph H is the infinite union of all $$$H_i'$$$.

All $$$G_i$$$ contain no self-loops or multiedges.

$$$^{\text{∗}}$$$A perfect matching is a subset of the edges such that every vertex is incident to exactly one of them

Input

The first line contains a single integer $$$k$$$ ($$$1 \le k \le 10$$$), the number of graphs. The descriptions of the graphs follows.

The first line of each graph contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 100$$$, $$$0 \le m \le 100$$$), the number of vertices and edges.

Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$l$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$, $$$0 \le l \le k$$$), indicating an edge $$$(u,v)$$$ with label $$$l$$$.

Output

Output "YES" if the infinite graph has a perfect matching, "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Examples
Input
1
6 6
1 3 0
1 4 0
3 5 1
4 6 1
2 5 0
2 6 0
Output
YES
Input
1
2 1
1 2 1
Output
NO
Input
1
3 2
1 3 1
2 3 0
Output
NO
Input
1
3 2
3 1 1
2 3 0
Output
YES
Input
2
3 3
1 2 0
1 3 0
2 3 2
3 2
3 1 2
2 3 0
Output
YES
Note

The first sample describes a pair of infinite binary trees. At first, it looks like this:

Then, each dashed edge is replaced with another copy of graph 1:

Continuing like this, each side becomes an infinite binary tree. Note that the sides form two different connected components, since no non-dashed edge ever connects them.

The second sample is a finite graph with two vertices and no edges. Each replacement of the edge with another copy of the graph does not change the graph.

The third sample is a path which is infinite in one direction, and an isolated vertex. The path grows by one vertex with each replacement step, the vertex stays isolated.

The fourth sample is a pair of paths which are infinite in one direction. At each step, one of the paths grows by one vertex, while the other one stays the same length; the paths take turns growing. It matters in which direction a replacement edge is given, since this is the only difference from the third sample.

The fifth sample is a path which is infinite in both directions. It is the forth sample with the two paths glued together with another two edges.

H. Hidden Message
time limit per test
3.5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

In class, you received a secret message scribbled on a piece of paper from one of your friends. But its content does not make sense to you. Then you remember that your friend is notoriously bad at spelling. They sometimes mix up letters, forget letters, or even add new ones. The best method to decipher the message is to guess and check. In order to make this faster, you want to write a program that does the checking for you.

Given the text from the piece of paper and a guess of what was the intended message, find the smallest number of character insertions, deletions, and replacements needed to change the intended text into the scribble you received. However, as you believe in your friends ability to get at least some things right, you expect the correct guess to require less than $$$5000$$$ edits. Thus, if the strings deviate further, your program should simply output $$$5000$$$.

Note: The timelimit on this problem is fairly tight; be careful about constant factors. In particular, we recommend avoiding dynamically sized arrays (e.g. 'std::vector') and using fixed-size arrays instead (e.g. 'std::array'). Depending on your implementation, it may be necessary to optimize out some factors of 2.

Input

The first line contains two integers $$$l_1$$$, $$$l_2$$$ ($$$1 \leq l_1, l_2 \leq 5 \cdot 10^5$$$), the lengths of the text you received and your guess, respectively.

The second line contains a string $$$s_1$$$ of length $$$l_1$$$ consisting of lower case Latin characters, the message you received.

The third line contains a string $$$s_2$$$ of length $$$l_2$$$ consisting of lower case Latin characters, your guess.

Output

Output the minimal number of edits to get from $$$s_2$$$ to $$$s_1$$$. If the answer is larger than $$$5000$$$, output $$$5000$$$ instead.

Examples
Input
5 4
abbac
abab
Output
2
Input
16 18
ltzmeatafterclas
letsmeetafterclass
Output
4
Note

For the first sample one can optain $$$s_1$$$ from $$$s_2$$$ by inserting the character 'b' in position $$$3$$$ and changing the last character from 'b' to 'c'. No sequence with fewer edits exists.

For the second sample we will remove 'e' from the second position, change 's' to 'z' and 'e' to 'a' in the appropriate positions and remove one 's' from the back of the string. No shorter edit sequence exists.

I. Isomorphism Is Interesting
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

A graph $$$G$$$ is called self-complementary if it is isomorphic to its complement, i.e. there exists some permutation $$$p$$$ on its vertices such that for $$$\forall u\not=v$$$: $$$u v$$$ is an edge in $$$G$$$ iff $$$p_u p_v$$$ is not an edge.

Given $$$n$$$, output a self-complementary graph $$$G$$$ on $$$n$$$ vertices, and the permutation $$$p$$$, or indicate that this is impossible.

Input

The only line of input contains one integer $$$n$$$ ($$$1 \leq n \leq 2000$$$).

Output

If there is no solution, print a single integer $$$-1$$$.

Otherwise, on the first line output one integer $$$m$$$ ($$$0 \leq m \leq \frac{n\cdot(n-1)}{2}$$$).

Afterwards output $$$m$$$ lines with two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \leq u_i,v_i \leq n$$$, $$$u_i \not=v_i$$$) representing an edge. You may not output the same edge multiple times, formally: $$$\{u_i,v_i\} \not= \{u_j,v_j\} \; \forall 1 \leq i \lt j \leq m$$$.

On the last line output $$$n$$$ distinct integers $$$p_i$$$ ($$$1 \leq p_i \leq n$$$) the permutation.

Examples
Input
2
Output
-1
Input
4
Output
3
1 2
2 3
3 4
2 4 1 3 

J. Just Maximal Increasing Subsequences
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Finding a longest increasing subsequence of an array is a well known problem. You may also know how to count the number of longest increasing subsequences. But do you know how to count the number of inclusion maximal increasing subsequences?

An increasing subsequence is a non empty list of indices $$$i_1 \lt \dots \lt i_k$$$ such that $$$a[i_1] \lt a[i_2] \lt \dots \lt a[i_k]$$$. Furthermore, it is inclusion maximal if there is no increasing subsequence $$$i^\prime_1 \lt \dots \lt i^\prime_{k^\prime}$$$ with $$$k^\prime \gt k$$$, which contains $$$i_1 \lt \dots \lt i_k$$$ as a subsequence.

Input

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$), the number of tests.

Each test consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The next line contains $$$n$$$ integers $$$a_i$$$ ($$$1\leq a_i \leq 10^9$$$).

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

Output

For each test output one number, the number of maximal increasing subsequences modulo $$$998\,244\,353$$$.

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

For the first example, there exists one maximal increasing subsequence, the entire array.

In the second example, there are two maximal increasing subsequences, one with array indices 1 and 3 and the other with indices 2 and 3.

K. Known At KIT
time limit per test
8 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

During a previous OCPC KIT contest, we noticed that other universities do not know how to $$$5$$$ color planar graphs. Thus, to keep you practicing, we will today present you with another graph coloring problem. This one will be easier for sure.

You are given $$$n$$$ line segments in the plane. Colour them in red and blue such that no two segments of the same colour intersect, or state that this is impossible.

Input

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).

Each of the next $$$n$$$ lines contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$-10^6 \le x_1, y_1, x_2, y_2 \le 10^6$$$), indicating a line segment from $$$(x_1, y_1)$$$ to $$$(x_2, y_2)$$$.

It is guaranteed that no two points in the input have the same $$$x$$$ or $$$y$$$ coordinate, and that no two line segments in the input intersect in more than a single point.

Output

The first line of the output should be either YES or NO, indicating whether there is a solution.

If the answer is YES, the second line should be a string of length $$$n$$$ consisting of the letters R and B, indicating the colour of each segment in the order they were given in the input.

Examples
Input
3
0 0 2 10
1 8 9 7
10 9 5 1
Output
YES
RBR
Input
3
7 0 2 10
1 8 9 7
10 9 5 1
Output
NO
Input
1
-1000000 1000000 1000000 -1000000
Output
YES
R

L. Large Strings
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Large Numbers are the best! The larger they are the better. As your team has condemned you to solve string problems, you try to find interest in them. What would a large string look like? Lexicographicaly large strings are simple an boring, so instead you found another way to define the size of a string.

For a given string, you want to replace all characters with digits such that the resulting number is a square number. Multiple occurrences of the same character have to be replaced with the same digit and no two different characters may be replaced by the same digit. The first character cannot be replaced by a zero. And did i forget to mention: the obtained number should obviously be as large as possible.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^5$$$). The description of the test cases follows. Each test case contains one string $$$s_i$$$ ($$$1 \leq |s_i| \leq 12$$$) consisting of lower case Latin letters.

Output

For each testcase output the largest square number that can be obtained from the described process. If it is not possible to obtain any square, print a single integer $$$-1$$$.

Example
Input
4
ab
aa
aff
qlcu
Output
81
-1
900
9801