0% found this document useful (0 votes)
551 views18 pages

Ecpc Competition

Uploaded by

sara.msk14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
551 views18 pages

Ecpc Competition

Uploaded by

sara.msk14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

ECPC Qualifications 2024 (Day 2)

AAST, Alexandria, Egypt, July, 2024

Problem A. Antiquities of Acre


Input file: standard input
Output file: standard output
Balloon Color: Black

Aamer bin Abdullah bin Al-Jarrah Al-Fihri, traveling through the lands of Palestine,
explores Acre’s labyrinthine alleys and towering walls, immersing himself in a city celebrated
for over 4,000 years as a bastion of resilience and cultural diversity. Acre’s strategic location
on the Mediterranean coast has fostered a rich tapestry of civilizations and architectural
marvels.
Suppose you have a sand clock with n sand grains initially. Each second, x sand grains drop from the
clock.
You want to calculate the number of sand grains that will remain in the clock after h hours, m minutes,
and s seconds.

Input
The input consists of five integers n, x, h, m, and s (1 ≤ n ≤ 1012 ) (1 ≤ x ≤ 103 ) (0 ≤ h, m, s ≤ 106 )

Output
Print a single integer — the number of sand grains remaining in the clock after h hours, m minutes, and
s seconds.

Example
standard input standard output
5000 1 1 5 20 1080

Page 1 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem B. Biblical Journeys in Bethlehem


Input file: standard input
Output file: standard output
Balloon Color: Pink

Aamer journeys to Bethlehem, renowned as the birthplace of Jesus Christ and steeped
in over 3,000 years of biblical history. Nestled among Judea’s hills, Bethlehem’s ancient
streets echo tales of prophets, pilgrimage, and enduring faith, making it a timeless symbol
of spiritual heritage.
There is a tree with n nodes numbered from 1 to n, where each node has a letter written on it. A path
on the tree is called beautiful if the word formed by writing down the letters on the nodes in order when
traversing the path is a palindrome.
Find the length of the longest beautiful path.

Input
The first line contains n, the number of nodes of the tree, (1 ≤ n ≤ 4 × 104 ).
The second line contains n lowercase letters, the ith of which is the letter attached to node i.
Each of the following n − 1 lines contains a pair of integers u and v. This means that there is an edge
between node u and node v in the tree.
It is guaranteed that the edges form a tree. The letters written on the nodes are lowercase letters from a
to z.

Output
A single integer, the length of the longest path satisfying the above constraint.

Examples
standard input standard output
8 4
b b a c c d d a
1 2
1 3
3 4
3 5
3 6
3 7
2 8
9 5
a c c e e a b d d
1 2
1 3
2 6
2 7
3 4
3 5
4 8
5 9

Page 2 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem C. Canals of Beersheba


Input file: standard input
Output file: standard output
Balloon Color: Light Blue

Aamer explores Beersheba’s intricate network of canals and ancient water management
systems, critical lifelines in the Negev Desert for millennia. Beyond their practicality,
these systems highlight Beersheba’s ingenuity and pivotal role as a crossroads of trade
and civilization in arid landscapes.
You are given a tree with n vertices.
Two players, Alice and Bob, play optimally. They start from two given nodes on this tree and move along
the edges. Alice and Bob can move from one node to an adjacent node in one second.
Each node they visit gets painted with a specific color: nodes visited by Alice turn blue, while those
visited by Bob turn red. If Alice and Bob reach the same node at the same time, the node becomes
purple, indicating that neither player can pass through it anymore.
If a player reaches a purple node, he is forced to backtrack to the last non-purple node he visited.
For each query, you are given two nodes: the starting point for Alice and the starting point for Bob. Your
task is to determine which player can paint more nodes in their respective colors. The winner is the player
who has the most nodes in their color (excluding purple nodes). If there is no winner, just print Draw.

Input
The first line of the input contains an integer n (2 ≤ n ≤ 105 ) — the number of vertices in the tree.
Each of the next n − 1 lines describes an edge of the tree (1 ≤ ui , vi ≤ n, ui 6= vi ).
The next line contains an integer q (1 ≤ q ≤ 105 ) — the number of queries.
The next q lines describe queries. The i-th line describes the i-th query and contains two integers a and
b (1 ≤ ai , bi ≤ n, ai 6= bi ) — the starting point for Alice and the starting point for Bob.

