A - Streamer Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

配信者の高橋君は L 時から R 時に配信をすることにしました。

高橋君には N 人のリスナーがおり、i 人目のリスナーは X_i 時から Y_i 時まで配信を見ることができます。

高橋君の配信を最初から最後まで見ることができるリスナーは何人いますか?

制約

  • 1\leq N\leq 100
  • 0\leq L\lt R\leq 23
  • 0\leq X_i\lt Y_i\leq 23
  • 入力は全て整数

入力

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

N L R
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

高橋君の配信を最初から最後まで見ることができるリスナーの数を出力せよ。


入力例 1

5 19 22
17 23
20 23
19 22
0 23
12 20

出力例 1

3

高橋君の配信を最初から最後まで見ることができるのは 1,3,4 人目のリスナーです。


入力例 2

3 12 13
0 1
0 1
0 1

出力例 2

0

高橋君の配信を最初から最後まで見ることができるリスナーはいません。


入力例 3

10 8 14
5 20
14 21
9 21
5 23
8 10
0 14
3 8
2 6
0 16
5 20

出力例 3

5

Score : 100 points

Problem Statement

Streamer Takahashi has decided to stream from L o'clock to R o'clock (using the 24-hour clock).

He has N listeners, and the i-th listener can watch the stream from X_i o'clock to Y_i o'clock.

How many listeners can watch Takahashi's stream from beginning to end?

Constraints

  • 1\leq N\leq 100
  • 0\leq L\lt R\leq 23
  • 0\leq X_i\lt Y_i\leq 23
  • All input values are integers.

Input

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

N L R
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Output the number of listeners who can watch Takahashi's stream from beginning to end.


Sample Input 1

5 19 22
17 23
20 23
19 22
0 23
12 20

Sample Output 1

3

The listeners who can watch Takahashi's stream from beginning to end are the 1st, 3rd, and 4th listeners.


Sample Input 2

3 12 13
0 1
0 1
0 1

Sample Output 2

0

No listeners can watch Takahashi's stream from beginning to end.


Sample Input 3

10 8 14
5 20
14 21
9 21
5 23
8 10
0 14
3 8
2 6
0 16
5 20

Sample Output 3

5
B - Subsegment Reverse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 N,L,R が与えられます。
長さ N の数列 A=(1,2,\dots,N) に対し、 L 項目から R 項目までを逆順に並べ替えるという操作を一度行いました。
操作後の数列を出力してください。

制約

  • 入力は全て整数
  • 1 \le L \le R \le N \le 100

入力

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

N L R

出力

操作後の数列を A'=(A'_1,A'_2,\dots,A'_N) として、以下の形式で出力せよ。

A'_1 A'_2 \dots A'_N

入力例 1

5 2 3

出力例 1

1 3 2 4 5

最初、 A=(1,2,3,4,5) です。
2 項目から 3 項目までを逆順に並べ替えた後の数列は (1,3,2,4,5) なので、これを出力してください。


入力例 2

7 1 1

出力例 2

1 2 3 4 5 6 7

L=R である場合もあります。


入力例 3

10 1 10

出力例 3

10 9 8 7 6 5 4 3 2 1

L=1 や R=N である場合もあります。

Score : 100 points

Problem Statement

You are given positive integers N, L, and R.
For a sequence A = (1, 2, \dots, N) of length N, an operation of reversing the L-th through R-th elements was performed once.
Print the sequence after this operation.

Constraints

  • All input values are integers.
  • 1 \leq L \leq R \leq N \leq 100

Input

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

N L R

Output

Let A' = (A'_1, A'_2, \dots, A'_N) be the sequence after the operation. Print it in the following format:

A'_1 A'_2 \dots A'_N

Sample Input 1

5 2 3

Sample Output 1

1 3 2 4 5

Initially, A = (1, 2, 3, 4, 5).
After reversing the second through third elements, the sequence becomes (1, 3, 2, 4, 5), which should be printed.


Sample Input 2

7 1 1

Sample Output 2

1 2 3 4 5 6 7

It is possible that L = R.


Sample Input 3

10 1 10

Sample Output 3

10 9 8 7 6 5 4 3 2 1

It is possible that L = 1 or R = N.

C - Seek Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N \times N のグリッド S と M\times M のグリッド T が与えられます。上から i 行目、左から j 列目のマス目をマス (i,j) と表します。

