A - No Majority

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

英小文字からなる文字列 x が以下の条件を満たすとき,xよい文字列と呼ぶことにします.

  • x の長さ 2 以上の (連続する) 部分文字列は,すべて以下の条件を満たす.
    • その部分文字列内で過半数を占める文字が存在しない.

例えば,acbca はよい文字列ではありません. これは,部分文字列 cbc のなかで c が過半数を占めているからです.

英小文字と ? からなる長さ N の文字列 S が与えられます. それぞれの ? を好きな英小文字に置き換ることで,S をよい文字列にしたいです. S をよい文字列にする方法が何通りあるかを 998244353 で割ったあまりを求めてください.

制約

  • 2 \leq N \leq 5000
  • S は英小文字と ? からなる長さ N の文字列

入力

入力は以下の形式で標準入力から与えられる.

N
S

出力

答えを出力せよ.


入力例 1

3
a?b

出力例 1

24

aab, abb 以外の方法が条件を満たします.


入力例 2

3
a?a

出力例 2

0

入力例 3

20
ugsyakganihodnwmktgi

出力例 3

1

入力例 4

20
??a???h?m?y?ts???tl?

出力例 4

444225229

Score : 400 points

Problem Statement

A string x consisting of lowercase English letters is said to be good if and only if the following condition is satisfied.

  • Every (contiguous) substring of x whose length is 2 or greater satisfies the following:
    • no character occupies the majority of that substring.

For example, acbca is not good because c occupies the majority of the substring cbc.

You are given a string S of length N consisting of lowercase English letters and ?. You want to replace each ? with a lowercase English letter of your choice to make S a good string. Find the number of ways to make S a good string, modulo 998244353.

Constraints

  • 2 \leq N \leq 5000
  • S is a string of length N consisting of lowercase English letters and ?.

Input

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

N
S

Output

Print the answer.


Sample Input 1

3
a?b

Sample Output 1

24

Every way other than aab and abb satisfies the condition.


Sample Input 2

3
a?a

Sample Output 2

0

Sample Input 3

20
ugsyakganihodnwmktgi

Sample Output 3

1

Sample Input 4

20
??a???h?m?y?ts???tl?

Sample Output 4

444225229
B - Unique XOR Path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

NM 列のグリッドがあります. あなたはグリッドの各マスに 0 以上 2^K-1 以下の整数を書き込み,以下の条件を満たしたいです.

  • 左上のマスを出発し,右または下に隣接するマスへの移動を繰り返して,右下のマスへと至るパスを考える. ここで,通ったマス (始点終点を含む) に書かれた整数の総 \mathrm{XOR}0 になるパスを,よいパスと呼ぶことにする.
  • よいパスはちょうど 1 つだけ存在し,それは文字列 S が表すパスである. 文字列 S が表すパスとは,各 i (1 \leq i \leq N+M-2) について,i 回目の移動の際,Si 文字目が R なら右,D なら下に進むようなパスである.

条件を満たす整数の書き込み方が存在するかどうか判定してください.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR}(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

  • 1 \leq T \leq 100
  • 2 \leq N,M \leq 30
  • 1 \leq K \leq 30
  • S はちょうど N-1 個の DM-1 個の R からなる文字列
  • 入力される数はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

T
case_1
case_2
\vdots
case_T

各ケースは以下の形式で与えられる.

N M K
S

出力

各ケースに対し,条件を満たす整数の書き込み方が存在する場合は Yes を,存在しないならば No を出力せよ. 出力中の各文字は英大文字・小文字のいずれでもよい.


入力例 1

4
2 2 1
RD
4 3 1
RDDDR
15 20 18
DDRRRRRRRDDDDRRDDRDRRRRDDRDRDDRRR
20 15 7
DRRDDDDDRDDDRRDDRRRDRRRDDDDDRRRDD

出力例 1

Yes
No
Yes
No

例えば 1 ケース目については,以下のようなグリッドを作れば良いです.

11
00

Score : 700 points

Problem Statement

We have a grid with N rows and M columns. You want to write an integer between 0 and 2^K-1 in each square in the grid to satisfy the following condition.

  • Consider a path that starts at the top-left square, repeatedly moves right or down to an adjacent square, and ends at the bottom-right square. Such a path is said to be good if and only if the total \mathrm{XOR} of the integers written on the squares visited (including the starting and ending points) is 0.
  • There is exactly one good path, which is the path represented by a string S. The path represented by the string S is a path that, for each i (1 \leq i \leq N+M-2), the i-th move is right if the i-th character of S is R and down if that character is D.

Determine whether there is a way to write integers that satisfies the condition.

For each input file, solve T test cases.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows.

  • When A \oplus B is written in binary, the k-th lowest bit (k \geq 0) is 1 if exactly one of the k-th lowest bits of A and B is 1, and 0 otherwise.
For instance, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).
Generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), which can be proved to be independent of the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1 \leq T \leq 100
  • 2 \leq N,M \leq 30
  • 1 \leq K \leq 30
  • S is a string containing exactly N-1 Ds and M-1 Rs.
  • All numbers in the input are integers.