Output
For each query:
— If Alice painted more nodes than Bob, print “Alice”.
— If Bob painted more nodes than Alice, print “Bob”.
— Otherwise, if they painted the same number of nodes, print “Draw”.

Example
standard input standard output
7 Draw
1 2 Alice
2 3
3 4
4 5
3 6
6 7
2
5 7
3 4

Page 3 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Note
Alice cannot pass through nodes visited by Bob, and Bob cannot pass through nodes visited by Alice.
first query:

second query:

Page 4 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem D. Diverse Culture of Al-Khalil


Input file: standard input
Output file: standard output
Balloon Color: Purple

Al-Khalil beckons Aamer with its bustling markets and ancient streets, reflecting a city
that has thrived as a cultural crossroads for over 4,000 years. Revered by Christianity and
Islam alike, al-Khalı̄l’s vibrant mosaic of cultures and traditions enriches its identity as a
hub of commerce, faith, and cultural exchange.
You are given a 2D grid of size n × m. You start at the top-left corner, and your goal is to reach the
bottom-right corner. Each cell in the grid can be either a normal cell represented by ‘.’ or a teleport cell
represented by ‘o’. You can move to any adjacent cell (up, down, left, right) in two seconds. Additionally,
standing on a teleport cell allows you to move to any other teleport cell on the grid in one second.
Find the minimum time required to reach the destination.

Input

• The first line contains a single integer t (1 ≤ t ≤ 104 ), the number of test cases.

• The first line of each test case contains two integers n and m (1 ≤ n, m ≤ 106 ), the dimensions of
the grid.

• This is followed by n lines, each containing m characters describing the grid.

• The sum of n × m for all test cases does not exceed 106 .

Output
Output a single integer, the minimum time required to reach the destination.

Example
standard input standard output
3 11
5 5 1
..... 12
.....
..o..
....o
...o.
3 3
o..
...
..o
4 4
....
....
....
....

Page 5 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem E. Exploration of Jericho’s Oases


Input file: standard input
Output file: standard output
Balloon Color: Orange

Jericho captivates Aamer with its ancient oases and desert landscapes, emblematic of a
city steeped in over 10,000 years of human settlement. Beyond its natural beauty, Jericho’s
resilience and archaeological treasures reflect its enduring role as a cultural and historical
gem of the Levant region.
Given a tree of n nodes, rooted at node 1. Each node i has a value ai written on it.
Define f (u, k) as the number of ways to choose a subset of nodes in the subtree of u such that the sum
of the values of these nodes is equal to k.
Find the sum of f (u, k) for each u such that 1 ≤ u ≤ n. Since the answer can be too large, print the
answer modulo 109 + 7.

Input
The first line of the input contains two integers n and k (2 ≤ n, k ≤ 5000), representing the size of the
tree and the required sum.
The second line of the input contains n space separated integers representing the array a (1 ≤ ai ≤ 5000).
Each of the next n−1 lines contain two integers u and v, representing the edges of the tree. It’s guaranteed
that these edges form a connected tree.

Output
Print a single integer representing the answer to the problem.

Examples
standard input standard output
5 5 10
3 5 5 5 5
2 4
1 3
3 5
1 2
2 4 1
2 2
1 2
3 4 0
5 5 5
1 2
2 3

Note
In the first example:

• f (1, 5) = 4.

• f (2, 5) = 2.

Page 6 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

• f (3, 5) = 2.

• f (4, 5) = 1.

• f (5, 5) = 1.

Therefore the answer is 4 + 2 + 2 + 1 + 1 = 10.

Page 7 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem F. Fishing Traditions of Tiberias


Input file: standard input
Output file: standard output
Balloon Color: Blue

Aamer explores Tiberias, where ancient fishing traditions intertwine with spiritual
significance and healing practices dating back over 2,000 years. Set on the shores of the
Sea of Galilee, Tiberias continues to embody a harmonious blend of nature, tradition, and
spiritual renewal.
Adnan had a bad day, so he decided to challenge Hamza with the following problem:
Given an array a with N distinct numbers, Hamza can apply 2 types of queries:

1. Shift the array cyclically to the left or right by k positions.

2. Check if there is a number x in the array. If there is such a number, print its index in the array
(possibly modified).

Since Hamza wants to win the challenge, he needs your help to apply these 2 types of queries to the array.

