Code Rush 2025
A. A Factory Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sebastián and Sebastian are playing a game.

The game consists of counting the number of materials and submaterials delivered to their factory while unloading a truck. The winner of the game will be the one who remembers most accurately how many materials and submaterials were received. The deal is that the loser has to take the winner out to dinner. Since neither of them wants to lose, both decide to cheat and secretly take notes to ensure their victory.

Each delivery is represented by three integers: the product, the material, and a corresponding submaterial. The full list contains N lines, each one representing a single delivery. But something went wrong. Due to a technical failure, the factory's official system lost all delivery records. Now, the only source of truth is the list that both Sebastián's created even if it contains repeated entries. They need your help to analyze their combined notes and generate a clean summary.

Input

First line contains one integer $$$n$$$ ($$$1$$$ $$$\le$$$ $$$n$$$ $$$\le$$$ $$$10^6$$$), indicating the size of the combined list of Sebastián and Sebastian. Then n lines contains three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1$$$ $$$\le$$$ $$$a$$$, $$$b$$$, $$$c$$$ $$$\le$$$ $$$10^{18}$$$), the product id, material and sub material received.

Output

For each product, print one line with three integers $$$A$$$, $$$B$$$, and $$$C$$$, where $$$A$$$ is the product id, $$$B$$$ is the number of distinct materials associated with that product, and $$$C$$$ is the number of distinct submaterials associated with that product. The products must be printed in increasing order.

Examples
Input
10
1 1 1
2 2 1
3 4 1
2 2 2
2 2 1
1 3 12
1 1 2
1 4 1
1 4 2
1 4 3
Output
1 3 6
2 1 2
3 1 1
Input
3
186 312 851372492
291 103 93522568
757 156 500988176
Output
186 1 1
291 1 1
757 1 1

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

Great! Now that you have a working neural net activation function, Sebastian got another task for you to make. Byte-Pair Encoding ($$$BPE$$$) is an algorithm commonly used in LLM tokenizers (such as GPT-4, Gemini, and other stuff that probably wrote your homework at 11pm last Friday). $$$BPE$$$ encodes large strings into smaller strings by using a translation table, which is based on a huuuge dataset.

However, Sebastian found something interesting in this dataset. Turns out that this entire string $$$s$$$ could be represented as the repeated concatenation of another string $$$t$$$! More formally, this means that there's an integer $$$k$$$ so that $$$s = t\mathbin\Vert t\mathbin\Vert ...\mathbin\Vert t$$$ ($$$t$$$ concatenated $$$k$$$ times). Can you help Sebastian find the smallest string $$$t$$$ where this is true?

Input

The first and only line of input contains $$$s$$$ $$$(1 \leq |s| \leq 10^5)$$$ — a contiguous (without any spaces) string that represents Sebastian's dataset.

Output

Print a single string — the string with shortest length $$$t$$$ where there exists an integer $$$k$$$ so that $$$t$$$ concatenated with itself $$$k$$$ times is $$$s$$$.

Example
Input
td
Output
td

C. Chess in 3D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the mystical kingdom of TriDChess, the ancient game of chess has evolved into a thrilling three—dimensional contest of strategy and intellect. The game is played on a board with dimensions $$$A \times B \times C$$$, where each of the three axes represents a spatial dimension. This board forms a cuboid, filled with possibilities for cunning moves and strategic placements.

The piece of focus in this game is the 3D Knight, a nimble entity that combines the powers of traditional chess knights with the freedom of movement afforded by the third dimension. These 3D Knights have the ability to move in an L—shape across the board, jumping over other pieces. Their movement is defined as follows:

1. L—Shaped Movement: A 3D Knight can move in an L—shape from its current position $$$(x, y, z)$$$:

  • Move two squares along one axis and one square along another axis:

  • $$$(x \pm 2, y \pm 1, z)$$$, $$$(x \pm 2, y, z \pm 1)$$$, $$$(x \pm 1, y \pm 2, z)$$$, $$$(x, y \pm 2, z \pm 1)$$$, $$$(x \pm 1, y, z \pm 2)$$$, $$$(x, y \pm 1, z \pm 2)$$$.

2. The Knight's jump allows it to move over other pieces, but it must land within the bounds of the board, where $$$1 \leq x \leq A$$$, $$$1 \leq y \leq B$$$, $$$1 \leq z \leq C$$$.

Additionally, there are between 1 and 500 blocked cells on the board where Knights cannot be placed. These cells are specified by their coordinates and must be avoided when placing Knights.

