Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Gran Premio de México 2023 - Primera Fecha

May 13th, 2023

Problems book

General Information

This problem set contains 11 problems; pages are numbered from 1 to 15, Without considering this page. Please,
verify your book is complete.

A) About your program


1) The code submitted to resolve a problem should be named : problem code.c, problem code.cpp, pro-
blem code.java or problem code.py, problem code.kt, where problem code is the uppercase letter that identifies
the problem. Remember that in Java the name of the main class should be the same as the name of the file.

B) About Input
1) Your program should read the input from standard input.
2) When an input line contains more than one value, these values are separated by a single white space. The
input does not contain any other white spaces.
3) Each line of input, including the last one contains exactly only one end of line character.
4) End of input is on the end of file.

C) About output
1) The output of your program should be written to standard output.
2) When an output line contains more than one value, these values should be separated by a white space. The
output does not contain any other white space.
3) Each line of output, including the last one, should contain exactly one end of line character.

v1.1
Gran Premio de México 2023 - Primera Fecha 1

Problem A
Aliases
You are designing a new system called NAME (Notable Aliases with Meaning and Essence) that
transforms people’s names into meaningful aliases. The process involves taking the first letter of each
name.
For example, the name ”Jose Osorio Jimenez Orozco”would have the NAME ”JOJO”.
However, a problem arises if two people have the same NAME, as it loses its essence and is no
longer unique.
Given a list of full names for all the members, your task is to determine how many unique NAMEs
there are.

Input
An integer N describes the number of names of the members (1 ≤ N ≤ 10000). Following will be
N lines, each one starting with an integer Xi (1 ≤ Xi ≤ 20) describing total words in the full name of
the person i, following that there will be Xi words representing the name of the person i. Each word
is consisting on only lowercase letters.

Output
A single integer representing the number of uniques NAMEs.

Input example 1 Output example 1


2 2
2 ivan ramirez
2 franco borquez

Input example 2 Output example 2


3 1
4 jose osorio jimenez orozco
4 juan orlando jay ocampo
2 juan perez
Gran Premio de México 2023 - Primera Fecha 2

Problem B
Bucket Storing
Once upon a time, in the quaint town of Algorithmville, there lived a young man named Ethan. Ethan
had an extraordinary passion and skill for packing coffee beans. He used two types of buckets to pack
coffee: standard buckets, which could hold up to K coffee beans, and ceremonial buckets, which were
larger and could hold up to L coffee beans.
On this particular day, Ethan packed coffee using standard coffee buckets. With his exceptional
packaging skills, he filled N standard buckets to the brim, meaning each of the N buckets contained
the maximum K coffee beans it could hold. As closing time approached, Ethan glanced at the calendar
and realized that he had missed International Coffee Day, which was to take place the following day.
What a shame it would be to serve coffee using his standard coffee buckets!
Determined not to waste any time, Ethan decided to transfer all the coffee from the standard
buckets to ceremonial buckets. Seeking assistance, he asked for your help. Can you tell Ethan how
many ceremonial buckets he needs to move the coffee from the standard buckets to the ceremonial
ones?

Input
The first and only line of input contains three integer numbers separated by a space, representing N ,
(1 ≤ N ≤ 1000) the number of standard buckets Ethan used to pack the coffee. K, the coffee beans a
standard bucket can hold, and L, the coffee beans the ceremonial bucket can hold (1 ≤ K ≤ L ≤ 1000)

Output
Print a line with a single integer number, the amount of ceremonial buckets Ethan needs to move
the coffee from the standard buckets to ceremonial buckets.

Input example 1 Output example 1


2 3 6 1

Input example 2 Output example 2


3 5 4 4

Input example 3 Output example 3


1000 500 1000 500
Gran Premio de México 2023 - Primera Fecha 3

Problem C
Counting Stars
A constellation is a group of stars that form a recognizable pattern in the night sky. Legendary Huron,
who is an amateur astronomer, wants to identify its own constellation: a spoke constellation.
Legendary Huron identified a region in the celestial sphere where no star belong to any recognized
constellation. After taking a photography of this region of the sky, he identified n stars and assigned
them coordinates (x, y). He defines a spoke constellation as a group of mk + 1 stars (0 ≤ m, k) in this
photography that meets the following properties:

• There is a star C in the group called the central star.

• Let d1 , d2 , . . . , dk be the list of different euclidean distances between C and the rest of stars in
the group such that d1 < d2 < · · · < dk . There are exactly m stars in the group that are at
distance di to C (for 1 ≤ i ≤ k). Recall that the euclidean p distance between two points with
coordinates (x1 , y1 ) and (x2 , y2 ), respectively, is equal to (x1 − x2 )2 + (y1 − y2 )2 .

• Let pi,1 , pi,2 , . . . , pi,m be the list of stars in the group that are at distance di to C. There are
exactly i + 1 stars in the segment between pi,j and C (including pi,j and C) for all 1 ≤ i ≤ k and
1 ≤ j ≤ m.

• All the stars in the segment between pi,j and C are included in the group for all 1 ≤ i ≤ k and
1 ≤ j ≤ m.

Help Legendary Huron to count the number of groups that can form a spoke constellation. Two
groups of stars are considered different if the central star is different in the two groups or if there is
an star that is included in one group but not in the other.

Input
The first line contains a single ineteger n (1 ≤ n ≤ 1000).
Each following n lines contains two integers x and y (−109 ≤ x, y ≤ 109 ) − the coordinates of the
stars.
It is guaranteed that all points are different.

Output
Print the number of groups of stars that forms a spoke constellation modulo 998244353.

Input example 1 Output example 1


2 4
0 0
1 1

Input example 2 Output example 2