Input
The first line contains t (1 ≤ t ≤ 10) – the number of test cases.
The first line of each test case contains the number N (1 ≤ N ≤ 2 · 105 ) – the array length.
The second line of each test case contains N numbers ai (−1018 ≤ ai ≤ 1018 ) – elements of the array a.
The third line of each test case contains the number Q (1 ≤ Q ≤ 3 · 105 ) – the number of queries.
The following Q lines contain queries. The queries are set like this:

1. 1 k (1 ≤ |k| < N ) – shift the array cyclically by k positions. If the number k is negative, then the
shift is carried out to the left; otherwise, the shift must be carried out to the right.

2. 2 x – check if there is a number x in the array.

It is guaranteed that the sum of N over all test cases does not exceed 2 · 105 .
It is guaranteed that the sum of Q over all test cases does not exceed 3 · 105 .

Output
For each query “2 x”, print a number in a separate line – the index of x in the array. If there is no x in
the array, print “-1” in a separate line. It is assumed that the elements of the array are numbered from 1.

Page 8 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Example
standard input standard output
2 -1
7 6
1 2 3 4 5 6 7 3
7 1
2 9 2
1 2
2 4
1 -2
2 3
1 -5
2 6
2
1 2
1
2 2

Page 9 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem G. Gateways of Beisan


Input file: standard input
Output file: standard output
Balloon Color: Red

Beisan, known historically as Beit She’an, invites Aamer to uncover its archaeological
wonders and strategic significance in the Jordan Valley. For over 5,000 years, Beisan’s ancient
gateways have served as conduits of cultural exchange and testament to its enduring legacy
as a pivotal junction of civilizations.
Adnan has a collection of n balls, each numbered from 1 to n.
He wants to select some balls from this collection in such a way that the total number of selected balls
forms a prime number.
Write a program to help Adnan determine the number of ways he can select the balls so that the total
number of selected balls is a prime number.
A prime number is an integer greater than one that has exactly two divisors: one and itself.
Note: When determining the number of ways, consider two selection methods to be different
if one method contains a ball that the other method doesn’t, regardless of the order of the
selected balls.

Input
The first line contains an integer q (1 ≤ q ≤ 102 ) representing the number of queries.
Each query contains a single integer n (1 ≤ n ≤ 106 ) representing the total number of balls
in Adnan’s collection for that query.

Output
For each query, output a single integer representing the number of ways Adnan can select
the balls such that the total number of selected balls forms a prime number.
Output the result modulo 109 + 7.

Example
standard input standard output
1 1
2

Page 10 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem H. Harbor Life in Jaffa


Input file: standard input
Output file: standard output
Balloon Color: Yellow

Aamer immerses himself in the vibrant harbor life of Jaffa, where maritime activity has
shaped its identity as a gateway for trade and cultural interaction. Navigating through its
bustling waters, Jaffa’s strategic importance and historical role as a hub of connectivity
come alive amidst the ebb and flow of commerce.
In the Faculty of Engineering, a group of students are experimenting with a unique mathematical challenge.
On a whiteboard, they start with a positive integer n. During their study session, they perform the following
action exactly k times:
Given the current number v written on the whiteboard, the students will arbitrary select one of its divisors
(including 1 and v itself ) and append this divisor to an array. The number v on the whiteboard will be
divided by this divisor. As the students are good in probability course, each divisor is chosen with equal
probability.
The students are curious to know how many different arrays can be formed after k selections.

Input
The first and last line contains two integers n (1 ≤ n ≤ 1015 ) and k (1 ≤ k ≤ 109 ) — the initial number
on the whiteboard and the number of selections.

Output
Output a single integer, the number of different arrays that can be formed after k selections. Print it
modulo 109 + 7.

Examples
standard input standard output
6 1 4
6 2 9

Page 11 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem I. Interwoven Cultures of Nablus


Input file: standard input
Output file: standard output
Balloon Color: Bronze

