E - Matrix Reducing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

H_1W_1 列の行列 A と、H_2W_2 列の行列 B が与えられます。

  • 1 \leq i \leq H_1 かつ 1 \leq j \leq W_1 を満たす整数の組 (i, j) について、行列 Ai 行目 j 列目の要素は A_{i, j} です。
  • 1 \leq i \leq H_2 かつ 1 \leq j \leq W_2 を満たす整数の組 (i, j) について、行列 Bi 行目 j 列目の要素は B_{i, j} です。

行列 A に対して、下記の 2 つの操作のうちどちらかを行うことを、好きなだけ( 0 回でも良い)繰り返すことができます。

  • A の行を任意に 1 つ選んで削除する。
  • A の列を任意に 1 つ選んで削除する。

行列 A を行列 B に一致させることができるかどうかを判定して下さい。

制約

  • 1 \leq H_2 \leq H_1 \leq 10
  • 1 \leq W_2 \leq W_1 \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 1 \leq B_{i, j} \leq 10^9
  • 入力中の値はすべて整数

入力

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

H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}

出力

行列 A を行列 B に一致させることができる場合は Yes を、 一致させることができない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
2 3
6 8 9
16 18 19

出力例 1

Yes

初期状態の行列 A から 2 列目を削除すると、行列 A

1 3 4 5
6 8 9 10
11 13 14 15
16 18 19 20

となります。そこからさらに 3 行目を削除すると、行列 A

1 3 4 5
6 8 9 10
16 18 19 20

となります。そこからさらに 1 行目を削除すると、行列 A

6 8 9 10
16 18 19 20

となります。そこからさらに 4 列目を削除すると、行列 A

6 8 9
16 18 19

となります。これは行列 B と一致します。 操作の繰り返しによって行列 A を行列 B に一致させることができるので Yes を出力します。


入力例 2

3 3
1 1 1
1 1 1
1 1 1
1 1
2

出力例 2

No

どのように操作を行っても、 行列 A を行列 B に一致させることはできません。 よって、No を出力します。

Score : 300 points

Problem Statement

You are given a matrix A with H_1 rows and W_1 columns, and a matrix B with H_2 rows and W_2 columns.

  • For all integer pairs (i, j) such that 1 \leq i \leq H_1 and 1 \leq j \leq W_1, the element at the i-th row and j-th column of matrix A is A_{i, j}.
  • For all integer pairs (i, j) such that 1 \leq i \leq H_2 and 1 \leq j \leq W_2, the element at the i-th row and j-th column of matrix B is B_{i, j}.

You may perform the following operations on the matrix A any number of (possibly 0) times in any order:

  • Choose an arbitrary row of A and remove it.
  • Choose an arbitrary column of A and remove it.

Determine if it is possible to make the matrix A equal the matrix B.

Constraints

  • 1 \leq H_2 \leq H_1 \leq 10
  • 1 \leq W_2 \leq W_1 \leq 10
  • 1 \leq A_{i, j} \leq 10^9
  • 1 \leq B_{i, j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H_1 W_1
A_{1, 1} A_{1, 2} \ldots A_{1, W_1}
A_{2, 1} A_{2, 2} \ldots A_{2, W_1}
\vdots
A_{H_1, 1} A_{H_1, 2} \ldots A_{H_1, W_1}
H_2 W_2
B_{1, 1} B_{1, 2} \ldots B_{1, W_2}
B_{2, 1} B_{2, 2} \ldots B_{2, W_2}
\vdots
B_{H_2, 1} B_{H_2, 2} \ldots B_{H_2, W_2}

Output

Print Yes if it is possible to make the matrix A equal the matrix B; print No otherwise. Note that the judge is case-sensitive.


Sample Input 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
2 3
6 8 9
16 18 19

Sample Output 1

Yes

Removing the 2-nd column from the initial A results in:

1 3 4 5
6 8 9 10
11 13 14 15
16 18 19 20

Then, removing the 3-rd row from A results in:

1 3 4 5
6 8 9 10
16 18 19 20

Then, removing the 1-st row from A results in:

6 8 9 10
16 18 19 20

Then, removing the 4-th column from A results in:

6 8 9
16 18 19

Now the matrix equals the matrix B.
Thus, we can make the matrix A equal the matrix B by repeating the operations, so Yes should be printed.


Sample Input 2

3 3
1 1 1
1 1 1
1 1 1
1 1
2

Sample Output 2

No

Regardless of how we perform the operations, we cannot make the matrix A equal the matrix B, so No should be printed.

F - Monotonically Increasing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。

注記

ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_NB_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で AB より早いと定義されます。

  • ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。

ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。

  • 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。

制約

  • 1 \le N \le M \le 10
  • 入力は全て整数。

入力

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

N M

出力

条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。


入力例 1

2 3

出力例 1

1 2 
1 3 
2 3 

条件を満たす数列は (1,2),(1,3),(2,3)3 個です。これらを辞書順で早い方から出力します。


入力例 2

3 5

出力例 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 

Score : 300 points

Problem Statement

Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.

Notes

For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:

  • there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.

An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:

  • A_i < A_{i+1} for all integers i (1 \le i \le N-1).

Constraints

  • 1 \le N \le M \le 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).