6 46
2 0
0 0
0 2
1 0
-2 0
0 -2
Gran Premio de México 2023 - Primera Fecha 4

Input example 3 Output example 3


9 139
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
Gran Premio de México 2023 - Primera Fecha 5

Problem D
Dynamic Collection
You are given a collection of elements, each of which is a positive integer. Duplicate elements are
allowed in this collection. There are two types of operations that you can perform on this collection.
Operation 1 is for updating the collection, and Operation 2 is for querying the collection. The format
for these operations are as follows:

• Operation 1: “1 k”

• Operation 2: “2 a b”

For Operation 1, k is an integer upon which the following rules will be applied in order:

1. If the element k is already in the collection, the collection remains the same.

2. If the element k is greater than any of the elements in the collection, it is added to the collection.

3. The first occurrence of the smallest element greater than k is replaced by k.

For Operation 2, a and b are two integers that define a range. The purpose of this operation is to
query how many elements in the collection fall within this range (inclusive).

Input
The input consists of a single test case.
The first line contains two positive integers, n and q (1 ≤ n ≤ 106 ; 1 ≤ q ≤ 106 ), which represent
the number of elements in the collection and the total number of operations to be performed on the
collection, respectively.
The second line contains exactly n space-separated integers a1 , a2 , ..., aN (1 ≤ ai ≤ 109 ).
The next q lines each represent an operation. Each operation is either of type 1 or type 2, and
follows the format: “1 k” or “2 a b”, (1 ≤ a, b, k ≤ 109 ; a ≤ b)

Output
For each Operation 2 (query operation) in the input, print a single line containing a positive integer,
which represents the total number of elements in the collection that fall within the range [a, b].

Input example 1 Output example 1


10 11 5
7 1 7 1 3 9 7 9 10 4 6
2 2 8 10
1 8 11
2 2 8 6
2 1 20 5
1 20 6
2 1 20
2 7 12
1 5
2 7 12
1 12
2 7 12
Gran Premio de México 2023 - Primera Fecha 6

Note
Let’s consider the first four operations in the example:
The initial collection is the following:
C = {7, 1, 7, 1, 3, 9, 7, 9, 10, 4}
After the operation “2 2 8” the output is 5.
After the operation “1 8” the collection becomes:
C = {7, 1, 7, 1, 3, 8, 7, 9, 10, 4}
Now, after operation “2 2 8” the output is 6.
After the operation “1 20” the collection becomes:
C = {7, 1, 7, 1, 3, 8, 7, 9, 10, 4, 20}
Gran Premio de México 2023 - Primera Fecha 7

Problem E
Employees Bonus
In a large global corporation, Acme Corp, with N employees, each employee has a clearly defined
hierarchy in the company’s organizational chart. Each employee is the head of a department and may
have a variable number of subordinates working directly or indirectly under their supervision.
Acme Corp has a peculiar way of distributing bonuses: when an employee receives a bonus intended
for their department, they divide it evenly among all employees who are directly or indirectly under
their command (including themselves in the distribution). If the bonus amount does not divide evenly
among all subordinates, the department head keeps the remainder.
The company has received Q bonuses that will be distributed among the employees according to
the aforementioned policy.
Each employee has a specific amount of money that they perceive as financial success.
Your task is to determine, for each employee, the first bonus (i.e., the order number of the bonus
in the sequence of Q bonuses) at which they achieved financial success.

Input
The input consists of several lines:
The first line contains two integers, N and Q (1 ≤ N ≤ 105 ; 1 ≤ Q ≤ 105 ), the number of employees
in the company and the number of bonuses respectively.
The second line contains N integers, a1 , a2 , ..., aN (1 ≤ ai ≤ 109 ), where ai represents the amount
of money that the i-th employee perceives as financial success.
The next N − 1 lines each contain two integers, u and v (1 ≤ u, v ≤ N ; u 6= v), which represent
that employee v is a direct subordinate of employee u.
The next Q lines each contain two integers, x and b (1 ≤ x ≤ N ; 1 ≤ b ≤ 109 ), where x represents
the employee to whom the bonus is given and b the amount of the bonus.

Output
Print N lines, where the i-th line contains an integer indicating the first bonus at which the i-th
employee achieved their financial success. If the i-th employee does not achieve their financial success,
print -1.

Input example 1 Output example 1


5 3 1
100 200 300 400 500 1
1 2 3
1 3 2
2 4 2
2 5
1 1000
2 1500
3 2000
Gran Premio de México 2023 - Primera Fecha 8

Problem F
Finding the Best Guess
Huron Casino is running a strange gambling game on trees. The game consists on guessing the final
value of the number S generated for the algorithm described below:
Given a tree with n nodes, where each node has an integer ai written on it, and an integer S
initially equal to 0. The algorithm consists on repeating the following steps n times:

1. Choose a random node u that has not been removed yet. All nodes that have not been removed
have the same probability of been chosen.

2. Add av to S for all the nodes v, such that there is a path from u to v passing through nodes
and edges that have not been removed yet.

3. Remove u and all the edges incident to it.

The game is played for a certain number of participants. The player (or players) whose guess is
closer to the final value of S is the winner of the game.
You are an expert on greedy algorithms, for that reason you think that betting for the expected
value of S is the best strategy. Write a program that finds the expected value of S.

Input
The first line contain an integer n (1 ≤ n ≤ 105 ).
The second line contains n integers ai (1 ≤ ai ≤ 105 ).
Each of the following n − 1 lines contains two integers u and v (1 ≤ u, v ≤ n) indicating that there
is an edge connecting nodes u and v.

Output
It can be shown that the expected value of S can be expressed as an irreducible fraction P/Q such
that P and Q are non-negative integers. Print P · Q−1 modulo 998244353.

