UTPC Contest 09-17-21 Div. 1 (Advanced)
C. Bugged Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Metal Guy is trying to create an artificial intelligence to help him process information and act as a personal assistant, but his current program has a bug! If any pair of integers sum up to a certain bugged number, the training program crashes and he must start over.

He realizes that he could simply process the data multiple times in separate sessions to avoid running into this issue. However, he's very impatient and only wants to have at most two sessions. Can he separate his data set into two such that no two numbers will sum up to the bugged number?

Input

On the first line, two integers: $$$1 \leq N \leq 10^6$$$ and $$$1 \leq S \leq 10^9$$$, indicating the size of the dataset and bugged number respectively. One the next line, $$$N$$$ integers $$$1 \leq a_i \leq 10^9$$$ representing the data set.

Output

One line with yes if he can separate the set into two, each without a pair summing to $$$S$$$, no otherwise (case insensitive).

Examples
Input
5 6
1 2 3 4 5
Output
yes
Input
8 6
1 1 1 1 3 3 3 3
Output
no

D. Cornfield Chase
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Superbman is escaping from his archrival Rex Ruthor's hideout and is pursued into a large cornfield of size $$$n$$$ meters by $$$m$$$ meters. Each square meter of the cornfield is either filled with thick and tall corn stalks (noted by a '*' on the grid) or is empty (noted by '.'). Superbman is running low on energy though and needs to begin his evasive maneuver quickly.

From recent memory, he has found that Rex tends to be lost whenever he makes sharp turns so he decides to make a right triangle with his flight path in order to completely bamboozle Rex. However, he wants to make the three turns (corresponding to the vertices of the triangle) in only the squares which contain the corn stalks. Additionally, Superbman doesn't like flying diagonally so he wants two of the sides of the triangle made by his flight path to be parallel to the sides of the square.

Given Superbman's complicated escape maneuver plan, help Superbman find the number of triangles that can be made on the cornfield which fit his specifications.

Input

The first line of input will be two space-separated integers $$$n$$$ and $$$m$$$ where $$$1 \leq n,m \leq 500$$$ and $$$n$$$ is the number of rows in the cornfield and $$$m$$$ is the number of columns.

The following $$$n$$$ lines will each contain $$$m$$$ characters (the only two possible characters are '*' and '.') to describe the field.

Output

Output a single integer, the number of triangles that satisfy Superbman's escape maneuver plan.

Examples
Input
2 2
**
*.
Output
1
Input
3 2
.*
*.
.*
Output
0

E. Ratman's Puzzle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ratman is attempting to save the city from one of his most dangerous nemeses, the Puzzler! Unfortunately, the Puzzler has distributed various bombs around Ratman's hometown, Gothem city, and Ratman must solve a series of math puzzles to defuse each bomb.

Specifically, each bomb is labelled with two numbers $$$n$$$ and $$$k$$$. For each bomb, Ratman must calculate the number of pairs of integers $$$a, b$$$ with $$$0 \leq a \lt b \lt 2^k$$$ such that $$$a \oplus b = n$$$, where $$$\oplus$$$ denotes the XOR operator. After doing so, he can plug the result into his bomb-defusing machine, which will neutralize the threat if the number is calculated correctly.

Write a program to help Ratman solve the Puzzler's riddles and defuse the bombs throughout Gothem city!

Input

The input will consist of two numbers $$$n$$$ $$$(1 \leq n \leq 10^{18})$$$ and $$$k$$$ $$$(1 \leq k \leq 60)$$$.

Output

Output the number of distinct pairs of integers $$$a, b$$$ such that $$$0 \leq a \lt b \lt 2^k$$$ and $$$a \oplus b = n$$$.

Examples
Input
7777 1
Output
0
Input
3 2
Output
2
Note

In the first sample, the only possible pair is $$$a = 0, b = 1$$$ and $$$0 \oplus 1 = 1 \neq 7777$$$.

In the second sample, there are two pairs that satisfy the above property. Specifically $$$a = 0, b = 3$$$ and $$$a = 1, b = 2$$$ both work.

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

