0% found this document useful (0 votes)
20 views

Problemset

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Problemset

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)

AAST, Egypt, June, 2024

Problem A. Batates and Palindrome


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

Given a rooted tree with n vertices, each vertex is associated with a character. The tree is traversed in
depth-first search (DFS) order starting from vertex 1. The children of each vertex are visited in ascending
order of their indices.
For example, if you are at vertex 1 and have vertices 4, 5, 2 as children, you should visit them in the
following order: 2, 4, 5.
The kth ancestor of a vertex in a tree is the vertex reached by moving k steps upward from the given
vertex. If there are fewer than k ancestors (i.e., the root is reached before taking k steps), then the kth
ancestor does not exist.
Given q queries, each query consists of two integers v and k:
Your task is to determine if the characters on the vertices that share the kth ancestor with a given vertex
v form a palindrome when listed in DFS order.
In other words, for all vertices u such that the kth ancestor of u is the same as the kth ancestor of v, put
the characters of these vertices in a string in the order they appeared in the DFS order and check if that
string is a palindrome or not.
It is guaranteed that the kth ancestor of the vertex v exists.

Input
The first line contains a single integer t (1 ≤ t ≤ 105 ) — the number of test cases.
The first line of each test case contains two integers n, q (1 ≤ n ≤ 2 · 105 ),(1 ≤ q ≤ 3 · 105 ) — the number
of vertices in the tree and the number of queries.
The next line contains n lowercase characters s1 , s2 , . . . , sn , where si is the character on vertex i.
The next line contains n − 1 integers p2 , p3 , . . . , pn (1 ≤ pi < i), meaning that the parent of vertex i is pi .
The next q lines contain two integers v, k (1 ≤ v ≤ n) it is guaranteed that the kth ancestor of the vertex
v exists.
It is guaranteed that the sum of n over all test cases does not exceed 2 · 105 and q does not exceed 3 · 105 .

Output
For each query if the characters on the vertices that share the kth ancestor with vertex v make a palindrome
print “YES”, otherwise print “NO”.
You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”,
“Yes”, and “YES” will be accepted as a positive answer.

Example
standard input standard output
1 YES
6 2 NO
a a b a a b
1 1 1 2 4
2 1
5 2

Page 1 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Note
For the first query, the vertices u that share the 1th ancestor with vertex 2 are 2, 3, and 4, in that order.
The string created from the characters in that order will be equal to “aba”, which is a palindrome.
For the second query, the vertices u that share the 2th ancestor with vertex 5 are 5 and 6, in that order.
The string created from the characters in that order will be equal to “ab”, which is not a palindrome.

Page 2 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem B. Batates and Aziza


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

Batates and Aziza recently broke up, and he had to get his ring back. Since Aziza was angry, she told
him that he had to get the ring within t days, or she wouldn’t give it to him. Batates and Aziza live in
two different cities.
There are n cities arranged in a circular way where city c1 comes after city cn . The only way to travel
between cities is to travel through bridges. The bridges are set in a way that every city ci has a one-way
bridge to city ci+k and city ci+k+1 .
If i + k exceeds n, then the correct city index is (i + k − n). Similarly, if i + k + 1 exceeds n, the correct
city index is (i + k + 1 − n). Each bridge can be crossed in one day.
You will be given the number of cities, the index of the city where Batates is currently located, the index
of the city where Aziza lives, the number of days Aziza has given to Batates, and k, the setting of the
bridges. You will have to determine whether Batates can reach Aziza within t days or if he is going to
lose his ring forever.

Input
The first line contains five integers separated by spaces: n (1 ≤ n ≤ 105 ) number of cities, t (1 ≤ t ≤ 105 )
number of days Aziza gave to Batates, k (0 ≤ k < n) setting of the bridges, a (1 ≤ a ≤ n) index of the
city where Batates is currently at, b (1 ≤ b ≤ n) index of the city where Aziza lives.

Output
If Batates can get his ring back, print “YES”; otherwise, print “NO”.
You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”,
“Yes”, and “YES” will be accepted as a positive answer.