Input example 1 Output example 1


2 3
1 1
1 2

Input example 2 Output example 2


6 465847442
1 5 6 6 8 2
1 2
1 3
3 4
3 5
2 6
Gran Premio de México 2023 - Primera Fecha 9

Problem G
Growing Game
In some house in some place, John and Jane were playing a game, the game consist on a pile of N
chips and they will be alternating turns with Jane starting. In each turn they need to remove at least
one chip from the pile, and the winner will be the one that takes the last chip.
But there is an interesting way in wich each one can take chips, in first turn (Jane’s turn) she can
only pick 1 chip, in the second turn (John’s turn) he can choose to take 1 or 2 chips, in the third turn
(Jane’s turn again) she can choose between taking 1, 2 or 3 chips. Generally speaking, on the i-th
turn, the player in turn should take at least 1 chip and maximum i chips.
Knowing the amount of chips in the pile when the game started, and assuming both John and
Jane will play optimally. Can you determine who will win?

Input
One integer N (1 ≤ N ≤ 5000) that represents the number of chips in the pile.

Output
Print ”Jane”if Jane will win the game or ”John”otherwise.

Input example 1 Output example 1


1 Jane

Input example 2 Output example 2


3 John

Input example 3 Output example 3


6 John
Gran Premio de México 2023 - Primera Fecha 10

Problem H
Hell or Paradise?
Gabimaru is an assassin who has just been sent to find the elixir of life on an island that appears to be
a Buddhist paradise. Unfortunately, during his first few hours on the island, he encounters monsters
that make him realize that the island is actually hell. One characteristic of these monsters is that they
have a string tattooed on their bodies.

After spending some time on the island, Gabimaru encounters a criminal who seems to have more
information about the whereabouts of the elixir of life. As you can imagine, the criminal won’t give this
information to Gabimaru for free. Instead, he will give it to him in exchange for doing the following:
First, the criminal hands Gabimaru a sheet containing a list of words. Gabimaru must then kill
one monster and divide the tattooed part of the monster’s body into pieces in a way that each piece
forms a word from the list provided by the criminal.
Gabimaru, not wanting to be bored during his mission, is now curious about the number of different
ways he can successfully complete this task.

Input
The first line consists of an integer N (1 ≤ N ≤ 105 ), which is the number of words in the criminal’s
list.
The following N lines cointain the words in the criminal’s list. One word per line ( 1 ≤ —word—
≤ 105 ). The sum of lenghts of the N words is at most 2 ∗ 105
The last line contains a string S (1 ≤ |S| ≤ 105 ), the string tattoed in the monster.
Every given string in the input consist of lowercase letters.

Output
Print the result modulo 109 + 7

Input example 1 Output example 1


5 2
buda
tao
bud
at
ao
budatao
Gran Premio de México 2023 - Primera Fecha 11

Input example 2 Output example 2


2 5
a
aa
aaaa
Gran Premio de México 2023 - Primera Fecha 12

Problem I
Improving Chewing Candy
You have obtained a job in a thriving candy factory, which has recently acquired a specialized machine
for producing chewy candy. Your job’s objective is to maximize this machine’s output, producing
unique candy chains never before seen in the market.
The peculiarity of this machine is that it only produces circular chains of chewy candy blocks,
represented by the letters of the English alphabet, from ‘a’ to ‘z’. If two blocks are represented by the
same letter, that means they share the same flavor.
Your challenge is to obtain a straight line of candy blocks from the circular chain, which is as long
as possible without being cloying. We define a chain as “cloying” if it contains more than k blocks of
the same flavor. However, due to the sticky nature of the chewy candy blocks, once you select a block
of a particular flavor, you must take all adjacent blocks of the same flavor.
For instance, suppose k = 2 and you have a circular chain of chewy candy represented by “abbc-
daa”. The longest chain of chewy candy you can obtain without it being cloying is “bbcd”. You
cannot select any ‘a’ flavored block, as these blocks are stuck together, forming a continuous block of
“aaa”, and would exceed the k limit.
Your task is to determine the longest candy chain that you can extract from the machine without
it being cloying.

Input
The input consists of two lines:
The first line contains two integers n and k (2 ≤ n ≤ 106 ; 1 ≤ k ≤ 106 ), representing the number
of blocks in the circular chain and the maximum number of blocks of the same flavor that can be had
in a chewy candy chain without it being cloying, respectively.
The second line contains a string s of size n, representing the circular chain of chewy candy blocks
produced by the machine. Each character of the string corresponds to a block of a flavor, with the
English alphabet letters, from ‘a’ to ‘z’.

Output
If there is no possible candy chain, print -1.
Otherwise, print two lines:
In the first line, print the length of the longest possible chewy candy chain according to the previous
conditions.
In the second line, print the found chain. If there are multiple solutions, print any of them.

Input example 1 Output example 1


9 2 5
aabccbaba bccba

Input example 2 Output example 2


3 4 -1
aaa
Gran Premio de México 2023 - Primera Fecha 13

Problem J
Jumping Reaction
Today, in chemistry class, Maximiliano is studying highly explosive substances, specifically those that
produce a “Jumping reaction”. Each substance has a jumping energy which is represented with an
integer number. When two substances having jumping energies a and b mix, the reaction will have ab
jumping energy.
During the class, the teacher mentioned an interesting property of the substances: when K subs-
tances are mixed, the reaction jumping energy will be the sum of the reactions that would happen if
all possible pairs were mixed. For example if K = 3 and the mixed substances have jumping energies
a, b, and c, their resulting energy would be ab + ac + bc.
Maximiliano will perform Q experiments using some of the N substances he can reach in class,
on each experiment Maximiliano will take all the substances in the range [L, R] and mix them. He is
afraid that the resulting jumping energy may make him jump off the lab, so He asked for your help
to tell him what would be the jumping energy of the reaction on each of the experiments he wants to
perform.
Help him solve his questions!

