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

Prob TreeScript

Uploaded by

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

Prob TreeScript

Uploaded by

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

The 47th International Collegiate Programming Contest

Asia Hong Kong Regional Contest

January 14

Problems
A TreeScript
B Big Picture
C Painting Grid
D Shortest Path Query
E Goose, goose, DUCK?
.F Sum of Numbers
G Paddle Star
H Another Goose Goose Duck Problem
.I Range Closest Pair of Points
.J Dice Game
.K Maximum GCD
.L Permutation Compression

Do not open before the contest has started.

Prepared by Zhejiang University


The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem A. TreeScript
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

TreeScript is a programming language developed for maintaining tree structures. In this problem, we will
learn how to create rooted trees in TreeScript.
In TreeScript, all tree nodes are stored in memory. Each tree node has a number and the address of
its parent node, and both are immutable, so they have to be determined when creating the node. In
particular, the address of the root node’s parent node is empty.
In order to access these nodes, the address of a node can be stored in a register. If there are m registers,
the registers can be written as r[0], r[1], ..., r[m − 1].
Now let’s learn the node creation statement:

r[i] = create(r[j], k);

where k is the node number, i and j are the indices of the registers, where 0 ≤ i, j < m and i = j is
possible. The effect of this statement is that a node numbered k is created, whose parent address is stored
in r[j], and then the new node’s address is stored in r[i]. Once each node has been created correctly,
you do not need to store the address of any more nodes; they will automatically execute the pre-defined
instructions. For reasons of space, we will learn about them later.
To check your learning, you need to create a rooted tree of size n. At first, the system will automatically
create the root node for you and store it in r[0]. So you only need to execute n − 1 additional creation
instructions to create the tree.
As you know, registers are very expensive, so you need to find the minimum value of the registers you
need.

Input
There are multiple test cases.
The first line of the input contains one integer T (1 ≤ T ≤ 105 ) - the number of test cases.
For each test case,
The first line contains one integer n (2 ≤ n ≤ 2 × 105 ) - the size of the tree.
The second line contains n integers p1 , p2 , · · · , pn , where node pi is the parent node of node i and 1 ≤ pi < i.
Specially, p1 = 0 and it means 1 is the root of the tree.
The sum of n over all test cases does not exceed 2 × 105 .

Output
For each test case, output the answer in one line.

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

Page 1 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem B. Big Picture


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Grammy has a big picture with n + 1 rows and m + 1 columns. Rows are numbered from 1 to n + 1 and
columns are numbered from 1 to m + 1.
Grammy decides to color this picture in a special way. For the i-th row, Grammy will color the leftmost
j (1 ≤ j ≤ m) cells black with probability pi,j . For the j-th column, Grammy will color the topmost i
(1 ≤ i ≤ n) cells black with probability qi,j . Operations are independent, and a cell could be colored more
than once.
Let us define the beauty value as the number of maximal orthogonally connected regions of the same
color. Before Grammy finishes her coloring, she wants to know the expected number of regions on the
picture. Please calculate the expected beauty value of the picture for her.
Two cells x and y are in the same orthogonally connected region if and only if they satisfy the following
constraints:

• They have the same color.

• x shares an edge with y or x shares an edge with some cell z while y and z are in the same
orthogonally connected region.

Input
The first line contains two integers n, m (1 ≤ n, m ≤ 1000), denoting the size of the picture.
Each of the next n lines contains m integers pi,j , denoting the probability of painting the leftmost j cells
of the i-th row black, modulo 998 244 353. It is guaranteed that the sum of the probability of each row is
1.
Each of the next n lines contains m integers qi,j , denoting the probability of painting the topmost i cells
of j-th column black, modulo 998 244 353. It is guaranteed that the sum of the probability of each column
is 1.

Output
Output a single integer, denoting the expected beauty value of the picture, modulo 998 244 353.
It can be shown that the answer can be expressed as an irreducible fraction xy , where x and y are integers
and y 6≡ 0 (mod 998 244 353). Output the integer equal to x · y −1 (mod 998 244 353). In other words,
output such an integer a that 0 ≤ a < 998 244 353 and a · y ≡ x (mod 998 244 353).

Page 2 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Examples
standard input standard output
3 3 3
0 0 1
1 0 0
0 0 1
0 1 0
0 0 0
1 0 1
2 2 2
499122177 499122177
499122177 499122177
499122177 499122177
499122177 499122177
3 3 308100111
332748118 332748118 332748118
332748118 332748118 332748118
332748118 332748118 332748118
332748118 332748118 332748118
332748118 332748118 332748118
332748118 332748118 332748118