Dr. Stronge has a lot of superhero friends, like the Hulc and Admiral USA, and he likes to keep tabs on all of their affairs (sometimes by looking into the future). Recently, Dr. Stronge has grown concerned about a brewing feud between the heroes, that could erupt into a full-blown civil war! But in order for a civil war to break out among the superheroes, there would need to be something powerful dividing them all...

To investigate this possibility, Dr. Stronge has sent a survey out to his $$$N$$$ superhero friends, asking each of them their favorite number $$$f_i$$$, and everyone responded. Dr. Stronge believes that a civil war is imminent if there exists some $$$d=a^p$$$ where integers $$$a, p \geq 2$$$ such that $$$d$$$ divides $$$f_i$$$ for all $$$i$$$. In other words, he wants to know whether each superhero's favorite number is divisible by a common divisor that is equal to some number taken to a higher power.

Help Dr. Stronge predict whether or not there will be a civil war! Given $$$N$$$ superhero favorite numbers, output the largest value of $$$d$$$ such that it divides all of the heroes' favorite numbers and is of the form $$$a^p$$$ for some $$$a, p \geq 2$$$, or output NO CIVIL WAR if there is no such divisor $$$d$$$.

Input

The first line of input contains an integer $$$N$$$ ($$$1 \leq N \leq 10^3$$$) denoting the number of superheroes surveyed by Dr. Stronge. The next line contains $$$N$$$ space-separated integers $$$f_1$$$ through $$$f_N$$$ ($$$1 \leq f_i \leq 10^6$$$) which are the superheroes' favorite numbers.

Output

On a single line, print the largest $$$d$$$ satisfying Dr. Stronge's civil war requirement, or NO CIVIL WAR if none exists.

Examples
Input
6
27 72 45 99 126 54
Output
9
Input
3
100 6 14
Output
NO CIVIL WAR

G. Spar-Lord's Voyage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Wicked Thamos is plotting to take over the universe after he becomes extremely powerful by collecting all of the legendary Eternity Rocks. However, our hero Spar-Lord refuses to let that happen without putting up a fight! Spar-Lord wants to obtain as many Eternity Rocks as possible, to keep them out of Thamos's possession.

Spar-Lord has planned out a space voyage to visit $$$N$$$ planets in order before he will finally rendezvous with the rest of his crew—the Custodians of the Galaxy. On the $$$i^{th}$$$ planet, Spar-Lord has the opportunity to obtain $$$r_i$$$ Eternity Rocks if he descends onto the planet and battles the enemies that are hoarding the rocks. However, if he does so, he will need to spend some time on his ship recovering from the battle, so he will not be able to descend onto any of the next $$$k$$$ planets, with indices $$$i+1$$$ through $$$i+k$$$. Spar-Lord also has the option to descend onto the $$$i^{th}$$$ planet for only a quick skirmish, where he will obtain $$$\frac{r_i}{2}$$$ Eternity Rocks rounded down to the nearest integer but only need to recover from the battle while passing over the next $$$\frac{k}{2}$$$ planets, also rounded down to the nearest integer. Of course, Spar-Lord can always choose not to descend onto a planet, and save his energy for a future, more critical battle.

Help Spar-Lord make the right choices to obtain as many Eternity Rocks as possible! Output the maximum number of Eternity Rocks that Spar-Lord will be able to collect.

Input

The first line of input contains the integers $$$N$$$ and $$$k$$$ ($$$1 \leq k \leq N \leq 10^6 $$$), denoting the number of planets on Spar-Lord's voyage and his battle recovery constant respectively. The next line contains $$$N$$$ space-separated integers $$$r_0$$$ through $$$r_N$$$, where $$$r_i$$$ $$$(0 \leq r_i \leq 10^3)$$$ is the number of Eternity Rocks available on the $$$i^{th}$$$ planet.

Output

Print the maximum number of Eternity Rocks that Spar-Lord should be able to accumulate along his space voyage.

Example
Input
8 2
135 466 904 575 492 259 796 317
Output
1767

