2020 UTPC Fall Puzzle Contest
A. Black
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You, the Black character, stroll into the Reactor room, ready to drum up the reactor machine for a long day of intergalactic travel. The start up mechanism requires you to diligently repeat a series of patterns on the reactor's button pad, each time copying the pattern of light flashes shown on a screen beside the pad. Conveniently, you happen to have picture perfect memory and so remember the location of every light flash you see. The only problem is, you get easily distracted and often miss button presses while you're looking away at something else.

Complete your important task! For each target pattern, given the light flashes that you see for several repeats of the pattern, determine whether or not you, with your perfect memory, can deduce the target pattern.

Note that the button pad contains 9 buttons, and you keep track of the button locations and corresponding light flashes according to the letters A through I. Importantly, also note that you have preconfigured the reactor pad to only generate target patterns without duplicate button presses, meaning there will be no repeated button presses or flashes within the target pattern.

Input

The first line of input contains an integer $$$T$$$ ($$$1 \leq T \leq 1000$$$), denoting the number of target patterns that you must enter into the reactor pad. Then for each target pattern, the first line contains two space-separated integers: $$$N$$$ ($$$1 \leq N \leq 9$$$) denoting the length of the target pattern and $$$R$$$ ($$$1 \leq R \leq 100$$$) denoting the total number of repeats of the target pattern you've witnessed. $$$R$$$ lines follow, each containing an integer $$$r_i$$$ ($$$1 \leq r_i \leq N$$$) followed by $$$r_i$$$ capital letters (A through I) denoting the locations of light flashes in the order that you saw them for that repeat of the target pattern. The data is guaranteed to be consistent with a valid target pattern.

Output

For each target pattern, output "NOT ENOUGH INFO" if you are unable to deduce it, or output the target pattern on one line if you are in fact able to figure it out with full confidence, given what you've seen.

Example
Input
3
5 3
3 A C E
3 B D E
4 A B D E
1 1
1 C
5 3
4 D A B C
3 E B C
3 D E A
Output
NOT ENOUGH INFO
C 
D E A B C 

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

Blue had a few seconds to stop and do wires in Storage before getting on with the rest of his investigation into the rest of the crew. Blue can complete wires by connecting wires between the wire slots of the same color together. However, it seems like the wire configuration that he has been given is a little more complicated than normal, and he might need additional help to figure out the desired wire pairings.

You will be given two sets of colored wire slots, represented by alphabetic characters (that could be uppercase or lowercase). You are guaranteed that all colored wire slots are distinct and any colored wire slots in the first set will be accompanied by a wire slot with the same color on the second set.

For each colored wire slot in the first set, you must figure out the one-based index of that colored wire slot in the second set so that Blue can connect the two slots. Print out all these one-based indexes together (not separated by anything).

Input

The first line will consist of a single integer $$$n$$$ ($$$1 \leq n \leq 52$$$), which gives the number of wire slots in each set. The next line consists of a string with $$$n$$$ distinct alphabetic characters that represent the first set of colored wire slots. The next line consists of a string with the same $$$n$$$ distinct alphabetic characters that represent the next set of colored wire slots.

Output

Print out the one-based index of the colored wire slot in the second set for each colored wire slot in the first set, separated by nothing.

Examples
Input
4
RYGB
BGRY
Output
3421
Input
15
bWLQefvOKuJpxGm
vKeQpJfbmLxGWOu
Output
813104371142156511129
Note

The first test case gives us the indices $$$[3, 4, 2, 1]$$$. The second test case gives us the indices $$$[8, 13, 10, 4, 3, 7, 1, 14, 2, 15, 6, 5, 11, 12, 9].$$$

C. Dark-Green
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Dark-Green is working on the divert power task when he realizes that there are a lot of rooms that currently require power diverted to them. Specifically, there are $$$n$$$ rooms that Dark-Green must divert power to and the $$$i$$$th room has $$$a_{i}$$$ pieces of electronics which require power.

