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

A - Circular Distance: Problem Statement

Uploaded by

niluhntr
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)
89 views

A - Circular Distance: Problem Statement

Uploaded by

niluhntr
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/ 15

A - Circular Distance

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 900 points

Problem Statement
There is a circle with circumference L, and L people are standing equally spaced along the circumference.
They are labeled as person 0, 1, ⋯ , L − 1 in clockwise order. Consider choosing N from these L people.
The cost of a choice is defined as follows.

For every pair of persons among the N chosen people, find the minimum distance one person has to
move along the circumference to reach the other person's position. The maximum value among these
distances is the cost.

Find the sum of costs over all possible choices, modulo 998244353.

Constraints
2 ≤ N ≤ L ≤ 106
All input values are integers.

Input
The input is given from Standard Input in the following format:

L N

Output
Print the answer.

Sample Input 1
4 2
Sample Output 1
8

The choices of N people and their corresponding costs are as follows:

(0, 1): cost 1


(0, 2): cost 2
(0, 3): cost 1
(1, 2): cost 1
(1, 3): cost 2
(2, 3): cost 1
The sum of these costs, 8, is the answer.

Sample Input 2
5 5

Sample Output 2
2

We can only choose all of them. In this case, the cost is 2.

Sample Input 3
13 5

Sample Output 3
7618

Sample Input 4
1000000 100000
Sample Output 4
664396470
B - 01 Graph Construction

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 900 points

Problem Statement
A pair of strings (S, T ) consisting of 0 and 1 is called good if and only if all of the following conditions are
satisfied.

S and T contain the same number of 0s.


S and T contain the same number of 1s.
Particularly, for a good string pair (S, T ), S and T have the same length.

For a good string pair (S, T ), we define an undirected graph G(S, T ) as follows.

Let L be the length of S . Create a graph g with vertices 1, 2, ⋯ , L.


Let n be the number of 0s in S . Let the indices of 0s in S be 1 ≤ a1 < a2 < ⋯ < an ≤ L. Let the
​ ​ ​

indices of 0s in T be 1 ≤ b1 < b2 < ⋯ < bn ≤ L. For each 1 ≤ i ≤ n, add an edge between


​ ​ ​

vertices ai and bi to g .
​ ​

Let m be the number of 1s in S . Let the indices of 1s in S be 1 ≤ c1 < c2 < ⋯ < cm ≤ L. Let the
​ ​ ​

indices of 1s in T be 1 ≤ d1 < d2 < ⋯ < dm ≤ L. For each 1 ≤ i ≤ m, add an edge between


​ ​ ​

vertices ci and di to g .
​ ​

The graph g obtained through the above steps is G(S, T ).

You are given an integer sequence A = (A1 , A2 , ⋯ , AN ) of length N . Find one good string pair (S, T )
​ ​ ​

satisfying all of the following conditions.

≤ L ≤ 105 .
Let L be the length of S . It satisfies N
For each pair 1 ≤ i, j ≤ N , vertices i and j belong to the same connected component in G(S, T ) if
and only if Ai = Aj .
​ ​

It can be proved that a solution always exists under the constraints of this problem.

Constraints
1 ≤ N ≤ 100
1 ≤ Ai ≤ N

All input values are integers.


Input
The input is given from Standard Input in the following format:

N
A1 A2 ⋯ AN
​ ​

Output
Print a solution in the following format:

L
S
T

Here, L is the length of S and T . If multiple solutions exist, any of them will be accepted.

Sample Input 1
3
1 2 1

Sample Output 1
4
0011
1100

For the S and T in the sample output, we obtain G(S, T ) as follows:

Prepare a graph g with 4 vertices.


The indices of 0s in S are (1, 2), and the indices of 0s in T are (3, 4). Add edges (1, 3) and (2, 4) to g .
The indices of 1s in S are (3, 4), and the indices of 1s in T are (1, 2). Add edges (3, 1) and (4, 2) to g .
Let G(S, T ) = g.
G(S, T ) has a connected component with vertices (1, 3) and another with vertices (2, 4). This satisfies all
the conditions, so this (S, T ) is a valid output.
Sample Input 2
5
1 2 3 4 5

Sample Output 2
5
01010
01010

Sample Input 3
6
1 1 1 1 1 1

