Acpc 2020
Acpc 2020
Coach Academy teaches students to think, create and code. One day the instructor gave the students this
interesting problem.
If you were one of these students, would you be able to solve it?
Given a set of N points on the circumference of a 2D circle centered at the origin, where the ith point is
located at ai degrees from the +ve x-axis.
You need to find the minimum integer arc measure M , such that you can covered all the points using no
more than K arcs of measure M . The arc covering must be contiguous with no gaps.
It is ok for two or more arcs to overlap.
Input
First line will be the number of test cases T , in each test case you will be given the following:
First line will have 2 integers N and K separated by a space. (1 N, K 360)
Followed by a line containing N integers separated by a space, each integer (0 Ai 359)
Output
For each test case, output one number in a line by itself, M the minimum arc measure.
Page 1 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Example
arc.in standard output
1 180
3 1
0 90 180
Note
An arc measure is the number of degrees it can cover from the circumference. For example, an arc measure
of 90 covers quarter of the circle.
Page 2 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Problem B. Basketball
Input file: basket.in
Output file: standard output
Balloon Color: Purple
Input
First line will be the number of test cases T , in each test case you will be given the following:
One line containing four integers: L, D, A, B separated by spaces. 109 A 0, 109 B 109 ,
0 L 109 and 0 < D < L
Output
For each test case, on a new line, if it is possible to score a point, output 2 numbers : y the position and
✓ the angle of the board (refer to the figure, output angles in radians). Otherwise output -1.
The answer (y, ✓) is considered correct if after hitting the board with an angle ↵, at a point p distant from
the midpoint by at most 10 4 , the ball will go through the hoop without intersecting any endpoint of the
line segment representing the hoop, following a line that makes an angle 2 [⇡ ↵ 10 4 , ⇡ ↵ + 10 4 ]
with the board.
Page 3 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Example
basket.in standard output
2 -1
2 1 -1 5 154.3340548626 -0.7071634234
14 5 -1 25
Note
It is possible for the ball to go through the hoop from below, hit the board and bounce back to score a
point.
Page 4 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Problem C. Completions
Input file: completions.in
Output file: standard output
Balloon Color: Gray
Input
First line will be the number of test cases T , in each test case you will be given the following:
The first line will have two integers separated by a space N1 and N2 , the lengths of the strings s1 and s2 .
where (1 N1 , N2 105 ).
The second line will have the string s1 of length N1 .
The third line will have the string s2 of length N2 .
Strings will contain only uppercase English letters.
Output
For each test case, output 1 integer in a line by itself, the answer to the problem.
Example
completions.in standard output
1 37
8 8
ABCDABQW
ABCGABQW
Page 5 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Problem D. Differences
Input file: differences.in
Output file: standard output
Balloon Color: Dark green
You have an array of pairs of integers (Xi , Fi ) where Fi is the frequency of Xi in some very big array A.
You will be given Q queries to be performed on A, each query will have 2 integers l and r.
For each query, output the number of pairs of integers (Ai , Aj ) with an absolute difference between l and
r (inclusive). In other words, the number of pairs (Ai , Aj ) such that l |Ai Aj | r.
Since this number is large, output it modulo 132120577.
Input
First line will be the number of test cases T , in each test case you will be given the following:
First line containing an integer N , the number of frequency pairs. (1 N 105 )
Followed by N lines where each line has 2 integers separated by a space: Xi and Fi . (1 Xi N ) and
(1 Fi 109 )
Followed by a line containing an integer Q, the number of queries. (1 Q 106 )
Followed by Q lines, each line containing 2 integers l and r separated by a space. (1 l, r N )
Output
For each test case, output Q integers, each integer on a separate line.
Each line should contain 1 integer, the answer to the ith query.
Example
differences.in standard output
1 3
3
1 1
2 1
3 1
1
1 3
Page 6 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Problem E. Endgame
Input file: endgame.in
Output file: standard output
Balloon Color: Orange
A rook endgame is a position in chess where all the pieces were captured, and the only remaining pieces
are the 2 kings and 1 rook (in our case, white is the one having the rook).
Although this end game is winning for white, it is not that easy to actually win this endgame. White
needs to know exactly what they need to do. Otherwise, it is easy for black to draw this endgame.
As many may know, the king in chess can move only one square at a time in any of the 8 directions, while
the rook can move any number of squares, but only vertically and horizontally.
Chess board consist of 8 ⇥ 8 squares, the rows are numbered 1 to 8, and the columns are numbered ‘a’ to
‘h’.
The position of a square is denoted by the letter of the column followed by the number of the row. For
example “c5” is the square in the 3rd column and the 5th row.
Given the position of the white king, the white rook, and the black king. It is black to move. Your task
is to output whether black’s king is under attack by white’s rook or not.
Input
First line will be the number of test cases T , in each test case you will be given the following:
One line containing 3 strings separated by spaces. The position of the white king, white rook, and black
king in that order.
Page 7 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Output
For each test case output a single line containing “YES” if the black king is attacked by the rook, or “NO”
otherwise.
Example
endgame.in standard output
1 YES
f3 d5 b5
Page 8 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Input
First line will be the number of test cases T , in each test case you will be given the following:
First line will contain an integer N , the length of the string. (1 N 105 )
Second line will contain N positive floating point numbers separated by spaces, the ith number is the
probability of bit i being a 1. All numbers on this line are 1.
Output
For each test case, output one number, the variance of Y .
Example
variance.in standard output
4 0.000000
1 0.250000
0.5 0.224400
2 1.079200
0.5 0.5
2
0.1 0.3
5
0.1 0.2 0.3 0.4 0.5
Note
The variance can be computed with the formula: V ariance(Y ) = Expectation(Y 2 ) Expectation(Y )2
Page 9 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
An onion graph is a collection of cycles C1 , C2 , ..., Ck , ..., C2 , C1 with some extra edges. We call each cycle
a ‘layer’.
Each layer is a cycle with the subscript number of nodes. For example, for k = 3, the graph is composed
of C1 , C2 , C3 , C2 , C1 (one node, two connected nodes, a triangle of three connected nodes, two connected
nodes and finally, one node).
In addition to the edges in each cycle, a node in any layer can be connected by a single edge to nodes of
adjacent layers. Also, the nodes in the C1 layers can be connected by a single edge. However, nodes from
non-adjacent layers cannot be connected by a single edge.
Given an onion graph with 1 k 5, count the number of 4-colorings. Since this number can be large,
print the result mod 132120577.
A 4-coloring is an assignment of colors (red, green, yellow and blue) to nodes, such that if two nodes are
connected by an edge, they must have different colors.
Input
First line will be the number of test cases T , in each test case you will be given the following:
The first line contains three integers separated by spaces n, k, m. (1 k 5)
Where n is the total number of nodes, k is such that the widest middle layer has k nodes and m is the
number of edges, the number of edges will be at least the edges required to make the cycles of a correct
onion graph, but nodes of adjacent cycles can be fully connected.
Followed by m lines, each contains a pair of integers separated by spaces, denoting 2 nodes to be connected
by a single edge.
The nodes are numbered layer by layer starting at 1 (first C1 has node 1, second layer C2 has nodes 2
and 3, ..etc.).
It is guaranteed that the input is a valid onion graph and that n = k 2 .
Output
For each test case, output the number of 4-colorings mod 132120577 in a line by itself.
Example
onion.in standard output
1 55296
9 3 5
2 3
4 5
5 6
6 4
7 8
Page 10 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Consider the following polynomial function f (S) used to hash a string Si of length k.
k 1 k 2
1 + ai,k 2 ⇤ Si,k 2 + ... + ai,2 ⇤ Si,2 + ai,1 ⇤ Si,1 (mod m)
k +a
fi (Si ) = ai,k ⇤ Si,k ⇤ Si,k 2
i,k 1
P j
In other words, fi (Si ) = kj=1 ai,j ⇤ Si,j (mod m)
Where ai,j are integer coefficients, m is the modulo, and Si,j represents the alphabet order of the j th
character in Si .
For example, if Si = “abz”, then Si,1 = 1, Si,2 = 2, Si,3 = 26 (i.e. ‘a’ = 1, ‘b’ = 2, .., ‘z’ = 26).
Given a list of n strings of equal length k, consisting of lowercase letters, your task is to find any list of
integer coefficients ai,j (1 i n, 1 j k) that guarantees no collisions occur.
In other words, you need to find any list of coefficients such that there does not exist a pair of strings Si
and Sj where i 6= j and fi (Si ) = fj (Sj ).
Notice that you do not have to use the same hashing function for all the strings. Instead, you can choose
a completely different function (list of coefficients) to hash each string.
Input
First line will be the number of test cases T , in each test case you will be given the following:
First line will be 3 integers n, k, m separated by spaces. (2 n, k, m 500)
Then n lines follow, each line has a string of length k consisting of lowercase english letters.
Output
For each test case:
If it is impossible to avoid collisions, output “No” in a single line.
Otherwise output “Yes” followed by n lines.
On the ith line, output k integers ai,1 ai,2 .... ai,k separated by spaces.
Example
hash.in standard output
1 Yes
5 5 11 4 0 0 0 0
abcde 3 8 0 0 0
fghij 0 2 0 0 0
klmno 10 2 0 0 0
pqrst 0 0 0 0 0
uvwxy
Page 11 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
You are given a circle C, a set of “obstacle” circles O1 , ..., On and two points S and E.
Count the minimum number of obstacles you will have to go through by taking a valid path from S to E.
A path is valid if all the points on the path lie inside C.
The following properties are guaranteed:
• Each obstacle will be intersecting with at most two other circles (can be C or any of the obstacles).
Input
First line will be the number of test cases T , in each test case you will be given the following:
First line contains four integers describing the coordinates of the points S and E.
( 109 Sx , Sy , Ex , Ey 109 )
Second line contains an integer n, the total number of circles (the big circle + (n-1) obstacles).
(1 n 104 )
Followed by n lines, each describing a circle (first C, followed by the n-1 obstacles).
Each line contains three integers r, x, and y, describing the radius of the circle and coordinates of the
center.
All integers on the same line are separated by spaces.
Output
For each test case output one line containing the number of obstacles you have to go through to pass from
S to E.
Example
circles.in standard output
1 1
0 2 0 -2
4
3 0 0
1 0 0
1 -2 0
1 2 0
Page 12 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Рис. 2: The valid path from S to E can pass through at least one obstacle.
Page 13 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
• p2 has [2].
• p3 has [2].
• A = [2, 1, 2, 2].
• B = [3, 2, 2].
Input
First line will be the number of test cases T , in each test case you will be given the following:
The first line contains 2 integers separated by a space N and M . (1 M N 15)
Followed by a line containing a list of N positive integers describing A. (1 Ai 2 ⇥ 109 )
Followed by a line containing a list of M positive integers describing B.
Integers that are in the same line are separated by spaces.
Output
For each test case, output N ⇤ M matrix (numbers in the row separated by spaces and each row in a line
by itself).
Each entry in the matrix is the answer for the pair (Ai , Bj ) for every 1 i N and 1 j M .
Example
judging.in standard output
1 0.3333 0.3333 0.3333
4 3 1.0000 0.0000 0.0000
2 1 2 2 0.3333 0.3333 0.3333
3 2 2 0.3333 0.3333 0.3333
Page 14 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
• The dragon starts at position 0 and starts running towards you immediately.
You have a sword. When the dragon reaches you, you can hit it with your sword, which injures it.
After getting hit, the dragon stops running and stays in its position for H seconds to heal, then continues
running after you.
Your sword can only hit the dragon X times before it breaks. If the dragon reaches you while your sword
is broken, it will eat you.
You survive if you reach position Z before the dragon eats you. Calculate the minimum number of sword
hits you need to survive or state that it is impossible.
Input
First line will be the number of test cases T , in each test case you will be given the following:
Five integers S1 , S2 , P, X, H, Z separated by spaces such that (1 S1 , S2 , P, H 109 ), (0 X 109 )
and (P Z 1018 )
Output
For each test case, if you can survive output the minimum number of sword hits you need to do in order
to survive, otherwise print “Impossible”.
Output one line for each test case.
Example
dragon.in standard output
3 Impossible
1 2 1 1 2 10 0
3 2 2 1 2 20 2
1 39 20 9 4 28
Page 15 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Two arrays are equal if the strings inside them have the same order according to the comparison definition
above.
In the pattern described above, “bbbb” is smaller than “bbab”, and P occurs twice in T , the first occurrence
at position 1 and second occurrence at position 2 (0-based).
Input
The first line will be the number of test cases T , in each test case you will be given the following:
The first line will have 2 integers, T and P separated by spaces (the lengths of the text and the pattern).
The second line will have T space-separated strings, denoting the array that represents the text.
The third line will have P space-separated strings, denoting the array that represents the pattern.
It is guaranteed that the total number of characters given will not exceed 107 .
It is guaranteed that all the strings have the same length and all characters are lower-case English letters.
Output
For each test case output 2 lines:
On the first line, the number of occurrences of the pattern in the text.
On the second line, the indices of the start of the pattern’s occurrences in the text separated by spaces
(0-based).
Example
pattern.in standard output
1 2
4 2 1 2
abcd aaaa abab aaba
bbbb bbab
Page 16 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Note
This note is to clarify the matching: if you have 4 strings in this order [second smallest, smallest, smallest,
largest] then you need to match any sub-array that matches that pattern, regardless of what the strings
actually are.
The comparison is done by the definition above.
Page 17 of 18
International Collegiate Programming Contest, Africa and Arab Collegiate Programming Championship 2020
Luxor, Egypt, March, 21st, 2021
Endure Capital is an early stage investment fund headed by entrepreneurs. Unlike many venture capital
firms, Endure prides itself on being founder-focused. It believes in the people behind the idea, and works
with them through challenges, hurdles and objectives.
Endure is making a research to find out which countries contain startups that make more money, so that
they invest in these countries.
You are given a list of country names, along with the profit each country makes in millions of dollars,
output the name of the country that makes most money.
Input
First line will be the number of test cases T, in each test case you will be given the following:
First line will be N , the number of countries. (1 N 100)
Followed by N lines, each line has a string C (a country name) followed by an integer P (the profit of
this country in millions of dollars), separated by a space. (1 P 1000)
The length of C will not exceed 10 characters.
It is guaranteed that all countries are unique, and that each country has a unique profit.
Output
For each test case, output the name of the country that makes most money.
Example
money.in standard output
1 Egypt
3
Sudan 300
Egypt 500
Tunis 400
Page 18 of 18