Examples
standard input standard output
7 2 4 1 4 YES
7 1 4 1 4 NO

Page 3 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem C. Batates and Friendz


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

Batates observed that his friend, Tamez, has a fascination with bracket sequences. To challenge Tamez,
Batates presented him with a problem involving bracket sequences.
Given a bracket sequence s in which some positions have been replaced with ‘?’, the task is to determine
the probability that s will form a valid regular bracket sequence when each ‘?’ is replaced randomly with
either ‘(’ or ‘)’.
The probability of replacing a ‘?’ with ‘(’ or ‘)’ is equal.
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic
expression by inserting characters ‘1’ and ‘+’ between the original characters of the sequence. For
example:

• bracket sequences “()()” and “(())” are regular (the resulting expressions are: “(1)+(1)” and
“((1+1)+1)”).

• bracket sequences “)(”, “(” and “)” are not.

Input
The first line contains one integer T (1 ≤ T ≤ 103 ) — the number of test cases.
For each test case:
The first line contains n (1 ≤ n ≤ 103 ) — the size of s.
The second line contains s — the bracket sequence.
It is guaranteed that the sum of n overall test cases does not exceed 103 .

Output
You should output one number — the answer modulo 109 + 7.
Formally, let M = 109 + 7. It can be shown that the answer can be expressed as an irreducible fraction
p
q , where p and q are integers and q 6≡ 0 (mod M ). Output the integer equal to p · q
−1 mod M . In other

words, output the integer x that 0 ≤ x < M and x · q ≡ p (mod M ).

Example
standard input standard output
4 1
10 0
(()())()() 250000002
3 992187507
(?)
6
(?()??
8
????????

Page 4 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem D. Batates and Subsequence


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

Batates loves to play with subsequences. He has a sequence of balls, each with a value written on it. He
wants to choose a subsequence of balls such that the difference between the values of every two consecutive
balls in the subsequence is no more than x. Batates is confused and needs your help to determine the
subsequence with the maximum sum of values on the balls under the given restriction.
Note that the subsequence can be empty, in which case its value will be 0.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements
without changing the order of the remaining elements.

Input
The first line contains two integers n, x (1 ≤ n ≤ 2 · 105 ) (0 ≤ x ≤ 109 ) the numbers of balls and the
maximum difference between every two consecutive balls.
The second line contains n integers a1 ,a2 ,....,an (−109 ≤ ai ≤ 109 ) the values written on each ball.

Output
Print a single integer the maximum value of subsequence Batates can get.

Examples
standard input standard output
5 3 16
1 3 5 7 15
10 1000 10000
-1000 100 15 3 9 -10 20 -200 10000 400

Page 5 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem E. Batates and Football


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

Batates is playing a football game consisting of n integers. There is a sequence of balls in a row In front
of him,
He starts playing as follows:
1) He starts kicking ball number a1 from the left side.
2) He starts kicking ball number an from the right side.
3) He starts kicking ball number a2 from the left side.
4) He starts kicking ball number an−1 from the right side.
5) and so on until the sequence is empty.
For example, if n = 5 and a = [1, 9, 2, 5, 4] then the sequence of kicking the ball is [1, 4, 9, 5, 2].
You are given the final sequence of kicked balls and you want to restore the original sequence.

Input
The first line contains one integer T (1 ≤ T ≤ 105 ) — the number of test cases.
Then follows T test cases.
The first line of the test case contains an integer n (1 ≤ n ≤ 5 · 105 ) — the length of the sequence of the
balls.
The second line contains n integers a1 ,a2 ,. . . ,an (1 ≤ ai ≤ 109 ) — the sequence of the balls kicked by
Batates
It is guaranteed that the sum of n overall test cases does not exceed 5 · 105 .

Output
Outputs the original sequence.

Example
standard input standard output
6 1 9 2 5 4
5 67
1 4 9 5 2 174 53 91 1 1 10 21 43
1 14324 14524 10 2
67 1 3
8 1
174 43 53 21 91 10 1 1
4
14324 2 14524 10
2
1 3
1
1

