A - Dice

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

6 つの面を持つサイコロが 3 個あります。
どのサイコロも、面には 1,2,3,4,5,6 が書かれています。

これらのサイコロを同時に振ったとき、出た目の合計が X になることはありますか?

制約

  • X1 以上 20 以下の整数

入力

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

X

出力

出た目の合計が X になることがあれば Yes 、なければ No を出力せよ。


入力例 1

15

出力例 1

Yes

例えば、出目が 4,5,6 のとき、合計は 15 となります。


入力例 2

2

出力例 2

No

Score : 100 points

Problem Statement

There are three dice, each with six faces.
Each die has 1,2,3,4,5,6 written on its faces.

When these dice are rolled simultaneously, is it possible for the sum of the rolled values to be X?

Constraints

  • X is an integer between 1 and 20, inclusive.

Input

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

X

Output

If it is possible for the sum of the rolled values to be X, output Yes; otherwise, output No.


Sample Input 1

15

Sample Output 1

Yes

For example, if the rolled values are 4,5,6, the sum is 15.


Sample Input 2

2

Sample Output 2

No
B - 456

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

6 つの面を持つサイコロが 3 個あります。
i 個目のサイコロの j 個目の面には A_{i,j} が書かれています。
どのサイコロも、各面が出る確率は \frac{1}{6} です。

これらのサイコロを同時に振ったとき、4,5,6 の書かれた目が 1 つずつ出る確率を求めてください。

制約

  • A_{i,j}1 以上 6 以下の整数

入力

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}

出力

答えを出力せよ。 真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解とみなされる。


入力例 1

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

出力例 1

0.0277777778

求める確率は \frac{1}{36} です。 0.027778 などの出力でも正解となります。


入力例 2

4 5 6 4 5 6
4 4 5 5 6 6
6 5 4 4 5 6

出力例 2

0.2222222222

求める確率は \frac{2}{9} です。

Score : 200 points

Problem Statement

There are three dice, each with six faces.
The j-th face of the i-th die has A_{i,j} written on it.
For each die, each face comes up with probability \frac{1}{6}.

When these dice are rolled simultaneously, find the probability that the values 4,5,6 each appear exactly once.

Constraints

  • A_{i,j} is an integer between 1 and 6, inclusive.

Input

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

A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1,5} A_{1,6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2,5} A_{2,6}
A_{3,1} A_{3,2} A_{3,3} A_{3,4} A_{3,5} A_{3,6}

Output

Output the answer. The answer is considered correct if the absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

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

Sample Output 1

0.0277777778

The desired probability is \frac{1}{36}. An output such as 0.027778 is also accepted.


Sample Input 2

4 5 6 4 5 6
4 4 5 5 6 6
6 5 4 4 5 6

Sample Output 2

0.2222222222

The desired probability is \frac{2}{9}.

C - Not Adjacent

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

a, b, c からなる文字列 S が与えられます。

S の空でない部分文字列であって、同じ文字が隣り合わないものの個数を 998244353 で割った余りを求めてください。

ただし、 2 つの部分文字列が文字列として一致しても、取り出す位置が異なるならば区別するものとします。

部分文字列とは S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ababc の部分文字列ですが、acabc の部分文字列ではありません。

制約

  • Sa, b, c からなる長さ 1 以上 3 \times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abbc

出力例 1

6

同じ文字が隣合わない部分文字列は以下の 6 個です。

  • a ( S1 文字目から 1 文字目まで)
  • b ( S2 文字目から 2 文字目まで)
  • b ( S3 文字目から 3 文字目まで)
  • c ( S4 文字目から 4 文字目まで)
  • ab ( S1 文字目から 2 文字目まで)
  • bc ( S3 文字目から 4 文字目まで)

2 番目と 3 番目のもののように、文字列として一致しても、取り出す位置が異なるならば区別することに注意してください。


入力例 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

出力例 2

760

Score : 300 points

Problem Statement

You are given a string S consisting of a, b, c.

Find the number of non-empty substrings of S in which no two adjacent characters are the same, modulo 998244353.

Two substrings are considered distinct if they are taken from different positions, even if they are identical as strings.

What is a substring? A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab is a substring of abc, but ac is not a substring of abc.

Constraints

  • S is a string of length between 1 and 3 \times 10^5, inclusive, consisting of a, b, c.

Input

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

S

Output

Output the answer.


Sample Input 1

abbc

Sample Output 1

6

The substrings in which no two adjacent characters are the same are the following six:

  • a (from the 1st to the 1st character of S)
  • b (from the 2nd to the 2nd character of S)
  • b (from the 3rd to the 3rd character of S)
  • c (from the 4th to the 4th character of S)
  • ab (from the 1st to the 2nd character of S)
  • bc (from the 3rd to the 4th character of S)

