A - Seats

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

配点 : 100

問題文

N 個の座席が並んでおり、座席には 1, 2, \ldots, N の番号が付けられています。

座席の状態は #, . からなる長さ N の文字列 S によって与えられます。Si 文字目が # のとき座席 i には人が座っていることを表し、Si 文字目が . のとき座席 i には人が座っていないことを表します。

1 以上 N - 2 以下の整数 i であって、以下の条件を満たすものの個数を求めてください。

  • 座席 i, i + 2 には人が座っており、座席 i + 1 には人が座っていない

制約

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

入力

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

N
S

出力

答えを出力せよ。


入力例 1

6
#.##.#

出力例 1

2

i = 1, 4 が条件を満たすので、答えは 2 です。


入力例 2

1
#

出力例 2

0

入力例 3

9
##.#.#.##

出力例 3

3

Score : 100 points

Problem Statement

There are N seats in a row, numbered 1, 2, \ldots, N.

The state of the seats is given by a string S of length N consisting of # and .. If the i-th character of S is #, it means seat i is occupied; if it is ., seat i is unoccupied.

Find the number of integers i between 1 and N - 2, inclusive, that satisfy the following condition:

  • Seats i and i + 2 are occupied, and seat i + 1 is unoccupied.

Constraints

  • N is an integer satisfying 1 \leq N \leq 2 \times 10^5.
  • S is a string of length N consisting of # and ..

Input

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

N
S

Output

Print the answer.


Sample Input 1

6
#.##.#

Sample Output 1

2

i = 1 and 4 satisfy the condition, so the answer is 2.


Sample Input 2

1
#

Sample Output 2

0

Sample Input 3

9
##.#.#.##

Sample Output 3

3
B - Your First Judge

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

配点 : 100

問題文

文字列 S が与えられるので、この文字列が Hello,World! と完全に一致するなら AC 、そうでないなら WA と出力してください。

「完全に一致する」とは?文字列 AB が完全に一致するとは、文字列 AB の長さが等しく、かつ全ての 1 \le i \le |A| を満たす整数 i について A の先頭から i 文字目と B の先頭から i 文字目とが(英大文字か小文字かも含めて)一致することを指します。

制約

  • 1 \le |S| \le 15
  • S は英大小文字, ,, ! のみからなる

入力

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

S

出力

答えを出力せよ。


入力例 1

Hello,World!

出力例 1

AC

文字列 SHello,World! と完全に一致します。


入力例 2

Hello,world!

出力例 2

WA

先頭から 7 文字目の W が、 Hello,World! では大文字ですが S では小文字です。よって SHello,World! と一致しません。


入力例 3

Hello!World!

出力例 3

WA

Score : 100 points

Problem Statement

Given a string S, print AC if it perfectly matches Hello,World!; otherwise, print WA.

What is a perfect match?Strings A is said to perfectly match B when the length of A is equal to that of B, and the i-th character of A is the same as the i-th character of B for every integer i such that 1 \le i \le |A|.

Constraints

  • 1 \le |S| \le 15
  • S consists of English lowercase letters, English uppercase letters, ,, and !.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

Hello,World!

Sample Output 1

AC

The string S perfectly matches Hello,World!.


Sample Input 2

Hello,world!

Sample Output 2

WA

The seventh character from the beginning should be an uppercase W in Hello,World!, but S has a lowercase w in that position. Thus, S does not match Hello,World!.


Sample Input 3

Hello!World!

Sample Output 3

WA
C - Vertical Reading

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

配点 : 200

問題文

英小文字からなる文字列 ST が与えられます。

以下の条件を満たす 1 \leq c \leq w < |S| となる整数の組 cw が存在するか判定してください。ただし、 |S| は文字列 S の長さを表します。ここで、w|S| 未満である必要があることに注意してください。

  • S を先頭から順に w 文字毎に区切ったとき、長さが c 以上の文字列の c 文字目を順番に連結した文字列が T と一致する

制約

  • ST は英小文字からなる文字列
  • 1 \leq |T| \leq |S| \leq 100

入力

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

S T

出力

条件を満たすような 1 \leq c \leq w < |S| となる整数の組 cw が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

atcoder toe

出力例 1

Yes

S2 文字毎に区切ると以下のようになります。

at
co
de
r

区切った後、 2 文字以上の文字列の 2 文字目を取り出し連結させたときの文字列は、 toe となり T と一致します。よって、 Yes を出力します。


入力例 2

beginner r

出力例 2

No

w=|S| であることはないため、条件を満たすような 1 \leq c \leq w < |S| となる整数の組 cw は存在しません。よって、 No を出力します。


入力例 3

verticalreading agh

出力例 3

No

Score : 200 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters.

Determine if there exists a pair of integers c and w such that 1 \leq c \leq w < |S| and the following condition is satisfied. Here, |S| denotes the length of the string S. Note that w must be less than |S|.

  • If S is split at every w characters from the beginning, the concatenation of the c-th characters of the substrings of length at least c in order equals T.

Constraints

  • S and T are strings consisting of lowercase English letters.
  • 1 \leq |T| \leq |S| \leq 100