Page 6 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem F. Another Challenge


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

Teams HRY and BAM decided to participate in a competitive programming contest this year. Team HRY
likes graphs a lot because they think graphs are challenging and that any other topic is boring. Team
BAM decided to give them a hard challenge using an array to prove them wrong.
Given an array of n numbers, for each index i (1 ≤ i < n), count the number of indices j such that j ≤ i
and for each k > i, aj > ak must hold.

Input
The first line contains a single integer t (1 ≤ t ≤ 104 ) — the number of challenges BAM has for HRY.
For each challenge, there will be 2 lines of input:
The first line contains a single integer n (1 ≤ n ≤ 105 ) — the number of elements in the array.
The second line contains n space-separated integers a1 , a2 , ..., an (1 ≤ ai ≤ 109 ) — the elements of the
array.
It’s guaranteed that the sum of n over all test cases won’t exceed 105 .

Output
For each index i (1 ≤ i < n), you should output the number of indices j for which the conditions above
are satisfied.

Example
standard input standard output
1 1 2 3
4
4 3 2 1

Page 7 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem G. Batates AND Array


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

Given an array of length n the score of a subsequence c of length m is c1 & c2 & c3 ... cm where & is the
bitwise and operation you are required to get the max score possible from the array.

Input
The first integer t (1 ≤ t ≤ 104 ) — the number of test cases

The first line of each test case contains integer n (1 ≤ n ≤ 105 ) — length of a
the second line containts n integers a1 , a2 , a3 , ..., an (0 ≤ ai ≤ 109 ) — array a

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

Output
For each test case, output one integer — the maximum possible score you can get from any subsequence
from the array a.

Example
standard input standard output
2 5
4 3
1 5 2 4
3
1 3 2

Page 8 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem H. Expected Length


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

Given a weighted directed acyclic graph (DAG) with n vertices and m edges.
You will pick a random path from node 1 to node n and find its length. Find the expected length of the
path you will pick modulo 109 + 7.
The length of the path is the sum of the weights of edges on the path. The probability of picking any
path from node 1 to node n is the same for all paths.

