A - 3,2,1,GO

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

配点 : 100

問題文

正の整数 N が与えられます。

N,N-1,\dots,1 をこの順にカンマ , で区切って出力してください。

制約

  • 1\leq N \leq 9
  • N は整数

入力

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

N

出力

N,N-1,\dots,1 をこの順にカンマ区切りで出力せよ。


入力例 1

9

出力例 1

9,8,7,6,5,4,3,2,1

入力例 2

5

出力例 2

5,4,3,2,1

入力例 3

1

出力例 3

1

Score : 100 points

Problem Statement

You are given a positive integer N.

Output N, N-1, \dots, 1 in this order, separated by commas ,.

Constraints

  • 1 \leq N \leq 9
  • N is an integer.

Input

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

N

Output

Output N, N-1, \dots, 1 in this order, separated by commas.


Sample Input 1

9

Sample Output 1

9,8,7,6,5,4,3,2,1

Sample Input 2

5

Sample Output 2

5,4,3,2,1

Sample Input 3

1

Sample Output 3

1
B - Split Ticketing

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

配点 : 200

問題文

N 個の駅 1,2,\dots,N があり、これらはこの順に西から東に一直線上に並んでいます。
AtCoder 鉄道の電車はこれら N 個の駅を通り、西から東に走っています。
1 \leq i \lt j \leq N を満たす任意の 2 整数 i,j について、駅 i から電車に乗って駅 j で降りるのにコストが C_{i,j} かかります。

以下のような 3 つの整数 a,b,c が存在するかを判定してください。

  • 1 \leq a \lt b \lt c \leq N
  • a から電車に乗って駅 c で降りるときにかかるコストよりも、駅 a から電車に乗って駅 b で降り、再度、駅 b から電車に乗って駅 c で降りるときにかかるコストの総和の方が小さい。

制約

  • 3 \leq N \leq 100
  • 1 \leq C_{i,j} \leq 10^9
  • 入力される値は全て整数

入力

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

N
C_{1,2} C_{1,3} \dots C_{1,N}
C_{2,3} \dots C_{2,N}
\vdots
C_{N-1,N}

出力

条件を満たす 3 整数 a,b,c が存在するならば Yes を、存在しないならば No1 行で出力せよ。


入力例 1

3
45 450
45

出力例 1

Yes

(a,b,c) として (1,2,3) を選ぶと、
C_{a,b}+C_{b,c}=C_{1,2}+C_{2,3}=45+45
C_{a,c}=C_{1,3}=450
なので、条件を満たします。


入力例 2

4
25 40 65
30 55
25

出力例 2

No

どのように (a,b,c) を選んでも、条件を満たしません。

Score : 200 points

Problem Statement

There are N stations 1, 2, \dots, N, arranged in a straight line from west to east in this order.
The AtCoder Railway train passes through these N stations and runs from west to east.
For any two integers i, j satisfying 1 \leq i \lt j \leq N, the cost of boarding the train at station i and getting off at station j is C_{i,j}.

Determine whether there exist three integers a, b, c such that:

  • 1 \leq a \lt b \lt c \leq N
  • The total cost of boarding the train at station a, getting off at station b, then boarding the train again at station b, and getting off at station c is less than the cost of boarding the train at station a and getting off at station c.

Constraints

  • 3 \leq N \leq 100
  • 1 \leq C_{i,j} \leq 10^9
  • All input values are integers.

Input

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

N
C_{1,2} C_{1,3} \dots C_{1,N}
C_{2,3} \dots C_{2,N}
\vdots
C_{N-1,N}

Output

If there exist three integers a, b, c satisfying the conditions, output Yes; otherwise, output No, on a single line.


Sample Input 1

3
45 450
45

Sample Output 1

Yes

Choosing (a, b, c) = (1, 2, 3),
C_{a,b}+C_{b,c}=C_{1,2}+C_{2,3}=45+45
C_{a,c}=C_{1,3}=450
so the conditions are satisfied.


Sample Input 2

4
25 40 65
30 55
25

Sample Output 2

No

No choice of (a, b, c) satisfies the conditions.

C - Puddles

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

配点 : 300

問題文