Input
The first line of input contains two numbers N and Q (2 ≤ N, Q ≤ 106 ) that represents the number
of substances and the number ofexperiments.
The next line contains N integer numbers Ai (1 ≤ Ai ≤ 106 ) representing the jumping energy of
the i-th substance.
The next Q lines will have two integers each, L and R (1 ≤ L ≤ R ≤ N ) representing the range of
the question.

Output
Print Q lines where each line represents the resulting jumping energy of the i-th experiment.
Because this number can be very large, print it modulo 109 + 7

Input example 1 Output example 1


5 3 2
1 2 3 4 5 85
1 2 47
1 5
3 5

Input example 2 Output example 2


10 2 53
3 1 5 2 3 1 5 6 1 1 5
7 10
2 3
Gran Premio de México 2023 - Primera Fecha 14

Problem K
Kingdom Power C
Today is the highly anticipated release of the new game “Kingdom Power C.” Humberto is incredibly
excited about this game as he has been eagerly awaiting its release for about two years.
This game is widely renowned for its extensive options that allow players to craft their own unique
adventure. The game is structured as a list of levels, with some levels only accessible after completing
a previous level. Upon completing a level, players are then required to choose and play one of the
levels that have been unlocked as a result.
Among these levels, there are “starting levels” that can be accessed without completing any pre-
vious levels, and “ending levels” that do not lead to further progression in the game. These ending
levels mark the conclusion of a game run.
Upon finishing a game run, players have the option to access “new game plus”. In this mode, they
can select another starting level and embark on a new playthrough. However, each successive new
game plus becomes increasingly challenging. Consequently, any levels played in previous runs become
inaccessible in all future new game pluses.
Humberto is determined to maximize the number of new game pluses he can experience! He seeks
assistance in determining the maximum quantity of runs he can undertake.

Input
The first line of input contains three integers separated by space N (1 ≤ N ≤ 100), S (1 ≤ S ≤ N ),
and E (1 ≤ E ≤ N ), representing the amount of levels in the game, the amount of starting levels, and
the amount of ending levels.
The next line contains S integer numbers Si (1 ≤ Si ≤ N ) separated by a space, representing the
S starting levels in the game.
The next line contains E integer numbers Ei (1 ≤ Ei ≤ N ) separated by a space, representing the
E ending levels.
−1)
The next line contains an integer number M (1 ≤ M ≤ (N )(N 2 ) representing the number of
prerequisites to access levels.
Each of the next M lines contains two integer numbers u and v (1 ≤ u, v ≤ N ), representing a
prerequisite in the game, which means after completing level u level v can be played.

Output
Print a line with a single integer number, the maximum number of runs Humberto can play in the
game.

Input example 1 Output example 1


3 1 1 1
2
3
3
1 2
2 3
1 3
Gran Premio de México 2023 - Primera Fecha 15

Input example 2 Output example 2


3 3 3 3
1 2 3
1 2 3
3
1 2
1 3
2 3

Input example 3 Output example 3


6 2 3 1
1 3
2 3 5
6
3 5
1 4
4 6
6 3
5 2
3 2

Input example 4 Output example 4


4 1 1 0
1
3
2
1 4
2 3
Gran Premio de México 2023 - Segunda Fecha

May 27th, 2023

Problems book

General Information

This problem set contains 14 problems; pages are numbered from 1 to 17, Without considering this page. Please,
verify your book is complete.

A) About your program


1) The code submitted to resolve a problem should be named : problem code.c, problem code.cpp, pro-
blem code.java or problem code.py, problem code.kt, where problem code is the uppercase letter that identifies
the problem. Remember that in Java the name of the main class should be the same as the name of the file.

B) About Input
1) Your program should read the input from standard input.
2) When an input line contains more than one value, these values are separated by a single white space. The
input does not contain any other white spaces.
3) Each line of input, including the last one contains exactly only one end of line character.
4) End of input is on the end of file.

C) About output
1) The output of your program should be written to standard output.
2) When an output line contains more than one value, these values should be separated by a white space. The
output does not contain any other white space.
3) Each line of output, including the last one, should contain exactly one end of line character.

v1.2
Gran Premio de México 2023 - Segunda Fecha 1

Problem A
Alaric magic partition
Once upon a time, in the mystical land of Numeria, there was a young wizard named Alaric. Alaric
possessed a powerful ability to manipulate numbers and was known for his expertise in partitioning
them in unique ways. One day, Alaric stumbled upon a mystical artifact that contained a number of
immense magnitude.
This number, denoted as N , was no ordinary number. It consisted of K digits and emanated a mys-
terious energy. Intrigued by this peculiar number, Alaric sought to unlock its secrets by partitioning
it.
However, Alaric could only create non-overlapping partitions from N , and each partition had to
satisfy one of two magical properties: it had to either be a prime number or a perfect square. The
remaining digits that were not used in any partition were deemed insignificant and were ignored.
Alaric’s goal was to determine the maximum number of non-overlapping partitions he could create
from N while adhering to the magical properties.
Can you assist Alaric in this enchanting task?

Input
The input consists of two lines:
The first line contains an integer K (1 ≤ K ≤ 106 ), representing the number of digits in N . The
second line contains a string N consisting of K digits.

Output
Output a single integer, representing the maximum number of non-overlapping partitions that can
be obtained from N .

Input example 1 Output example 1


3 1
687

Input example 2 Output example 2


5 2
10067

Input example 3 Output example 3


2 2
52
Gran Premio de México 2023 - Segunda Fecha 2