H. Land Bridge
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An evil supervillain has stranded some innocent civilians in an archipelago! Luckily, the legendary flying superhero Agamemnon has come to save the day!

The archipelago can be mapped in an $$$N \times N$$$ grid. The civilians are stuck at location $$$(1,1)$$$ and Agamemnon has spotted a helipad at $$$(N, N)$$$.

Agamemnon has a special ability: he can build land between these islands, effectively replacing a square of water with land. By connecting the civilians to the helipad (they can only travel in the $$$4$$$ cardinal directions), he hopes to quickly evacuate them.

However, as is common with many omnipotent individuals, he has grown incredibly lazy and despite wanting to help, wants to expend minimal effort, so he can resume feeding his addiction to competitive programming.

Given an $$$N \times N$$$ map of the region and which cells are land and water, help Agamemnon determine the smallest amount of land he must create to effectively evacuate the civilians.

Input

The first line contains a single number, $$$1 \leq N \leq 500$$$. The next $$$N$$$ lines display the grid, containing $$$N$$$ digits each, either $$$0$$$ for water or $$$1$$$ for land.

Output

A single integer, the smallest amount of land needed to evacuate the civilians.

Example
Input
5
11010
00000
01011
01000
01101
Output
2
Note

The civilians $$$(1,1)$$$ and helipad $$$(N, N)$$$ will always be on land.

I. Sling Ring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Stephen finds a slightly defective sling ring while training at Kamar-Taj that will open portals, but going through each portal takes time rather than being instant. Portals are bidirectional so that someone on either side can get through to the other side. To practice, he opens a bunch of portals between various locations of interest. However, he isn't well trained enough yet to maintain all of the portals, so the portals begin to collapse over time.

Stephen wonders how quickly he can get between any two arbitrary locations as the portals are collapsing. Can you help him?

Input

First line contains $$$N, M, Q$$$, the number of locations, portals, and queries. $$$ 2 \leq N \leq 300, 1 \leq M \leq Q \leq 10000$$$

The next $$$M$$$ lines are in the form $$$u_i$$$ $$$v_i$$$ $$$w_i$$$, indicating a portal from location $$$u_i$$$ to $$$v_i$$$ (0 indexed) that takes $$$w_i$$$, $$$0 \leq u_i, v_i \lt N$$$ and $$$0 \leq w_i \leq 10000$$$.

Then the next $$$Q$$$ lines are in the form $$$q$$$ $$$u_i$$$ $$$v_i$$$, where $$$q$$$ being 0 indicates a portal collapse between $$$u_i$$$ and $$$v_i$$$ and 1 for a query for fastest to get from $$$u_i$$$ to $$$v_i$$$. It is guaranteed that there will be $$$M$$$ lines where $$$q$$$ is 0, i.e. all portals will collapse.

Output

For each query where $$$q$$$ is 1, print the quickest you can get from location $$$u_i$$$ to $$$v_i$$$. If you cannot get from $$$u_i$$$ to $$$v_i$$$, print IMPOSSIBLE.

Example
Input
3 3 6
0 1 1
0 2 3
1 2 1
1 0 2
0 1 2
1 0 2
0 0 1
0 0 2
1 0 2
Output
2
3
IMPOSSIBLE

J. The Culk's Incredible Buffet
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Culk is infamous for his humongous appetite, but his doctor told him to cut back on the Hobbit-like eating habits. It goes without saying that the Culk expressed his displeasure by "CULK SMASH"ing the doctor's office before a compromise was made. Now the Culk can eat a massive meal every day, as long as it is composed of exactly $$$K$$$ distinct course types (each labeled by an integer from $$$1$$$ to $$$K$$$, inclusive) in the order that the doctor has prescribed (increasing numerical value). Since it is a Herculean task to provide the necessary dishes to the dieting Culk, the Revengers team have outsourced the task to a revolving sushi bar.