Note
There is only one possible picture in the first example, which is shown as follows. There are 3 maximal
orthogonally connected regions in the picture, so the beauty value of the picture is 3.

Page 3 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem C. Painting Grid


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Grammy has an n × m wall covered by squares. Each small square on the wall is of unit size and should
be painted into one color completely. She wants to color the wall into black and white. Grammy likes the
concept of diversity, so she decided to make each row look different from all previous rows and also make
each column look different from all previous columns. As she was about to paint, she found her paint was
just enough - half of white paint and half of black paint, both with an amount to paint exactly nm 2 unit
area. Please help Grammy to satisfy her diversity condition using limited paint.

Input
The input contains multiple test cases.
The first line contains a single integer T (1 ≤ T ≤ 2000), denoting the number of test cases.
For each test case:
The only line contains two integers n, m (1 ≤ n, m ≤ 1000). It is guaranteed that the sum of nm does not
exceed 106 .

Output
For each test case, if no solution exists, output “NO”. Otherwise, output “YES” followed by n lines. Each
line should contain m characters. 0 denotes a white square and 1 denotes a black square in the solution.

Example
standard input standard output
5 NO
1 1 YES
2 2 10
2 4 01
4 4 YES
5 10 1100
0110
YES
1100
0110
0000
1111
YES
1111100000
0101010101
0011011001
0000111110
1111000001

Page 4 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem D. Shortest Path Query


Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 1024 megabytes

You will be given a directed acyclic graph with n vertices, labeled by 1, 2, . . . , n. There are m edges in
the graph, each edge is either black or white. It is guaranteed that you can reach every vertex from the
1-st vertex.
You will be given q queries. In the i-th query, you will be given three integers ai , bi and xi . You need to
report the length of the shortest path from the 1-st vertex to the xi -th vertex if we regard the length of
each black edge as ai and regard the length of each white edge as bi .

Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 50 000, 1 ≤ m ≤ 100 000), denoting
the number of vertices and the number of directed edges.
In the next m lines, the i-th line contains three integers ui , vi and ci (1 ≤ ui < vi ≤ n, vi − ui ≤ 1 000,
0 ≤ ci ≤ 1), describing a directed edge from the ui -th vertex to the vi -th vertex. When ci = 0, its color is
black, and when ci = 1, its color is white.
The next line contains a single integer q (1 ≤ q ≤ 50 000), denoting the number of queries.
Each of the next q lines contains three integers ai , bi and xi (1 ≤ ai , bi ≤ 10 000, 1 ≤ xi ≤ n), denoting a
query.
It is guaranteed that you can reach every vertex from the 1-st vertex.

Output
For each query, print a single line containing an integer, denoting the length of the shortest path.

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

Page 5 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem E. Goose, goose, DUCK?


Input file: standard input
Output file: standard output
Time limit: 4 seconds
Memory limit: 1024 megabytes

In the game ‘Goose Goose Duck’, the geese’s target is to complete the tasks and stay alive. However the
saboteurs, ducks, will try to stop the geese from completing the tasks by killing them.
Now consider a game consisting of n geese and k ducks. The geese are numbered from 1 to n, and the i-th
goose can complete the task numbered ai . The geese decided to dispatch an interval of geese to complete
the task, which means they will choose two integers l, r, satisfying 1 ≤ l ≤ r ≤ n, and all geese numbered
i which satisfy that l ≤ i ≤ r will go to complete their task. Such a decision is called a plan, two plans
are considred different if and only if the interval is different.
The ducks, will crouch at a task location, and kill all the geese trying to complete this task. They can
not choose a task location which more than k geese will come, because they can’t kill them all and there
will be witness, they also can not choose a task location which less than k geese will come, becuase they
will kill their teammates by mistake. In other words, they can only choose a task location where exactly
k geese will come.
A plan is said to be dangerous if and only if there exist a task location that the ducks can ambush. Please
help the geese to count how many plans are not dangerous for the geese. Please notice that the geese
do not have to complete all the tasks with the plan.

Input
The first line contains two integers n, k (1 ≤ n, k ≤ 106 ).
The second line contains n integers, the i-th integer ai (1 ≤ ai ≤ 106 ) denotes the task number of the
goose numbered i.

Output
Output one line containing one integer, denoting the answer.

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

Page 6 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem F. Sum of Numbers


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Given n digits from ‘1’ to ‘9’, you can add k ‘+’ to turn it into an expression, find the minimum value of
the expression.