Sample Input 1

2 3

Sample Output 1

1 2 
1 3 
2 3 

The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.


Sample Input 2

3 5

Sample Output 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 
G - Sleep Log

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

高橋くんは睡眠記録をつけています。 睡眠記録は奇数長の数列 A=(A _ 1(=0), A _ 2,\ldots,A _ N) で表され、奇数番目は起床時刻を、偶数番目は就寝時刻を表しています。 より厳密には、睡眠記録をつけている間に高橋くんは次のような睡眠をとりました。

  • すべての 1\leq i\leq\dfrac{N-1}2 を満たす整数 i について、睡眠記録をつけ始めてから A _ {2i} 分後ちょうどに寝て、A _ {2i+1} 分後ちょうどに起きた。
  • それ以外の時間に寝ることも起きることもなかった。

次の Q 個の質問に答えてください。 i 番目の質問では、0\leq l _ i\leq r _ i\leq A _ N を満たす整数の組 (l _ i,r _ i) が与えられます。

  • 睡眠記録をつけ始めてから l _ i 分後ちょうどから r _ i 分後ちょうどまでの r _ i-l _ i 分のうち、高橋くんが寝ていたのは何分間ですか?

制約

  • 3\leq N\lt2\times10^5
  • N は奇数
  • 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
  • 1\leq Q\leq2\times10^5
  • 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \ldots A _ N
Q
l _ 1 r _ 1
l _ 2 r _ 2
\vdots
l _ Q r _ Q

出力

答えを Q 行で出力せよ。 i 行目には i 番目の質問の答えを整数として出力せよ。


入力例 1

7
0 240 720 1320 1440 1800 2160
3
480 1920
720 1200
0 2160

出力例 1

480
0
960

高橋くんは、以下の図のように睡眠をとりました。

それぞれの質問の答えは以下のようになります。

  • 睡眠記録をつけ始めてから 480 分後から 1920 分後の間、高橋くんは 480 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 1920 分後の 3 つの睡眠をとりました。睡眠時間の合計は 240+120+120=480 分です。
  • 睡眠記録をつけ始めてから 720 分後から 1200 分後の間、高橋くんは睡眠をとりませんでした。睡眠時間の合計は 0 分です。
  • 睡眠記録をつけ始めてから 0 分後から 2160 分後の間、高橋くんは 240 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 2160 分後の 3 つの睡眠をとりました。睡眠時間の合計は 480+120+360=960 分です。

よって、それぞれの行に 480,0,960 と出力してください。


入力例 2

21
0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000
10
77 721
255 541
478 970
369 466
343 541
42 165
16 618
222 592
730 983
338 747

出力例 2

296
150
150
49
89
20
279
183
61
177

Score : 450 points

Problem Statement

Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.

  • For every integer i such that 1\leq i\leq\dfrac{N-1}2, he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log.
  • He did not fall asleep or wake up at any other time.

Answer the following Q questions. For the i-th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N.

  • What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?

Constraints

  • 3\leq N\lt2\times10^5
  • N is odd.
  • 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
  • 1\leq Q\leq2\times10^5
  • 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
  • All input values are integers.

Input

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

N
A _ 1 A _ 2 \ldots A _ N
Q
l _ 1 r _ 1
l _ 2 r _ 2
\vdots
l _ Q r _ Q

Output

Print the answer in Q lines. The i-th line should contain an integer answering to the i-th question.


Sample Input 1

7
0 240 720 1320 1440 1800 2160
3
480 1920
720 1200
0 2160

Sample Output 1

480
0
960

Takahashi slept as shown in the following figure.

The answers to each question are as follows.

  • Between 480 minutes and 1920 minutes after starting the sleep log, Takahashi slept from 480 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 1920 minutes in 3 sleep sessions. The total sleep time is 240+120+120=480 minutes.
  • Between 720 minutes and 1200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 0 minutes.
  • Between 0 minutes and 2160 minutes after starting the sleep log, Takahashi slept from 240 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 2160 minutes in 3 sleep sessions. The total sleep time is 480+120+360=960 minutes.

Therefore, the three lines of the output should contain 480, 0, and 960.


Sample Input 2

21
0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000
10
77 721
255 541
478 970
369 466
343 541
42 165
16 618
222 592
730 983
338 747

Sample Output 2