The challenge you face is to determine the maximum number of these agile 3D Knights that can be placed on the cuboid board such that no two Knights are capable of attacking each other. This means no two Knights should be in a position where one could reach the other in a single move.

Input

The first line contains three integers $$$A$$$, $$$B$$$, and $$$C$$$ ($$$1 \leq A, B, C \leq 10$$$), representing the dimensions of the 3D board.

The second line contains an integer $$$K$$$ ($$$1 \leq K \leq 500$$$), representing the number of blocked cells.

The next $$$K$$$ lines each contain three integers $$$x, y, z$$$ ($$$1 \leq x \leq A$$$, $$$1 \leq y \leq B$$$, $$$1 \leq z \leq C$$$), representing the coordinates of a blocked cell.

Output

Output a single integer that represents the maximum number of 3D Knights that can be positioned on the $$$A \times B \times C$$$ board without any two Knights threatening one another.

Example
Input
3 3 3
1
2 2 2
Output
14

D. Distinct Token Arrangements
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sergio must leave for an urgent matter, so he invents a game to keep his little brother Javier busy. In the game, Javier is given two kinds of tokens:

  • Unique tokens: There are $$$m$$$ tokens labeled from $$$1$$$ to $$$m$$$ (each token can be used at most once).
  • Zero tokens: There are $$$n$$$ tokens, all labeled $$$0$$$ (these tokens are identical; using one "$$$0$$$" is indistinguishable from another).

To finish the game Javier needs to create all the possible distinct arrangements.

An arrangement is defined as an ordered sequence of tokens of length $$$L$$$, where $$$1 \le L \le n$$$. In forming an arrangement you must obey these rules:

  • You may use a unique token at most once in any arrangement.
  • You may use the $$$0$$$ tokens as many times as needed (up to the arrangement's length; since you have exactly $$$n$$$ copies, no arrangement can have more than $$$n$$$ $$$0$$$ tokens).
  • Two arrangements are considered distinct if there exists an index $$$i$$$ such that the token in position $$$i$$$ of one arrangement is different from the token in position $$$i$$$ of the other.

Your task is to help Javier figure out how many arrangements he needs to create in order to finish the game. Because this number can be huge, output it modulo $$$10^9+7$$$.

Input

A single line containing two integers $$$m$$$ $$$(1 \le m \le 1000)$$$ and $$$n$$$ $$$(1 \le n \le 1000)$$$.

Output

Output a single line with one integer the number of distinct arrangements modulo $$$10^9 + 7$$$.

Example
Input
2 2
Output
10
Note

In the first case, if $$$m = 2$$$ and $$$n = 2$$$ the possible arrangements are:

  • Length 1: $$$[0], [1], [2]$$$
  • Length 2: $$$[0, 0], [0, 1],[1, 0],[0, 2],[2, 0],[1, 2],[2, 1]$$$
In total, there are 3 + 7 = 10 distinct arrangements.

E. Experiment with cells
time limit per test
9.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Dr senku

Dr Senku, the scientist, has discovered a new kind of cell! They are a really interesting kind of organism that interacts with electricity in weird ways. Each cell can either be red colored or blue colored. When zapped with electricity, the cell transitions from its color to the contrary, that is, a red cell will become blue and a blue cell will become red.

Senku has set the cells in a rectangular matrix of size $$$N\times M$$$. He can zap a whole column or row of the matrix, changing the color of all its cells. He can also zap an individual cell by hand, but this takes a lot of work, so he tries to avoid it.

This morning, he got a request from his university director to make it so all cells are red by the end of the day. Since he is very lazy, he first wants to know what the minimum number of times he will have to zap an individual cell to achieve this. Help him find this number!

Input

On the first line, numbers $$$N,M$$$ ($$$1\leq N\times M\leq760$$$). On the next $$$N$$$ lines, $$$M$$$ letters either "R" or "B" representing the cell is blue or red at the moment.

Output

Just a number, the minimum number of times he will have to zap an individual cell.

Examples
Input
2 2
BB
BB
Output
0
Input
2 2
BR
RR
Output
1
Note

"You get ten billion points"

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

While working at Meta AI, Sebastian developed low-latency translation models for deployment on edge devices such as mobile phones or his pretty garbage ThinkPad L390. While developing these models, he got the task of programming the popular $$$softmax$$$ function, but there's a problem: Since these low-power devices lack floating-point support, all operations must be done with integers under a large prime $$$p$$$. The softmax function is defined as: $$$$$$softmax_{int}(x_i)=\frac{2^{x_i}}{\sum_{j=1}^{n} 2^{x_j}}$$$$$$ where $$$x$$$ is a vector of (in this case) integer inputs, the denominator is the sum of all the inputs in our batch, and $$$x_i$$$ is the input to our softmax function, which is also in our input batch with an index $$$i$$$. Since Sebastian's AI model will be deployed on thousands of edge devices, he needs an extremely efficient algorithm to help him compute this function. Can you help him?

Remember that, since we need only integer outputs, we can represent $$$softmax(x_i)$$$ as a rational fraction $$$\frac{P}{Q}$$$ with $$$Q \gt 0$$$. To give this answer, you should print a single integer $$$P\times Q^{-1} \mod p$$$. Assume $$$p = 10^9 + 7$$$.

Input
  • The first line contains two integers $$$n, i$$$ ($$$1 \leq n \leq 10^5, i \leq n$$$) — the number of inputs in the batch and the index $$$i$$$ of the input to our softmax function $$$x_i$$$.
  • The second line contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_i \leq 10^9$$$), representing the input vector to our softmax function.