Input

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

T
case_1
case_2
\vdots
case_T

Each case is in the following format:

N M K
S

Output

For each case, print Yes if there is a way to write integers that satisfies the condition, and No otherwise. Each character in the output may be either uppercase or lowercase.


Sample Input 1

4
2 2 1
RD
4 3 1
RDDDR
15 20 18
DDRRRRRRRDDDDRRDDRDRRRRDDRDRDDRRR
20 15 7
DRRDDDDDRDDDRRDDRRRDRRRDDDDDRRRDD

Sample Output 1

Yes
No
Yes
No

As an example, for the first case, you can make the grid as follows.

11
00
C - Large Heap

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 800

問題文

(1,2,\cdots,2^N-1) の順列 P=(P_1,P_2,\cdots,P_{2^N-1}) を考えます. P が以下の条件をすべて満たすとき,それをヒープ的な順列と呼ぶことにします.

  • P_i < P_{2i} (1 \leq i \leq 2^{N-1}-1)
  • P_i < P_{2i+1} (1 \leq i \leq 2^{N-1}-1)

整数 A,B が与えられます. U=2^A, V=2^{B+1}-1 とします.

ヒープ的な順列を一様ランダムに 1 つ選んだ際,P_U<P_V である確率を \text{mod }998244353 で求めてください.

確率 \text{mod }{998244353} の定義

求める確率は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、求める有理数を既約分数 \frac{P}{Q} で表した時、Q \neq 0 \pmod{998244353} となることが証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R \lt 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 2 \leq N \leq 5000
  • 1 \leq A,B \leq N-1
  • 入力される数はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

N A B

出力

答えを出力せよ.


入力例 1

2 1 1

出力例 1

499122177

ヒープ的な順列は,P=(1,2,3),(1,3,2)2 つです. P_2<P_3 となる確率は 1/2 です.


入力例 2

3 1 2

出力例 2

124780545

入力例 3

4 3 2

出力例 3

260479386

入力例 4

2022 12 25

出力例 4

741532295

Score : 800 points

Problem Statement

Consider a permutation P=(P_1,P_2,\cdots,P_{2^N-1}) of (1,2,\cdots,2^N-1). P is said to be heaplike if and only if all of the following conditions are satisfied.

  • P_i < P_{2i} (1 \leq i \leq 2^{N-1}-1)
  • P_i < P_{2i+1} (1 \leq i \leq 2^{N-1}-1)

You are given integers A and B. Let U=2^A and V=2^{B+1}-1.

Find the probability, modulo 998244353, that P_U<P_V for a heaplike permutation chosen uniformly at random.

Definition of probability modulo 998244353

It can be proved that the sought probability is always rational. Additionally, under the constraints of this problem, when the sought rational number is represented as an irreducible fraction \frac{P}{Q}, it can be proved that Q \neq 0 \pmod{998244353}. Therefore, there is a unique integer R such that R \times Q \equiv P \pmod{998244353} and 0 \leq R \lt 998244353. Print this R.

Constraints

  • 2 \leq N \leq 5000
  • 1 \leq A,B \leq N-1
  • All numbers in the input are integers.

Input

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

N A B

Output

Print the answer.


Sample Input 1

2 1 1

Sample Output 1

499122177

There are two heaplike permutations: P=(1,2,3),(1,3,2). The probability that P_2<P_3 is 1/2.


Sample Input 2

3 1 2

Sample Output 2

124780545

Sample Input 3

4 3 2

Sample Output 3

260479386

Sample Input 4

2022 12 25

Sample Output 4

741532295
D - Same Descent Set

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