Input

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

S T

Output

Print Yes if there exists a pair of integers c and w such that 1 \leq c \leq w < |S| and the condition is satisfied, and No otherwise.


Sample Input 1

atcoder toe

Sample Output 1

Yes

If S is split at every two characters, it looks like this:

at
co
de
r

Then, the concatenation of the 2nd characters of the substrings of length at least 2 is toe, which equals T. Thus, print Yes.


Sample Input 2

beginner r

Sample Output 2

No

w=|S| is not allowed, and no pair of integers 1 \leq c \leq w < |S| satisfies the condition. Thus, print No.


Sample Input 3

verticalreading agh

Sample Output 3

No
D - Adjacency List

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

配点 : 200

問題文

1, \dots, N と番号付けられた N 個の都市と、都市間を結ぶ M 本の道路があります。
i \, (1 \leq i \leq M) 番目の道路は都市 A_i と都市 B_i を結んでいます。

以下の指示に従い、N 行にわたって出力してください。

  • 都市 i \, (1 \leq i \leq N) と道路で直接結ばれた都市が d_i 個あるとし、それらを昇順に都市 a_{i, 1}, \dots, a_{i, d_i} とおく。
  • i \, (1 \leq i \leq N) 行目には、d_i + 1 個の整数 d_i, a_{i, 1}, \dots, a_{i, d_i} を、この順番で空白区切りで出力せよ。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (i \neq j) ならば (A_i, B_i) \neq (A_j, B_j)
  • 入力される値は全て整数

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

問題文の指示に従い、N 行にわたって出力せよ。


入力例 1

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

出力例 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

都市 1 と道路で直接結ばれているのは都市 2, 3, 6 です。よって、d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6 であるので、1 行目には 3, 2, 3, 6 をこの順番で空白区切りで出力します。

a_{i, 1}, \dots, a_{i, d_i} は昇順に並んでいなければならないことに注意してください。例えば、1 行目に 3, 3, 2, 6 をこの順番で出力した場合、不正解となります。


入力例 2

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

出力例 2

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

Score : 200 points

Problem Statement

There are N cities numbered 1, \dots, N, and M roads connecting cities.
The i-th road (1 \leq i \leq M) connects city A_i and city B_i.

Print N lines as follows.

  • Let d_i be the number of cities directly connected to city i \, (1 \leq i \leq N), and those cities be city a_{i, 1}, \dots, city a_{i, d_i}, in ascending order.
  • The i-th line (1 \leq i \leq N) should contain d_i + 1 integers d_i, a_{i, 1}, \dots, a_{i, d_i} in this order, separated by spaces.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) if (i \neq j).
  • All values in the input are integers.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Print N lines as specified in the Problem Statement.


Sample Input 1

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

Sample Output 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

The cities directly connected to city 1 are city 2, city 3, and city 6. Thus, we have d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6, so you should print 3, 2, 3, 6 in the first line in this order, separated by spaces.

Note that a_{i, 1}, \dots, a_{i, d_i} must be in ascending order. For instance, it is unacceptable to print 3, 3, 2, 6 in the first line in this order.


Sample Input 2

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

Sample Output 2

4 2 3 4 5
4 1 3 4 5
4 1 2 4 5
4 1 2 3 5
4 1 2 3 4
E - Socks

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

配点 : 300

問題文

N 枚の靴下があります。i 枚目の靴下の色は A_i です。

あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?

  • まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。

制約

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

6
4 1 7 4 1 4

出力例 1

2

以下のようにして、2 回の操作を行うことができます。

  • 色が 1 である靴下を 2 枚選んでペアにする。
  • 色が 4 である靴下を 2 枚選んでペアにする。

このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。


入力例 2

1
158260522

出力例 2

0

入力例 3

10
295 2 29 295 29 2 29 295 2 29

出力例 3

4

Score : 300 points

Problem Statement

You have N socks. The color of the i-th sock is A_i.

You want to perform the following operation as many times as possible. How many times can it be performed at most?

  • Choose two same-colored socks that are not paired yet, and pair them.

Constraints

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print an integer representing the answer.


Sample Input 1

6
4 1 7 4 1 4

Sample Output 1

2

You can do the operation twice as follows.

  • Choose two socks with the color 1 and pair them.
  • Choose two socks with the color 4 and pair them.

Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.


Sample Input 2

1
158260522

Sample Output 2

0

Sample Input 3

10
295 2 29 295 29 2 29 295 2 29

Sample Output 3

4
F - 321-like Searcher

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

配点 : 300

問題文

以下の条件を満たす正整数 x321-like Number と呼びます。 この定義は A 問題と同様です。

  • x の各桁を上から見ると狭義単調減少になっている。
  • すなわち、xd 桁の整数だとすると、 1 \le i < d を満たす全ての整数 i について以下の条件を満たす。
    • ( x の上から i 桁目 ) > ( x の上から i+1 桁目 )

なお、 1 桁の正整数は必ず 321-like Number であることに注意してください。

例えば、 321,96410,1 は 321-like Number ですが、 123,2109,86411 は 321-like Number ではありません。

K 番目に小さい 321-like Number を求めてください。