S,T の各マスの色はそれぞれ N^2個の文字 S_{i,j} \; (1\leq i,j\leq N) および M^2 個の文字 T_{i,j} \; (1\leq i,j\leq M) によって表されます。 S_{i,j} が . のとき S のマス (i,j) は白色、S_{i,j} が # のとき S のマス (i,j) は黒色で塗られています。T についても同様です。

S の中から T を探してください。具体的には、以下の条件を満たす a,b \; (1 \leq a,b \leq N-M+1) を出力してください。

  • すべての i,j \; (1\leq i,j \leq M) について、S_{a+i-1,b+j-1} = T_{i,j}

制約

  • 1 \leq M \leq N \leq 50
  • N,M は整数
  • S_{i,j},T_{i,j} は . または #
  • 条件を満たす a,b はちょうど 1 組存在する。

入力

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

N M
S_{1,1}S_{1,2}\dots S_{1,N}
S_{2,1}S_{2,2}\dots S_{2,N}
\vdots
S_{N,1}S_{N,2}\dots S_{N,N}
T_{1,1}T_{1,2}\dots T_{1,M}
T_{2,1}T_{2,2}\dots T_{2,M}
\vdots
T_{M,1}T_{M,2}\dots T_{M,M}

出力

a,b をこの順に空白区切りで 1 行に出力せよ。


入力例 1

3 2
#.#
..#
##.
.#
#.

出力例 1

2 2

S の 2 行目から 3 行目、2 列目から 3 列目の 2 \times 2 マスが T と一致します。


入力例 2

2 1
#.
##
.

出力例 2

1 2

Score : 200 points

Problem Statement

You are given an N \times N grid S and an M \times M grid T. The cell at the i-th row from the top and the j-th column from the left is denoted by (i,j).

The colors of the cells in S and T are represented by N^2 characters S_{i,j} (1\leq i,j\leq N) and M^2 characters T_{i,j} (1\leq i,j\leq M), respectively. In grid S, cell (i,j) is white if S_{i,j} is ., and black if S_{i,j} is #. The same applies for grid T.

Find T within S. More precisely, output integers a and b (1 \leq a,b \leq N-M+1) that satisfy the following condition:

  • S_{a+i-1,b+j-1} = T_{i,j} for every i,j (1\leq i,j \leq M).

Constraints

  • 1 \leq M \leq N \leq 50
  • N and M are integers.
  • Each of S_{i,j} and T_{i,j} is . or #.
  • There is exactly one pair (a,b) satisfying the condition.

Input

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

N M
S_{1,1}S_{1,2}\dots S_{1,N}
S_{2,1}S_{2,2}\dots S_{2,N}
\vdots
S_{N,1}S_{N,2}\dots S_{N,N}
T_{1,1}T_{1,2}\dots T_{1,M}
T_{2,1}T_{2,2}\dots T_{2,M}
\vdots
T_{M,1}T_{M,2}\dots T_{M,M}

Output

Print a and b in this order, separated by a space on one line.


Sample Input 1

3 2
#.#
..#
##.
.#
#.

Sample Output 1

2 2

The 2 \times 2 subgrid of S from the 2nd to the 3rd row and from the 2nd to the 3rd column matches T.


Sample Input 2

2 1
#.
##
.

Sample Output 2

1 2
D - The Middle Day

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

AtCoder 国の暦では、一年は 1,2,\dots,M 番目の月の M か月からなり、そのうち i 番目の月は 1,2,\dots,D_i 番目の日の D_i 日からなります。
さらに、 AtCoder 国の一年の日数は奇数、即ち D_1+D_2+\dots+D_M は奇数です。
一年の真ん中の日は何番目の月の何番目の日か求めてください。
言い換えると、 1 番目の月の 1 番目の日を 1 日目としたときの (D_1+D_2+\dots+D_M+1)/2 日目が何番目の月の何番目の日かを求めてください。

制約

  • 入力は全て整数
  • 1 \le M \le 100
  • 1 \le D_i \le 100
  • D_1 + D_2 + \dots + D_M は奇数

入力

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

M
D_1 D_2 \dots D_M

出力

答えが a 番目の月の b 番目の日であるとき、以下の形式で出力せよ。

a b

入力例 1

12
31 28 31 30 31 30 31 31 30 31 30 31