Problem B
Bogo Sort Probability
Once upon a time, in the realm of Sortingland, there was a peculiar sorting algorithm known as Bogo
Sort. This algorithm was notorious for its inefficiency, as it relied on sheer luck to sort an array
correctly. Despite its shortcomings, Bogo Sort possessed a fascinating characteristic: it had a small
probability of sorting an array correctly in a single iteration.
In this kingdom, you are given an array of N integers, where N represents the size of the array. The
array elements are initially in a random order. Your task is to determine the probability, represented
as a number congruent to P/Q (mod (109 + 7)), that the Bogo Sort algorithm will correctly sort the
array in a single iteration. Here, P is the numerator and Q is the denominator of the probability.
Bogo Sort is a simple yet inefficient algorithm. It works as follows:
1.- Check if the array is sorted. If it is, then the sorting is complete.
2.- If the array is not sorted, randomly shuffle the elements to create a new permutation.
3.- Repeat steps 1 and 2 until the array is sorted.
Additionally, you will be given a series of K queries, each represented by two integers, A and B.
In each query, you need to update the value at position A of the array with the value B. After each
update, you need to recalculate and output the updated probability of Bogo Sort correctly sorting the
array in a single iteration.

Input
The input consists of multiple lines.
The first line contains two integers N and K (1 ≤ N, K ≤ 106 ), representing the size of the array
and the number of queries.
The second line contains N integers a1 , a2 , . . . , aN (1 ≤ ai ≤ 109 ), representing the initial elements
of the array.
The following K lines contain the queries. Each query consists of two integers A and B (1 ≤ A ≤
N, 1 ≤ B ≤ 109 ), representing the position and the updated value in the array.

Output
Output the initial probability of Bogo Sort correctly sorting the array as a number congruent to
P/Q (mod (109 + 7)), where P is the numerator and Q is the denominator of the probability. After
each query, output the updated probability in the same format.

Input example 1 Output example 1


5 2 808333339
3 2 5 4 1 616666671
2 1 616666671
4 6

Input example 2 Output example 2


4 2 41666667
2 7 3 5 83333334
1 3 83333334
1 7
Gran Premio de México 2023 - Segunda Fecha 3

Input example 3 Output example 3


3 2 166666668
1 2 3 333333336
2 1 1
3 1
Gran Premio de México 2023 - Segunda Fecha 4

Problem C
Choose Two
There is a street in your town that contains N houses in a row. Each house i has a height Hi .
A dome is built by selecting a continuous range of houses [l, r], and its height is equal to the
maximum height among them, i.e., the height h of the dome is given by h = max {Hi }.
l≤i≤r
The Intrepid Construction Planning Crew wants to build exactly two non-overlapping, similar
domes on the street. Two domes are similar if their heights are the same.
Compute the number of ways to choose the two domes to build modulo 109 + 7.
For example, if N = 8 and H = {2, 7, 4, 8, 6, 6, 6, 5}, then there are 9 possible ways to choose the
two domes:

1. {2, 7, 4, 8, [6], [6], 6, 5}

2. {2, 7, 4, 8, [6], [6, 6], 5}

3. {2, 7, 4, 8, [6], [6, 6, 5]}

4. {2, 7, 4, 8, [6], 6, [6], 5}

5. {2, 7, 4, 8, [6], 6, [6, 5]}

6. {2, 7, 4, 8, 6, [6], [6], 5}

7. {2, 7, 4, 8, 6, [6], [6, 5]}

8. {2, 7, 4, 8, [6, 6], [6], 5}

9. {2, 7, 4, 8, [6, 6], [6, 5]}

Input
The first line of input contains an integer N (1 ≤ N ≤ 2 · 106 ) — The number of houses in the
street.
The second line of input contains N integers Hi (1 ≤ Hi ≤ N ) — The i-th integer is the height of
the i-th house.

Output
Print a single line — The answer to the problem.

Input example 1 Output example 1


8 9
2 7 4 8 6 6 6 5

Input example 2 Output example 2


10 248
6 5 5 4 6 1 6 5 2 6
Gran Premio de México 2023 - Segunda Fecha 5

Problem D
Draconis Subarrays
Once upon a time in the mystical land of Draconis, there existed two powerful arrays: M and N .
These arrays were filled with positive integers, each carrying its own magical essence. The inhabitants
of the land were intrigued by the concept of similarity between arrays. They discovered that two
arrays, M and N , could be considered similar if it was possible to transform a subarray of N into M
by adding or subtracting a constant value to each element.
You are now summoned to solve a puzzle. Given two arrays, M and N , your task is to determine
the number of subarrays of N that are similar to M . Will you be able to unravel this mystical
connection?

Input
The input consists of multiple lines. The first line contains two integers M and N (1 ≤ M ≤
N ≤ 106 ), representing the lengths of arrays M and N respectively. The second line contains M
space-separated positive integers m1 , m2 , . . . , mM (1 ≤ mi ≤ 109 ), representing the magical elements
of array M . The third line contains N space-separated positive integers n1 , n2 , . . . , nN (1 ≤ ni ≤ 109 ),
representing the mystical elements of array N .

Output
Output a single integer, the number of subarrays of N that are similar to M .

Input example 1 Output example 1


4 6 3
1 2 3 4
10 11 12 13 14 15

Input example 2 Output example 2


3 10 4
1 1 1
2 2 2 3 3 3 4 4 4 4

Input example 3 Output example 3


2 6 1
5 8
10 12 1 4 3 9

Notes
A subarray is defined as a contiguous sequence of elements within an array.
Gran Premio de México 2023 - Segunda Fecha 6