Note that, as with the 2nd and 3rd entries, two substrings are considered distinct if they are taken from different positions, even if they are identical as strings.


Sample Input 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

Sample Output 2

760
D - Not Adjacent 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

a, b, c からなる文字列 S が与えられます。

S の空でない部分列であって、同じ文字が隣り合わないものの個数を 998244353 で割った余りを求めてください。

ただし、 2 つの部分列が文字列として一致しても、取り出す位置が異なるならば区別するものとします。

部分列とは S部分列とは、S から 0 文字以上を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。 例えば、ab, acabc の部分列ですが、ca, bbabc の部分列ではありません。

制約

  • Sa, b, c からなる長さ 1 以上 3 \times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abbc

出力例 1

11

同じ文字が隣合わない部分列は以下の 11 個です。

  • a ( S1 文字目)
  • b ( S2 文字目)
  • b ( S3 文字目)
  • c ( S4 文字目)
  • ab ( S1,2 文字目)
  • ab ( S1,3 文字目)
  • ac ( S1,4 文字目)
  • bc ( S2,4 文字目)
  • bc ( S3,4 文字目)
  • abc ( S1,2,4 文字目)
  • abc ( S1,3,4 文字目)

2 番目と 3 番目のもののように、文字列として一致しても、取り出す位置が異なるならば区別することに注意してください。


入力例 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

出力例 2

378217423

998244353 で割った余りを出力してください。

Score : 400 points

Problem Statement

You are given a string S consisting of a, b, c.

Find the number of non-empty subsequences of S in which no two adjacent characters are the same, modulo 998244353.

Two subsequences are considered distinct if they are taken from different positions, even if they are identical as strings.

What is a subsequence? A subsequence of S is a string obtained by removing zero or more characters from S and concatenating the remaining characters in their original order. For example, ab, ac are subsequences of abc, but ca, bb are not subsequences of abc.

Constraints

  • S is a string of length between 1 and 3 \times 10^5, inclusive, consisting of a, b, c.

Input

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

S

Output

Output the answer.


Sample Input 1

abbc

Sample Output 1

11

The subsequences in which no two adjacent characters are the same are the following 11:

  • a (the 1st character of S)
  • b (the 2nd character of S)
  • b (the 3rd character of S)
  • c (the 4th character of S)
  • ab (the 1st, 2nd characters of S)
  • ab (the 1st, 3rd characters of S)
  • ac (the 1st, 4th characters of S)
  • bc (the 2nd, 4th characters of S)
  • bc (the 3rd, 4th characters of S)
  • abc (the 1st, 2nd, 4th characters of S)
  • abc (the 1st, 3rd, 4th characters of S)

Note that, as with the 2nd and 3rd entries, two subsequences are considered distinct if they are taken from different positions, even if they are identical as strings.


Sample Input 2

cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca

Sample Output 2

378217423

Output the count modulo 998244353.

E - Endless Holidays

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

AtCoder 王国には N 個の都市があり、それぞれ都市 1,2,\dots,N と呼ばれています。 また都市同士を双方向に結ぶ M 本の道路があり、i 番目の道路は都市 U_i と都市 V_i を結んでいます。 どの都市間もいくつかの道路を辿ることで行き来可能です。

AtCoder 王国では一週間が W 日からなります。 一週間は曜日 1,2,\dots,W と進んでいき、曜日 W の次の日は曜日 1 に戻ります。

都市ごとにいくつかの曜日が休日となっています。 都市 i の休日の情報は長さ W の文字列 S_i で与えられ、S_ij 文字目が o のとき曜日 j が休日であることを、x のとき曜日 j が平日であることを意味します。

高橋君は曜日 1 の日の昼に、好きな都市を選んでそこを訪問します。 それ以降毎日夜に一度、今いる都市にとどまるか、道路で直接結ばれた都市のいずれかに移動することを繰り返します。 毎日昼の時点でいる都市が休日であるように移動を続けることが可能ならば Yes を、不可能ならば No を出力してください。

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

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq U_i \lt V_i \leq N
  • どの都市間もいくつかの道路を辿ることで行き来可能である
  • 2 \leq W \leq 10
  • T,N,M,U_i,V_i,W は整数
  • S_io, x からなる長さ W の文字列
  • 全てのテストケースにおける N の総和は 10^5 以下
  • 全てのテストケースにおける M の総和は 10^5 以下

入力

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

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

ここで \mathrm{case}_ii 番目のテストケースの入力を意味する。各テストケースは以下の形式で与えられる。

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
W
S_1
S_2
\vdots
S_N

出力