出力例 1

7 2

この入力では、 1 年は 31+28+31+30+31+30+31+31+30+31+30+31=365 日からなります。
真ん中の日は (365+1)/2 = 183 日目であり、これを求めることを考えます。

  • 1,2,3,4,5,6 番目の月に含まれる日数の合計は 181 日です。
  • 7 番目の月の 1 番目の日は 182 日目です。
  • 7 番目の月の 2 番目の日は 183 日目です。

以上から、答えが 7 番目の月の 2 番目の日であることが分かります。


入力例 2

1
1

出力例 2

1 1

入力例 3

6
3 1 4 1 5 9

出力例 3

5 3

Score : 200 points

Problem Statement

In the calendar of AtCoderLand, a year consists of M months: month 1, month 2, \dots, month M. The i-th month consists of D_i days: day 1, day 2, \dots, day D_i.
Furthermore, the number of days in a year is odd, that is, D_1+D_2+\dots+D_M is odd.
Find what day of what month is the middle day of the year.
In other words, let day 1 of month 1 be the first day, and find a and b such that the ((D_1+D_2+\dots+D_M+1)/2)-th day is day b of month a.

Constraints

  • All input values are integers.
  • 1 \le M \le 100
  • 1 \le D_i \le 100
  • D_1 + D_2 + \dots + D_M is odd.

Input

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

M
D_1 D_2 \dots D_M

Output

Let the answer be day b of month a, and print it in the following format:

a b

Sample Input 1

12
31 28 31 30 31 30 31 31 30 31 30 31

Sample Output 1

7 2

In this input, a year consists of 31+28+31+30+31+30+31+31+30+31+30+31=365 days.
Let us find the middle day, which is the ((365+1)/2 = 183)-th day.

  • Months 1,2,3,4,5,6 contain a total of 181 days.
  • Day 1 of month 7 is the 182-th day.
  • Day 2 of month 7 is the 183-th day.

Thus, the answer is day 2 of month 7.


Sample Input 2

1
1

Sample Output 2

1 1

Sample Input 3

6
3 1 4 1 5 9

Sample Output 3

5 3
E - Mixture

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

N 種類の薬品 1,2,\dots,N があります。あなたの目標はこれらが全て混ざった状態にすることです。
0, 1 からなる長さ 2^N-1 の文字列 S が与えられます。この文字列は次の情報を表します。

  • まず、 1 種類以上の薬品が混ざった状態 i ( 1 \le i \le 2^N-1 ) を次のように定義する。
    • i を 2 進法で表記した際に下から k ( 1 \le k \le N ) 桁目が 1 である、またその時に限り、薬品 k が含まれている。
    • 例えば、 13 を 2 進法で表記すると 1101_{(2)} となるため、状態 13 は薬品 1,3,4 が混ざった状態を表現します。
  • S の i 文字目が 0 であるとき、状態 i は 安全 である。
  • S の i 文字目が 1 であるとき、状態 i は 危険 である。

あなたは次の操作を使って薬品を混ぜ合わせます。

  • まず、空の瓶を用意する。
  • 次に、以下を繰り返す。
    • まだ瓶に注いでいない薬品を 1 種類選択し、瓶に注ぐ。
    • この時、瓶の中で混ざった薬品が危険な状態であってはならない。

この操作によって全ての薬品が混ざった状態を作れるか判定してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • T は 1 以上 40000 以下の整数
  • N は 1 以上 18 以下の整数
  • S は 0, 1 からなる長さ 2^N-1 の文字列
  • ひとつの入力に含まれる |S| = 2^N-1 の総和は 5 \times 10^5 を超えない

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。

N
S

出力

T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。
各テストケースについて、全ての薬品が混ざった状態を作れるなら Yes 、作れないなら No と出力せよ。


入力例 1

5
3
0010000
3
0010110
1
1
2
100
4
001110010101110

出力例 1

Yes
No
No
Yes
Yes

この入力には 5 個のテストケースが含まれます。