However, due to the strange effects of space travel, he realizes that the amount of power that each room needs is not linear in the number of devices present in the room. He deduces the following recursive function $$$f(x)$$$ defining the amount of power the room needs when there are $$$x$$$ devices in the room:

  • $$$f(0) = 0$$$
  • $$$f(2 \cdot x) = f(x)$$$
  • $$$f(2 \cdot x + 1) = f(x) + 1$$$

Given this, Dark-Green would like to figure out the number of unique pairs of the $$$n$$$ rooms that require the same amount of power.

Input

The first line of input will contain $$$n$$$ which is the number of rooms that require power diverted to them ($$$1 \leq n \leq 10^{4}$$$).

The second line of input will contain $$$n$$$ space-separated integers where the $$$i$$$th integer, $$$a_i$$$, represents the number of devices room $$$i$$$ requires ($$$0 \leq a_i \leq 10^{9}$$$).

Output

A single integer representing the number of unique pairs of rooms that need the same amount of power.

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

In the first example, computing $$$f(x)$$$ for all the values in the array yields us the same answer for all of them so any pair works and so there are $$$3$$$ unique pairs.

D. Light-Green
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Light-Green was priming the shields when she realized that the current hexagonal shields are not optimal to protect the ship. She decides to arrange the shields into an $$$n \times n$$$ square grid arrangement with a shield in each of the cells of the grid. Additionally, each of the shields can be installed in one of two ways, either facing left or facing right.

Now, say that we have an $$$n \times n$$$ matrix $$$S$$$ where $$$S_{i,j}$$$ (for $$$1 \leq i, j \leq n$$$) equals 'L' if the shield at that position in the matrix is facing left or 'R' if it is facing right. Now, Light-Green has derived that the optimal shield arrangement must have the following properties:

  • For every $$$S_{i,j}$$$ that equals 'R', none of the squares $$$S_{i + 1,j}$$$, $$$S_{i - 1,j}$$$, $$$S_{i,j - 1}$$$, and $$$S_{i,j + 1}$$$ can equal 'R'.
  • $$$S$$$ must be both horizontally and vertically symmetrical or more formally, it must be true that $$$S_{i,j} = S_{n - i + 1, j}$$$ and $$$S_{i,j} = S_{i, n - j + 1}$$$ for every possible pair $$$i,j$$$ (where $$$1 \leq i,j \leq n$$$).
  • There are exactly $$$k$$$ squares in the matrix which are equal to 'R'.

Now, priming the shields takes time so Light-Green wants to prime as few shields as possible. Given an integer $$$k$$$, figure out the smallest positive integer $$$n$$$ such that there exists an $$$n \times n$$$ matrix satisfying the optimal properties that were listed above.

Input

The single line of input will contain a single integer $$$k$$$ ($$$1 \leq k \leq 10^{12}$$$), representing the number of entries in the matrix which are equal to 'R'.

Output

Output a single integer, representing the smallest positive integer $$$n$$$ such that there exists an $$$n \times n$$$ matrix satisfying the optimal properties.

Examples
Input
4
Output
3
Input
12
Output
5
Note

In the first example, you can construct a $$$3 \times 3$$$ matrix with $$$\{1,0,1\}$$$ in the first row, $$$\{0, 0, 0\}$$$ in the second row, and $$$\{1, 0, 1\}$$$ in the third row and this satisfies all the conditions (and there is no $$$1 \times 1$$$ or $$$2 \times 2$$$ matrix which can fit the conditions) so $$$3$$$ is our answer.

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

Orange is currently in the Cafeteria, and wants to set the world record for completing the "Trash" task. Unfortunately for Orange, the devious imposters have replaced the trivial click-and-drag interface with a complex two-dimensional grid of lattice points that Orange must travel through to get the trash lever to activate. A single move consists of Orange moving the trash lever (originally placed on a starting lattice point) to one of the four lattice points exactly 1 unit away from the current position and continuing to move until reaching the ending lattice point. Orange is known for his jittery fingers, and he absolutely cannot tolerate moving in the same direction twice in a row (e.g. he cannot move from $$$(0, 1)$$$ to $$$(0, 2)$$$ and then from $$$(0, 2)$$$ to $$$(0, 3)$$$). Overanxious Orange now wonders how long it will take him to complete his task, since his run would by default be a world record for this brand new task variant. You need to tell him the number of moves he needs to make from start to finish while keeping in mind his odd tendencies.