T 行出力せよ。i 行目には i 番目のテストケースについての答えを出力せよ。


入力例 1

3
4 4
1 2
1 4
2 4
2 3
3
xxo
xox
oxo
oxx
1 0
4
oooo
5 5
1 4
2 3
4 5
3 4
2 5
7
oxxxxxx
xxoxxxo
xxxoxox
xoxxoxx
oxxxoxx

出力例 1

Yes
Yes
No

一つ目のテストケースについて、例えば都市 4 \to 2 \to 1 \to 4 \to 2 \to 1 \to \cdots と移動を繰り返すことで条件を満たすことができます。 他にも都市 3 \to 2 \to 3 \to 3 \to 2 \to 3 \to \cdots と移動を繰り返すことでも条件を満たすことができます。

二つ目のテストケースについて、都市 1 にとどまり続けることで条件を満たすことができます。

三つ目のテストケースについて、条件を満たすように移動を繰り返すことはできません。

Score : 450 points

Problem Statement

The Kingdom of AtCoder has N cities, called city 1,2,\dots,N. There are M bidirectional roads connecting pairs of cities, where the i-th road connects cities U_i and V_i. Any pair of cities can be reached from each other by traversing some roads.

In the Kingdom of AtCoder, a week consists of W days. A week proceeds through days 1,2,\dots,W, and the day after day W is day 1.

Each city has certain days of the week that are holidays. The holiday information for city i is given as a string S_i of length W: if the j-th character of S_i is o, day j is a holiday; if it is x, day j is a weekday.

Takahashi chooses a city he likes and visits it at noon on day 1. Each night thereafter, he repeatedly chooses to either stay in his current city or move to a city directly connected by a road. Output Yes if it is possible for him to keep moving so that the city he is in at noon every day is a holiday, and No otherwise.

T test cases are given; solve each of them.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq U_i \lt V_i \leq N
  • Any pair of cities can be reached from each other by traversing some roads.
  • 2 \leq W \leq 10
  • T,N,M,U_i,V_i,W are integers.
  • S_i is a string of length W consisting of o, x.
  • The sum of N over all test cases is at most 10^5.
  • The sum of M over all test cases is at most 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

Here, \mathrm{case}_i denotes the input for the i-th test case. Each test case is given in the following format:

N M
U_1 V_1
U_2 V_2
\vdots
U_M V_M
W
S_1
S_2
\vdots
S_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
4 4
1 2
1 4
2 4
2 3
3
xxo
xox
oxo
oxx
1 0
4
oooo
5 5
1 4
2 3
4 5
3 4
2 5
7
oxxxxxx
xxoxxxo
xxxoxox
xoxxoxx
oxxxoxx

Sample Output 1

Yes
Yes
No

For the first test case, for example, the condition can be satisfied by repeatedly moving along cities 4 \to 2 \to 1 \to 4 \to 2 \to 1 \to \cdots. Alternatively, the condition can also be satisfied by repeatedly moving along cities 3 \to 2 \to 3 \to 3 \to 2 \to 3 \to \cdots.

For the second test case, the condition can be satisfied by staying in city 1 indefinitely.

For the third test case, it is impossible to keep moving while satisfying the condition.

F - Plan Holidays

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

高橋君は N 日間のスケジュールを決めようとしています。初期状態ではどの日も休日ではありません。

以下のいずれかの操作を好きな回数繰り返すことができます。

  • 1 以上 N 以下の整数 i を選び、i 日目を休日にする。この操作にはコストが A_i かかる。
  • 2 以上 N-1 以下の整数 i のうち i-1 日目と i+1 日目がすでにどちらも休日であるような i を選び、i 日目を休日にする。この操作にはコストはかからない。

連続する K 日以上の休日を作るために必要なコストの総和の最小値を求めてください。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数
  • 全てのテストケースにおける N の総和は 2\times 10^5 以下

入力

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

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

ここで \mathrm{case}_ii 番目のテストケースの入力を意味する。各テストケースは以下の形式で与えられる。

N K
A_1 A_2 \dots A_N

出力

T 行出力せよ。i 行目には i 番目のテストケースについての答えを出力せよ。


入力例 1

3
5 2
3 1 4 1 5
6 4
24 3 22 39 4 29
15 7
220651272 302798780 874479994 657822311 613294668 479624013 241168404 610547619 762548286 256160531 823041612 951553052 226556081 649525901 153805947

出力例 1

2
29
1902064780

一つ目のテストケースについて、以下のように操作すると 2 日以上連続する休日を作ることができます。

  • 2 日目を一種類目の操作で休日にする。コストが 1 かかる。
  • 4 日目を一種類目の操作で休日にする。コストが 1 かかる。
  • 3 日目を二種類目の操作で休日にする。コストはかからない。