1 番目のケースは次の通りです。

  • 3 種類の薬品が存在する。
  • 薬品 1,2 が混ざった状態 3 のみが危険で、他の状態は安全である。
  • 例えば、以下の手順で全ての薬品が混ざった状態を作ることができます。
    • はじめに、瓶に薬品 2 を注ぐ。瓶の中で薬品 2 のみが混ざっており、これは状態 2 なので安全である。
    • 次に、瓶に薬品 3 を注ぐ。瓶の中で薬品 2,3 が混ざっており、これは状態 6 なので安全である。
    • 最後に、瓶に薬品 1 を注ぐ。瓶の中で薬品 1,2,3 が混ざっており、これは状態 7 なので安全である。

2 番目のケースは次の通りです。

  • 3 種類の薬品が存在する。
  • 薬品 1,2 が混ざった状態 3 、薬品 1,3 が混ざった状態 5 、薬品 2,3 が混ざった状態 6 が危険で、他の状態は安全である。
  • このケースについて、全ての薬品が混ざった状態を作ることはできません。

3 番目のケースは次の通りです。

  • 1 種類の薬品が存在する。
  • 薬品 1 のみが混ざった状態 1 が危険であるため、全ての薬品が混ざった状態を作ることはできません。

Score : 350 points

Problem Statement

There are N types of chemicals 1,2,\dots,N. Your goal is to create a state where all of them are mixed.
You are given a string S of length 2^N-1 consisting of 0 and 1, which represents the following information:

  • First, define state i (1 \le i \le 2^N-1) where one or more types of chemicals are mixed as follows:
    • When the k-th digit (1 \le k \le N) from the bottom in the binary representation of i is 1, and only then, chemical k is included.
    • For example, 13 in binary is 1101_{(2)}, so state 13 represents a state where chemicals 1,3,4 are mixed.
  • When the i-th character of S is 0, state i is safe.
  • When the i-th character of S is 1, state i is dangerous.

You mix chemicals using the following operation:

  • First, prepare an empty bottle.
  • Next, repeat the following:
    • Choose one type of chemical that has not yet been poured into the bottle and pour it into the bottle.
    • At this time, the chemicals mixed in the bottle must not be in a dangerous state.

Determine whether this operation can create a state where all chemicals are mixed.

You are given T test cases; solve each of them.

Constraints

  • T is an integer between 1 and 40000, inclusive.
  • N is an integer between 1 and 18, inclusive.
  • S is a string of length 2^N-1 consisting of 0 and 1.
  • The sum of |S| = 2^N-1 in each input does not exceed 5 \times 10^5.

Input

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

N
S

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if it is possible to create a state where all chemicals are mixed, print Yes; otherwise, print No.


Sample Input 1

5
3
0010000
3
0010110
1
1
2
100
4
001110010101110

Sample Output 1

Yes
No
No
Yes
Yes

This input contains five test cases.

The 1st case is as follows:

  • There are three types of chemicals.
  • Only state 3 where chemicals 1,2 are mixed is dangerous, and the other states are safe.
  • For example, you can create a state where all chemicals are mixed with the following procedure:
    • First, pour chemical 2 into the bottle. Only chemical 2 is mixed in the bottle, which is state 2, so it is safe.
    • Next, pour chemical 3 into the bottle. Chemicals 2,3 are mixed in the bottle, which is state 6, so it is safe.
    • Finally, pour chemical 1 into the bottle. Chemicals 1,2,3 are mixed in the bottle, which is state 7, so it is safe.

The 2nd case is as follows:

  • There are three types of chemicals.
  • State 3 where chemicals 1,2 are mixed, state 5 where chemicals 1,3 are mixed, and state 6 where chemicals 2,3 are mixed are dangerous, and the other states are safe.
  • For this case, it is impossible to create a state where all chemicals are mixed.

The 3rd case is as follows:

  • There is one type of chemical.
  • Since state 1 where only chemical 1 is mixed is dangerous, it is impossible to create a state where all chemicals are mixed.
F - Rotate and Sum Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

Q 個のクエリが与えられるので順に処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。

  • 1 c: A の先頭の要素を末尾に移動させる操作を c 回行う。
  • 2 l r: \displaystyle \sum_{i=l}^r A_i の値を出力する。

制約

  • 1\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • 1\le A_i \le 10^9
  • 1\le c\le N
  • 1\le l\le r \le N
  • 2 つ目の形式のクエリが 1 つ以上存在する
  • 入力される値は全て整数

入力

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

N Q
A_1 A_2 \ldots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリ \text{query}_i は以下の 2 種類のいずれかの形式で与えられる。