Input
There are multiple test cases.
The first line of the input contains one integer T (1 ≤ T ≤ 2 × 104 ) - the number of test cases.
For each test case,
The first line contains two integers n (2 ≤ n ≤ 2 × 105 ) and k (1 ≤ k ≤ 6, k < n) - the number of digits
and the number of ‘+’.
The second line contains a string of length n, which consists of digits from ‘1’ to ‘9’.
The sum of n over all test cases does not exceed 2 × 105 .

Output
For each test case, output the answer in one line.

Example
standard input standard output
2 9696
8 1 6
45455151
2 1
42

Page 7 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem G. Paddle Star


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Putata is fascinated with playing Zoe these days. Putata is practicing Zoe’s ability, paddle star at this
moment.
This ability allows Zoe to fire a star that she can redirect in flight. The damage dealt by the star is
positively correlated to its flying distance. Formally, assume Zoe is currently at point X and she fires in
any direction. At any moment, Zoe can change the direction of the star. If the star is at point Y , and the
new direction leads to some point Z, then the track of the star will be X → Y → Z. If the star meets
any enemy on its trace, it will immediately disappear and cause damage to the enemy.
Since Putata is not skilled enough to control this ability, the stars he fires always fit the following
restrictions. The first segment XY always has length l1 , and the second segment Y Z always has length
l2 . Assume Zoe is facing direction XP
~ (which is a certain direction), and let hXP ~ i be θ degrees,
~ , XY
hXY , Y Zi be φ degrees, then θ ∈ [−α, α] and φ ∈ [−β, β]. You can refer to the following picture to help
~ ~
you understand.

Putata’s rival, Budada, wants to know the total area that could possibly be attacked by Putata’s Zoe.
However, Budada is busy with math problems, so he wants you to help him solve this problem.

Input
The first line contains one integer T (1 ≤ T ≤ 105 ), the number of test cases.
For each test case output one line contains four integers l1 , l2 , α and β (1 ≤ l2 ≤ l1 ≤ 109 , 0 ≤ α ≤ 90,
0 ≤ β < 180), indicating the attributes of the ability.

Output
Print one line for each test case, each line contains one real number, indicating the answer.
Your answer will be considered correct if its absolute or relative error does not exceed 10−6 . Formally, let
|a−b|
your answer be a, and the jury’s answer be b. Your answer will be considered correct if max(1,|b|) ≤ 10−6 .

Page 8 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Example
standard input standard output
5 3.490658503988659
2 1 20 20 0.000000000000000
3 3 0 0 3367.157611906510738
20 20 90 120 1098.863278984081717
20 10 50 170 373.960489570087645
100 10 1 93

Page 9 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem H. Another Goose Goose Duck Problem


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Teacher Rice likes playing the famous game ‘Goose Goose Duck’. In the game, Teacher Rice plays a duck
and his goal is to kill the geese. Every time he kills a goose, he should wait a seconds for his killing skill
to cool down. Since Teacher Rice’s role is the Serial Killer, the time Teacher Rice waits depends on which
type of goose he kills. Because Teacher Rice is a skilled killer, he can make the waiting time a to be an
arbitrary integer in [l, r].
Teacher Rice meets a goose every b seconds. Once Teacher Rice meets a goose, he can choose to kill the
goose if his killing skill is ready, otherwise the goose runs away immediately and he can not kill this goose.
Teacher Rice wants to know the minimum time he needs to kill k geese.

Input
There are four integers in one line, l, r, b, k (1 ≤ l ≤ r ≤ 109 , 1 ≤ b, k ≤ 109 ).

Output
Output one integer denotes the time Teacher Rice needs to kill k geese.

Examples
standard input standard output
6 6 3 3 18
2 3 5 4 20

Page 10 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem I. Range Closest Pair of Points Query


Input file: standard input
Output file: standard output
Time limit: 7 seconds
Memory limit: 1024 megabytes

The closest pair of points problem is a well-known problem of computational geometry. In this problem,
there are n points p1 , p2 , . . . , pn in the Euclidean plane. You will be given q queries. In the i-th query,
you will be given two integers li and ri (1 ≤ li < p ri ≤ n). You need to find a pair of points (u, v) such
that li ≤ u < v ≤ ri and the Euclidean distance (xu − xv )2 + (yu − yv )2 between point pu and pv is
minimized.

Input
The first line of the input contains two integers n and q (2 ≤ n ≤ 250 000, 1 ≤ q ≤ 250 000), denoting the
number of points and the number of queries.
In the next n lines, the i-th line contains two integers xi and yi (1 ≤ xi , yi ≤ 108 ), describing the coordinate
of pi .
Each of the next q lines contains two integers li and ri (1 ≤ li < ri ≤ n), denoting a query.