Output

Print a single integer — the modular softmax probability with the input $$$x_i$$$. Note that this probability must be expressed as a single integer in the form $$$P\times Q^{-1} \mod (10^9+7)$$$.

Examples
Input
1 1
643212363
Output
1
Input
1 1
279118545
Output
1

G. Game of Marbles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While having a Discord meeting, Sebastian and Sebastián discovered they have the same name! Any time another person called them out, they would both try to answer at the same time, generating confusion between everyone. To prevent this, they planned out a game so that the winner could keep the name, while the loser would change their name to Notbastian (Not Sebastian). The game works as follows:

  • $$$n$$$ marbles are placed on the ground, and Sebastian picks out the marbles with an odd number written on them.
  • On the contrary, Sebastián will pick the marbles with an even number written on them.
  • The winner will be the person with the larger amount of marbles between the two.
  • If there's a tie in the amount of marbles Sebastian and Sebastián picked, Sebastian will win, since he has a greater Codeforces rating, granting him anything he wants.
Sebastián got bored quickly with the game and put you to play in his place instead. Given $$$n$$$ marbles, print "Sebastian" if Sebastián gets to keep his name, and "Notbastian" if not.
Input

The first line of input contains $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the number of marbles placed on the ground. The second line of input contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the numbers written on the marbles.

Output

Print a single string — "Sebastian" if Sebastián gets to keep his name, and "Notbastian" if not.

Examples
Input
4
1 2 2 4
Output
Sebastian
Input
4
1 1 2 2
Output
Notbastian

H. Hiding the One Piece
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The beginning of the age of pirates

Gol D. Roger, the pirate king, conquered everything this world has to offer. Before being executed, he hid his treasure, the "One Piece" on an island on the Grand Line. The Grand line is a group of $$$N$$$ islands in the sea numerated from $$$1$$$ to $$$N-1$$$. Due to its weather conditions, the Grand line cannot be traversed as any typical sea; any travel on the Grand line has to start on the Red line, which is represented by the number $$$0$$$, and follow the sea routes. There are $$$N-1$$$ unidirectional sea routes that connect two islands. From each island, it is also possible to go back directly to the Red Line. It is known that every island can be reached from the Red line following sea Routes.

Roger will hide the $$$m$$$ maps to his treasure on islands throughout the Grand line, and each map will have to be recovered in order and then brought back to the Red line before you can go to the next one. To make sure the journey to the One Piece is as fun as it should be, Roger wants to know $$$\sum b_i$$$ where $$$b_i$$$ represents how many times a pirate crew that started on the Red line, and has followed the shortest path that follows the conditions (Get the maps in order, always return to the Red line after recovering a map), has passed through the island $$$b_i$$$ before collecting map $$$i$$$.

Input

A number $$$N$$$ ($$$1\leq N\leq 100000$$$), indicating the number of islands on the Grand line. On the next $$$N-1$$$ lines, two numbers $$$a_i$$$ and $$$b_i$$$ indicating there is a sea route from $$$a_i$$$ to $$$b_i$$$ On the next line is a number $$$m$$$ ($$$1\leq m\leq 100000$$$), the number of maps Roger will hide. On the final line, $$$m$$$ numbers $$$a_i$$$ ($$$1\leq a_i\leq N-1$$$), indicating the island where the $$$i$$$-th map will be hidden.

Output

A number $$$B$$$ that indicates the sum of all $$$b_i$$$

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

