A - Chompers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる文字列 S と正の整数 N が与えられます。S の長さは 2N+1 以上です。

S の先頭と末尾から N 文字ずつ取り除いて得られる文字列を求めてください。

制約

  • S は英小文字からなる文字列
  • N は整数
  • 2N+1 \leq |S| \leq 30
  • 1 \leq N \leq 10

入力

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

S
N

出力

答えを出力せよ。


入力例 1

chemotherapy
3

出力例 1

mother

chemotherapy の先頭 3 文字 (che) と末尾 3 文字 (apy) を取り除くと、mother が得られます。


入力例 2

thermometer
4

出力例 2

mom

入力例 3

burger
1

出力例 3

urge

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters and a positive integer N. The length of S is at least 2N+1.

Find the string obtained by removing N characters from the beginning and N characters from the end of S.

Constraints

  • S is a string consisting of lowercase English letters.
  • N is an integer.
  • 2N+1 \leq |S| \leq 30
  • 1 \leq N \leq 10

Input

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

S
N

Output

Output the answer.


Sample Input 1

chemotherapy
3

Sample Output 1

mother

Removing the first three characters (che) and the last three characters (apy) from chemotherapy gives mother.


Sample Input 2

thermometer
4

Sample Output 2

mom

Sample Input 3

burger
1

Sample Output 3

urge
B - Count Adjacent Cells

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

HW 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。

マス (x_1, y_1) とマス (x_2, y_2)辺で隣接するとは、|x_1 - x_2| + |y_1 - y_2| = 1 が成り立つことをいいます。

すべてのマスについて、そのマスに辺で隣接するマスの個数を求めてください。

制約

  • 1 \leq H,W \leq 50
  • 入力される値はすべて整数

入力

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

H W

出力

答えを以下の形式で出力せよ。

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

ここで、x_{i,j} はマス (i,j) に辺で隣接するマスの個数を表す。


入力例 1

4 5

出力例 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

マス (1, 5) に辺で隣接するマスは、マス (1, 4), (2, 5)2 個です。

マス (2, 3) に辺で隣接するマスは、マス (1, 3), (2, 2), (2, 4), (3, 3)4 個です。

マス (4, 2) に辺で隣接するマスは、マス (3, 2), (4, 1), (4, 3)3 個です。


入力例 2

1 1

出力例 2

0

マス (1,1) に辺で隣接するマスは存在しません。


入力例 3

12 8

出力例 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is denoted as cell (i, j).

Cells (x_1, y_1) and (x_2, y_2) are said to be edge-adjacent when |x_1 - x_2| + |y_1 - y_2| = 1.

For every cell, find the number of cells that are edge-adjacent to it.

Constraints

  • 1 \leq H, W \leq 50
  • All input values are integers.

Input

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

H W

Output

Output the answer in the following format:

x_{1,1} x_{1,2} \cdots x_{1,W}
x_{2,1} x_{2,2} \cdots x_{2,W}
\vdots
x_{H,1} x_{H,2} \cdots x_{H,W}

Here, x_{i,j} represents the number of cells that are edge-adjacent to cell (i, j).


Sample Input 1

4 5

Sample Output 1

2 3 3 3 2
3 4 4 4 3
3 4 4 4 3
2 3 3 3 2

The cells edge-adjacent to cell (1, 5) are cells (1, 4), (2, 5), for a total of two cells.

The cells edge-adjacent to cell (2, 3) are cells (1, 3), (2, 2), (2, 4), (3, 3), for a total of four cells.

The cells edge-adjacent to cell (4, 2) are cells (3, 2), (4, 1), (4, 3), for a total of three cells.


Sample Input 2

1 1

Sample Output 2

0

There are no cells edge-adjacent to cell (1, 1).


Sample Input 3

12 8

Sample Output 3

2 3 3 3 3 3 3 2
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
3 4 4 4 4 4 4 3
2 3 3 3 3 3 3 2
C - C Stands for Center

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英大文字からなる文字列 S が与えられます。
以下の条件を全て満たす S の部分文字列 (連続する部分列) がいくつあるか求めてください。

  • 奇数個の文字からなる。
  • 中央の文字が C である。厳密には、抜き出した部分文字列が l 文字からなるとき、その (l+1)/2 文字目が C である。