Problem E
Earnings Report
You’re working on a financial reporting platform. Your task is to calculate earnings obtained during
certain time ranges, based on the salary information of different jobs. You’ll be given N jobs and Q
queries. Each query has a range of dates [L, R] and you must calculate the total earnings obtained in
that range among all job salaries.
Each job has the following data:

• Amount: Salary amount

• StartDate: Start date of the job

• EndDate: End date of the job (or its absence, indicating that the job is still ongoing)

• Type: Salary type

There are three types of salaries, which determine when the payment is made and what dates are valid
for the start of a job:

• “weekly”: The payment is made every Friday. A job of this type can only start on a Monday,
that is, the first business day after the payday.

• “bi-weekly”: The payment is made on the 15th and the last day of each month. A job of this
type can only start on the 1st or 16th of a month, that is, the day after the payday.

• “monthly”: The payment is made on the last day of each month. A job of this type can only
start on the first day of a month, that is, the day after the payday.

If there’s no end date, it means the job is still ongoing.


If there’s an end date, and it falls within a query range, payments made on, or before that end
date are included in the total.
If a payment is made a day after the end date, it’s not included in the total.

Input
In the first line of input, there will be two integers N, Q (1 ≤ N ≤ 103 ; 1 ≤ Q ≤ 105 ), representing
the number of jobs in the database, and the number of queries, respectively.
The next N lines will contain the data of one job in the format: “Amount StartDate EndDate Type”
without the double quotes, where Amount is an integer (1 ≤ Amount ≤ 109 ), StartDate and EndDate
are in the format: DD/MM/YYYY (01 ≤ DD ≤ 31; 01 ≤ M M ≤ 12; 2000 ≤ Y Y Y Y ≤ 9999), Type
will be a string representing one of the salary types described before.
The EndDate could be “None”, indicating that the job is still ongoing.
Afterward, there are Q lines, each with a query in the format: DD/MM/YYYY DD/MM/YYYY.

Output
For each query, print in a single line the total earnings obtained in that range among all job
salaries.
Gran Premio de México 2023 - Segunda Fecha 7

Input example 1 Output example 1


3 2 437152
2 01/10/8467 25/09/9231 monthly 112462
5 13/06/7064 08/01/7520 weekly
4 01/05/6875 None bi-weekly
01/01/2000 31/12/9999
22/07/8260 28/01/9241

Notes
• How to Determine a Leap Year:
A year is a leap year if it satisfies the following conditions: The year is evenly divisible by 4;
and, If the year can be evenly divided by 100, it is NOT a leap year, unless; The year is also
evenly divisible by 400. Then it is a leap year.

• Weekday of 01/01/2000:
The 1st of January 2000 fell on a Saturday.

• Days per month from Jan to Dec in a non-leap year: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31]
Gran Premio de México 2023 - Segunda Fecha 8

Problem F
Fibonacci Fever
Given n and k you’re asked to compute
n
X
fik
i=1

where fn is the n-th fibonacci number. The n-th fibonacci number can be computed as:

f1 = f2 = 1
fn = fn−1 + fn−2 , ∀n ≥ 3
Since the answer could be large, print it modulo 109 + 7.

Input
The first line of input contains two integers n and k (1 ≤ n ≤ 1018 , 1 ≤ k ≤ 105 ) — The limits of
the sum and the exponent of all the terms.

Output
Print a single line — The value of the required sum modulo 109 + 7.

Input example 1 Output example 1


1 10 1

Input example 2 Output example 2


5 10 9825700

Input example 3 Output example 3


10 1 143

Input example 4 Output example 4


2 1 2

Input example 5 Output example 5


3 1 4

Input example 6 Output example 6


4 1 7
Gran Premio de México 2023 - Segunda Fecha 9

Problem G
Guessing Two Steps into the Multiverse
Stephen is aware of the existence of n different universes. Every minute, a new one-way portal between
two universes is discovered.
Stephen is interested in determining the number of ways he could travel from one universe to
another one by going through exactly two known portals at any point in time. He is also interested
in identifying the maximum number of additional travel paths between any two universes, utilizing
precisely two portals, that could possibly emerge with the introduction of a single new portal the next
minute. At minute zero, there are no known portals. Also, a portal may be used more than once.
You and your friend Ned have offered to write a program that can perform these calculations.
Stephen feels weird about it, since he is only used to work with magic, but he will allow it.

Input
The first line of the input contains two space separated integers n (1 ≤ n ≤ 105 ) and t (1 ≤ t ≤ 105 )
— the number of known universes and the number of minutes where new portals will be discovered.
After that, t lines follow. The i-th line (1 ≤ i ≤ t) contain two space separated integers u and
v (1 ≤ u, v ≤ n) that indicate that in the i-th minute, a one-way portal from the u-universe to the
v-universe was discovered.

Output
For each minute i = 1, 2, . . . , t output a line with two space separated integers. The first integer
in each line should be the updated number of ways of reaching one universe from another by using
exactly two portals in the i-th minute. The second one should be the maximum additional number of
ways that could be discovered in the (i + 1)-th minute.

Input example 1 Output example 1


4 6 0 2
1 2 2 3
2 1 5 5
1 1 8 5
2 2 9 5
3 3 10 5
4 4

Input example 2 Output example 2


1 3 1 3
1 1 4 5
1 1 9 7
1 1
Gran Premio de México 2023 - Segunda Fecha 10