Nablus’s winding streets and lively markets reflect a city that has flourished as a commercial
and cultural center for over 2,000 years. Offering Aamer a glimpse into its history, he
discovered how Nablus has been considered the main commercial and industrial center in
Palestine, and even well-known for its sweets like Nabulsi Kunafa.
Adnan and Besho, both passionate about football, are competing in a contest where they take turns
shooting a ball over n rounds. In each round, the players lose stamina equal to the distance the ball
travels, and they must shoot the ball at least one meter.
A player wins if their longest shot overall across all rounds is greater than their opponent’s longest shot
across all rounds. Specifically, if Adnan’s shots are a1 , a2 , . . . , an and Besho’s shots are b1 , b2 , . . . , bn , Adnan
wins if max(a1 , a2 , . . . , an ) > max(b1 , b2 , . . . , bn ).
The contest lasts for n rounds. Adnan and Besho each start with an initial stamina of a and b, respectively.
Adnan follows a strategic plan: for the first n − 1 rounds, he shoots the ball exactly one meter forward
each round. In the final round, he uses all his remaining stamina to shoot the ball as far as he can.
Given the number of rounds n, Adnan’s initial stamina a, and Besho’s initial stamina b, your task is to
calculate the probability that Adnan wins if he plays optimally. This requires determining the number of
ways Besho can play that result in Adnan winning, divided by the total number of ways Besho can play.
Different strategies for Besho are considered distinct if he exerts different efforts in at least one round.
Besho can distribute his stamina across the n rounds in any manner, as long as the total stamina used
does not exceed b and he shoots the ball at least one meter in each round.

Input
The first line contains t, the number of test cases (1 ≤ t ≤ 10).
The next t lines give you three integers n, a, and b (1 ≤ n ≤ 105 , 1 ≤ a, b ≤ 106 , n ≤ a, b).
It is guaranteed that the sum of n over all test cases does not exceed 105 , the sum of a over all test cases
does not exceed 106 , and the sum of b over all test cases does not exceed 106 .

Output
Print Adnan’s probability of winning the game. It is guaranteed that the required expected value can be
represented as an irreducible fraction p/q. You need to print the value (p · q −1 ) modulo 109 + 7.

Examples
standard input standard output
2 800000006
3 5 6 900000007
3 5 5
1 1
100000 1000000 500000

Page 12 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem J. Journey in Safed’s Mysteries


Input file: standard input
Output file: standard output
Balloon Color: Silver

Safed beckons Aamer with its mystical allure and spiritual significance, steeped in over 2,000
years of cultural and historical richness. As he navigates its winding alleys and sacred sites,
Safed’s reputation as a spiritual beacon, offering moments of contemplation and discovery.
Adnan and Sadek are friends who love video games. Sadek creates a browser extension to help Adnan find
out the release date and name of free games.
The game name is encrypted by alternating letters from the start and end, and the release date is encoded
using letters A to J for digits 0 to 9. Decode the game name and release date.

Input
The first line contains the encoded game name, denoted by string S (1 ≤ |S| ≤ 1000).
The second line contains the encoded release date, denoted by string T (|T | = 10).

Output
Print two lines:
The first line should contain the decoded name of the game.
The second line should display the decoded release date.

Example
standard input standard output
GVt-a Gta-V
AD/BA/CACE 03/10/2024

Page 13 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem K. Kaleidoscope of Ramla’s Diversity


Input file: standard input
Output file: standard output
Balloon Color: Green

Ramla’s urban landscape reveals layers of cultural diversity and historical heritage spanning
over 1,300 years. Founded during the early Islamic period, Ramla’s streets and markets
showcase its role as a vibrant hub of trade, agriculture, and religious tolerance, shaped by
centuries of multicultural influence.
There are n persons standing in a line. The ith of them has initially an amount of money equal to mi .
They will play k rounds. In each round, every person will make the following process:

• Calculate the total amount of money the other persons have except him, let’s call this amount A.

• Calculate the amount of money he currently has, let’s call this amount B.

• Then his money will become equal to A − B.

After the k rounds, each person will have a new amount of money. You are required to print the new
amount for each person.
It’s guaranteed that each person will never have a negative amount of money in any of the k rounds.
Since the answer may be too large, print it modulo 998244353.

Input
The first line will contain two integers n, k (2 ≤ n ≤ 105 , 0 ≤ k ≤ 1018 ).
The next n lines, each of them will contain one integer mi (0 ≤ mi ≤ 1012 ), where mi is the money that
the ith person initially has.

Output
Print n lines, the ith of them has the amount of money that the ith person will have after k rounds.
The answer should be modulo 998244353.

Examples
standard input standard output
4 3 44
7 68
4 52
6 36
8
5 0 1
1 0
0 7
7 6
6 2
2

Note
The rounds of the first test case:

Page 14 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

• Round 0: 07 04 06 08

• Round 1: 11 17 13 09

• Round 2: 28 16 24 32

• Round 3: 44 68 52 36

Page 15 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem L. Logistics of Lod’s Connectivity