(1,2,\cdots,N) の順列のペア (P,Q)=((P_1,P_2,\cdots,P_N),(Q_1,Q_2,\cdots,Q_N)) であって,次の条件を満たすものの個数を 998244353 で割ったあまりを求めてください.

  • すべての i (1 \leq i \leq N-1) について,以下のいずれかの条件が成り立つ.
    • P_i < P_{i+1} かつ Q_i < Q_{i+1}
    • P_i > P_{i+1} かつ Q_i > Q_{i+1}

制約

  • 2 \leq N \leq 2 \times 10^5
  • 入力される数はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

N

出力

答えを出力せよ.


入力例 1

2

出力例 1

2

(P,Q)=((1,2),(1,2))(P,Q)=((2,1),(2,1))2 つが条件を満たします.


入力例 2

3

出力例 2

10

入力例 3

4

出力例 3

88

入力例 4

10

出力例 4

286574791

Score : 1000 points

Problem Statement

Find the number of pairs (P,Q)=((P_1,P_2,\cdots,P_N),(Q_1,Q_2,\cdots,Q_N)) of permutations of (1,2,\cdots,N) that satisfy the following condition, modulo 998244353.

  • For every i (1 \leq i \leq N-1), one of the two conditions below holds.
    • P_i < P_{i+1} and Q_i < Q_{i+1}.
    • P_i > P_{i+1} and Q_i > Q_{i+1}.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • All numbers in the input are integers.

Input

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

N

Output

Print the answer.


Sample Input 1

2

Sample Output 1

2

Two pairs, (P,Q)=((1,2),(1,2)) and (P,Q)=((2,1),(2,1)), satisfy the condition.


Sample Input 2

3

Sample Output 2

10

Sample Input 3

4

Sample Output 3

88

Sample Input 4

10

Sample Output 4

286574791
E - Number of Cycles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

この問題では,順列と言った際には (1,2,\cdots,N) の順列を指すものとします.

順列 a=(a_1,a_2,\cdots,a_N) に対し,f(a)a のサイクルの個数と定義します. より正確には,f(a) の値は次のように定義されます.

  • 1 から N までの番号のついた N 頂点からなる無向グラフを考える. そして,各 1 \leq i \leq N について,頂点 i と頂点 a_i 間を結ぶ辺を追加する. このときのグラフの連結成分の個数を f(a) とする.

順列 P=(P_1,P_2,\cdots,P_N) および整数 K が与えられます. 次の条件を満たす順列 x が存在するかどうか判定し,存在するなら一つ構築してください.

  • y_i=P_{x_i} として,順列 y を定める.
  • この時,f(x)+f(y)=K である.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 2 \leq K \leq 2N
  • (P_1,P_2,\cdots,P_N)(1,2,\cdots,N) の順列
  • 1 つの入力ファイルにつき,N の総和は 2 \times 10^5 を超えない
  • 入力される数はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

T
case_1
case_2
\vdots
case_T

各ケースは以下の形式で与えられる.

N K
P_1 P_2 \cdots P_N

出力

各ケースに対し,条件を満たす順列 x が存在しない場合は No と,存在する場合は以下の形式で答えを出力せよ.

Yes
x_1 x_2 \cdots x_N

Yes, No を出力する際,各文字は英大文字・小文字のいずれでもよい. 解が複数存在する場合,どれを出力しても正解とみなされる.


入力例 1

3
3 3
1 3 2
2 2
2 1
4 8
1 2 3 4

出力例 1

Yes
2 1 3
No
Yes
1 2 3 4

1 つめのケースでは,x=(2,1,3) とすれば y=(3,1,2) となり,f(x)+f(y)=2+1=3 となります.

Score : 1400 points

Problem Statement

In this problem, a permutation of (1,2,\cdots,N) is occasionally called just a permutation.

For a permutation a=(a_1,a_2,\cdots,a_N), let f(a) be the number of cycles in a. More accurately, the value of f(a) is defined as follows.

  • Consider an undirected graph consisting of N vertices numbered 1 to N. For each 1 \leq i \leq N, add an edge connecting vertex i and vertex a_i. Then, let f(a) be the number of connected components in this graph.

You are given a permutation P=(P_1,P_2,\cdots,P_N) and an integer K. Determine whether there is a permutation x that satisfies the following condition, and construct one if it exists.

  • Let y_i=P_{x_i} to define a permutation y.
  • Then, f(x)+f(y)=K holds.