1 c
2 l r

出力

問題文の指示に従って 2 つ目の形式のクエリへの答えを改行区切りで出力せよ。


入力例 1

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

出力例 1

8
9

各クエリは以下のように処理されます。

  • 1 つ目のクエリ: A_1+A_2+A_3=3+1+4=8 なので 8 を出力します。
  • 2 つ目のクエリ: A=(3,1,4,5) が A=(1,4,5,3) に変化します。
  • 3 つ目のクエリ: A_2+A_3=4+5=9 なので 9 を出力します。

入力例 2

5 7
1 2 4 8 16
2 1 5
1 4
1 5
2 1 5
2 2 4
1 1
2 3 3

出力例 2

31
31
7
4

Score : 350 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

Process Q queries in order. There are two types of queries, given in the following formats:

  • 1 c: Perform the operation of moving the first element of A to the end c times.
  • 2 l r: Output the value of \displaystyle \sum_{i=l}^r A_i.

Constraints

  • 1\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • 1\le A_i \le 10^9
  • 1\le c\le N
  • 1\le l\le r \le N
  • At least one query of the second type exists.
  • All input values are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query \text{query}_i is given in one of the following two formats:

1 c
2 l r

Output

Following the instructions in the problem statement, output the answers to the second type queries, separated by newlines.


Sample Input 1

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

Sample Output 1

8
9

Each query is processed as follows:

  • First query: A_1+A_2+A_3=3+1+4=8, so output 8.
  • Second query: A=(3,1,4,5) changes to A=(1,4,5,3).
  • Third query: A_2+A_3=4+5=9, so output 9.

Sample Input 2

5 7
1 2 4 8 16
2 1 5
1 4
1 5
2 1 5
2 2 4
1 1
2 3 3

Sample Output 2

31
31
7
4
G - Square Permutation

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

数字のみからなる、長さ N の文字列 S が与えられます。

S を並べ替えてできる文字列を十進法の整数として解釈したもののうち、平方数であるようなものがいくつあるか求めてください。

より厳密には、次のようになります。

S の先頭から i 番目 (1\leq i\leq N) の数字に対応する数を s _ i とします。

(1, \ldots, N) の順列 P=(p _ 1,p _ 2,\ldots,p _ N) によって \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} と書ける整数のうち、平方数であるようなものがいくつあるか求めてください。

制約

  • 1\leq N\leq 13
  • S は数字のみからなる長さ N の文字列
  • N は整数

入力

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

N
S

出力

答えを 1 行で出力せよ。


入力例 1

4
4320

出力例 1

2

P=(4,2,3,1) とすると、s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2 となります。
P=(3,2,4,1) とすると、s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2 となります。

これら以外の並べ替え方では平方数にならないため、2 を出力してください。


入力例 2

3
010

出力例 2

2

P=(1,3,2) もしくは P=(3,1,2) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2 となります。
P=(2,1,3) もしくは P=(2,3,1) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2 となります。

これら以外の並べ替え方では平方数にならないため、2 を出力してください。 異なる並べ替え方でも、並べ替えた結果の数が同じなら 1 つと数えることに注意してください。


入力例 3

13
8694027811503

出力例 3

840

Score : 425 points

Problem Statement

You are given a string S of length N consisting of digits.

Find the number of square numbers that can be obtained by interpreting a permutation of S as a decimal integer.

More formally, solve the following.

Let s _ i be the number corresponding to the i-th digit (1\leq i\leq N) from the beginning of S.

Find the number of square numbers that can be represented as \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} with a permutation P=(p _ 1,p _ 2,\ldots,p _ N) of (1, \dots, N).

Constraints

  • 1\leq N\leq 13
  • S is a string of length N consisting of digits.
  • N is an integer.

Input

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

N
S

Output

Print the answer in a single line.


Sample Input 1

4
4320

Sample Output 1

2

For P=(4,2,3,1), we have s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2.
For P=(3,2,4,1), we have s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2.

No other permutations result in square numbers, so you should print 2.


Sample Input 2

3
010

Sample Output 2

2

For P=(1,3,2) or P=(3,1,2), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2.
For P=(2,1,3) or P=(2,3,1), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2.

No other permutations result in square numbers, so you should print 2. Note that different permutations are not distinguished if they result in the same number.


Sample Input 3

13
8694027811503