制約

  • 入力は全て整数
  • 1 \le K
  • 321-like Number は K 個以上存在する

入力

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

K

出力

K 番目に小さい 321-like Number を整数として出力せよ。


入力例 1

15

出力例 1

32

321-like Number は小さいものから順に (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) です。
このうち 15 番目に小さいものは 32 です。


入力例 2

321

出力例 2

9610

入力例 3

777

出力例 3

983210

Score : 300 points

Problem Statement

A positive integer x is called a 321-like Number when it satisfies the following condition. This definition is the same as the one in Problem A.

  • The digits of x are strictly decreasing from top to bottom.
  • In other words, if x has d digits, it satisfies the following for every integer i such that 1 \le i < d:
    • (the i-th digit from the top of x) > (the (i+1)-th digit from the top of x).

Note that all one-digit positive integers are 321-like Numbers.

For example, 321, 96410, and 1 are 321-like Numbers, but 123, 2109, and 86411 are not.

Find the K-th smallest 321-like Number.

Constraints

  • All input values are integers.
  • 1 \le K
  • At least K 321-like Numbers exist.

Input

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

K

Output

Print the K-th smallest 321-like Number as an integer.


Sample Input 1

15

Sample Output 1

32

The 321-like Numbers are (1,2,3,4,5,6,7,8,9,10,20,21,30,31,32,40,\dots) from smallest to largest.
The 15-th smallest of them is 32.


Sample Input 2

321

Sample Output 2

9610

Sample Input 3

777

Sample Output 3

983210
G - Attend Many Events

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

配点 : 450

問題文

AtCoder Land には N 個の地点と M 個の道があります。i 個目の道は地点 a_ib_i を双方向に結び、通過するのに d_i 分かかります。

これから、AtCoder Land で K 個のイベントが開催されます。i 個目のイベントに参加するには、時刻 t_i 分に地点 c_i にいる必要があります。イベントにかかる時間は 0 分です。

あなたは、時刻 0 分に地点 1 にいます。適切に行動したとき、最大で何個のイベントに参加できるか求めてください。

制約

  • 1 \leq N \leq 1000
  • 0 \leq M \leq 1000
  • 1 \leq K \leq 1000
  • 1 \leq a_i < b_i \leq N
  • i \neq j なら (a_i, b_i) \neq (a_j, b_j)
  • 1 \leq d_i \leq 10^9
  • 1 \leq c_i \leq N
  • 1 \leq t_i \leq 10^9
  • i \neq j なら (c_i,t_i) \neq (c_j,t_j)
  • 入力はすべて整数

入力

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

N M K
a_1 b_1 d_1
a_2 b_2 d_2
\vdots
a_M b_M d_M
c_1 t_1
c_2 t_2
\vdots
c_K t_K

出力

答えを出力せよ。


入力例 1

3 3 3
1 2 1
1 3 3
2 3 7
1 8
2 2
3 7

出力例 1

2

たとえば、次のように行動することで、2 つのイベントに参加することができます。

  • はじめ、時刻 0 分に地点 1 にいる
  • 1 を通って時刻 1 分に地点 2 に到着する
  • 時刻 2 分に地点 2 でイベント 2 に参加する
  • 1 を通って時刻 3 分に地点 1 に到着する
  • 2 を通って時刻 6 分に地点 3 に到着する
  • 時刻 7 分に地点 3 でイベント 3 に参加する

3 つ以上のイベントに参加することはできないので、答えは 2 です。


入力例 2

1 0 2
1 1
1 100

出力例 2

2

道が存在しないため、地点 1 から動くことはできませんが、地点 1 で開催される 2 つのイベントに参加することができます。


入力例 3

5 8 10
1 2 149622151
1 4 177783960
4 5 118947237
1 3 33222944
1 5 295060863
3 5 110881471
2 3 34104208
3 4 273071547
2 650287940
4 839296263
3 462224593
1 492601449
4 384836991
1 191890310
5 576823355
3 782177068
3 404011431
1 818008580

出力例 3

6
H - King Bombee

実行時間制限: 2 sec / メモリ制限: 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_i1 以上 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 - Range Power Sum

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

配点 : 550

問題文

正整数 N,K および長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

\displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K の値を 998244353 で割った余りを求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 10
  • 0\leq A_i < 998244353
  • 入力は全て整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3 2
3 1 2

出力例 1

75

求める値は A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75 です。


入力例 2

1 10
0

出力例 2

0

入力例 3

10 5
91 59 85 60 57 72 12 3 27 16

出力例 3

428633385

998244353 で割った余りを求めることに注意してください。

Score : 550 points

Problem Statement

You are given positive integers N, K, and an integer sequence of length N: A = (A_1, A_2, \dots, A_N).

Find \displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K, modulo 998244353.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 10
  • 0 \leq A_i < 998244353
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3 2
3 1 2

Sample Output 1

75

The value is A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75.


Sample Input 2

1 10
0

Sample Output 2

0

Sample Input 3

10 5
91 59 85 60 57 72 12 3 27 16

Sample Output 3

428633385

Be sure to find the sum modulo 998244353.