"I will be the king of the pirates"

I. Integer dyslexia
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Paulino, while writing new problems, realized that he suffers from integer dyslexia. Paulino's integer dyslexia is very unusual because when he writes a sequence of numbers, he often starts with a different digit instead of the first one and then appends the skipped digits at the end. For example, he might write 123 as 231 or 312.

Given the correct number and the number that Paulino wrote, can you determine if they represent the same sequence?

Input

The input consists of two numbers N and P, each with a maximum length of 100,000 digits. N is the correctly written number, and P is the one written by Paulino.

Output

Print "YES" if the two numbers are the same; otherwise, print "NO" (without quotes).

Examples
Input
123 231
Output
YES
Input
123 213
Output
NO

J. Join the art class
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Appa and Winnie are dogs with very different passions. While Winnie loves drawing, Appa is fascinated by mathematics.

Seeing her love for drawing, Winnie decides to join the Public Art Classes, where the first lesson is to create geometric figures using only '.', '/', and '\', as shown below:

Winnie's drawing.

Since Appa does not have the opportunity to join any course, he decides to take Winnie's drawings and calculate the area of the shapes. However, he knows nothing about mathematics, except that each '.' represents a 1×1 unit. So he asks for your help in calculating the area of the shapes Winnie draws.

Input

The first line contains two integers, N and M ($$$2 \leq N, M \leq 100$$$), indicating the width and height of the matrix. Next, M lines of size N will be read, containing the characters '.', '/', and '\'. There will always be a figure for which the area must be calculated.

Output

A single line containing the area of the figure, printed with two decimal places.

Example
Input
14 12
....../\......
...../..\.....
..../....\....
.../......\...
../........\..
./..........\.
.\........../.
..\......../..
...\....../...
....\..../....
.....\../.....
......\/......
Output
72.00

K. K-token Language Optimization
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sebastian is working on optimizing a system for Automatic Speech Recognition (ASR), and he's faced with a challenging task in improving the efficiency of speech data segmentation. Given a sequence of integer tokens representing features extracted from speech, his goal is to partition this sequence into the smallest number of contiguous subarrays such that each subarray contains at most $$$k$$$ unique elements.

In the context of ASR, each subarray represents a segment of speech, and the number of unique tokens in a segment must be limited to prevent overwhelming the system's memory and computational resources. Sebastian needs to minimize the number of such segments required while ensuring that each segment remains within the constraints for efficient processing.

Given an array $$$a$$$ of size $$$n$$$ (where $$$a_i$$$ is the token representing a feature in the ASR system) and an integer $$$k$$$ (the maximum number of unique tokens allowed in a segment), Sebastian must determine the minimum number of subarrays such that each subarray contains at most $$$k$$$ unique tokens.

Input
  • The first line contains two integers $$$n, k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq min(1000, n)$$$) — the size of the array and the maximum amount of unique tokens each subarray can contain.
  • The second line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq 1000$$$) representing the array of tokens.
Output

An integer representing the minimum number of contiguous subarrays that can be formed, each containing at most $$$k$$$ unique elements.

Examples
Input
3 1
1 2 3
Output
3
Input
3 2
1 2 3
Output
2
Input
3 1
1 1 1
Output
1

L. Los Ratones
time limit per test
0.9 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

German is a passionate fan of "Los Ratones" and has just finished watching all their matches from last year. This year, however, he hasn't seen any matches yet. The list a represents the number of times Bausffs died in each match this year.

German is particularly intrigued by Bausffs's unique playing style. He wants to watch matches that start in a specific way, represented by the list b, which details how he prefers to see Bausffs die each day. Additionally, he wants these matches to end in a way that makes him happy, represented by the list c. He calls this way of watching the games Gerbausffs's Law.

German is eager to start watching the matches but doesn't have the time to figure out all the different ways he can watch matches that begin in the form b and end in the form c.

Please help German because his friend Wiritos didn't want to help him.

The GOAT.
Input

On the first line, tree numbers $$$n,m,k$$$ indicating the size of array $$$a$$$ ($$$1\leq n \leq 100000$$$) ($$$1\leq m,k \leq n$$$).

Second line contains $$$n$$$ numbers ($$$ 1 \leq a_i \leq 10000 $$$)

Third line contains $$$m$$$ numbers ($$$ 1 \leq b_i \leq 10000 $$$)

Fourth line contains $$$k$$$ numbers ($$$ 1 \leq c_i \leq 10000$$$)

Output

How many distinct Gerbausffs's law exists