Sample Output 3

840
H - King Bombee

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 頂点 M 辺の単純無向グラフが与えられます。このグラフの頂点には 1 から N の番号が付けられており、辺には 1 から M の番号が付けられています。辺 i は頂点 U_i と頂点 V_i の間を結んでいます。

整数 K, S, T, X が与えられます。以下の条件を満たす数列 A = (A_0, A_1, \dots, A_K) は何通りありますか?

  • A_i は 1 以上 N 以下の整数
  • A_0 = S
  • A_K = T
  • 頂点 A_i と頂点 A_{i + 1} の間を直接結ぶ辺が存在する
  • 数列 A の中に整数 X\ (X≠S,X≠T) は偶数回出現する ( 0 回でも良い)

ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割ったあまりを求めてください。

制約

  • 入力は全て整数
  • 2≤N≤2000
  • 1≤M≤2000
  • 1≤K≤2000
  • 1≤S,T,X≤N
  • X≠S
  • X≠T
  • 1≤U_i<V_i≤N
  • i≠j ならば (U_i, V_i)≠(U_j, V_j)

入力

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

N M K S T X
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを 998244353 で割ったあまりを出力せよ。


入力例 1

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

出力例 1

4
  • (1, 2, 1, 2, 3)
  • (1, 2, 3, 2, 3)
  • (1, 4, 1, 4, 3)
  • (1, 4, 3, 4, 3)

の 4 個が条件を満たします。(1, 2, 3, 4, 3) や (1, 4, 1, 2, 3) は 2 が奇数回出現するため、条件を満たしません。


入力例 2

6 5 10 1 2 3
2 3
2 4
4 6
3 6
1 5

出力例 2

0

グラフは連結であるとは限りません。


入力例 3

10 15 20 4 4 6
2 6
2 7
5 7
4 5
2 4
3 7
1 7
1 4
2 9
5 10
1 3
7 8
7 9
1 6
1 2

出力例 3

952504739

998244353 で割ったあまりを求めてください。

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered from 1 through N, and the edges are numbered from 1 through M. Edge i connects Vertex U_i and Vertex V_i.

You are given integers K, S, T, and X. How many sequences A = (A_0, A_1, \dots, A_K) are there satisfying the following conditions?

  • A_i is an integer between 1 and N (inclusive).
  • A_0 = S
  • A_K = T
  • There is an edge that directly connects Vertex A_i and Vertex A_{i+1}.
  • Integer X\ (X≠S,X≠T) appears even number of times (possibly zero) in sequence A.

Since the answer can be very large, find the answer modulo 998244353.

Constraints

  • All values in input are integers.
  • 2≤N≤2000
  • 1≤M≤2000
  • 1≤K≤2000
  • 1≤S,T,X≤N
  • X≠S
  • X≠T
  • 1≤U_i<V_i≤N
  • If i ≠ j, then (U_i, V_i) ≠ (U_j, V_j).

Input

Input is given from Standard Input in the following format:

N M K S T X
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer modulo 998244353.


Sample Input 1

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

Sample Output 1

4

The following 4 sequences satisfy the conditions:

  • (1, 2, 1, 2, 3)
  • (1, 2, 3, 2, 3)
  • (1, 4, 1, 4, 3)
  • (1, 4, 3, 4, 3)

On the other hand, (1, 2, 3, 4, 3) and (1, 4, 1, 2, 3) do not, since there are odd number of occurrences of 2.


Sample Input 2

6 5 10 1 2 3
2 3
2 4
4 6
3 6
1 5

Sample Output 2

0

The graph is not necessarily connected.


Sample Input 3

10 15 20 4 4 6
2 6
2 7
5 7
4 5
2 4
3 7
1 7
1 4
2 9
5 10
1 3
7 8
7 9
1 6
1 2

Sample Output 3

952504739

Find the answer modulo 998244353.

I - Teleporter Setting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 個の町と M 個のテレポーターがあり、 町は町 1, 町 2, \ldots, 町N と番号づけられています。
それぞれのテレポーターは 2 つの町を双方向に結んでおり、テレポーターを使用する事によってその 2 つの町の間を 1 分で移動することができます。

i 番目のテレポーターは町 U_i と町 V_i を双方向に結んでいますが、 いくつかのテレポーターについては結ぶ町の片方が決まっておらず、 U_i=0 のときそのテレポーターが結ぶ町の片方は町 V_i であるが、 もう片方が未定であることを意味します。

