contest-1
contest-1
Syria
June 2024
Al-Baath Collegiate Programming Contest 2024
Syria,
Once upon a time in a boring class of electronic systems, Kifah and Ali were sitting there talking about
the problem set of Al-Baath contest. Kindhearted Ali came up with a cute probability problem and told
Kifah about it. Since Kifah doesn’t like cute problems, he couldn’t prevent himself from making some
changes to make it a little bit harder. And here is the problem.
In this problem, you will be given an array a1 , a2 , ..., an . Imagine an array b1 , b2 , ..., bn where bi ∈ [1, ai ] with
a uniform distribution. A subarray of b is one of its contiguous subsequences (i.e. an array [bl , bl+1 , ..., br ]
for some integers l and r such that 1 ≤ l ≤ r ≤ n).
We define the function f (l, r, x) of the subarray [bl , bl+1 , ..., br ] as the probability of
max([bl , bl+1 , ..., br ]) ≤ x.
You will be asked two types of queries:
It can be shown that this answer can always be expressed as a fraction P/Q where P, Q are coprime
integers and Q 6= 0 mod 109 + 7. Output the value of (P · Q−1 ) modulo 109 + 7.
Input
The first line contains a single integer T c (1 ≤ T c ≤ 105 ) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 105 ) — the size of the array a.
The second line contains n integers a1 , a2 , . . . , an (1 ≤ ai ≤ 105 ).
The third line of each test case contains a single integer q (1 ≤ q ≤ 105 ) — the number of queries.
Each of the following q lines has one of two types:
• Type 1: 1 p v where (1 ≤ p ≤ n, 1 ≤ v ≤ 105 ) — the position of the replaced integer and the new
value, respectively.
It is guaranteed that the sum of n and q over all test cases doesn’t exceed 105 .
Output
For each query of the type 2, print f (l, r, x).
Page 1 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Examples
standard input standard output
1 500000004
3
1 2 3
2
1 2 6
2 1 3 3
1 242668409
5 929198973
32 45 12 43 36 340051682
5 135000001
2 1 5 17
2 2 4 13
1 3 1000
2 1 4 40
2 2 5 55
Page 2 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
You are given three integers x, y, z and a string S consisting of n lowercase English letters.
For four integers l1 , r1 , l2 , r2 where (1 < l1 ≤ r1 < l2 ≤ r2 < n), let’s define the function F as follows:
You need to find four integers l1 , r1 , l2 , r2 where (1 < l1 ≤ r1 < l2 ≤ r2 < n) that maximize the value
F (l1 , r1 , l2 , r2 ) and satisfy the following conditions:
Note that S[l...r] is a substring of the string S that starts from position l and ends at position r, more
formally S[l...r] = Sl Sl+1 ...Sr−1 Sr , and if l > r then S[l..r] is an empty string.
If there are no integers l1 , r1 , l2 , r2 satisfying the mentioned conditions, print 0.
Input
The first line contains four integers n, x, y, z (4 ≤ n ≤ 106 , 1 ≤ x, y, z ≤ 109 ) — where n is the number of
letters in the string S.
The second line contains the string S, consisting of n lowercase English letters.
Output
Output a single integer — the maximum possible value of the function F , and if there are no integers
l1 , r1 , l2 , r2 satisfying the mentioned conditions, print 0.
Examples
standard input standard output
14 5 10 1 32
abreabqqytpsyt
5 2 3 5 10
aaaaa
Note
In the first test, the four integers are l1 = 5, r1 = 6, l2 = 9, r2 = 10.
S[5...6] = ab which equals to a prefix of S.
S[9...10] = yt which equals to a suffix of S.
(6 − 5) ∗ 5 + (10 − 9) ∗ 10 + (9 − 6) ∗ 1 + 10 + 5 − 1 = 32.
Page 3 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
You are given a grid of (n ∗ m) points , each point contains a hidden box. When you collect it, you
either gain some experience points "XPs" , or you lose some for being a horrible greedy player.
The grid has a guardian who won’t let you take more than k boxes. Moreover, each n points forming
a column has its own guardian. This guardian won’t let you open more than i boxes in the column
numbered i (in column number x, you can collect at most x boxes).
You want to level-up, so you want to gather as much XPs as possible.
* Columns are indexed 1 based (i : 1 2 3 ... m)
Input
The first line contains three integers n, m, and k (1 ≤ n ∗ m ≤ 106 ), (0 ≤ k ≤ n ∗ m), the dimensions
of the grid and the maximum number of boxes you can take.
Follow n lines, each containing m separated integers: ai1 , ai2 , ..., aim (−109 ≤ aij ≤ 109 ), the value of
each box in the grid.
Output
Print a single integer, the maximum amount of XPs you can get.
Examples
standard input standard output
1 1 1 1000
1000
5 2 10 107045
8 6666
5 1000
6 8457
9699 88889
125 0
3 3 6 26
2 -1 5
-2 3 3
4 5 6
Page 4 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Aghiad and Aram love math so much. They consider playing video games, sports, and any other activity
rather than math a waste of time. But sometimes they get bored from their math problems and want to
do something fun without wasting their time. To kill boredom, they came up with a math game that will
let them have some fun and waste no time. They write down an array consisting of n integers. They play
in turns, in each turn:
Now they check if they both scored a point (they are not playing against each other, they are playing
together). In this turn, they score a point if and only if the pair they have chosen fulfills this condition:
a3i +i3
a3j +j 3
= ai +i
aj +j and 1 ≤ i < j ≤ n
If they score a point, they add it to the total and write down on a paper the pair (i, j) so they can
remember it. They cannot choose any pair that was chosen earlier. The game finishes when there are no
pairs to choose from. In one day, they wrote an array a of length n, and they were too busy to play this
game, but they were too curious about the total score of this array, so they asked you to tell them the
final score.
Input
The first line of the input contains a single integer t (1 ≤ T ≤ 104 ) — the number of test cases in the test.
The first line of each case contains a single integer n (2 ≤ n ≤ 2 · 105 ) — the number of elements in the
array.
The second line of each case contains n integers a1 , a2 , . . . , an (1 ≤ aj ≤ 109 ) — the elements of the array.
It is guaranteed that the sum of the values of n over all test cases does not exceed 2 · 105 .
Output
For each test case, output the score that Aghiad and Aram would achieve if they play the game.
Example
standard input standard output
2 1
3 3
2 1 1
3
3 3 2
Note
In the first testcase:
a31 +13 23 +13 2+1 a1 +1
a32 +23
= 13 +23
=1= 1+2 = a2 +2
In the second testcase, all the pairs satisfy the condition of getting a point.
Page 5 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Kifah heard that some contestants in Al-Baath University doesn’t know how to read an integer in c++.
So he prepared a hard test to find out.
you will read an integer n, so we make sure you are a good programer. Then, you can ask the jury the
verdict you want. You can print “AC” (without quotes) if you want “Accepted” verdict, or you can print
any thing else.
Input
The only line has one integer n (1 ≤ n ≤ 1000) some nonsense integer.
Output
Print “AC” (without quotes) if you want “Accepted” verdict. Otherwise, print any thing.
Hint: You are here to get Accepted.
Example
standard input standard output
5 AC
Page 6 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Abdullah and his friend Kif ah live in a real country called BP , which has n cities that can be described
as a weighted tree rooted at node 1.
kif ah wants to buy some presents for his girlfriend but unfortunately he doesn’t have any money, so he
will borrow some money from his friend Abdullah.
Abdullah is a good friend, so he will help Kif ah.
Abdullah works as a TAXI driver. For a ride from city a to city b (note that city b must be in the subtree
of the a, more formally the city a must lie on the simple path between city 1 and city b), Abdullah earns
an amount of money equal to the sum of weights in the simple path between a and b , and he spends
hours equal to the number of edges in the simple path between a and b.
Kif ah loves his girlfriend so much and wants to buy her q presents.
Abdullah will give you the construction of the country and then for each present he will tell you the city
cityi that he is currently located in and the price pi of the present that Kif ah wants to buy. Can you
tell him the minimum possible number of hours required to make money more than or equal to pi . If it’s
impossible to earn that amount of money just print −1.
Input
The first line contains an integer n(1 ≤ n ≤ 105 ), the number of the cities in BP .
The next n − 1 lines describe the roads. The ith line contains three integers ui , vi and wi
(1 ≤ ui , vi ≤ n, ui 6= vi , 1 ≤ wi ≤ 109 ), meaning that there is an edge between the nodes ui and vi
with weight wi .
The next line contains a single integer q (1 ≤ q ≤ 105 ), the number of presents that Kif ah wants to buy
for his girlfriend.
For the following q lines, the ith line contains two integers cityi , pi (1 ≤ cityi ≤ n, 1 ≤ pi ≤ 1018 ) meaning
that Abdullah will start from cityi and wants to earn an amount of money more than or equal to pi .
Output
For each present, print the minimum possible number of hours required to make money more than or
equal to pi if Abdullah started the ride from cityi , or if it’s impossible to earn that amount of money, just
print −1.
Example
standard input standard output
5 2
1 2 3 -1
2 3 4 -1
2 4 2
3 5 6
3
1 6
2 11
5 2
Page 7 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Note
To buy the first present, Abdullah can go from city 1 to city 3 spending 2 hours, and earning an amount
of money equal to 7 which is greater than 6.
For the second present, the maximum amount of money Abdullah can earn is 10 by having a ride to the
city 5, 10 is lower than 11, so the answer is −1.
For the third present, Abdullah can’t go to any city (because there are no cities in the subtree of the city
5), so the answer is −1.
Page 8 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Problem G. Da7doo7
Input file: standard input
Output file: standard output
Balloon Color: Yellow
Da7doo7 believed he had mastered competitive programming contests, so he decided to turn his attention
to playing basketball.
p0
Da7doo7 intends to shoot n balls into the basket. The probability of him scoring the first shot is q0 .
For subsequent shots, his probability of scoring depends on whether he made the previous shot or not:
p1
If he scored shot i − 1, the probability of scoring shot i is q1 .
p2
If he missed shot i − 1, the probability of scoring shot i is q2 .
Da7doo7 is asking for your help to find the expected value of the number of balls that will score.
We can show that the answer can be written in the form Q
P
where P, Q are coprime integers and Q 6= 0 mod
998244353. Output the value of (P · Q ) modulo 998244353.
−1
Input
The first line contains a single integer T (1 ≤ T ≤ 5 × 104 ) — the number of testcases.
The first line of each testcase contains an integer n (1 ≤ n ≤ 1012 ) — the number of balls he will shoot.
The second line of each testcase contains six integers p0 , q0 , p1 , q1 , p2 , q2 (0 ≤ p0 , p1 , p2 ≤ 103 ,
1 ≤ q0 , q1 , q2 ≤ 103 ).
Output
An integer representing the expected value of the number of balls that will score, modulo 998244353.
Example
standard input standard output
5 249561089
2 965715715
5 10 2 10 3 10 403450441
10 210477862
7 10 5 10 2 10 330198862
5
1 5 7 10 9 10
100
1 1 1 10 1 1
100000000
75 100 55 100 23 100
Page 9 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Ali wanted to make a very hard problem, so he thought really hard and after a week of continuous work,
he came up with this problem:
You have a set M containing all numbers from 1 to n. In one operation, you can choose an integer x and
subtract it from all numbers that are greater than or equal to it. If a number becomes zero, you remove
it from the set. What is the minimum number of operations required to make the set empty?
Input
The only line of input contains one integer n (1 ≤ n ≤ 100) ,as described in the problem.
Output
Print one integer, the minimum number of operations required to make the set empty.
Examples
standard input standard output
1 1
2 2
47 6
Note
In the first example, the set is {1} so we choose x = 1, and after this operation, the set will be empty.
In the second example, the set is: {1, 2}, we can choose x = 2 so the set will become: {1} ,and after that
we choose x = 1.
Page 10 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
In a rectangular room, bored Kifah lives. Kifah used to walk scanning the whole room in a specific
pattern while observing the digits written on the tiles of the room. At that moment, he wondered about
the following imaginary problem:
Imagine a rectangular room with dimensions n × m. You scan the room using the following pattern:
• Scan the first line with length m from the left to the right.
• Scan the second line with length m from the right to the left.
• Scan the third line with length m from the left to the right.
• And so on.
On each tile of the n × m tiles, one digit is written. Imagine that there are some determined rectangles
that cover some of the room. All the rectangles are inside the room and they may overlap. The borders of
the rectangles and the borders of the room are parallel to each other. You have a sheet of paper and an
empty basket. At the beginning, the sheet of paper is considered empty but if you want to answer what
is the number written on the paper, you can say it’s zero. While scanning the room using the previous
pattern, you will do the following:
• If the current tile is covered by at least one of the rectangles, you do one of the following:
• If the current tile is not covered by any of the rectangles, you put the paper in the basket and
continue with a new empty paper (even if the paper is empty, you put it in the basket and consider
that it has a number of zero).
At the end of your walk, you look inside the basket. If there is at least one paper in the basket with a
number written on it (the number that consists of the digits you added to the paper while walking) within
the range [A, B], we consider the walk as a good walk. We consider two walks different if the decision
made at some tile in the first walk differs from the decision made at the same tile in the second walk
even if both of the decisions have led to the same result. The two numbers A and B are your favourite
numbers so you know them previously.
You will be asked multiple queries, for each query, you will be given a set of rectangles and asked to
answer the following question: For all the possible walks that you can make, what is the number of the
good walks.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 20) — the dimension of the room.
Then you will read a grid ai,j of n lines and m columns (1 ≤ i ≤ n; 1 ≤ j ≤ m). Where (1 ≤ ai,j ≤ 9). —
the digits written on the tiles of the room.
Page 11 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
The next line has two integers A, B (1 ≤ A, B ≤ 1018 ) — your favourite numbers.
The next line contains one integer q (1 ≤ q ≤ 1000) — the number of queries.
For each query, the first line is s (1 ≤ s ≤ 1000) — the number of rectangles at this query.
For the following s lines, each line contains four numbers x1 , y1 , x2 , y2 (1 ≤ x1 ≤ x2 ≤ n; 1 ≤ y1 ≤ y2 ≤ m)
— the upper left corner of the rectangle (x1 , y1 ) and the lower right corner of the rectangle (x2 , y2 )
respectively.
Output
For each query, print the number of good walks considering the given rectangles in that query. Output
the answer modulo 109 + 7.
Examples
standard input standard output
2 3 2
5 3 1 6
3 9 2
2 30
2
1
1 1 1 1
1
1 3 2 3
3 4 1686
3 6 3 9
1 3 4 2
7 1 8 9
525 4487960024
1
6
2 4 2 4
2 1 2 2
1 3 2 3
2 4 3 4
2 4 2 4
1 1 1 2
Note
For the first query there are 3 different walks:
Page 12 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
• you add 1 to the left then add 2 to the left, getting 21.
• you add 1 to the left then add 2 to the right, getting 12.
• you add 1 to the right then add 2 to the left, getting 21.
• you add 1 to the right then add 2 to the right, getting 12.
Page 13 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
The setter of this problem hates long statements’ problems, so the problem is:
The Score of array b of size m equals:
m−1
X
gcd(bi , bi+1 )
i=1
Input
The first line contains a single integer t(1 ≤ t ≤ 100) — the number of testcases.
Then the descriptions of t testcases follow.
The first line of each testcase contains two integers n and k— the size of the array a (2 ≤ n ≤ 2 · 105 ) and
k explained above (1 ≤ k ≤ n − 1).
The second line consists of n integers a1 , a2 , ..., an (1 ≤ ai ≤ 4 · 105 ). It’s guaranteed that the sum of n
over all testcases won’t exceed 2 · 105 .
Output
Output for each testcase the maximum Score.
Example
standard input standard output
2 30
5 1 22
2 4 8 16 32
5 2
2 4 1 16 32
Page 14 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
In an imaginary country called IM, there are n cities and m directed roads. However, these roads may
not connect all the cities.
Kifah has been a resident of IM for many years and knows the country well. On a sunny morning, while
chatting with his friend Abdullah, a claim was made. Abdullah stated that he had moved to IM but was
unsure of his exact location. Skeptical, Kifah didn’t believe him and demanded proof. In response, Abdullah
provided a set of city pairs, asserting that for each pair, there exists a path starting from the first city
and ending at the second, passing through Abdullah’s current city.
Can you help Kifah to check if there is any contradiction in Abdullah’s claims.
In this Problem you will be given a graph consists of n nodes and m directed edges. Note that the graph
may not be connected, but it is guaranteed that there is no multi-edges or self-loops.
Then you will be given a set of q pairs of nodes (a, b) that claims: there is a path starts from a, ends at b
and passes by the city where Abdullah is. These paths may traverse nodes or roads multiple times.
Your goal is to help Kifah determine whether Abdullah’s claims hold without contradiction, thus verifying
his honesty.
Input
The first line contains a single integer T c (1 ≤ T c ≤ 5 · 104 ) — the number of test cases.
The first line of each test case contains two integers n, m (1 ≤ n ≤ 5 · 104 , 1 ≤ m ≤ min(n · (n − 1), 105 ))
— the number of vertices and edges in the graph respectively.
For the following m lines, each line contains two integers (u, v) (1 ≤ u, v ≤ n), describing a directed edge
from vertex u to vertex v. It is guaranteed that there are no multi-edges or self-loops in the graph.
The next line contains a single integer q (1 ≤ q ≤ 5 · 104 ), the number of pairs Abdullah provided.
For the following q lines, each line contains two integers (a, b) (1 ≤ a, b ≤ n), describing a claim that
says: There is a path starts from a, ends at b and pass by the city where Abdullah is. These paths may
traverse nodes or roads multiple times.
It is guaranteed that both the sum of n and q over all test cases doesn’t exceed 5 · 104 .
It is guaranteed that the sum of m over all test cases doesn’t exceed 105 .
Output
For each test case, output "YES"(without quotes) if Kifah can’t find any contradiction in Abdullah’s
claims, and "NO"(without quotes) otherwise.
Page 15 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Example
standard input standard output
2 YES
4 3 NO
1 2
2 3
3 4
2
1 3
2 4
5 4
1 2
2 3
4 3
5 3
2
1 3
5 4
Note
In the first testcase, both nodes 2 and 3 meet the claims. For node 2 the path 1 → 2 → 3 meets the first
pair (1, 3), and the path 2 → 3 → 4 meets the second pair (2, 4). So we can’t say that Abdullah is a Liar,
that’s why we provide "YES".
In the second testcase, there is no node that meets the constraints. For the first pair in the constraints
(1, 3) the only path that meets this constraint is 1 → 2 → 3. On the other hand, for the second pair in
the constraints (5, 4), there is no path that can start from 5 and end at 4. So we can say that Abdullah is
a Liar because there is a contradiction, that’s why we provide "NO".
Page 16 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
A truck driver was asked to transport 2n boxes of active chemical substances. He is in a hurry and does
not care about bumps in the road that will cause the substances in the back to react with each other.
Initially, there were n boxes of type A and n boxes of type B placed over each other carefully in a certain
arrangement forming a tower of height 2n.
The truck jumps upon hitting a road bump, and all the boxes fly in the air. A random box (uniformly
picked from boxes in the air) hits the truck ground first, then another random box (also picked uniformly)
hits the first box, and so on. Only if an active box hits another active box beneath it of the opposite type
(A hits B or B hits A), they react with each other, releasing a gas C, and both boxes become inactive.
The clever driver smells the gases after each bump and stops to get rid of the inactive boxes, then places
the remaining active boxes over each other with any order he wants carefully (without causing reactions)
and continues driving recklessly!
What is the expected number of bumps after which the truck becomes empty, and the driver gets fired?
It can be shown that this answer can always be expressed in the form P/Q where P, Q are coprime integers
and Q 6= 0 mod 998244353. Output the value of (P · Q−1 ) modulo 998244353.
Input
The first line contains a single integer 1 ≤ T ≤ 20 — the number of test cases.
Each of the following t lines contains an integer 0 ≤ n ≤ 200 — both the number of A boxes and the
number of B boxes.
Output
For each of the testcases, print the expected number of bumps modulo 998244353.
Example
standard input standard output
4 0
0 1
1 332748119
2 232923684
3
Note
For the case n = 3, one way the process can happen:
After the first bump, the boxes get shuffled randomly, and the reaction happens from left to right (down
to up in the problem statement) as follows:
ABAABB -> [AB]AABB -> [AB]A[AB]B -> [C]A[C]B
The driver then gets rid of the reactants. When the second bump comes, the remaining boxes get shuffled
randomly and react again, for example:
BA -> [BA] -> [C]
Finally, the driver gets rid of the reactants, and the truck becomes empty; here we needed 2 bumps.
Page 17 of 18
Al-Baath Collegiate Programming Contest 2024
Syria,
Ali is playing a very boring game called BOX. In this game, you have a box that contains x coins, and
there is a road which contains n checkpoints where you can get or lose coins. The road can be represented
as an array a containing n integers where ai represents the amount of coins that you may gain or lose at
the ith checkpoint. You start at the beginning of the road "at index 0". The game goes as follows:
You start at index zero with an initial amount of coins equals to x. You start visiting the checkpoints
from left to right without skipping any of them and with the capability of ending the game any time you
want.
At each checkpoint, you get or lose an amount of coins defined by ai . If at any point, the total amount of
coins you have becomes negative, you lose the game.
You can do this operation only for once: pay c coins "if you have them"and choose an index i where i
is after your current checkpoint, then reverse the subsegment [i, n].
The goal of the game is to maximize the amount of coins you have. Ali isn’t very good at this kind of
game (he only plays shooters) so he asked you to help him. Can you find the maximum amount of coins
Ali can get?
Input
The first line contains three integers: n, x, c (1 ≤ n ≤ 106 ), (0 ≤ x, c ≤ 109 ), number of checkpoints, the
initial value of coins in the box, and the cost of flipping a segment.
The second line contains n integers, a1 , a2 , ..., an (−109 ≤ ai ≤ 109 )
Output
Print one integer, the maximum amount of coins Ali can get.
Examples
standard input standard output
4 0 1 7
1 -2 3 4
13 19 22 87
12 30 -1 15 -55 -9 17 5 -4 6 -15 10 15
Note
In the first example, we move to the first checkpoint and add it to x, so now x = 1, then we pay c = 1
and flip the subsegment [2, 4], so the array becomes: [1 4 3 −2], now we move to the second and the third
checkpoints and then we end the game, so the final result will be: x = 0 + 1 − 1 + 4 + 3 = 7.
Page 18 of 18