Output
For each query, print a single line containing an integer, denoting the value of (xu − xv )2 + (yu − yv )2 .

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

Page 11 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem J. Dice Game


Input file: standard input
Output file: standard output
Time limit: 4 seconds
Memory limit: 1024 megabytes

Putata and Budada are playing an interesting game. They play this game with a dice having n faces.
Every integer between 0 and n − 1 are written on exactly one face, and when they roll this dice, each
side will face up with equal probability. In other words, rolling the dice will result in a uniform random
integer between 0 and n − 1 with equal probability.
The game has two rounds. In the first round, the following happens:

• Putata will roll the dice and get an integer as the result, say x.

In the second round, Budada can choose to do one of the following things:

• End the game, and the score of the game will be x.

• Roll the dice again, let the result be y, and the game will end, the score of the game will be x ⊕ y.
Here ⊕ denotes binary exclusive-or operation.

Putata and Budada wants to maximize the score of the game, and they are clever so that they will always
make the best choice. Please write a program to calculate for some given n, the expectation of the score
of the game.
It can be shown that the answer can be expressed as an irreducible fraction xy , where x and y are integers
and y 6≡ 0 (mod 998 244 353). Output the integer equal to x · y −1 (mod 998 244 353). In other words,
output such an integer a that 0 ≤ a < 998 244 353 and a · y ≡ x (mod 998 244 353).

Input
The input contains several test cases. The first line contains an integer T (1 ≤ T ≤ 104 ).
For the following T lines, each line contains an integer n (1 ≤ n ≤ 998 244 352), denoting one question.

Output
Output T lines, each line denotes the answer for one test case.

Example
standard input standard output
4 0
1 249561089
2 776412276
3 2
4

Page 12 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem K. Maximum GCD


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Grammy has a array of length n. She recently learned about the concept of greatest common divisor(GCD).
Recall that the GCD of an array is the maximum integer d such that every element in the array is divisible
by d. Grammy thinks that the GCD of an array should be as large as possible so that the array can be
beautiful.
You want to help Grammy to make her array beautiful, so you decided to do some(possibly, zero) modulo
operations on each of the elements in the array. In other words, you can choose a number ai (1 ≤ i ≤ n) in
the array and choose another integer x and replace ai with (ai mod x) in each operation. Since Grammy
does not want 0 to appear in her array, you cannot change ai into 0 by doing the modulo operation.
Now, your task is to calculate the maximum GCD of the array after several(possibly, zero) modulo
operations.

Input
The first line contains a single integer n (1 ≤ n ≤ 105 ), denoting the number of elements in the array.
The second line contains n positive integers ai (1 ≤ ai ≤ 109 ), denoting the initial elements of Grammy’s
array.

Output
Output a single integer, denoting the maximum GCD of the array after any number of modulo operations.

Example
standard input standard output
3 3
3 10 7

Page 13 of 14
The 2023 ICPC Asia Hong Kong Regional Programming Contest
The Chinese University of Hong Kong, January, 14, 2023

Problem L. Permutation Compression


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 1024 megabytes

Grammy has a permutation of length n. She wants to delete some useless elements in the permutation, so
she decided to use some magic tool to delete them. There are k magic tools, the i-th of them can delete
the maximum element of an interval of length exactly li . Each magic tool can be used at most once.
Before using the tool, Grammy shows you her blueprint of the array after deletion. The new array consists
of exactly m distinct elements from 1 to n. Please help Grammy to determine whether it is possible to
delete the elements by using the magic tool.

Input
There are multiple test cases. The first line contains an integer T (1 ≤ T ≤ 105 ), denoting the number of
test cases.
For each testcase:
The first line contains 3 integers n, m, k (1 ≤ m ≤ n ≤ 2 × 105 , 1 ≤ k ≤ 2 × 105 ), denoting the length of
the permutation, the length of the compressed array, and the parameter of the magic tool.
The second line contains n distinct integers ai (1 ≤ ai ≤ n), denoting the initial permutation. It is
guaranteed that the elements are distinct.
The third line contains m distinct integers bi (1 ≤ bi ≤ n), denoting the array after compression. It is
guaranteed that the elements are distinct.
The fourth line contains k integers li (1 ≤ li ≤ n), denoting the magic tools.
It is guaranteed that n ≤ 2 × 105 and k ≤ 2 × 105 .
P P

Output
For each testcase, output “YES” or “NO” in a separate line, denoting the answer to the problem.

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

Page 14 of 14

You might also like