The sushi bar outputs an ordered menu of $$$N$$$ course dishes, each of course type $$$1\dots K$$$, in a rotating cycle (assume the number of cycles is infinite for this very busy sushi bar). For example, if the menu consists of courses $$$1$$$, $$$2$$$, and $$$3$$$ in that order, the first $$$7$$$ dishes outputted by the sushi bar would be $$$1$$$, $$$2$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$3$$$, and $$$1$$$. Although the head chef is being paid a handsome amount to service the needs of the Culk, they want the Culk to wait the least amount of time between the first dish served and the last dish consumed in order to minimize the chances of him wrecking the restaurant. Each course dish takes an equal amount of time to prepare, so minimizing the number of dishes served between and including the first and last dishes the Culk consumes is equivalent. Given that the Culk eats every dish type $$$1\dots K$$$ exactly once in increasing order, what is the minimum number of dishes the Culk needs to sit through to meet his dietary requirements?

Input

The first line of input contains two integers $$$N$$$ and $$$K$$$ ($$$1 \leq K \leq N \leq 500000$$$) representing the number of dishes on the menu and the number of course types. The second line of input contains $$$N$$$ positive integers at most $$$K$$$ that represent the dish types on the menu in order. It is guaranteed that there is at least one instance of dish $$$x$$$ being served on the menu for $$$1 \leq x \leq K$$$ (in other words, the Culk will be able to finish his full meal).

Output

Output a single integer representing the number of dishes that will be outputted between the first and last courses, inclusive, consumed by the Culk if his time seated at the restaurant is minimized.

Example
Input
5 4
4 3 1 2 1
Output
9
Note

For the sample input, the Culk can start by consuming the 3rd dish of the 1st cycle. The 4th dish of the 1st cycle, the 2nd dish of the 2nd cycle, and the 1st dish of the 3rd cycle complete the meal for a total of $$$1 + 1 + 3 + 4 = 9$$$ dishes.

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

Shuki is developing weapons of the future for the Wakindan army. She has created a factory line to create the alloys of the metals she wants for the weapons. She recently got a shipment of $$$v$$$ tons of vibranium and $$$s$$$ tons of steel.

Shuki arranges the metals onto her factory line by placing one ton of a specific metal into each distinct position on the line. However, Dr. Stronge was recently tweaking with some of the settings and seems to have messed up how the process works. Shuki now finds that it always takes the last two filled positions (say position $$$i$$$ and position $$$i + 1$$$ where position $$$i + 2$$$ is empty) and applies the following modification:

  • If both positions contain steel it combines the steel and turns it into vibranium and places it in position $$$i$$$.
  • For any other combination of metals, it turns the combination into steel and places it in position $$$i$$$.

If there is only one position on the line which contains an alloy, then that is the alloy that Shuki is then left with.

Help Shuki figure out how many starting arrangements of the metals (this can be quite large so you can give it modulo $$$10^{9} + 7$$$) will lead to the ending alloy being of type $$$g$$$.

Input

The first and only line of input contains $$$3$$$ space-separated integers $$$s$$$, $$$v$$$, and $$$g$$$ ($$$0 \leq s, v \leq 10^{5}$$$ and $$$s + v \geq 1$$$ and then $$$0 \leq g \leq 1$$$) representing the number of tons of vibranium, the number of tons of steel, and the goal alloy type. For conciseness, the goal alloy type will be $$$0$$$ if you want steel and $$$1$$$ if you want vibranium.

Output

Print how many starting arrangements of the metals will lead to Shuki ending up with the alloy of type $$$g$$$ (modulo $$$10^{9} + 7$$$).

Examples
Input
1 1 0
Output
2
Input
1 1 1
Output
0
Input
2 2 0
Output
4

L. Space Tourism
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Galesa and her group of fellow lovable misfits, together known as the Saviours of the Solar System, are attempting to make a little extra profit by shuttling groups of paying customers between locations in the star system.

Travel in the star system is heavily regulated by the Galactic Council, so Galesa and her crew cannot travel arbitrarily between the $$$n$$$ locations in the star system. Rather, the available paths between the $$$n$$$ locations form a tree, so there is a unique path that Galesa must take between every two distinct locations.