Input

The first line of input contains a single integer $$$C$$$ ($$$1 \leq C \leq 1000$$$) representing the number of scenarios you must consider with Orange. Each of then next $$$C$$$ lines will represent a task scenario using four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$ ($$$0 \leq a, b, c, d \leq 10^{18}$$$). Orange needs to move the trash lever from lattice point $$$(a, b)$$$ to lattice point $$$(c, d)$$$.

Output

Output $$$C$$$ lines, each with a single integer representing the number of moves required in the corresponding scenario.

Example
Input
2
1 1 3 3
0 1 1 1
Output
4
1
Note

A possible path in the first scenario is $$$(1, 1)$$$ to $$$(1, 2)$$$ to $$$(2, 2)$$$ to $$$(2, 3)$$$ to $$$(3, 3)$$$.

F. Red
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Red has gone by the admin table a few times to try to figure out where the rest of the crewmates are. From the admin table, she can view the number of people in the rooms across the ship. Red has a list of observations that include both the time of observation and the number of people Red saw in rooms at the time.

After the rest of the crewmates meet up to figure out what happened since the last meeting, each crewmate gives a series of time intervals where they were in a room that the admin table tracks. As the one responsible for manning the admin table, Crewmate Red must be able to run through these time intervals and figure out if there's some inconsistency between the number of people who say that they're in rooms and what was actually observed by the admin panel. Given the observations and time intervals, figure out the earliest time where there's an inconsistency between the number of people who claim that they're in rooms and the admin table observations. If there are no inconsistencies, output $$$-1$$$.

Input

The first line will consist of two integers $$$n$$$ and $$$q$$$ $$$(1 \leq n, q \leq 10^4)$$$, which give the number of time intervals given by crewmates and the number of observations on the admin table, respectively. The next $$$n$$$ lines consist of two integers $$$l_i, r_i$$$ $$$(1 \leq l_i \leq r_i \leq 10^{18})$$$, which means that a crewmate claims to be in a room detected by the admin table for all times on the inclusive interval $$$[l_i, r_i]$$$. The next $$$q$$$ lines consist of two integers $$$t_i, v_i$$$ $$$(1 \leq t_i \leq 10^{18}, 0 \leq v_i \leq 10^{18})$$$, which gives an admin table observation at time $$$t_i$$$ where Red saw $$$v_i$$$ people in rooms.

Output

Output a single integer, which gives the earliest time where Red saw an inconsistency between the claimed intervals and her observations.

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

For the first input, the number of people in the rooms at times $$$[1, 6]$$$ are $$$[1, 2, 2, 2, 1, 1]$$$ according to the given time intervals. The first inconsistency is at time $$$5$$$, where the admin table observation gives $$$0$$$ people, but the number of people according to the crewmate intervals is $$$1$$$. For the second input, there are no inconsistencies.

G. White
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

White has navigated through the treacherous hallways of the map to reach the admin room where she is hopefully safe from the imposters. As she went to do the id card swipe, however, she realized that she dropped her wallet somewhere on the map. After determining that it was too unsafe to go retrieve her wallet, she decides to try to salvage the task by engaging the backup verification system which lets her enter her unique id code into the system.