Sample Output 3
6
011111
111110

Sample Input 4
10
1 2 3 2 4 3 4 4 5 6

Sample Output 4
21
000101010111100011011
011010000010101111110
C - Ball Redistribution

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1200 points

Problem Statement
There are N boxes numbered from 1 to N , and N balls numbered from 1 to N .

You have assigned Snuke the following procedure as homework.

Put each ball into any box he likes. Multiple balls can be placed in the same box, and there can be boxes
without balls.
For i = 1, 2, ⋯ , N in this order, perform the following operations.
If box i contains no balls, do nothing.
If box i contains balls, take all of them out and arrange them in a line in any order he likes. Let k be
the number of balls taken out, and (x1 , x2 , ⋯

, xk ) be the ball numbers in the line. For each
​ ​

1 ≤ j ≤ k , put ball xj into box xj+1 . Here, xk+1 means x1 . All these operations of putting balls
​ ​ ​ ​

into boxes happen simultaneously.

He claims that he has completed the homework and reports the final state to you. Specifically, he says that
ball i is in box Ai after the procedure.

You doubt whether he has correctly performed the procedure. Determine whether the state he reported is a
possible result of the procedure.

There are T test cases for each input.

Constraints
1 ≤ T ≤ 250000
1 ≤ N ≤ 250000
1 ≤ Ai ≤ N

The sum of N over all test cases in each input is at most 250000.
All input values are integers.
Input
The input is given from Standard Input in the following format:

T
case1 ​

case2 ​


caseT ​

Each test case is given in the following format:

N
A1 A2 ⋯ AN
​ ​

Output
For each test case, print Yes if the state Snuke reported is a possible result of the procedure, and No
otherwise.

Sample Input 1
5
3
1 1 1
3
2 2 2
5
1 2 3 4 5
10
8 3 8 10 1 5 3 1 6 4
10
1 5 1 2 4 8 8 6 7 3
Sample Output 1
Yes
No
Yes
No
Yes

Consider the first test case. The following steps yield the state Snuke reported, so the answer is Yes.

Put balls 1, 2, 3 into boxes 1, 1, 2, respectively.


Take out the balls from box 1 and arrange them as (2, 1).
Put ball 2 into box 1.
Put ball 1 into box 2.
Take out the balls from box 2 and arrange them as (1, 3).
Put ball 1 into box 3.
Put ball 3 into box 1.
Take out the balls from box 3 and arrange them as (1).
Put ball 1 into box 1.
Now, balls 1, 2, 3 are all in box 1.
D - Sum of Hash of Lexmin

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1200 points

Problem Statement
There is a rooted tree T with N vertices numbered from 1 to N . Vertex 1 is the root, and the parent of
vertex i (2 ≤ i ≤ N ) is Pi (Pi < i).
​ ​

A permutation x = (x1 , x2 , ⋯ , xN ) of (1, 2, ⋯ , N ) is judged to be a good permutation or not by the


​ ​ ​

following criteria.

Consider the following operation on x.


Choose two adjacent elements u and v in x such that u and v are in an ancestor-descendant
relationship in T . It does not matter which is the ancestor and which is the descendant. Then,
swap u and v .
If it is possible to obtain a permutation that is lexicographically strictly smaller than the initial state by
performing the above operation zero or more times, x is not a good permutation. If it is impossible to
obtain a permutation lexicographically smaller than the initial state by any such operations, x is a good
permutation.

You are given a positive integer B . For a permutation x, define hash(x) = ∑1≤i≤N B i−1 × xi .

Find the sum of hash(x) over all good permutations x, modulo 998244353.

What is lexicographical order on sequences?

Constraints
2 ≤ N ≤ 100
1 ≤ B < 998244353
1 ≤ P i < i (2 ≤ i ≤ N )

All input values are integers.

Input
The input is given from Standard Input in the following format:

N B
P2 P3 ⋯ PN
​ ​ ​
Output
Print the answer.

Sample Input 1
3 100
1 1

Sample Output 1
50502

For example, x= (3, 1, 2) is not a good permutation, because by swapping 3 and 1, which are in an
ancestor-descendant relationship, we can obtain (1, 3, 2), a lexicographically smaller permutation.