ただし、部分文字列が文字列として一致していても、抜き出した文字の位置が異なる場合、それぞれ別に数えます。

制約

  • S は英大文字からなる長さ 1 以上 5 \times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

ABCCA

出力例 1

5

この入力では、 S= ABCCA です。
問題文中の条件を満たす部分文字列は以下の 5 つです。

  • S 中の 1 文字目から 5 文字目までを抜き出した ABCCA
  • S 中の 2 文字目から 4 文字目までを抜き出した BCC
  • S 中の 3 文字目から 3 文字目までを抜き出した C
  • S 中の 3 文字目から 5 文字目までを抜き出した CCA
  • S 中の 4 文字目から 4 文字目までを抜き出した C

入力例 2

XYZ

出力例 2

0

入力例 3

SMBCPROGRAMMINGCONTEST

出力例 3

11

Score : 300 points

Problem Statement

You are given a string S consisting of uppercase English letters.
Find the number of substrings (contiguous subsequences) of S that satisfy all of the following conditions.

  • It consists of an odd number of characters.
  • Its middle character is C. More formally, if the extracted substring consists of l characters, its ((l+1)/2)-th character is C.

Even if two substrings are identical as strings, they are counted separately if they are extracted from different positions.

Constraints

  • S is a string consisting of uppercase English letters with length between 1 and 5 \times 10^5, inclusive.

Input

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

S

Output

Output the answer.


Sample Input 1

ABCCA

Sample Output 1

5

In this input, S= ABCCA.
The substrings satisfying the conditions in the problem statement are the following five:

  • ABCCA, extracted from the 1st to 5th characters of S
  • BCC, extracted from the 2nd to 4th characters of S
  • C, extracted from the 3rd to 3rd characters of S
  • CCA, extracted from the 3rd to 5th characters of S
  • C, extracted from the 4th to 4th characters of S

Sample Input 2

XYZ

Sample Output 2

0

Sample Input 3

SMBCPROGRAMMINGCONTEST

Sample Output 3

11
D - Chalkboard Median

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

黒板に整数 X1 つ書かれています。

Q 個のクエリが与えられるので、順に処理してください。i 個目 (1\le i\le Q) のクエリは以下の通りです。

2 つの整数 A_i,B_i が与えられる。黒板に新たに 2 つの整数 A_i,B_i を書く。

その後、黒板に書かれた 2i+1 個の整数の中央値を出力する。

制約

  • 1\le X\le 10^9
  • 1\le Q\le 2\times 10^5
  • 1\le A_i,B_i\le 10^9
  • 入力される値は全て整数

入力

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

X
Q
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

出力

Q 行出力せよ。

i 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

5
3
2 3
1 2
8 9

出力例 1

3
2
3

1 つ目のクエリでは黒板に書かれた整数は 5,2,3 となり、これらの整数の中央値は 3 です。

2 つ目のクエリでは黒板に書かれた整数は 5,2,3,1,2 となり、これらの整数の中央値は 2 です。

3 つ目のクエリでは黒板に書かれた整数は 5,2,3,1,2,8,9 となり、これらの整数の中央値は 3 です。


入力例 2

1
4
2 3
4 5
6 7
8 9

出力例 2

2
3
4
5

入力例 3

278117031
7
167642909 517897721
148434323 567739597
319926999 481642530
659199879 252516557
49913403 798318034
89701408 892537201
199166668 742285869

出力例 3

278117031
278117031
319926999
319926999
319926999
319926999
319926999

Score : 400 points

Problem Statement

There is one integer X written on a blackboard.

You are given Q queries to process in order. The i-th query (1 \le i \le Q) is as follows.

Two integers A_i and B_i are given. Write two new integers A_i and B_i on the blackboard.

Then, output the median of the 2i+1 integers written on the blackboard.

Constraints

  • 1 \le X \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i, B_i \le 10^9
  • All input values are integers.

Input

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

X
Q
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

Output

Output Q lines.

The i-th line should contain the answer to the i-th query.


Sample Input 1

5
3
2 3
1 2
8 9

Sample Output 1

3
2
3