Input file: standard input
Output file: standard output
Balloon Color: Gold

The city of Lod is located to the southeast of the city of Jaffa, about 15 km away from it,
and to the northeast of the city of Ramla, about 5 km away from it. Lod is 50 meters above
sea level. It occupies an important position as it is a transportation node where railway
lines meet, and it is the meeting point of Jaffa, Haifa, Al-Quds, and Egypt. Through it, the
traveler can move from Egypt to Palestine and then to Lebanon and Turkey.
A teacher is teaching a group of n students online. He wants to conduct daily online sessions for them.
The session length is m hours, but there are interruptions due to electricity outages. For each student,
the electricity goes off every day for e hours starting from hour ai every day. The hours are numbered
from 0 to 23.
The teacher needs assistance in determining the starting hour of the session to maximize the number of
attending students. You’ll provide the teacher with the number of students that will attend the sessions.

• Note: The session can start on one day and continue to the second day. For example, the session
can run from hour 22 of the first day to hour 1 of the second day, taking into account that days are
cyclical.

• The student is considered as attending the session if he attends the entire session.

Input

• The first line contains a single integer t (1 ≤ t ≤ 104 ) — number of test cases.

• The first line of each test case contains three integers n, m, and e (1 ≤ n ≤ 2 · 105 ), (1 ≤ m ≤ 24),
(0 ≤ e ≤ 24) — the number of students, session length, and duration of electricity outage,
respectively.

• The second line of each test case contains n integers a1 , a2 , . . . , an (0 ≤ ai ≤ 23) — the starting hour
when electricity goes off for each student i.

• It is guaranteed that the sum of n over all test cases does not exceed 2 · 105 .

Output
- For each test case, output the maximum number of students that can attend the session.

Example
standard input standard output
3 2
3 3 19 8
5 3 0 1
10 1 9
0 23 10 15 10 6 15 13 17 6
1 10 0
3

Page 16 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Problem M. Majestic Crossroads Al-Nasserah


Input file: standard input
Output file: standard output
Balloon Color: Light Green

Al-Nasserah in the heart of Lower Galilee, overlooks the Marj Ibn Amer plain and serves as
a transition between this plain and the mountainous Upper Galilee. Historically significant,
it connected main roads between Syria, Egypt, Jordan, and Palestine, attracting commercial
caravans.
Bakkar and Rashida are playing a game with a series of pockets, each containing a unique value. Initially,
there are n pockets, and they are colored in red and blue, alternating, starting with blue.
The game consists of n rounds. In each round, Bakkar will collect all the pockets colored blue, and Rashida
will collect all the pockets colored red. They will then calculate their scores for the round by adding the
value of each pocket they collected to the power of 2, i.e., 2ai , where ai is the value of the i-th pocket.
The player with the highest score wins the round. After each round, the pocket with the highest value is
removed from the game, and the remaining pockets are recolored to maintain the initial structure, with
the first pocket being blue and alternating with red.
For Example Let’s say there are 5 pockets with the following values and colors:

 
3(Blue) 1(Red) 5(Blue) 2(Red) 4(Blue)

In the first round:

• Bakkar’s score: 23 + 25 + 24 = 8 + 32 + 16 = 56

• Rashida’s score: 21 + 22 = 2 + 4 = 6

Bakkar wins the first round.


After removing the pocket with the highest value (5), the pockets are recolored starting with blue. The
new arrangement and colors will be represented as following:

 
3(Blue) 1(Red) 2(Blue) 4(Red)

For the next round, you will continue with this process until all pockets are removed, determining the
winner for each round.

Input
The first line contains a single integer n (1 ≤ n ≤ 105 ) — the number of pockets.
The second line contains n integers a1 , a2 , ..., an (1 ≤ ai ≤ 109 ) , where ai is the value of the i-th pocket.

Output
Print n lines. For each round, print the name of the winner of that round, “Bakkar“ if Bakkar wins,
“Rashida“ if Rashida wins.
It is guaranteed that there will always be a winner in each round.

Page 17 of 18
ECPC Qualifications 2024 (Day 2)
AAST, Alexandria, Egypt, July, 2024

Examples
standard input standard output
5 Bakkar
3 1 4 2 5 Bakkar
Bakkar
Rashida
Bakkar
6 Bakkar
10 2 8 3 5 7 Rashida
Rashida
Bakkar
Rashida
Bakkar

Page 18 of 18

You might also like