Each of the $$$n$$$ locations have an associated enjoyment value $$$e_i$$$, which gives the amount of overall enjoyment that Saviours of the Solar System derive from traveling that location $$$i$$$. The overall enjoyment of traveling between two locations $$$i$$$ and $$$j$$$ is the product of all the enjoyment values for each location on the path between locations $$$i$$$ and $$$j$$$. Note that the Saviours of the Solar System still derive enjoyment from staying at a single location and only collecting the $$$e_i$$$ for that location, so this would still count as a path.

Given that Galesa and the Saviours of the Solar System are going to be going on a lot of these trips, they want to calculate the overall enjoyment that they'll experience. Therefore, they want you to calculate the sum of the enjoyment values for each possible path that they could take throughout the star system. Because this value is large, output the answer mod $$$10^9 + 7$$$.

Input

The first line consists of a single integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$. The next line consists of $$$n$$$ integers $$$e_i$$$ $$$(1 \leq e_i \leq 10^5)$$$, which gives the enjoyment value for each of the $$$i$$$ locations in the star system. The next $$$n - 1$$$ lines consist of two integers $$$u_i, v_i$$$ $$$1 \leq u_i, v_i \leq n$$$, which indicates that Galesa can travel between locations $$$u_i$$$ and $$$v_i$$$.

Output

Output a single integer giving the sum of the enjoyment values for each possible path that Galesa and the Saviours of the Solar System could take throughout the star system mod $$$10^9 + 7$$$

Examples
Input
3
5 2 3
1 2
1 3
Output
65
Input
5
5 2 3 4 6
1 2
1 3
2 4
3 5
Output
1251
Note

For the first sample, there are paths with overall enjoyment $$$5, 2, 3, 10, 15, 30$$$, which together sum to $$$65$$$.

M. Ominous Chess
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor Y and Magnato have entered a showdown with the fate of humanity decided by the winner. After exhausting themselves in a physical fight that ended evenly, they decide to settle the winner through $$$t$$$ games of their favorite childhood game. The game is a variant on simple chess designed as follows:

At the start of a single game, you use a board with $$$n$$$ rows and $$$m$$$ columns and each player has $$$n$$$ rook pieces. Then, for each of the $$$n$$$ rows, a third-party neutral robot (humans can't be trusted to be objective with their future on the line) will choose two distinct columns at random and place one of Professor's rooks in the position of the grid defined by that row and one of the chosen columns and one of Magnato's rooks in the position of the grid defined by that row and the other chosen column. After the game is set up, the players alternate taking turns and on a single turn, they can choose one of their rooks and move it left or right any number of spaces to another column in that row (and within the boundaries of the grid). Furthermore, one player may not move their rook past the other player's rook or into the column where the other player's rook is currently residing. When a player can no longer move any of their rooks, the other player will be considered the winner of that game.

Given that Professor Y will always go first and both players will use optimal strategies, output the winner of each of their $$$t$$$ games.

Input

The first line of input will have a single integer $$$t$$$ representing the number of games played ($$$1 \leq t \leq 50$$$).

For each of the $$$t$$$ games, the first line will contain two space-separated integers $$$n$$$ and $$$m$$$ representing the number of rows and number of columns of the board ($$$1 \leq n \leq 2 \cdot 10^{5}$$$ and $$$1 \leq m \leq 10^{18}$$$). It will then be followed by $$$n$$$ lines with the $$$i$$$th line containing $$$2$$$ space-separated integers $$$c_i$$$ and $$$p_i$$$ representing the column that Professor's rook is located at and the column that Magnatoo's rook is located at in the $$$i$$$th row ($$$1 \leq c_i, p_i \leq m$$$ and $$$c_i \neq p_i$$$).

It is guaranteed that the sum of the number of rows over all the test cases will be less than or equal to $$$2 \cdot 10^{5}$$$.

Output

For each of the $$$t$$$ games, output Professor Y or Magnato depending on who wins that game if both players use optimal strategies.

Example
Input
2
2 5
1 3
2 4
3 10
2 6
10 5
3 9
Output
Magnato
Professor Y