In the first query, the integers written on the blackboard become 5, 2, 3, and their median is 3.

In the second query, the integers written on the blackboard become 5, 2, 3, 1, 2, and their median is 2.

In the third query, the integers written on the blackboard become 5, 2, 3, 1, 2, 8, 9, and their median is 3.


Sample Input 2

1
4
2 3
4 5
6 7
8 9

Sample Output 2

2
3
4
5

Sample Input 3

278117031
7
167642909 517897721
148434323 567739597
319926999 481642530
659199879 252516557
49913403 798318034
89701408 892537201
199166668 742285869

Sample Output 3

278117031
278117031
319926999
319926999
319926999
319926999
319926999
E - Count 123

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

以下の条件をすべて満たす長さ X_1+X_2+X_3 の数列 A = (a_1, \cdots, a_{X_1 + X_2 + X_3}) としてあり得るものの個数を 998244353 で割ったあまりを求めてください。

  • A1X_1 個、2X_2 個、3X_3 個含む。
  • 隣接する要素の差の絶対値は 1 以下である。つまり、1 \leq i \leq X_1+X_2+X_3-1 を満たす任意の整数 i について、|a_{i+1} - a_i| \leq 1 が成り立つ。

制約

  • 1 \leq X_1, X_2, X_3 \leq 10^6
  • 入力される値はすべて整数

入力

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

X_1 X_2 X_3

出力

答えを出力せよ。


入力例 1

2 2 1

出力例 1

9

条件を満たす数列は以下の 9 通りです。

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

入力例 2

5 3 4

出力例 2

204

入力例 3

998244 998353 998107

出力例 3

701926019

Score : 450 points

Problem Statement

Find the number, modulo 998244353, of sequences A = (a_1, \cdots, a_{X_1 + X_2 + X_3}) of length X_1+X_2+X_3 that satisfy all of the following conditions.

  • A contains exactly X_1 copies of 1, exactly X_2 copies of 2, and exactly X_3 copies of 3.
  • The absolute difference between adjacent elements is at most 1. That is, for every integer i satisfying 1 \leq i \leq X_1+X_2+X_3-1, we have |a_{i+1} - a_i| \leq 1.

Constraints

  • 1 \leq X_1, X_2, X_3 \leq 10^6
  • All input values are integers.

Input

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

X_1 X_2 X_3

Output

Output the answer.


Sample Input 1

2 2 1

Sample Output 1

9

The sequences satisfying the conditions are the following nine:

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

Sample Input 2

5 3 4

Sample Output 2

204

Sample Input 3

998244 998353 998107

Sample Output 3

701926019
F - Critical Misread

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

英小文字からなる K 個の文字列 S_i が与えられます。
英小文字からなる長さ N の文字列であって、 S_1,S_2,\dots,S_K のいずれも部分文字列 (連続する部分列) として含まないものの個数を 998244353 で割った余りを求めてください。

制約

  • N1 以上 10^9 以下の整数
  • K1 以上 10 以下の整数
  • S_i は英小文字からなる長さ 1 以上 10 以下の文字列

入力

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

N K
S_1
S_2
\vdots
S_K

出力

答えを出力せよ。


入力例 1

3 2
aa
ab

出力例 1

17474
  • 英小文字からなる長さ 3 の文字列は 26^3=17576 個あります。
  • 英小文字からなる長さ 3 の文字列のうち先頭 2 文字が aa であるものは 26 個あります。
  • 英小文字からなる長さ 3 の文字列のうち末尾 2 文字が aa であるものは 26 個あります。
  • 英小文字からなる長さ 3 の文字列のうち先頭 2 文字が ab であるものは 26 個あります。
  • 英小文字からなる長さ 3 の文字列のうち末尾 2 文字が ab であるものは 26 個あります。
  • 英小文字からなる長さ 3 の文字列のうち全体が aaa であるものは 1 個あります。
  • 英小文字からなる長さ 3 の文字列のうち全体が aab であるものは 1 個あります。
  • 以上の事柄を組み合わせることで、このテストケースに対する答えが 17474 であることが確認できます。

入力例 2

1 1
ab

出力例 2

26

入力例 3

1000000000 10
contest
tester
error
orange
angel
elegant
antitese
sextuple
pleasure
surely