H 行、横 W 列のグリッドがあります。
上から i 行目左から j 列目のマスは S_{i,j}# のとき黒く、 . のとき白く塗られています。

白マスからなる四方位に連結な領域のうち、黒マスに囲まれたものの個数を求めてください。

より厳密には次の通りです。

上から i 行目左から j 列目のマスをマス (i,j) と表します。
2 つのマス (i,j),(i',j') が隣接しているとは、|i-i'|+|j-j'|=1 であることと定めます。
白マスの集合 C が連結であるとは、C に属するどの 2 マス c,c' に対しても、C に属する隣接するマスへの移動を繰り返すことで c から c' へ移動できることと定めます。
空でない白マスの連結な集合であって、極大なものを白マスの連結成分と定めます。
白マスの連結成分であって、グリッドの最外周(すなわち、 1 行目、 H 行目、 1 列目、 W 行目)のマスを含まないものの個数を求めてください。

制約

  • 3 \leq H,W \leq 10^3
  • H,W は整数
  • S_{i,j}#.

入力

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

H W
S_{1,1}S_{1,2}\dots S_{1,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

出力

答えを出力せよ。


入力例 1

5 15
##########..###
#...#######.###
####....###..##
######.########
########....###

出力例 1

2

上から 2 行目左から 2,3,4 列目の 3 マスからなる領域と、上から 3 行目左から 5,6,7,8 列目及び上から 4 行目左から 7 列目の計 5 マスからなる領域の 2 個です。


入力例 2

10 22
######################
####.#################
###...################
##.###.##.....########
##.....##.####.#######
.######.#......#.....#
.######.#.####.#.#####
#########.....##.#####
################.#####
################.....#

出力例 2

4

Score : 300 points

Problem Statement

There is a grid with H rows and W columns.
The cell at the i-th row from the top and j-th column from the left is painted black if S_{i,j} is #, and white if S_{i,j} is ..

Among the four-directionally connected regions consisting of white cells, find the number of those that are surrounded by black cells.

Below is a more formal statement.

We denote the cell at the i-th row from the top and j-th column from the left as cell (i, j).
Two cells (i, j) and (i', j') are said to be adjacent if and only if |i-i'|+|j-j'|=1.
A set C of white cells is said to be connected if and only if, for any two cells c and c' in C, it is possible to move from c to c' by repeatedly moving to an adjacent cell in C.
A non-empty connected set of white cells that is maximal is called a connected component of white cells.
Find the number of connected components of white cells that do not contain any cell on the outermost border of the grid (that is, in row 1, row H, column 1, or column W).

Constraints

  • 3 \leq H, W \leq 10^3
  • H and W are integers.
  • S_{i,j} is # or ..

Input

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

H W
S_{1,1}S_{1,2}\dots S_{1,W}
\vdots
S_{H,1}S_{H,2}\dots S_{H,W}

Output

Output the answer.


Sample Input 1

5 15
##########..###
#...#######.###
####....###..##
######.########
########....###

Sample Output 1

2

There are two such regions: the region consisting of the three cells in row 2 from the top and columns 2, 3, 4 from the left, and the region consisting of a total of five cells: columns 5, 6, 7, 8 from the left in row 3 from the top and column 7 from the left in row 4 from the top.


Sample Input 2

10 22
######################
####.#################
###...################
##.###.##.....########
##.....##.####.#######
.######.#......#.....#
.######.#.####.#.#####
#########.....##.#####
################.#####
################.....#

Sample Output 2

4
D - Minimize Range

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

配点 : 400

問題文

長さ N の正整数列 A と正整数 K が与えられます。
数列 A に対して、以下の操作を何回でも行うことができます。

  • 1 以上 N 以下の整数 i を一つ選び、A_iK を足す。

\max(A)-\min(A) としてあり得る値の最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力される値は全て整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを 1 行で出力せよ。


入力例 1

3 10
3 21 9

出力例 1

4

まず、i=1 を選ぶと数列は A=(13,21,9) になります。
次に、i=3 を選ぶと数列は A=(13,21,19) になります。
次に、i=1 を選ぶと数列は A=(23,21,19) になります。
このとき、\max(A)-\min(A)=23-19=4 となります。
\max(A)-\min(A)3 以下にすることはできないので、答えは 4 です。


入力例 2

5 6
4 100 5 10 450

出力例 2

2

Score : 400 points

Problem Statement

You are given a sequence A of N positive integers and a positive integer K.
You can perform the following operation on the sequence A any number of times.

  • Choose an integer i with 1 \leq i \leq N, and add K to A_i.

Find the minimum possible value of \max(A) - \min(A).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 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

Output the answer on a single line.


Sample Input 1

3 10
3 21 9

Sample Output 1

4

First, choosing i=1 makes the sequence A=(13,21,9).
Next, choosing i=3 makes the sequence A=(13,21,19).
Next, choosing i=1 makes the sequence A=(23,21,19).
At this point, \max(A)-\min(A)=23-19=4.
It is impossible to make \max(A)-\min(A) at most 3, so the answer is 4.


Sample Input 2

5 6
4 100 5 10 450

Sample Output 2

2
E - Fibonacci String

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

配点 : 450

問題文

文字列 X,Y が与えられます。文字列の列 S_1,S_2,\dots を以下で定義します。

  • S_1=X
  • S_2=Y
  • i\geq 3 のとき、S_iS_{i-1}S_{i-2} をこの順に連結したもの

i=1,2,\ldots,Q について以下の問題に答えてください。

問題:整数 L_i,R_i と文字 C_i が与えられる。 S_{10^{18}}L_i 文字目から R_i 文字目までに文字 C_i が何個含まれるか求めよ。

制約

  • X,Y は英小文字からなる長さ 1 以上 10^4 以下の文字列
  • 1 \leq Q \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^{18}
  • C_i は英小文字
  • 与えられる数値は全て整数

入力

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

X
Y
Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q

出力

Q 行出力せよ。
i 行目には S_{10^{18}}L_i 文字目から R_i 文字目までに文字 C_i が何個含まれるかを出力せよ。


入力例 1

a
b
6
2 7 a
1 3 b
3 7 b
1 9 c
1 1000000000000000000 b
1000000000000000000 1000000000000000000 a

出力例 1

3
2
3
0
618033988749894848
1

S_3,S_4,S_5 はそれぞれba, bab, babba となります。

S_{10^{18}}babbababbabba... であり、その 2 文字目から 7 文字目に a3 個含まれます。

Score : 450 points

Problem Statement

You are given strings X and Y. Define a sequence of strings S_1, S_2, \dots as follows.

  • S_1 = X
  • S_2 = Y
  • For i \geq 3, S_i is the concatenation of S_{i-1} and S_{i-2} in this order.

For each i = 1, 2, \ldots, Q, answer the following problem.

Problem: You are given integers L_i, R_i and a character C_i. Find how many times character C_i appears in the L_i-th through R_i-th characters of S_{10^{18}}.

Constraints

  • X and Y are strings of lowercase English letters of length between 1 and 10^4, inclusive.
  • 1 \leq Q \leq 10^5
  • 1 \leq L_i \leq R_i \leq 10^{18}
  • C_i is a lowercase English letter.
  • All given numerical values are integers.

Input

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

X
Y
Q
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_Q R_Q C_Q

Output

Output Q lines.
The i-th line should contain how many times character C_i appears in the L_i-th through R_i-th characters of S_{10^{18}}.


Sample Input 1

a
b
6
2 7 a
1 3 b
3 7 b
1 9 c
1 1000000000000000000 b
1000000000000000000 1000000000000000000 a

Sample Output 1

3
2
3
0
618033988749894848
1

S_3, S_4, S_5 are ba, bab, babba, respectively.

S_{10^{18}} is babbababbabba..., and the second through seventh characters contain three occurrences of a.

F - Strongly Connected 2

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

配点 : 525

問題文

辺と頂点に番号がついた N 頂点 N-1+M 辺の有向グラフがあります。
1 \leq i \leq M に対し辺 i は頂点 X_i から頂点 Y_i への有向辺であり、1 \leq i \leq N-1 に対し辺 M+i は頂点 i+1 から頂点 i への有向辺です。

1,2,\dots,M のうちいくつか( 0 個でもよい) の辺を選ぶ方法は 2^M 通りありますが、そのうち選んだ辺を削除したあとのグラフが強連結となるものは何通りありますか。998244353 で割った余りを求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1 \leq X_i < Y_i \leq N
  • 入力は全て整数

入力

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

N M
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

答えを出力せよ。


入力例 1

4 3
1 4
1 3
2 4

出力例 1

5

選ぶ辺の番号の集合が \{\}, \{1\},\{2\},\{3\},\{2,3\}5 通りのいずれかであるとき、グラフは強連結となります。


入力例 2

10 11
1 4
1 4
3 9
2 5
3 4
9 10
6 9
4 10
1 3
8 10
4 7

出力例 2

1297

与えられるグラフは多重辺を持つことがあります。

Score : 525 points

Problem Statement

There is a directed graph with N vertices and N-1+M edges, with edges and vertices numbered.
For 1 \leq i \leq M, edge i is a directed edge from vertex X_i to vertex Y_i, and for 1 \leq i \leq N-1, edge M+i is a directed edge from vertex i+1 to vertex i.

There are 2^M ways to choose some (possibly zero) edges from among edges 1, 2, \dots, M. Among these ways, how many result in the graph being strongly connected after deleting the chosen edges? Find the count modulo 998244353.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1 \leq X_i < Y_i \leq N
  • All input values are integers.

Input

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

N M
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

Output the answer.


Sample Input 1

4 3
1 4
1 3
2 4

Sample Output 1

5

The graph is strongly connected when the set of chosen edge indices is one of \{\}, \{1\}, \{2\}, \{3\}, \{2,3\}, giving five ways.


Sample Input 2

10 11
1 4
1 4
3 9
2 5
3 4
9 10
6 9
4 10
1 3
8 10
4 7

Sample Output 2

1297

The given graph may have multi-edges.

G - Random Subtraction

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

配点 : 575

問題文

長さ N の非負整数列 A が与えられます。
A の要素数が 1 になるまで、以下の操作を繰り返し行います。

  • 1 \leq i,j \leq |A| を満たす相異なる 2 整数 i,j を一様ランダムに選ぶ。
  • A_i,A_j をそれぞれ a,b と置く。
  • A から i 番目の要素と j 番目の要素を削除する。
  • A の末尾に a-b を追加する。

最終的な A の唯一の要素を x とします。 x^2 の期待値を \mod 998244353 で求めてください。

期待値 \pmod{998244353} の定義

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

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 998244352
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを 1 行で出力せよ。


入力例 1

3
4 5 0

出力例 1

665496263

まず (i,j) として (3,1) を選ぶと、数列は (5,-4) になります。
次に (i,j) として (1,2) を選ぶと、数列は (9) になります。
x^2\frac{1}{3} の確率で 81\frac{2}{3} の確率で 1 になり、期待値は \frac{83}{3} です。


入力例 2

5
450 2026 3 21 100

出力例 2

669406799

Score : 575 points

Problem Statement

You are given a sequence A of N non-negative integers.
Repeat the following operation until A has exactly one element.

  • Choose two distinct integers i and j satisfying 1 \leq i, j \leq |A| uniformly at random.
  • Let a and b be A_i and A_j, respectively.
  • Remove the i-th and j-th elements from A.
  • Append a - b to the end of A.

Let x be the only element of A at the end. Find the expected value of x^2, modulo 998244353.

Definition of expected value modulo 998244353

It can be proved that the expected value to be found is always a rational number. It can also be proved that under the constraints of this problem, when expressed as an irreducible fraction \frac{P}{Q}, we have Q {{}\not\equiv{}} 0 \pmod{998244353}. Thus, there is a unique integer R satisfying R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Find this R.

Constraints

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

Input

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

N
A_1 A_2 \dots A_N

Output

Output the answer on a single line.


Sample Input 1

3
4 5 0

Sample Output 1

665496263

First, choosing (i, j) = (3, 1) makes the sequence (5, -4).
Next, choosing (i, j) = (1, 2) makes the sequence (9).
x^2 equals 81 with probability \frac{1}{3} and 1 with probability \frac{2}{3}, so the expected value is \frac{83}{3}.


Sample Input 2

5
450 2026 3 21 100

Sample Output 2

669406799