Input
The first line contains two integers n, m , (2 ≤ n ≤ 105 ), (n − 1 ≤ m ≤ min( n∗(n−1)
2 , 3 × 105 ) — the
number of nodes and the number of edges.
Then m lines contain the descriptions of edges.
Each of them is described by three integers vi , ui , wi , (1 ≤ vi , ui ≤ n, vi 6= ui , 1 ≤ wi ≤ 109 ), where vi , ui
are numbers of the nodes connected by this edge and wi is the weight of the edge.

Output
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.

Example
standard input standard output
4 4 5
1 2 1
1 3 2
2 4 3
3 4 4

Note
In the sample test case, there are 2 possible paths from node 1 to node n:
1 -> 2 -> 4 with total weight of 4
1 -> 3 -> 4 with total weight of 6
Thus the expected length is equal to (4 + 6)/2 = 5

Page 9 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem I. Two Baskets


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

You are given an array of size n, and you have two empty baskets. You need to put each number in the
array in one of the two baskets.
The value of a basket is the Bitwise ORing of all elements in it. Find out the minimum difference of the
values of the two baskets.

Input
The first line contains an integer n – the size of the array. (1 ≤ n ≤ 20)
The second line contains n integers – the elements of the array. (1 ≤ ai ≤ 109 )

Output
Output an integer, the minimum difference of the values of the two baskets.

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

Page 10 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem J. Batates and his ball


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

A boy named Batates was always curious about the shortest distance his ball will travel to reach a certain
point. His friend Hamood loves challenges, so he challenged Batates to stand at the corner (0, 0) of a
rectangular room with its lower-left corner at (0, 0) and upper-right corner at (n, m). The task is to throw
the ball so that it goes directly to the corner (n, m) without hitting the borders of the room. Calculate
the distance that the ball will travel to reach (n, m).

Input
The first integer t (1 ≤ t ≤ 105 ) — the number of test cases.
The only line of each test contains two case integers n, m (1 ≤ n, m ≤ 109 ).

Output
For each test case output a single integer the distance the ball will travel reach (n, m) without hitting the
borders of the room rounded up to the nearest integer on a separate line.

Example
standard input standard output
3 7
4 5 12
6 10 2
1 1

Page 11 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem K. Batates and Money


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

In a small town, there lived a poor yet lovable boy named Batates. Struggling to make ends meet, he
decided to ask his friends for some money. With a heart full of hope, he approached them, hoping they
would understand his dire situation and lend a helping hand.
Batates has n friends and there are m relations between them.
Each friend i has some money donated, denoted as ai .
Each group of friends decides to collect all the money they have and gives it to Batates.
Batates wants your help to find out the maximum amount of money he can collect by asking one group
only.

Input
first line containing two integers n and m (1 ≤ n ≤ 105 , 0 ≤ m ≤ 2 · 105 ) Batates’ friends and the number
of relationships between them.
then n numbers money owned by each friend (1 ≤ ai ≤ 109 ).
then m lines contains 2 integers x, y means that x, y are friends (1 ≤ x, y ≤ n).

Output
maximum money he can collect from asking one group only

Examples
standard input standard output
4 3 58
15 20 6 17
1 2
3 4
4 1
5 3 210
200 10 30 40 13
1 2
5 3
3 4

Page 12 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem L. Sherlock Thoughts


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

One day Batates visited Sherlock in his apartment but when Batates entered the room he found Sherlock
drowning in his thoughts but he notices something interesting while Sherlock was thinking he can’t stop
walking, he was walking back and forth in the room.
Batates kept watching Sherlock walking until he stops and solve the mystery, Given the the number of
times Sherlock has walked in the room and the width of the room distance can you tell Watson the amount
of time Sherlock has taken to solve the mystery , Note that Sherlock walk half meter in one second.

Input
The first line contains a single integer t (1 ≤ t ≤ 105 ) — the number of test cases.
The first and only line of each test case contains two double n and w (1 ≤ n, w ≤ 109 ) — the number of
times Sherlock walked and the room width respectively.

Output
For each test case, print the amount of time Sherlock needed to solve the mystery.
Your answer is considered correct if its absolute or relative error doesn’t exceed 10−6

Example
standard input standard output
3 17.5
2.500 3.500 4.669
1.750 1.334 9.370296
1.278 3.666

Page 13 of 14
The 2024 ICPC Egyptian Collegiate Programming Contest for Teens(Online Qualification)
AAST, Egypt, June, 2024

Problem M. Batates and GCD


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

Given an array of n integers and q queries, there are three types of queries:

• 1 l r: Let g be the GCD of the subarray from index l to r. Divide each element in the subarray by
g.

• 2 l r: Output the sum of the subarray from index l to r.

• 3: Undo the last applied query of the first type.

Each type 3 query undoes the most recently applied type 1 query that has not been previously canceled.
For each query of type 3, it is guaranteed that there is a query of type 1 that has not been canceled.

Input
The first line contains a single integer t (1 ≤ t ≤ 105 ) — the number of test cases.
The first line of each test case contains two integers n, q (1 ≤ n, q ≤ 105 ) — the length of the array a, the
number of queries.
The next line contains n integers a1 , a2 , . . . , an (1 ≤ ai ≤ 109 ) — the array a.
The next q lines contain the queries as described above.
It is guaranteed that in the queries of the first and second type (1 ≤ l ≤ r ≤ n).
It is guaranteed that the sum of n and q over all test cases does not exceed 105 .

Output
For each query of the second type, output the sum of the subarray from index l to r.

Example
standard input standard output
1 5
6 6 10
2 6 10 5 5 15 15
1 3 6
2 4 6
1 1 3
2 1 6
3
2 1 6

Page 14 of 14

You might also like