296
150
150
49
89
20
279
183
61
177
H - Nearest Black Vertex

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 個の頂点と M 本の辺からなる、単純(自己ループおよび多重辺を含まない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。

各頂点を白または黒で塗る方法であって、下記の 2 つの条件をともに満たすものが存在するかを判定し、存在する場合はその一例を示してください。

  • 1 個以上の頂点が黒で塗られている。
  • すべての i = 1, 2, \ldots, K について、下記の条件が成り立つ。
    • 頂点 p_i と「黒で塗られた頂点のうち頂点 p_i からの距離が最小であるもの」の距離がちょうど d_i である。

ここで、頂点 u と頂点 v の距離は、uv を結ぶパスの辺の本数としてあり得る最小値として定義されます。

制約

  • 1 \leq N \leq 2000
  • N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
  • 1 \leq u_i, v_i \leq N
  • 0 \leq K \leq N
  • 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
  • 0 \leq d_i \leq N
  • 与えられるグラフは単純かつ連結
  • 入力はすべて整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
K
p_1 d_1
p_2 d_2
\vdots
p_K d_K

出力

問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しない場合は、No を出力せよ。
存在する場合は、下記の形式の通り、1 行目に Yes と出力し、2 行目に各頂点を塗る方法を表す文字列 S を出力せよ。 ここで、S は長さ N の文字列であり、i = 1, 2, \ldots, N について Si 文字目は、頂点 i を黒で塗るとき 1 、白で塗るとき 0 である。

Yes
S

答えが複数ある場合、どれを出力しても正解とみなされる。


入力例 1

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

出力例 1

Yes
10100

例えば、頂点 1, 3 を黒に、頂点 2, 4, 5 を白に塗るという方法が、問題文中の条件を満たします。
実際、i = 1, 2, 3, 4, 5 について、頂点 i と「黒で塗られた頂点のうち頂点 i からの距離が最小であるもの」の距離を A_i で表すと、 (A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2) であり、特に、A_1 = 0, A_5 = 2 が成り立ちます。


入力例 2

5 5
1 2
2 3
3 1
3 4
4 5
5
1 1
2 1
3 1
4 1
5 1

出力例 2

No

問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しないため、No を出力します。


入力例 3

1 0
0

出力例 3

Yes
1

Score : 500 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges (a simple graph contains no self-loop and no multi-edges).
For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i bidirectionally.

Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists.

  • At least one vertex is painted black.
  • For every i = 1, 2, \ldots, K, the following holds:
    • the minimum distance between vertex p_i and a vertex painted black is exactly d_i.

Here, the distance between vertex u and vertex v is the minimum number of edges in a path connecting u and v.

Constraints

  • 1 \leq N \leq 2000
  • N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
  • 1 \leq u_i, v_i \leq N
  • 0 \leq K \leq N
  • 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
  • 0 \leq d_i \leq N
  • The given graph is simple and connected.
  • All values in the input are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
K
p_1 d_1
p_2 d_2
\vdots
p_K d_K

Output

If there is no way to paint each vertex black or white to satisfy the conditions, print No.
Otherwise, print Yes in the first line, and a string S representing a coloring of the vertices in the second line, as shown below.
Here, S is a string of length N such that, for each i = 1, 2, \ldots, N, the i-th character of S is 1 if vertex i is painted black and 0 if white.

Yes
S

If multiple solutions exist, you may print any of them.


Sample Input 1

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

Sample Output 1

Yes
10100

One way to satisfy the conditions is to paint vertices 1, 3 black and vertices 2, 4, 5 white.
Indeed, for each i = 1, 2, 3, 4, 5, let A_i denote the minimum distance between vertex i and a vertex painted black, and we have (A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2), where A_1 = 0, A_5 = 2.


Sample Input 2

5 5
1 2
2 3
3 1
3 4
4 5
5
1 1
2 1
3 1
4 1
5 1

Sample Output 2

No

There is no way to satisfy the conditions by painting each vertex black or white, so you should print No.


Sample Input 3

1 0
0

Sample Output 3

Yes
1
I - Reordering

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

文字列 S が与えられます。S の空でない、連続するとは限らない部分列を並び替えて得られる文字列は何種類ありますか?

答えは非常に大きくなる場合があるので、998244353 で割ったあまりを出力してください。

制約

  • S は英小文字のみからなる長さ 1 以上 5000 以下の文字列

入力

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

S

出力

S の部分列を並び替えて得られる文字列の種類数を 998244353 で割ったあまりを出力せよ。


入力例 1

aab

出力例 1

8

S の部分列を並び替えて得られる文字列は、a, b, aa, ab, ba, aab, aba, baa8 種類です。


入力例 2

aaa

出力例 2

3

入力例 3

abcdefghijklmnopqrstuvwxyz

出力例 3

149621752

998244353 で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

Given is a string S. How many different strings can be obtained as a permutation of a non-empty, not necessarily contiguous subsequence of S?

Since the count can be enormous, print it modulo 998244353.

Constraints

  • S is a string of length 1 and 5000 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of different strings that can be obtained as a permutation of a subsequence of S, modulo 998244353.


Sample Input 1

aab

Sample Output 1

8

There are 8 different strings that can be obtained as a permutation of a subsequence of S: a, b, aa, ab, ba, aab, aba, baa.


Sample Input 2

aaa

Sample Output 2

3

Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

149621752

Be sure to print the count modulo 998244353.