A - Circular Distance: Problem Statement
A - Circular Distance: Problem Statement
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
Sample Input 2
5 5
Sample Output 2
2
Sample Input 3
13 5
Sample Output 3
7618
Sample Input 4
1000000 100000
Sample Output 4
664396470
B - 01 Graph Construction
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.
For a good string pair (S, T ), we define an undirected graph G(S, T ) as follows.
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
vertices ci and di to g .
You are given an integer sequence A = (A1 , A2 , ⋯ , AN ) of length N . Find one good string pair (S, T )
≤ 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
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
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
Problem Statement
There are N boxes numbered from 1 to N , and N balls numbered from 1 to N .
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
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.
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
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.
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).
following criteria.
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.
Constraints
2 ≤ N ≤ 100
1 ≤ B < 998244353
1 ≤ P i < i (2 ≤ i ≤ N )
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.
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
Problem Statement
You are given an N × N integer matrix A = (Ai,j )1≤i≤N ,1≤j≤N and an integer M .
decreasing.
Define f (x) = ∏1≤i≤M Axi ,yi .
Constraints
1 ≤ N ≤ 50
1 ≤ M ≤ 50
0 ≤ Ai,j < 998244353
Input
The input is given from Standard Input in the following format:
N M
A1,1 A1,2 ⋯ A1,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
= 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