For each input file, solve T test cases.

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 2 \leq K \leq 2N
  • (P_1,P_2,\cdots,P_N) is a permutation of (1,2,\cdots,N).
  • In each input file, the sum of N does not exceed 2 \times 10^5.
  • All numbers in the input are integers.

Input

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

T
case_1
case_2
\vdots
case_T

Each case is in the following format:

N K
P_1 P_2 \cdots P_N

Output

For each case, if no permutation x satisfies the condition, print No. Otherwise, print your answer in the following format:

Yes
x_1 x_2 \cdots x_N

Each character in Yes or No in the output may be either uppercase or lowercase. If multiple solutions exist, any of them will be accepted.


Sample Input 1

3
3 3
1 3 2
2 2
2 1
4 8
1 2 3 4

Sample Output 1

Yes
2 1 3
No
Yes
1 2 3 4

In the first case, letting x=(2,1,3) makes y=(3,1,2), satisfying f(x)+f(y)=2+1=3.

F - Spanning Trees of Interval Graph

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 2200

問題文

あなたはある単純無向グラフを持っています. このグラフの各頂点には整数区間が書かれており,区間 [i,j] (1 \leq i \leq j \leq N) が書かれているような頂点は C_{i,j} 個あります. また,これら以外の区間が書かれた頂点はありません.

任意の 2 つの頂点について,それらに書かれた区間が共通部分を持つとき,またそのときのみ,その 2 頂点間の間に無向辺が存在します. ここで,区間 [a,b] と区間 [c,d] が共通部分を持つとは,\max(a,c) \leq \min(b,d) であるという意味です.

このグラフの全域木の個数を 998244353 で割ったあまりを求めてください. なお,すべての頂点は互いに区別できるものとします.

制約

  • 2 \leq N \leq 400
  • 1 \leq C_{i,j} \leq 10^4
  • 入力される数はすべて整数

入力

入力は以下の形式で標準入力から与えられる.

N
C_{1,1} C_{1,2} \cdots C_{1,N}
C_{2,2} \cdots C_{2,N}
\vdots
C_{N,N}

出力

答えを出力せよ.


入力例 1

2
1 2
1

出力例 1

8

入力例 2

3
1 1 1
1 1
1

出力例 2

99

入力例 3

4
8 3 8 10
1 5 3
1 6
4

出力例 3

971555314

入力例 4

10
2742 5611 1401 5439 5220 8571 4998 4194 7443 2492
2393 3201 9106 1649 2456 1984 7159 9679 7695
808 4600 2573 7771 5839 9504 4381 3223
5325 2564 456 5050 6963 913 9072
310 1521 9816 6205 2988 3614
4810 2979 2852 9242 6290
7551 7139 7228 699
4869 889 7597
4239 5970
865

出力例 4

234850531

Score : 2200 points

Problem Statement

You have a simple undirected graph. Each vertex in this graph has an integer interval written on it, and there are C_{i,j} vertices with the interval [i,j] (1 \leq i \leq j \leq N). There are no vertices with other intervals.

For any two vertices, there is an undirected edge between them if and only if the written intervals intersect. Here, an interval [a,b] and an interval [c,d] intersect if and only if \max(a,c) \leq \min(b,d).

Find the number of spanning trees of this graph, modulo 998244353. Here, all vertices are pairwise distinguishable.

Constraints

  • 2 \leq N \leq 400
  • 1 \leq C_{i,j} \leq 10^4
  • All numbers in the input are integers.

Input

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

N
C_{1,1} C_{1,2} \cdots C_{1,N}
C_{2,2} \cdots C_{2,N}
\vdots
C_{N,N}

Output

Print the answer.


Sample Input 1

2
1 2
1

Sample Output 1

8

Sample Input 2

3
1 1 1
1 1
1

Sample Output 2

99

Sample Input 3

4
8 3 8 10
1 5 3
1 6
4

Sample Output 3

971555314

Sample Input 4

10
2742 5611 1401 5439 5220 8571 4998 4194 7443 2492
2393 3201 9106 1649 2456 1984 7159 9679 7695
808 4600 2573 7771 5839 9504 4381 3223
5325 2564 456 5050 6963 913 9072
310 1521 9816 6205 2988 3614
4810 2979 2852 9242 6290
7551 7139 7228 699
4869 889 7597
4239 5970
865

Sample Output 4

234850531