i=1,2,\ldots,N それぞれについて、次の問題を解いてください。

結ぶ町の片方が未定となっているテレポーターの結ぶ先をすべて町 i とする。 この時に町 1 から町 N まで移動するのに最小で何分かかるか求めよ。 町 1 から町 N までテレポーターのみを使って移動するのが不可能な場合は -1 を出力せよ。

制約

  • 2 \leq N \leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 0\leq U_i<V_i\leq N
  • i \neq j ならば (U_i,V_i)\neq (U_j,V_j)
  • 入力は全て整数

入力

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

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

N 個の整数を空白区切りで出力せよ。 ここで、k 番目の整数は i=k とした時の問題に対する答えである.


入力例 1

3 2
0 2
1 2

出力例 1

-1 -1 2

結ぶ先が未定となっているテレポーターの結び先を町 1 としたとき、
1 番目と 2 番目のテレポーターはともに町 1 と町 2 を結びます。 このとき、町 1 から町 3 への移動はできません。

結ぶ先が未定となっているテレポーターの結び先を町 2 としたとき、
1 番目のテレポーターは町 2 同士を、 2 番目のテレポーターは町 1 と町 2 を結びます。 このときもやはり、町 1 から町 3 への移動はできません。

結ぶ先が未定となっているテレポーターの結び先を町 3 としたとき、
1 番目のテレポーターは町 3 と町 2 を、 2 番目のテレポーターは町 1 と町 2 を結びます。
この時、次のようにして町 1 から町 3 へ 2 分で移動できます。

  • 2 番目のテレポーターを使用し、町 1 から町 2 まで移動する。
  • 1 番目のテレポーターを使用し、町 2 から町 3 まで移動する。

よって、-1,-1,2 をこの順に出力します。

結ぶ先が未定となっているテレポーターの結び先によっては、 同じ町同士を結ぶテレポーターが存在する可能性や、 ある 2 つの町を結ぶテレポーターが複数存在する可能性がある事に注意してください。


入力例 2

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

出力例 2

3 3 3 3 2

Score : 500 points

Problem Statement

There are N towns numbered Town 1, Town 2, \ldots, Town N.
There are also M Teleporters, each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute.

The i-th Teleporter connects Town U_i and Town V_i bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; U_i=0 means that one of the towns the i-th Teleporter connects is Town V_i, but the other end is undetermined.

For i=1,2,\ldots,N, answer the following question.

When the Teleporters with undetermined ends are all determined to be connected to Town i, how many minutes is required at minimum to travel from Town 1 to Town N? If it is impossible to travel from Towns 1 to N using Teleporters only, print -1 instead.

Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1\leq M\leq 3\times 10^5
  • 0\leq U_i<V_i\leq N
  • If i \neq j, then (U_i,V_i)\neq (U_j,V_j).
  • All values in input are integers.

Input

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

Output

Print N integers, with spaces in between. The k-th integer should be the answer to the question when i=k.


Sample Input 1

3 2
0 2
1 2

Sample Output 1

-1 -1 2

When the Teleporters with an undetermined end are all determined to be connected to Town 1,
the 1-st and the 2-nd Teleporters both connect Towns 1 and 2. Then, it is impossible to travel from Town 1 to Town 3.

When the Teleporters with an undetermined end are all determined to be connected to Town 2,
the 1-st Teleporter connects Town 2 and itself, and the 2-nd one connects Towns 1 and 2. Again, it is impossible to travel from Town 1 to Town 3.

When the Teleporters with an undetermined end are all determined to be connected to Town 3,
the 1-st Teleporter connects Town 3 and Town 2, and the 2-nd one connects Towns 1 and 2. In this case, we can travel from Town 1 to Town 3 in two minutes.

  • Use the 2-nd Teleporter to travel from Town 1 to Town 2.
  • Use the 1-st Teleporter to travel from Town 2 to Town 3.

Therefore, -1,-1, and 2 should be printed in this order.

Note that, depending on which town the Teleporters with an undetermined end are connected to, there may be a Teleporter that connects a town and itself, or multiple Teleporters that connect the same pair of towns.


Sample Input 2

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

Sample Output 2

3 3 3 3 2