ICPC Preli
ICPC Preli
ICPC Preli
Problem A
Almost Perfect Prime Factor!
A perfect prime factor is a prime factor that exists in all the elements of an array. An almost
perfect factor is a prime factor that exists at least once in an element of the array, but not in all of
the elements of the array.
You are given an array A of N positive integers and an integer K. You have to find the longest
subarray of A that has exactly K almost prime factors and no perfect prime factor in that
subarray.
Input
The first line will contain a single integer T. Each test case starts with two integers N and K. The
following line will contain N space-separated integers A1, A2, ..., AN denoting the elements of
array A.
Output
For each case, print one line with “Case <x>: <y>”, where x is the case number, and y is the
length of the longest subarray of A that has exactly K almost prime factors and no perfect prime
factor.
Constraints
● 1 ≤ T ≤ 10
4
● 1 ≤ N ≤ 10
5
● 1≤K≤N
● 1 ≤ A ≤ 10
6
Page 1
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem B
Bob and Numbers
Bob loves number theory. Up until now, he has solved countless number theory problems on
several online platforms. One of those platforms is ForceCode. He especially likes to solve
problems on ForceCode due its tremendous collections of number theory challenges.
One day while surfing through ForceCode he stumbled upon a unique number theory challenge
which involves finding the modulo of some numbers by A and A - 1. As he dived into solving this
intriguing problem, an idea struck him. What if he could create a new problem inspired by this
mathematical concept? A problem that would not only test programming skills but also offer a
unique twist to mathematics.
After spending several hours, he came up with the following problem. Given two positive
integers A and N, you need to find:
𝑁
∑ 𝐾(𝑖) where 𝐾(𝑖) = |𝑖 % 𝐴 − 𝑖 % (𝐴 − 1)|
𝑖=1
Here, a % b is the remainder after dividing a by b. Moreover, |a| is the absolute value of a.
Even though Bob solved countless problems that involve mathematics and number theory, he
finds this problem a bit challenging for his level of experience. Hence, he needs your help.
Would you be able to help Bob solve this problem?
Input
Input will have multiple test cases. The first line of the input contains an integer, T (1 ≤ T ≤ 105),
representing the number of test cases. Each of the next following T lines will have two positive
integers A (2 ≤ A ≤ 105) and N (1 ≤ N ≤ 1012).
Output
For each case, print the case number followed by the answer. Please, see the sample for
details.
Page 2
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem C
Counting Ones II
Given a N x N binary matrix and a positive integer K, you are tasked with performing scaling
operations on this matrix by a factor K and counting '1's within some specified ranges.
The scaling of the binary matrix by factor K means replacing each element with a K x K matrix
filled with the same element. See the example below for better understanding.
Once the scaling is done, the resulting matrix is then merged into a binary string. More formally,
the final string will be 𝑆 = 𝑟1 + 𝑟2 + 𝑟3 + ... ... + 𝑟𝑁𝐾 where 𝑟𝑖 is the ith row of the scaled
matrix. The length of the final string is |S| = N2 K2.
Example of scaling:
For K = 2
Matrix Matrix after scaling
0 00
00
0011
01 0011
10 1100
1100
001100
001100
010 110011
101 110011
010 001100
001100
You will then be given some ranges on the merged binary string. You need to answer how many
1's are there within those ranges.
Input
Input will have multiple test cases. The first line of the input contains an integer, T (1 ≤ T ≤ 100),
representing the number of test cases.
Page 3
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Output
For each case, print the case number. Then print the answer to the queries associated with that
case in separate lines. Please, see the sample I/O for details.
Page 4
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem D
Sequence
You are given two strings P and Q having lower case english letters. A sequence {x1, x2, x3…xk}
is interesting if there is any sequence {y1, y2, y3, … yk} such that,
● for all 1 ≤ i ≤ k, P[1..xi] appears as a substring of Q ending at yi index. Note that, P[1..xi]
is a string formed by taking the first xi characters of P.
● yi < yi+1 for all 1 ≤ i ≤ k - 1. Note that this condition is only considered for k ≥ 2, for k = 1
first condition is sufficient.
How many distinct interesting non-empty sequences {x1, x2, x3…xk} can you make?
Input
First line will contain a single integer T representing the test cases. For each test case there will
be 2 lines. The first line will represent string P and second line will represent string Q.
Constraints
● 1 ≤ T ≤ 2 x 104
● 1 ≤ |P|, |Q| ≤ 3 x 105
● sum of |P| overall test cases ≤ 3 x 105
● sum of |Q| overall test cases ≤ 3 x 105
Output
For each test case print a single line representing the answer modulo 998244353.
Explanation
For the first test case distinct interesting sequences are {1}, {1, 1}, {2}, {1, 2}.
● For sequence {1}, end indices are, {1} or {2}.
● For sequence {1, 1} end indices are {1, 2}.
● for sequence {2}, end indices are {1}.
● for sequence {1, 2} end indices are {1, 2}.
Page 5
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem E
Sub-Array
You are given an array A consists of N integers. Now for the given array, you have to find the
number of non-empty subarrays which are not Beautiful.
Example:
A = [ 1, 0, 1, 0 ] where N = 4
Input
Input starts with an integer T denoting the number of test cases. Each case starts with an
integer N denoting the number of integers in the array. Next line will contain N integers of array
A, separated by spaces.
Output
For each case, print the case number and the result in a single line.
Constraints:
● 1 ≤ T ≤ 10
● 1 ≤ N ≤ 105
● 0 ≤ A[i] ≤ 231 -1
Page 6
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem F
Mesmerizing Fireflies
One night, while camping in the woods, I found a mesmerizing sight, several fireflies floating in
the air, without any movement. I was so mesmerized by the sight that I decided to put my tent
over the fireflies. Geometrically speaking, we can consider the space to be 2 dimensional, the x
axis being the ground and y axis being vertical direction. The ith firefly was at position (𝑥𝑖,𝑦𝑖)
where 𝑦𝑖 > 0. My tent is a triangle with two vertices on the x axis and a third vertex at a point
above the x axis. I wanted to put the tent so that each firefly was either strictly inside my tent or
on the border of my tent (i.e. the edge of the triangle). Note that the tent must be stable,
meaning that if the x coordinates of the base points are 𝑥𝐴 < 𝑥𝐵 and the x coordinate of the top
point is 𝑥𝐶, then 𝑥𝐴 ≤ 𝑥𝐶 ≤ 𝑥𝐵 must be ensured.
Now being the competitive programmer I am, I started to think how I could put my tent up so
that the area covered by the tent (i.e. the area covered by the triangle described) would be
minimal. Of course, that is your job (because I don't have time to solve the problem, I just want
to look at the fireflies).
Due to a very difficult technical reason that you won’t understand (and neither do I), just the
value of the minimal area of the triangle won’t be useful. Instead, you will have to also find a
rectangle with minimal area having two adjacent points on the x axis such that each firefly is
strictly inside the rectangle or on the border of the rectangle. If the minimal area of the triangle is
A and the minimal area of the rectangle is B then I want to know the value of A / (2 × B).
Input
First line of input will contain a single integer T denoting the number of independent test cases.
Then the descriptions of T test cases follow. Each test case will start with a line containing an
integer n denoting the number of fireflies. Then n lines follow, the ith one containing two integers
xi and yi denoting the position of the ith firefly.
● 1 ≤ T ≤ 20
● 2 ≤ n ≤ 50000
● -108 ≤ xi ≤ 108
● 0 < yi ≤ 108
● In a single test case, no two fireflies will be at the same position
● All fireflies will not have the same x coordinate in one test case
Page 7
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Output
For each test case, output the value A / (2 × B) in a separate line as described above. Print the
value rounded to exactly 7 digits after decimal (even if they are all 0’s). If the value is less
than 1 then make sure to print a 0 before the decimal. Do not print extra space or extra
leading 0’s.
In the first test case, the minimal area of the rectangle is 25 and the minimal area of the triangle
is 50.
In the second test case, the minimal area of the rectangle is 750 and the minimal area of the
triangle is 625.
Page 8
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem G
Bowling Figure
You are provided with a sequence of ball-by-ball information for a specific bowler during a
cricket match. Each ball can result in various outcomes:
You may safely assume that there are no wide balls, no-balls, leg byes or any other extra runs in
the provided information. Your task is to calculate the bowler's bowling figures based on the
given ball-by-ball information.
Input
The first line contains a single integer T (1 ≤ T ≤ 1000), the number of test cases.
For each test case, the second line contains a string S, 1≤ |S| ≤ 60 and each character in the
string represents a ball. The ball descriptions are either 'W' for a wicket or a digit '0' to '6'
representing the runs conceded in that ball.
Output
For each test case, output the bowler's bowling figures in the format: "X.Y over(s) Z run(s) P
wicket(s)". Here, X is the total number of over(s), Y is the total number of ball(s), Z is the total
run(s) conceded, and P is the total wicket(s) taken by the bowler. Use “s” in output due to plural
form if over, run or wicket is greater than 1. See the sample input output below for more clarity.
Page 9
ICPC Dhaka Regional Preliminary Contest, 2023 Hosted by: BUBT
Problem H
Filler Problem
Just like anime, contests also have filler. A problem low in quality, but necessary to increase the
total number of problems. Here is one.
You are given a sequence of N non-negative integers a1, a2, …, aN. For each prefix of this
sequence, determine if the elements can be partitioned into 3 sets with equal sum.
Input
The first line will contain a single integer T (1 ≤ T ≤ 5), number of test cases. First line of each
test case will have a single integer N (1 ≤ N ≤ 120), the size of the sequence. The second line
𝑁
has N space separated non-negative integers a1, a2, …, aN. For one test case, ∑ ai ≤ 15000
𝑖=1
holds.
Output
For each test case, print the case number in a single line followed by N integers in separate
lines. Print 1 if the prefix can be partitioned. Otherwise print 0.
Page 10