この操作方法のコストの総和は 2 です。コスト 2 未満で 2 日以上連続する休日を作ることはできないため、2 を出力してください。

Score : 525 points

Problem Statement

Takahashi is trying to decide his schedule for N days. Initially, none of the days are holidays.

He can repeat either of the following operations any number of times:

  • Choose an integer i between 1 and N, inclusive, and make day i a holiday. This operation costs A_i.
  • Choose an integer i between 2 and N-1, inclusive, such that both day i-1 and day i+1 are already holidays, and make day i a holiday. This operation is free.

Find the minimum total cost required to create a consecutive block of K or more holidays.

T test cases are given; solve each of them.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All input values are integers.
  • The sum of N over all test cases is at most 2\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

Here, \mathrm{case}_i denotes the input for the i-th test case. Each test case is given in the following format:

N K
A_1 A_2 \dots A_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

3
5 2
3 1 4 1 5
6 4
24 3 22 39 4 29
15 7
220651272 302798780 874479994 657822311 613294668 479624013 241168404 610547619 762548286 256160531 823041612 951553052 226556081 649525901 153805947

Sample Output 1

2
29
1902064780

For the first test case, a consecutive block of at least two holidays can be created by performing operations as follows:

  • Make day 2 a holiday using the first type of operation. This costs 1.
  • Make day 4 a holiday using the first type of operation. This costs 1.
  • Make day 3 a holiday using the second type of operation. This is free.

The total cost of this sequence of operations is 2. It is impossible to create a consecutive block of two or more holidays with a cost less than 2, so output 2.

G - Count Holidays

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

高橋君はこれから N 日間についての勤務予定を、各日を勤務日か休日のどちらかに設定することで作成しようとしています。

勤務に関する条件を表す長さ N の文字列 S が与えられます。 Si 文字目が x のとき i 日目は必ず勤務日にしなければなりません。. のとき i 日目は勤務日と休日のどちらにすることもできます。

条件を満たす勤務予定は S に含まれる . の個数を q としたとき全部で 2^q 通り考えられます。 k=1,2,\dots,N について次の問題を解いてください。

条件を満たす勤務予定 2^q 通りのうち、最長の連続する休日がちょうど k 日であるような決め方の総数を 998244353 で割ったあまりを求めてください。

制約

  • N1 以上 2 \times 10^5 以下の整数
  • S., x からなる長さ N の文字列

入力

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

N
S

出力

N 行出力せよ。i 行目には k=i のときの答えを出力せよ。


入力例 1

5
.x...

出力例 1

9
4
2
0
0

休日を o で表すとき、条件を満たす勤務予定はそれぞれ以下のようになります。

  • k=1oxxxx, oxoxx, oxoxo, oxxox, oxxxo, xxoxx, xxoxo, xxxox, xxxxo
  • k=2oxoox, oxxoo, xxoox, xxxoo
  • k=3oxooo, xxooo

入力例 2

7
.......

出力例 2

33
47
27
12
5
2
1

入力例 3

20
.....x...x..........

出力例 3

9359
75312
94664
46840
23680
7168
3072
1280
512
256
0
0
0
0
0
0
0
0
0
0

Score : 600 points

Problem Statement

Takahashi is creating a work schedule for N days by designating each day as a workday or a holiday.

You are given a string S of length N representing the constraints on work days. If the i-th character of S is x, day i must be a workday. If it is ., day i can be either a workday or a holiday.

There are 2^q valid work schedules satisfying the constraints, where q is the number of . characters in S. For each k=1,2,\dots,N, solve the following problem:

Among the 2^q valid work schedules satisfying the constraints, find the number, modulo 998244353, of schedules in which the longest consecutive block of holidays is exactly k days.

Constraints

  • N is an integer between 1 and 2 \times 10^5, inclusive.
  • S is a string of length N consisting of ., x.

Input

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

N
S

Output

Output N lines. The i-th line should contain the answer for k=i.


Sample Input 1

5
.x...

Sample Output 1

9
4
2
0
0

Denoting holidays as o, the valid work schedules are as follows:

  • k=1: oxxxx, oxoxx, oxoxo, oxxox, oxxxo, xxoxx, xxoxo, xxxox, xxxxo
  • k=2: oxoox, oxxoo, xxoox, xxxoo
  • k=3: oxooo, xxooo

Sample Input 2

7
.......

Sample Output 2

33
47
27
12
5
2
1

Sample Input 3

20
.....x...x..........

Sample Output 3

9359
75312
94664
46840
23680
7168
3072
1280
512
256
0
0
0
0
0
0
0
0
0
0