Trouble is that she can't seem to remember her exact id code but she does remember how the id code was computed. Given an input $$$x$$$, the id creation function creates another number $$$\phi(x)$$$ by replacing every digit $$$y$$$ in $$$x$$$ with it's 'mirror' digit, $$$9 - y$$$ (and then stripping all leading zeros). It then returns the product of $$$x$$$ and $$$\phi(x)$$$ as your id code. (For example, if $$$40$$$ was passed into the function then $$$\phi(40) = 59$$$ so the id code would be $$$40 \cdot 59 = 2360$$$). Now, White was given a range of possible inputs to the function when she first created her id and she chose the input that gave her the maximum possible integer as her id code (which in hindsight is probably the reason she can't remember her code).

Unfortunately, she is not quite sure on what interval she was first given so she gives you $$$t$$$ candidate intervals, where the $$$i$$$th interval is of the form $$$[l_i,r_i]$$$, and would like you to compute what the value of her id code would be for that interval of numbers (aka for each interval $$$[l_i,r_i]$$$, compute the maximum possible product of $$$x$$$ and $$$\phi(x)$$$ where $$$l_i \leq x \leq r_i$$$).

Input

The first line will contain a single integer, $$$t$$$, representing the number of intervals that will be given ($$$1 \leq t \leq 1000$$$).

The following $$$t$$$ lines will each contain two space-separated integers, so the $$$i$$$th line will have $$$l_i$$$ and $$$r_i$$$, where $$$1 \leq l_i \leq r_i \leq 10^{9}$$$, representing the bounds on the value of $$$x$$$ used to compute the id code for that interval.

Output

Output $$$t$$$ lines each containing a single integer representing the id code of White for that interval of numbers (the maximum product of $$$x$$$ and $$$\phi(x)$$$ where $$$l_i \leq x \leq r_i$$$).

Example
Input
3
1 7
7 10
8 14
Output
20
890
1190
Note

In the first interval of the sample test case, if you choose $$$x = 4$$$ then we get $$$\phi(x) = 5$$$ which yields us the maximum possible id code of $$$20$$$.

H. Yellow
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Yellow's senses are tingling and he believes that a crewmate is dead! He realizes that without knowing where the body is, the best course of action is to call an emergency meeting. However, one of the imposters wants to prevent him from reaching the button to buy time for their partner to silence Yellow (forever, just to be super clear and more ominous).

The map can be viewed as a grid with $$$r$$$ rows and $$$c$$$ columns. The rows are numbered from $$$1$$$ to $$$r$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$c$$$ from left to right. The cell at $$$i$$$-th row and $$$j$$$-th column is denoted as $$$(i, j)$$$. There are three types of cells.

  1. Wall: Yellow cannot go into these cells.
  2. Door: Yellow can go into these cells, but they CAN also be blocked by the imposters.
  3. Hallway: Yellow can go into these cells, and they CANNOT be blocked by the impostors.

Yellow is currently at cell $$$(1, 1)$$$. He can move between two cells if they are orthogonally connected (in other words, sharing a side). However, he cannot move to wall cells or door cells blocked by an imposter.

The imposters must not let Yellow reach cell $$$(r, c)$$$, the location of the emergency button. To do this, they can lock any number of doors that they desire to. Note that $$$(1, 1)$$$ and $$$(r, c)$$$ might be door cells (blame the wack map design, not the problem writers), and if one of them is locked, Yellow cannot reach the button.

It is imperative that the least number of doors possible is blocked, as too many locked doors will hurt the imposters by preventing their own movement.

Input

The first line of input contains two integers $$$R$$$ and $$$C$$$ ($$$2 \leq R, C \leq 1000$$$) representing the number of rows and columns in the grid. Each of the next $$$R$$$ lines contains $$$C$$$ characters, with every line representing some row of the grid. A character can be one of the following

  1. "#" represents a wall
  2. "." represents a door
  3. "@" represents a hallway
Output

Output a single integer representing the minimum number of doors that need to be closed to stop Yellow from getting to the emergency button. If this is impossible, output $$$-1$$$ instead.

Examples
Input
4 4
@..#
....
....
#..@
Output
2
Input
4 4
@..#
..#.
.#..
#..@
Output
0
Input
4 4
@@@@
..#@
.#.@
#..@
Output
-1