出力例 3

698570468

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

Score : 525 points

Problem Statement

You are given K strings S_i consisting of lowercase English letters.
Find the number, modulo 998244353, of strings of length N consisting of lowercase English letters that do not contain any of S_1, S_2, \dots, S_K as a substring (contiguous subsequence).

Constraints

  • N is an integer between 1 and 10^9, inclusive.
  • K is an integer between 1 and 10, inclusive.
  • S_i is a string consisting of lowercase English letters with length between 1 and 10, inclusive.

Input

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

N K
S_1
S_2
\vdots
S_K

Output

Output the answer.


Sample Input 1

3 2
aa
ab

Sample Output 1

17474
  • The number of strings of length 3 consisting of lowercase English letters is 26^3=17576.
  • The number of strings of length 3 consisting of lowercase English letters whose first two characters are aa is 26.
  • The number of strings of length 3 consisting of lowercase English letters whose last two characters are aa is 26.
  • The number of strings of length 3 consisting of lowercase English letters whose first two characters are ab is 26.
  • The number of strings of length 3 consisting of lowercase English letters whose last two characters are ab is 26.
  • The number of strings of length 3 consisting of lowercase English letters whose entirety is aaa is 1.
  • The number of strings of length 3 consisting of lowercase English letters whose entirety is aab is 1.
  • Combining the above facts, it can be confirmed that the answer for this test case is 17474.

Sample Input 2

1 1
ab

Sample Output 2

26

Sample Input 3

1000000000 10
contest
tester
error
orange
angel
elegant
antitese
sextuple
pleasure
surely

Sample Output 3

698570468

Find the count modulo 998244353.

G - Children Yearn for the Evil Kindergarten

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

10^{100} 人の園児がゲーム会場にいます。はじめはどの園児もメダルを持っていません。

園児は、脱落脱出のいずれかをした時点で、またその時点に限り、会場を去ります。

ゲームは N 日からなります。i 日目 (1 \leq i \leq N) は以下の一連の処理を順に実行します。

  • 会場にいる園児の持っているメダルをすべて回収し、その合計枚数を s とおく。
  • s + A_i 枚のメダルを、会場にいる園児のあいだで自由に分配する(会場に園児がいない場合は何もしない)。
  • 会場にいる園児のうち、メダル所持数が B_i 枚未満の者は脱落する。メダル所持数が B_i 枚以上の者は、メダルを B_i 枚ずつ失う。
  • 会場にいる園児のうち、メダル所持数が C_i 枚以上の者は、各々がこのタイミングで脱出するか会場に残るかを選ぶ。

N 日間が終わった時点で会場に残っている園児は、脱落します。

最終的に脱出する園児の人数としてあり得る最大値を求めてください。

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

制約

  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^6
  • 1 \leq B_i \leq 10^6
  • 1 \leq C_i \leq 10^6
  • 全てのテストケースにおける N の総和は 3 \times 10^5 以下
  • 入力される値はすべて整数

入力

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

T
\mathrm{case}_{1}
\mathrm{case}_{2}
\vdots
\mathrm{case}_{T}

各テストケースは以下の形式で与えられる。

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

出力

各テストケースに対する答えを順に、改行区切りで出力せよ。


入力例 1

2
4
16 2 3
15 2 4
1 3 5
20 5 5
2
41404 1 941738
211877 205711 417821

出力例 1

5
0