Problem H
How Many Groups
Jaime’s company has decided to switch to a new customer service system. The system update will
address, among other things, a common issue that occurs when any of the employees go on vacation.
The company has N employees with IDs ranging from 1 to N, and each employee has a supervisor
with ID si , except for the general supervisor. In the system, each employee belongs to a group gi and
attends only support tickets assigned to the group they belong. It should be noted that if an employee
is the only one in a group and goes on vacation, no one will handle the tickets for that group during
their absence.
The system update will allow each employee to receive tickets not only from their own group
but also from the groups to which each employee on their line of supervision belongs. The line of
supervision for an employee e includes their immediate supervisor, the supervisor’s supervisor, and so
on until reaching the general supervisor.
Upon learning about this update, many employees have complained, they claim that this update
only makes it easier for supervisors to take vacations while they have more work to do. The company’s
management has asked for your help in measuring the workload for each employee on the new system.
Given the number of employees, the supervisor for each employee, and the group they belong to, your
task is to count, for each employee, how many different groups they will have to handle tickets for
after the system update.

Input
The first line of input contains a single integer N (1 ≤ N ≤ 106 ), the number of employees on the
company.
The second line contains N integer numbers separated by a space, where the i-th number represents
the ID of the supervisor si (0 ≤ si ≤ 106 ) for the employee with ID = i. The general supervisor is the
only employee where si = 0. It is guaranteed all employees line of supervision can reach the general
supervisor.
The third and last line contains N integer numbers separated by a space, where the i-th number
represents the group gi (1 ≤ gi ≤ N ) assigned on the system for the employee with ID = i.

Output
Output a line with N integers separated by a space. The i-th number is the number of different
groups for which the employee with ID = i will have to handle tickets after the system update.

Input example 1 Output example 1


6 1 1 1 1 1 1
0 1 2 3 4 5
1 1 1 1 1 1

Input example 2 Output example 2


6 1 2 3 4 5 6
0 1 2 3 4 5
1 2 3 4 5 6
Gran Premio de México 2023 - Segunda Fecha 11

Input example 3 Output example 3


6 1 2 3 3 3 4
0 1 2 3 4 5
1 2 3 3 2 6

Input example 4 Output example 4


9 3 2 2 3 1 3 2 2 3
2 3 5 3 0 7 8 5 8
1 9 9 3 2 3 2 7 5
Gran Premio de México 2023 - Segunda Fecha 12

Problem I
Iron Fist Ketil vs King Canute
King Canute is attempting to expropriate Iron Fist Ketil’s farm. However, Ketil is a proud man who
refuses to give up his lands, not even to the king. He asks his men to prepare for the fight of their
lives against King Canute’s troops.

Unfortunately, Ketil’s men are farmers and lack any battlefield experience. On the other hand,
Canute’s troops are composed of seasoned soldiers who have conquered nations. As you can imagine,
the odds are heavily stacked against Ketil.
Therefore, Ketil estimates that he needs K of his men to kill just one of Canute’s soldiers. Given
N , the number of Ketil’s men, M the number of Canute’s men, and K, is it possible for Ketil to win
the battle?
It should be noted that Ketil’s men have limited stamina, so they can only engage in combat with
one soldier throughout the entire battle.

Input
A single line containing N - the number of Ketil’s men, M - the number of Canute’s men, and K
- the number of Ketil’s men required to kill one of Canute’s men. (1 ≤ N, M, K ≤ 106 )

Output
Print a single line containing:
’Iron fist Ketil’ if Ketil can win the battle.
’King Canute’ otherwise

Input example 1 Output example 1


12 5 2 Iron fist Ketil

Input example 2 Output example 2


1 1 1 Iron fist Ketil

Input example 3 Output example 3


1 2 2 King Canute
Gran Premio de México 2023 - Segunda Fecha 13

Problem J
JP’s List of Trips
JP is coordinating a bus agency which is responsible for all the bus drivers of the state, as well as all
the trips that their buses do.
A trip is always between two distinct cities, and is defined by the roads that the bus driver takes
departing from the starting city and ending in the destination city, no road should be repeated in the
path of a trip.
Because bus drivers are very creative, they find it boring to stick to rules when driving, they don’t
like to “take the shortest path”, or “always visit cities with lower index” so it is very difficult to predict
what their trip will look like, but JP believes there should be a way to know for some cities what is
the trip a bus driver will take, so he brought to you a list with all the possible buses trips, and asked
for your help.
Help JP to find for each possible trip in his list if he can be sure on what path a bus driver will
take between the cities on the trip.

Input
 The first line of inputn contains o three integer  numbers, N, M and Q
5 5 N (N −1) 5
2 ≤ N ≤ 10 , N − 1 ≤ M ≤ min 10 , 2 , 1 ≤ Q ≤ 10 representing the number of cities,
the number of roads and the number of trips in JP’s list, respectively.
Each of the following M lines contains two integers A and B (1 ≤ A, B ≤ N ) describing a road
between cities A and B. It is guaranteed that it is possible to reach each city from any other city.
Each of the following Q lines contains two different integer numbers separated by a space, S, and
E, (1 ≤ S, E ≤ N ), describing a trip in JPs list.

Output
Output Q lines, where the i-th line contains the answer to the i-th trip in JPs list, being “YES”
if he can be sure of what the path in the trip will or “NO” if he can’t be sure.

Input example 1 Output example 1


5 4 3 YES
1 2 YES
5 4 YES
3 1
2 5
1 3
5 3
3 4

Input example 2 Output example 2


5 5 2 NO
1 2 NO
2 3
3 4
4 1
5 1
1 2
5 1
Gran Premio de México 2023 - Segunda Fecha 14