Example
Input
8 1 1
135 136 135 136 135 136 135 136
135
136
Output
10
Note

"Why am I not good if I also die the same number of times as Bausffs?"

M. Math lesson
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor S is teaching his students A, T, and R the arcane arts of mathematical magic (otherwise known as mathgic). Part of this is learning to draw geometric figures, so they have to complete the following exercise.

They were be given a 2D plane, first A drew $$$n$$$ vertical lines $$$X=x_0, X=x_1,\ldots,X=x_{n-1}$$$. Afterwards J drew $$$m$$$ horiontal lines $$$Y=y_0,Y=y_1,\ldots, Y=y_{m-1}$$$. Finally R drew a diagonal line $$$D$$$ from point $$$(h,0)$$$ to $$$(h+l,l)$$$.

The challenge that was then given to them is:

For each point that the line $$$D$$$ has integer coordinates, a ray is drawn from there perpendicular to the line and to the right. i.e. from point $$$(x,0)$$$ the ray goes in direction $$$(x-1,1)$$$. Then answer, which line will it intersect first?

Input

On the first line, two numbers $$$N,M$$$ such that $$$1\leq N,M,N+M\leq 10^6 $$$

On the second line $$$N$$$ integers $$$x_0,x_1,\ldots,x_{n-1}$$$ ($$$0\leq x_i\leq 2\times10^6$$$) representing the $$$N$$$ lines drawn by A, all of them diferent.

On the third line $$$M$$$ integers $$$y_0,y_1,\ldots,y_{m-1}$$$ ($$$0\leq y_i \leq 2\times10^6$$$) representing the $$$M$$$ lines drawn by T, all of them diferent.

In the fourth and last line, the two numbers $$$0\leq h,l \leq 10^6$$$.

Output

The answers from the challenge in the order of the points from were the rays are drawn, from left to right. If the first line intersected is vertical print the letter A followed by the index of the line. Otherwise, if the first line is horizontal, print the letter T followed by the index of the line. In case of ties where it intersects a horizontal and vertical line at the same time, print the horizontal one. In case the ray doesn't intersect any line, print -1.

Example
Input
1 1
1
3
0 3
Output
T0 A0 A0 T0 

N. Nature's Delights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The Malinche mountain, in Puebla

Sergio is an avid hiker who loves exploring mountain trails and deep valleys. On his latest adventure, he recorded the elevation at three consecutive points along his path. Now, he wants to determine whether he just climbed over a peak or descended into a valley.

Given three integers $$$a, b, c$$$ representing the elevations at three consecutive points along Sergio's hike, determine whether these points form a mountain (a peak) or a valley:

  • A mountain occurs if $$$a \lt b \gt c$$$ (i.e., the middle elevation is the highest).
  • A valley occurs if $$$a \gt b \lt c$$$ (i.e., the middle elevation is the lowest).

Can you help Sergio determine if he climbed a mountain or a valley?

Input

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of hikes Sergio did. Each of the next $$$t$$$ lines contains 3 integers $$$a, b, c$$$ ($$$1 \leq a, b, c \leq 1000$$$) — the recorded elevations.

Output

Output $$$t$$$ strings — MOUNTAIN if Sergio climbed a mountain, or VALLEY if Sergio climbed a valley. It is guaranteed that the elevations will correspond to one of these categories.

Examples
Input
1
1 2 1
Output
MOUNTAIN
Input
2
1 2 1
3 1 2
Output
MOUNTAIN
VALLEY

O. Obfuscation technique
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the near future, an elite team of cybersecurity researchers known as the Digital Sentinels discovers that a clandestine group has developed a sophisticated obfuscation technique to hide messages within sentences. This technique allows critical information to go unnoticed, camouflaged among seemingly harmless data.

During an undercover operation, the Sentinels intercept a text file that, at first glance, appears to contain only random characters. However, they suspect that this file hides a deactivation code essential to stop an out-of-control artificial intelligence threatening to take over global infrastructures. The string of text they have detected is 45 4c 20 43 4f 44 49 47 4f 20 44 45 20 44 45 53 41 43 54 49 56 41 43 49 4f 4e 20 45 53 3a 20 22 43 4f 44 45 3a 52 55 53 48 3a 54 45 43 22.

As a member of the Digital Sentinels, you are tasked with decrypting this code to stop the artificial intelligence that is about to cause disruption in the global network. Given this message, output whatever the message requests from you.

Input

This is an only exit problem, so there is no input

Output

Whatever the message requests from you :)