= (1, 2, 3) and x = (1, 3, 2). Thus, the answer is


In this sample, the good permutations are x
hash((1, 2, 3)) + hash((1, 3, 2)) = 30201 + 20301 = 50502.

Sample Input 2
5 100
1 2 3 4

Sample Output 2
504030201

In this sample, any two vertices are in an ancestor-descendant relationship. Therefore, the only good
permutation is x = (1, 2, 3, 4, 5).

Sample Input 3
10 248730679
1 2 1 2 5 6 1 8 1
Sample Output 3
856673861

Sample Input 4
20 480124393
1 2 3 2 3 4 3 8 3 4 11 10 4 14 15 12 17 18 19

Sample Output 4
488941820
E - Sort and Match

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1200 points

Problem Statement
You are given an N × N integer matrix A = (Ai,j )1≤i≤N ,1≤j≤N and an integer M .
​ ​

For an integer sequence x = (x1 , x2 , ⋯ , xM ) of length M consisting of integers between 1 and N ,


​ ​ ​

inclusive, define f (x) as follows:

Let y = (y1 , y2 , ⋯ , yM ) be the sequence obtained by sorting the elements of x to be non-


​ ​

decreasing.
Define f (x) = ∏1≤i≤M Axi ,yi .

​ ​

For each k = 1, 2, ⋯ , N , solve the following problem:


Find the sum of f (x) over all sequences x satisfying x1 ​
= k , modulo 998244353.

Constraints
1 ≤ N ≤ 50
1 ≤ M ≤ 50
0 ≤ Ai,j < 998244353

All input values are integers.

Input
The input is given from Standard Input in the following format:

N M
A1,1 A1,2 ⋯ A1,N
​ ​ ​

A2,1 A2,2 ⋯ A2,N


​ ​ ​


AN ,1 AN ,2 ⋯ AN ,N
​ ​ ​

Output
Print the answer for each k = 1, 2, ⋯ , N in this order.
Sample Input 1
2 2
1 2
3 4

Sample Output 1
5 22

All possible x and the corresponding values of f (x) are as follows.

x = (1, 1): y = (1, 1), f (x) = A1,1 × A1,1


​ ​
=1
x = (1, 2): y = (1, 2), f (x) = A1,1 × A2,2
​ ​
=4
x = (2, 1): y = (1, 2), f (x) = A2,1 × A1,2
​ ​
=6
x = (2, 2): y = (2, 2), f (x) = A2,2 × A2,2
​ ​

= 16
Thus, the answer is 1 + 4 = 5 for k = 1, and 6 + 16 = 22 for k = 2.

Sample Input 2
2 3
1 2
3 4

Sample Output 2
27 118

Sample Input 3
5 4
785439575 250040585 709423541 945005786 19237225
404191279 250876592 22672563 519729086 344065186
273714212 560047125 139793596 542901248 520999410
855572558 498896932 418633758 742973826 248730678
238856535 319502970 908902333 164543594 245101681
Sample Output 3
216530400 726773157 717209375 797938347 957133905

Sample Input 4
10 50
197971506 714635866 966125570 768080799 80565655 459117401 256810168 775681305 582857561 94863
1706
437330820 321722967 531470304 255908811 45459908 504089816 695016247 91058795 905527130 308604
30
151769562 979797105 619322493 298241991 360690308 480124392 297323928 284686636 922571073 6277
98362
753705854 712639027 721488863 69714419 485979799 88704853 758288417 423073188 595934547 862645
14
272481811 622712481 221745114 225051881 433378197 985573661 210619166 851716760 283615535 8348
97126
366075547 933505674 858395194 490049033 22039836 361481447 735151983 518458804 422209788 28981
946
907645400 111982636 978445563 686621115 486475154 734616351 587635888 527524079 454844580 8268
49288
868863954 490627044 967828344 887235558 138021910 435784230 343307056 118718683 547282350 7576
93154
32344652 101428952 585897722 693817619 790433406 848494439 873744405 604427602 951889915 98912
5209
865548541 642980476 603592935 911086893 466178404 79002814 902745597 825893950 147052664 80575
3279

Sample Output 4
862518623 606960987 762676180 606184511 762408948 47716007 968649097 788324707 140177479 48406
3588

You might also like