Problem K
Knockout Spell
John is an enthusiast of online role-playing games. This afternoon, he started playing a new game
where he can choose from different types of fantastic characters. As usual, John always chooses a
wizard. Like in most role-playing games, John’s character starts at level 1, and as he progresses in the
game and defeats enemies, his level increases. At the maximum level, a wizard has the ability to cast
the knockout spell.
The knockout spell is a skill that is cast over a square area measuring K cells on each side, as long
as the corners of that area have the same type of terrain. Since this spell is obtained at the maximum
level, it is the most powerful and it knocks out all enemies within the invoked area.
John has acquired a digital map of the game, which is represented by a square matrix measuring
N rows and N columns. Each cell in the matrix contains a number between 0 and 9, representing
the type of terrain on the map. Since John has already leveled up his wizard to the maximum level,
he wants to know the number of different areas over which he can cast the knockout spell. The map
is too large to count the areas manually so he asked for your help: Given the map of the game and
the size K of the squares in which a wizard can cast the knockout spell, you need to determine in how
many different areas John can cast the knockout spell.

Input
The first line of input contains two integer numbers separated by a space N (2 ≤ N ≤ 1000) and
K (2 ≤ K ≤ N ), representing the size of the map, and the size of the square where the knockout spell
can be casted.
Each of the next N lines contains N an integer number between 0 and 9, representing, the type of
terrain on each cell of the map.

Output
Print a line with an integer number, the number of different areas where John can cast the knockout
spell on the game.

Input example 1 Output example 1


2 2 1
0 0
0 0

Input example 2 Output example 2


2 2 0
1 2
1 1

Input example 3 Output example 3


5 3 5
1 5 1 6 1
1 7 8 9 5
1 1 1 1 1
1 2 3 4 1
1 1 1 1 1
Gran Premio de México 2023 - Segunda Fecha 15

Problem L
Less Time to Solve
You are participating in the ICPC (International Collegiate Programming Contest) with a team of
three members. Each team member has a different coding speed: member 1 has a coding speed of A,
member 2 has a coding speed of B, and member 3 has a coding speed of C. Additionally, you have a
list of N programming problems, and each problem takes Xi units of time to solve.
Since your team members can code in parallel, you want to determine the minimum time it will
take for your team to solve all the problems.
Each team member’s contribution to solving a problem is proportional to their coding speed.
Specifically, if a problem takes Xi units of time to code, member 1 will take Xi /A units of time,
member 2 will take Xi /B units of time, and member 3 will take Xi /C units of time.
Your task is to calculate the minimum time required for your team to solve all the problems,
assuming they can work on the problems simultaneously.

Input
The input consists of two lines. The first line contains four positive integers N (1 ≤ N ≤ 50), A,
B, and C (1 ≤ A, B, C ≤ 10), representing the number of problems and coding speeds of the three
team members, respectively.
The second line contains N positive integers X1 , X2 , . . . , XN (1 ≤ Xi ≤ 10), representing the time
required to solve each problem.

Output
Output a single integer, representing the minimum time (rounded up to the nearest integer) it will
take for your team to solve all the problems.

Input example 1 Output example 1


4 10 6 6 1
5 7 6 1

Input example 2 Output example 2


6 2 5 4 4
4 7 7 3 6 6

Input example 3 Output example 3


1 7 3 3 2
8
Gran Premio de México 2023 - Segunda Fecha 16

Problem M
Modify the Array
You are given an array which is initially a permutation of n elements: A = [a1 , a2 , . . . , an ].
The array can be modified using the following operation:

• Let A = [a1 , a2 , . . . , ak ] be the current array. Select two indices l and r (1 ≤ l ≤ r ≤ k), remove
the elements al , al+1 , . . . ar from the array, and insert min(al , al+1 , . . . , ar ) in their place. In
other words, the array becomes A = [a1 , a2 , . . . , al−1 , min(al , al+1 , . . . , ar ), ar+1 , . . . ak ].

Print the number of different arrays that you can get by performing the given operation any number
of times (possibly zero) modulo 998244353.

Input
The first line contains an integer n (1 ≤ n ≤ 5000).
The following line contains n space-separated integers a1 , a2 , . . . , an (1 ≤ ai ≤ n). It is guaranteed
that [a1 , a2 , . . . , an ] forms a permutation of size n.

Output
Print the answer modulo 998244353.

Input example 1 Output example 1


5 16
1 2 3 4 5

Input example 2 Output example 2


5 9
3 5 2 4 1
Gran Premio de México 2023 - Segunda Fecha 17

Problem N
Necklace
One day, Isaac joined a group of tourists walking by the Agua Azul park in Guadalajara, he was
listening to some guides who said that Guadalajara is also called a pearl in the desert. He was
wondering why would it be called that way.
So, he continued walking and found a magical pearl necklace! The necklace is conformed by two
types of pearls.

• Type 1 pearls: allow you to gain ai points.

• Type 2 pearls: allow you to exchange ai points for ai coins.

Isaac doesn’t want to be greedy, so he decided to take at most K pearls from the necklace, but he
can’t take pearls without cutting the necklace to remove them. So he decided to cut the necklace at
some point and then take pearls from either side starting from it. Each time that Isaac takes a pearl
from any of the sides, he can only take the pearl that is on the front of the side at that moment.
He can only take type 2 pearls if he has enough points to immediately exchange them for coins.
Help Isaac know what is the maximum amount of coins he can get!

Input
The first line of input contains two integers separated by a space N and K 1 ≤ N ≤ 105 , 1 ≤ K ≤ 100 ,


representing the number of pearls in the necklace, and the maximum number of pearls Isaac will take
from the necklace.
The second line of input contains N integers, representing each of the ai (1 ≤ ai ≤ 109 ) values of
each pearl in the necklace.
The third and las line contains N integer numbers, representing the type 1 or 2 of the pearl in
each position of the necklace.

Output
Print a line with a single integer number, the maximum amount of coins Isaac can get from the
necklace.

Input example 1 Output example 1


2 2 1
1 1
1 2

Input example 2 Output example 2


6 4 8
2 2 4 4 6 1
1 2 1 1 2 1

You might also like