0% found this document useful (0 votes)
750 views10 pages

Problemset - ICPC Dhaka Preliminary 2021

Here are the steps to solve this problem: 1. Generate all substrings of X and store them in a set S1. 2. Generate all substrings of Y and store them in a set S2. 3. Iterate through S1 and S2 and check if the concatenation of any two substrings forms a palindrome. Add such pairs to the answer set. 4. Return the size of the answer set which is the number of ways to form a pairdrome. The time complexity of this algorithm is O(|X|*|Y|) where |X| and |Y| are the lengths of the two strings. The space complexity is O(|X|

Uploaded by

Someone00
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)
750 views10 pages

Problemset - ICPC Dhaka Preliminary 2021

Here are the steps to solve this problem: 1. Generate all substrings of X and store them in a set S1. 2. Generate all substrings of Y and store them in a set S2. 3. Iterate through S1 and S2 and check if the concatenation of any two substrings forms a palindrome. Add such pairs to the answer set. 4. Return the size of the answer set which is the number of ways to form a pairdrome. The time complexity of this algorithm is O(|X|*|Y|) where |X| and |Y| are the lengths of the two strings. The space complexity is O(|X|

Uploaded by

Someone00
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/ 10

ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem A
A Omicron Juice

I have three kids- Alpha, Beta and Gamma. Every morning I need to prepare them for school, serve them
breakfast and say them bye. You might think that I don’t like all these. But to be honest I do like it, specially
I have fun serving them breakfast. Every morning I serve them Omicron Juice. But I can’t always pour the
juice in equal amounts. If they don’t get equal amounts of juice then they start fighting. So I try my best to
make them equal. To be more specific…

There are three glasses with juice of amount A, B and C units. I have another small empty glass which can
hold 1 unit of juice. Using this small glass I can take 1 unit of juice from one glass and pour it to another
glass. Can you figure out if they will go to the school peacefully or are they going to start a fight? I promise
if there is any possibility for a peaceful morning I will definitely make that happen! Just to clarify, the
morning would be peaceful if all those three glasses contain equal amounts of juice, the extra 1 unit glass is
empty and during the entire process you do not spill any juice. Also assume that the kids glass can hold
any amount of juice.

Input
The input begins with the number of cases T (1 ≤ T ≤ 10,000). In each of the following T lines you will find
three non negative integers: A, B, C (each of at most 20).

Output
For each case, output the case number and print if the morning is “Peaceful” or if they are going to have
“Fight”. Please see the sample for details.

Sample Input Output for Sample Input


2 Case 1: Peaceful
3 5 1 Case 2: Fight
3 4 1

1
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem B
B Omicron Juice Again

I have three kids- Alpha, Beta and Gamma. Every morning I need to prepare them for school, serve them
breakfast and say them bye. You might think that I don’t like all these. But to be honest I do like it, specially
I have fun serving them breakfast. Every morning I serve them Omicron Juice. But I can’t always pour the
juice in equal amounts. If they don’t get equal amounts of juice then they start fighting. So I try my best to
make them equal. To be more specific…

There are three glasses with juice of amount A, B and C units. I have another empty glass which can hold
exactly K units of juice (after all I am Kappa!). In a move using the extra glass, I can transfer K units of juice
from one of the kids glasses to another kids glass as long as the source glass has at least K units of juice.
If I tell you A, B, C and K, can you figure out if they will go to the school peacefully or are they going to start
a fight? I promise if there is any possibility for a peaceful morning I will definitely make that happen! Just to
clarify, the morning would be peaceful if all those three glasses contain equal amounts of juice, the extra K
unit glass is empty and during the entire process you do not spill any juice. Also assume that the kids glass
can hold any amount of juice.

Input
The input begins with the number of cases T (1 ≤ T ≤ 200,000). In each of the following T lines you will find
four non negative integers: A, B, C, K (each of at most 20). K is a positive integer.

Output
For each case, output the case number and print if the morning is “Peaceful” or if they are going to have
“Fight”. Please see the sample for details.

Sample Input Output for Sample Input


2 Case 1: Fight
3 5 2 2 Case 2: Peaceful
3 9 6 3

Note: You might have to use faster I/O.

2
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem C
C Bitonic Beaver

A permutation, 𝑃[1 ··· 𝑛] of length n is a sequence of integers where each integer from 1 to n appears
exactly once.
A permutation is called Bitonic if it is first increasing then decreasing. Formally, A permutation 𝑃[1 ··· 𝑛] is
bitonic if there exists 1 ≤ 𝑖 ≤ 𝑛 such that, 𝑃[1] < 𝑃[2] < ··· 𝑃[𝑖] and 𝑃[𝑖] > 𝑃[𝑖 + 1] > ··· 𝑃[𝑛].
For example, [1, 2, 4, 3], [3, 4, 2, 1] and [1, 2, 3, 4] are bitonic sequences. In particular, increasing or
decreasing sequences are bitonic as well.
A permutation P is said to be compatible with another permutation Q if
𝑃[𝑄[1]], 𝑃[𝑄[2]], 𝑃[𝑄[3]], ··· 𝑃[𝑄[𝑛]] is bitonic.

For example, Let P = [2, 1, 4, 3] and Q = [1, 4, 3, 2]. Then,


𝑃[𝑄[1]], 𝑃[𝑄[2]], 𝑃[𝑄[3]], ··· 𝑃[𝑄[𝑛]]
= 𝑃[1], 𝑃[4], 𝑃[3], 𝑃[2]
= 2, 3, 4, 1
which is bitonic, so, P is compatible with Q.

Mrs Beaver has k permutations, 𝑄1, 𝑄2, 𝑄3, ···, 𝑄𝑘 each of length n. She wants to find the number of
permutations P (of length n) such that P is compatible with all 𝑄𝑖 (1 ≤ 𝑖 ≤ 𝑘). Since this number can be
large, she is happy to find its value modulo 998244353.

Input 5
The first line will contain a single integer 𝑇 (1 ≤ 𝑇 ≤ 10 ).
6
Each test-case starts with a single line containing n and k (1 ≤ 𝑛, 𝑘 ≤ 10 ). Each of the next k lines
contain n integers each representing a permutation Qi. Each integer from 1 to n appears exactly once in
each line.
6
The sum of 𝑛𝑘 over all test cases does not exceed 10 .

Output
For each case, output a single integer, the number of valid permutations modulo 998244353.

Sample Input Output for Sample Input


2 8
4 2 0
2 1 4 3
3 4 1 2
5 3
2 1 4 5 3
4 1 5 3 2
3 1 4 2 5

3
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem D
D Lazy Squirrel

There is a rooted tree of N nodes numbered from 1 to N and 1 is the root of the tree. Each node contains a
card on which there is an English letter written in Capital. There is a squirrel who wants to stay in this tree
tonight.

The squirrel believes staying in a node X (1 ≤ X ≤ N) makes him happy, if the letters written in the cards on
nodes in the shortest path from root to X forms a palindromic sequence. Now the squirrel wants to know
what is the probability of being happy, if he randomly chooses a node for staying tonight. Since the squirrel
is lazy, he wants your help.

Input
The first line will contain a single integer T (1 ≤ T ≤ 100). Each test case starts with an integer N (1 ≤ N ≤
106), denoting the number of nodes of the Tree. The following line will contain a string S, where i’th
character of S represents the letter written on the card of i’th node (|S| = N ,‘A’ ≤ Si ≤ ‘Z’). Next N - 1 lines
will contain two integers U and V (1 ≤ U, V ≤ N, U ≠ V) indicating there is an edge between U and V.
Summation of N over all test cases ≤ 106.

Output
For each case, print the case number and the result in P/Q format. Where P is the numerator and Q is the
denominator. P and Q should be relatively prime. See the samples for details.

Sample Input Output for Sample Input


2 Case 1: 3/5
5 Case 2: 1/1
ABAAC
1 2
1 3
2 4
2 5
1
A

Explanation:
For the first case, there are three possible nodes where the squirrel will be happy to stay tonight.
1. X = 1 (A , 1)
2. X = 3 (AA , 1->3)
3. X = 4 (ABA , 1->2->4)

4
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem E
E Pairdrome

A palindrome is a string if it reads the same backward as forward, for example, "a", "aba", and
"axddxa" are palindromes but "ab", "icpc", and "dhaka" are not. And a substring of a string is a
contiguous subsequence of that string, for example, "i", "c", "cp", and "icpc" are some of the
substrings of "icpc".
In this problem, you are given two strings 𝑋 and 𝑌 consisting of lowercase English letters. You need to
calculate the number of ways you can form a pairdrome from those strings. A pairdrome is a palindrome
and it is a concatenation of two substrings, one from 𝑋 and another from 𝑌.
More formally, suppose you have two strings 𝑋 and 𝑌. A string 𝑇 is a pairdrome if
1. 𝑇 is a palindrome.
2. 𝑇 = 𝑋[𝑎, 𝑏] + 𝑌[𝑐, 𝑑] , where 1 ≤ 𝑎 ≤ 𝑏 ≤ |𝑋| and 1 ≤ 𝑐 ≤ 𝑑 ≤ |𝑌| and 𝑆[𝑖, 𝑗] is a substring
of S starting from the index 𝑖 and ending at index 𝑗 and 1 ≤ 𝑖 ≤ 𝑗 ≤ |𝑆|. (We are using 1-based
indexing, here |𝑆| denotes the length of the string 𝑆 and the plus (+) sign denotes string
concatenation.)

For example, suppose 𝑋 is "kan" and 𝑌 is "zan". Here we have 5 pairdromes, they are
1. 𝑋[2, 2] + 𝑌[1, 2] = "𝑎" + "𝑧𝑎" = "𝑎𝑧𝑎"
2. 𝑋[2, 2] + 𝑌[2, 2] = "𝑎" + "𝑎" = "𝑎𝑎"
3. 𝑋[2, 3] + 𝑌[2, 2] = "𝑎𝑛" + "𝑎" = "𝑎𝑛𝑎"
4. 𝑋[3, 3] + 𝑌[2, 3] = "𝑛" + "𝑎𝑛" = "𝑛𝑎𝑛"
5. 𝑋[3, 3] + 𝑌[3, 3] = "𝑛" + "𝑛" = "𝑛𝑛"

9
Since the answer can be quite large, you need to print the answer modulo 10 + 7.

Input
The first line will contain a single integer T (1 ≤ T ≤ 100). Each test case will contain two strings X and Y
separated by a space in one line. These strings consist of lowercase English letters. The length of each
5
string will be less than or equal to 10 . And in a test file, the summation of all the string lengths will not
5
exceed 2 × 10 .

Output
Print the case number followed by the answer to that test case in this format “Case X: Y” (without quotes),
where X is the case number and Y is the number of ways for forming a pairdrome modulo 109 + 7. Please
see the sample for details.

Sample Input Output for Sample Input


4 Case 1: 5
kan zan Case 2: 20
dhaka taka Case 3: 2
meow max Case 4: 0
x y

5
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem F
F A Lonely Queen

We have a chessboard with n rows and m columns. The queen is standing at the Sy-th column of the Sx-th
row, i.e. (Sx, Sy). She wants to move to the cell (Dx, Dy). The queen can move in 8 directions at any time.
The cost of moving in the i-th direction is Ci. Formally,
from the cell (Ax, Ay), she can perform the following
moves:

1. Move to the cell (Ax - k, A y - k) with C1 cost.


2. Move to the cell (Ax - k, A y ) with C2 cost.
3. Move to the cell (Ax - k, A y + k) with C3 cost.
4. Move to the cell (Ax, A y - k) with C4 cost.
5. Move to the cell (Ax, A y + k) with C5 cost.
6. Move to the cell (Ax + k, A y - k) with C6 cost.
7. Move to the cell (Ax + k, A y ) with C7 cost.
8. Move to the cell (Ax + k, A y + k) with C8 cost.

Here, k can be any positive integer provided that the queen does not go out of the chessboard.

There are q cells, each of which has an obstacle. The queen cannot go to a cell containing an obstacle or
go over an obstacle. There are also p one-way magic tunnels. A tunnel can be defined by two cells (Ux, Uy),
(Vx, Vy) and an integer W. This means that if the queen is at cell U currently, she can move to cell V using
this tunnel with cost W. A cell can have any number of tunnels originating from or ending at it. Regardless
of the positions of the endpoints of the tunnels and the obstacles, the queen can always use a tunnel to
move from the starting cell U of the tunnel to the cell V at the end of the tunnel. The endpoints of the
tunnels, cell S and cell D do not have any obstacles.

You need to find out the minimum cost for the queen to move from cell S to cell D.

Input
The first line will contain a single integer T denoting the number of test cases. Each test case will have four
integers n, m, p, and q in the first line. The next line will contain four integers Sx, Sy, Dx, and Dy, denoting the
source cell S and the destination cell D. The next line will contain 8 integers where the i-th integer denotes
the value of Ci. Each of the next p lines will contain 5 integers Ux, Uy, Vx, Vy, and W, denoting the magic
tunnels. The following q lines will contain 2 integers Bx and By denoting a cell with an obstacle.

6
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Constraints
● 1 ≤ T ≤ 100
● 1 ≤ n,m ≤ 5x104
● 0 ≤ q ≤ (n x m)
● 0 ≤ Σ 𝑝 over all test cases ≤ 5x104
● 1 ≤ row of any cell ≤ n
● 1 ≤ column of any cell ≤ m
● 1 ≤ W ≤ 109
● 1 ≤ Ci ≤ 109
● 1 ≤ Σ(𝑛 × 𝑚) over all test cases ≤ 105

Output
For each case, print the case number and the answer in a single line. If it is not possible for the queen to
reach the destination cell from the starting cell, the answer should be "-1". Please see the sample for
details.

Sample Input Output for Sample Input


3 Case 1: 2
1 4 4 0 Case 2: -1
1 1 1 2 Case 3: 3
4 6 8 1 5 1 9 1
1 1 1 3 1
1 2 1 1 5
1 2 1 2 4
1 2 1 1 6
4 5 0 5
4 5 3 1
40 1 97 70 47 1 25 80
2 1
3 3
3 4
3 5
4 4
4 5 2 5
4 5 3 1
40 1 97 70 47 1 25 80
4 5 3 2 1
2 3 2 5 27
2 1
3 3
3 4
3 5
4 4

7
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem G
G Alice, Bob and Tree

Alice and Bob met together after a long time. As a token of appreciation, Bob presented Alice a colorful gift
box. After opening the box, she was amused by a beautiful tree with the following specification.
● A tree has N vertices and N-1 edges. All the vertices of the tree are connected.
● Each of the vertices has an associated weight with it, the weight of the ith vertex is 𝑊𝑖.
● The distance between any two adjacent vertices is 1

Now, Bob asked Alice to choose the tree's root vertex in such a way that the value of the following formula
is maximized.

𝑁
𝐷𝑖𝑠𝑡𝑎𝑛𝑐𝑒(𝑟𝑜𝑜𝑡, 𝑖)
∑ ( (𝑊𝑟𝑜𝑜𝑡 − 𝑊𝑖) × (− 1) )
𝑖=1

If there are several such vertices for which the value of the above formula is maximized, then Alice has to
choose the root with the smallest label. If Alice can answer the question successfully, Bob promised to gift
her a colorful balloon. Since Alice is not good at solving problems, she asked for your help. Can you solve
this problem for her?

Input
Input starts with an integer T denoting the number of test cases. Each of the T test cases will start with an
integer N denoting the number of vertices in a single line. Next line will contain N space-separated integers
𝑊1, 𝑊2, …, 𝑊𝑁 denoting the weight of each vertex. Then there will be N-1 lines of input containing two
space-separated integers U and V indicating there is an edge between them.

Output
For each case, print one line with “Case <x>: <y>”, where x is the case number and y is the vertex chosen
as the root of the tree.

Constraints
● 1 ≤ T ≤ 20
● 1 ≤ N ≤ 30,000
● 1 ≤ Wi ≤ 10,000
● 1 ≤ U, V ≤ N & U ≠ V

Sample Input Output for Sample Input


1 Case 1: 3
3
4 5 6
1 2
3 2

8
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem H
H Update Query Problem
Given two strings P and Q. You can do M number of operations of three types of following formats: each of
which is update or query. They will follow the format described below:
● Update: 1 L1 R1 C1
You will assign character C1 from L1 index(1-based) to R1 index of string P.
● Update: 2 L2 R2 C2
You will assign character C2 from L2 index(1-based) to R2 index of string Q.
● Query: 3 L1 R1 L2 R2
Take any prefix X (possibly empty or the entire substring) from P[L1…..R1] and any suffix Y (possibly
empty…) from Q[L2…..R2] then concat X and Y. Count the number of distinct strings X + Y (X + Y
can be empty if an empty prefix and empty suffix is taken) you can get.

Input
First line will contain a single integer representing the number of test cases T. For each test case the first
line will contain string P and the second line will contain string Q. Third line will contain a single integer M
which represents the number of operations. Then each of the M lines will contain the above update or
query.

Constraints
● 1 ≤ Number of test case ≤ 100
● 1 ≤ Length(P), Length(Q) ≤ 2 x 105
● 1 ≤ M ≤ 2 x 105
● 1 ≤ L1 ≤ R1 ≤ Length(P)
● 1 ≤ L2 ≤ R2 ≤ Length(Q)
● Sum of length(P) over all test cases ≤ 2 x 105
● Sum of length(Q) over all test cases ≤ 2 x 105
● Sum of M over all test cases ≤ 2 x 105
● P and Q will contain lower case english letters only.

Output
For each query you need to print a single integer representing the answer.
Notes
Please use fast input output for this problem.

Sample Input Output for Sample Input


1 9
abc
de
2
1 1 2 f
3 2 3 1 2

9
ICPC Dhaka Regional Preliminary Contest, 2021 Hosted by: BUBT

Problem I
I Hovercraft

Bangladesh is arguably still a land of rivers but hovercrafts are not frequently seen. In this problem you are
requested to find the minimum distance to destination using a hovercraft with certain limitations. The
limitation is that it must cover D distance on land so that the engine can warm up and then it can cover at
most D distance in the water. The bridge should also be considered as land.

You will have to cross a river of width L starting from the start (As shown in the image) and then reach a
destination that is W distance away from the other side of the bridge. From the starting location you can
cross the river via the bridge and then go to the destination and avoid water completely. In this case your
total crossed distance will be L+W. Alternatively after crossing the river partially by the bridge you can go
through the water and again through land to reach your destination. In this way the total distance covered
will be less than L+W. Given the value of L and W (Here always W>L), your job is to find the minimum
distance that needs to be covered to reach the destination from the starting point.

Input
The input file contains at most 1000 lines of input. Each line contains two positive integers that denote the
value of L and W (0 < L < W < 1000). Input is terminated by a line containing two zeroes which should not
be processed.

Output
For each line of input, produce one line of output. This line should contain a floating-point number that
denotes the minimum possible distance. This number should have four digits after the decimal point. You
can assume that input will be such that small precision errors will not cause any difference in the printed
output.

Sample Input Output for Sample Input


11 23 31.2500
7 13 18.2500
0 0

10

You might also like