1 番目のテストケースについて考えます。園児たちが以下のように行動することで、脱出人数は 5 人になります。

  • 1 日目の開始時点で 10^{100} 人の園児から s = 0 枚のメダルを回収する。その後、以下のように行動する。
    • 0 + 16 = 16 枚のメダルを配分し、各園児のメダル所持数を (5,5,2,2,2,0,\dots,0) にする。
    • メダルを持っていない 10^{100} - 5 人が脱落し、残りの 5 人のメダル所持数は (3,3,0,0,0) になる。
    • メダルを 3 枚持っている園児 2 人が脱出を選び、残りの 3 人のメダル所持数は (0,0,0) になる。
  • 2 日目の開始時点で 3 人の園児から s = 0 枚のメダルを回収する。その後、以下のように行動する。
    • 0 + 15 = 15 枚のメダルを配分し、各園児のメダル所持数を (6,6,3) にする。
    • 誰も脱落せず、残りの 3 人のメダル所持数は (4,4,1) になる。
    • メダルを 4 枚持っている園児 1 人が脱出を選び、残りの 2 人のメダル所持数は (4,1) になる。
  • 3 日目の開始時点で 2 人の園児から s = 5 枚のメダルを回収する。その後、以下のように行動する。
    • 5 + 1 = 6 枚のメダルを配分し、各園児のメダル所持数を (3,3) にする。
    • 誰も脱落せず、残りの 2 人のメダル所持数は (0,0) になる。
    • 誰も脱出しない。
  • 4 日目の開始時点で 2 人の園児から s = 0 枚のメダルを回収する。その後、以下のように行動する。
    • 0 + 20 = 20 枚のメダルを配分し、各園児のメダル所持数を (10,10) にする。
    • 誰も脱落せず、残りの 2 人のメダル所持数は (5,5) になる。
    • メダルを 5 枚持っている園児 2 人が脱出を選び、会場から園児がいなくなる。

2 番目のテストケースにおいて、園児は 1 人も脱出できません。

Score : 625 points

Problem Statement

There are 10^{100} kids at a game venue. Initially, no kid has any medals.

A kid leaves the venue exactly when they either drop out or escape.

The game consists of N days. On day i (1 \leq i \leq N), the following sequence of operations is performed in order.

  • Collect all medals held by the kids at the venue, and let s be the total number of collected medals.
  • Distribute s + A_i medals freely among the kids at the venue (if there are no kids at the venue, do nothing).
  • Among the kids at the venue, those with fewer than B_i medals drop out. Those with at least B_i medals each lose B_i medals.
  • Among the kids at the venue, those with at least C_i medals each choose whether to escape at this point or remain at the venue.

Kids who remain at the venue at the end of the N days drop out.

Find the maximum possible number of kids who ultimately escape.

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

Constraints

  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^6
  • 1 \leq B_i \leq 10^6
  • 1 \leq C_i \leq 10^6
  • The sum of N over all test cases is at most 3 \times 10^5.
  • All input values are integers.

Input

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

T
\mathrm{case}_{1}
\mathrm{case}_{2}
\vdots
\mathrm{case}_{T}

Each test case is given in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

Output

Output the answers for the test cases in order, separated by newlines.


Sample Input 1

2
4
16 2 3
15 2 4
1 3 5
20 5 5
2
41404 1 941738
211877 205711 417821

Sample Output 1

5
0

Consider the first test case. By acting as follows, five kids can escape.

  • At the start of day 1, collect s = 0 medals from 10^{100} kids. Then, proceed as follows.
    • Distribute 0 + 16 = 16 medals so that the kids' medal counts become (5, 5, 2, 2, 2, 0, \dots, 0).
    • The 10^{100} - 5 kids with no medals drop out, and the remaining 5 kids' medal counts become (3, 3, 0, 0, 0).
    • The 2 kids with 3 medals each choose to escape, and the remaining 3 kids' medal counts become (0, 0, 0).
  • At the start of day 2, collect s = 0 medals from 3 kids. Then, proceed as follows.
    • Distribute 0 + 15 = 15 medals so that the kids' medal counts becomes (6, 6, 3).
    • No one drops out, and the remaining 3 kids' medal counts become (4, 4, 1).
    • 1 kid with 4 medals chooses to escape, and the remaining 2 kids' medal counts become (4, 1).
  • At the start of day 3, collect s = 5 medals from 2 kids. Then, proceed as follows.
    • Distribute 5 + 1 = 6 medals so that the kids' medal counts becomes (3, 3).
    • No one drops out, and the remaining 2 kids' medal counts become (0, 0).
    • No one escapes.
  • At the start of day 4, collect s = 0 medals from 2 kids. Then, proceed as follows.
    • Distribute 0 + 20 = 20 medals so that the kids' medal counts becomes (10, 10).
    • No one drops out, and the remaining 2 kids' medal counts become (5, 5).
    • The 2 kids with 5 medals each choose to escape, and the venue becomes empty.

In the second test case, not a single kid can escape.