A - Sanitize Hands

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

消毒液の入ったボトルがあり、その消毒液によってちょうど M 本の手を消毒することができます。

N 人の宇宙人が順に手の消毒を行いに来ます。
i 人目 (1\leq i\leq N) の宇宙人は H_i 本の手を持っており、それぞれ自身のすべての手を 1 回ずつ消毒したいと考えています。

何人目の宇宙人までがすべての手を消毒できるか求めてください。
ただし、ある宇宙人が消毒を始める時点で、自身のすべての手を消毒する分の消毒液が残っていなかったとしても、その宇宙人はその消毒液を使い切ってしまうものとします。

制約

  • 1\leq N,M\leq 100
  • 1\leq H_i\leq 100
  • 入力はすべて整数

入力

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

N M
H_1 H_2 \ldots H_N

出力

何人目の宇宙人までが自身のすべての手を消毒できるか出力せよ。


入力例 1

5 10
2 3 2 5 3

出力例 1

3

次の手順で宇宙人は自身の手を消毒します。

  • 1 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、10-2=8 本の手を消毒できます。
  • 2 人目の宇宙人は自身の 3 本の手を消毒します。残りの消毒液によって、8-3=5 本の手を消毒できます。
  • 3 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、5-2=3 本の手を消毒できます。
  • 4 人目の宇宙人は 5 本の手を持っていますが、消毒液は 3 本分しかないため消毒液を使い切り、かつ自身のすべての手を消毒できません。

よって、3 人目の宇宙人までが自身のすべての手を消毒できるため、3 を出力します。


入力例 2

5 10
2 3 2 3 5

出力例 2

4

入力例 3

1 5
1

出力例 3

1

すべての宇宙人が自身の手を消毒することができます。

Score : 100 points

Problem Statement

There is a bottle of disinfectant that can disinfect exactly M hands.

N aliens come one by one to disinfect their hands.
The i-th alien (1 \leq i \leq N) has H_i hands and wants to disinfect all of their hands once.

Determine how many aliens can disinfect all of their hands.
Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.

Constraints

  • 1 \leq N, M \leq 100
  • 1 \leq H_i \leq 100
  • All input values are integers.

Input

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

N M
H_1 H_2 \ldots H_N

Output

Print the number of aliens who can disinfect all of their hands.


Sample Input 1

5 10
2 3 2 5 3

Sample Output 1

3

The aliens disinfect their hands in the following steps:

  • The first alien disinfects their two hands. The remaining disinfectant can disinfect 10-2=8 hands.
  • The second alien disinfects their three hands. The remaining disinfectant can disinfect 8-3=5 hands.
  • The third alien disinfects their two hands. The remaining disinfectant can disinfect 5-2=3 hands.
  • The fourth alien has five hands, but there is only enough disinfectant for three hands, so they use up the disinfectant without disinfecting all of their hands.

Thus, the first three aliens can disinfect all of their hands, so print 3.


Sample Input 2

5 10
2 3 2 3 5

Sample Output 2

4

Sample Input 3

1 5
1

Sample Output 3

1

All aliens can disinfect their hands.

B - Uppercase and Lowercase

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字と英大文字からなる文字列 S が与えられます。S の長さは奇数です。
S に含まれる大文字の個数が小文字の個数よりも多ければ、S に含まれる全ての小文字を大文字に変換してください。
そうでない場合は、S に含まれる全ての大文字を小文字に変換してください。

制約

  • S は英小文字および英大文字からなる文字列
  • S の長さは 1 以上 99 以下の奇数

入力

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

S

出力

問題文の指示に従って文字を変換した後の S を出力せよ。


入力例 1

AtCoder

出力例 1

atcoder

AtCoder に含まれる小文字の個数は 5 個、大文字の個数は 2 個です。よって AtCoder に含まれる全ての大文字を小文字に変換した atcoder が答えとなります。


入力例 2

SunTORY

出力例 2

SUNTORY

SunTORY に含まれる小文字の個数は 2 個、大文字の個数は 5 個です。よって SunTORY に含まれる全ての小文字を大文字に変換した SUNTORY が答えとなります。


入力例 3

a

出力例 3

a

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase and uppercase English letters. The length of S is odd.
If the number of uppercase letters in S is greater than the number of lowercase letters, convert all lowercase letters in S to uppercase.
Otherwise, convert all uppercase letters in S to lowercase.

Constraints

  • S is a string consisting of lowercase and uppercase English letters.
  • The length of S is an odd number between 1 and 99, inclusive.

Input

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

S

Output

Print the string S after converting the letters according to the problem statement.


Sample Input 1

AtCoder

Sample Output 1

atcoder

The string AtCoder contains five lowercase letters and two uppercase letters. Thus, convert all uppercase letters in AtCoder to lowercase, which results in atcoder.


Sample Input 2

SunTORY

Sample Output 2

SUNTORY

The string SunTORY contains two lowercase letters and five uppercase letters. Thus, convert all lowercase letters in SunTORY to uppercase, which results in SUNTORY.


Sample Input 3

a

Sample Output 3

a
C - Sierpinski carpet

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

非負整数 K に対して、以下のようにレベル K のカーペットを定義します。

  • レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
  • K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。 このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
    • 中央のブロックはすべて白いマスからなる。
    • 他の 8 個のブロックは、レベル (K-1) のカーペットである。

非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。

制約

  • 0\leq N \leq 6
  • N は整数

入力

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

N

出力

3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、.# からなる長さ 3^N の文字列 S_i を出力せよ。
S_ij 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき # とし、白いとき . とせよ。


入力例 1

1

出力例 1

###
#.#
###

レベル 1 のカーペットは次のような 3\times 3 のグリッドです。

これを出力形式にしたがって出力すると出力例のようになります。


入力例 2

2

出力例 2

#########
#.##.##.#
#########
###...###
#.#...#.#
###...###
#########
#.##.##.#
#########

レベル 2 のカーペットは 9\times 9 のグリッドとなります。

Score : 250 points

Problem Statement

For a non-negative integer K, we define a level-K carpet as follows:

  • A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
  • For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
    • The central block consists entirely of white cells.
    • The other eight blocks are level-(K-1) carpets.

You are given a non-negative integer N.
Print a level-N carpet according to the specified format.

Constraints

  • 0 \leq N \leq 6
  • N is an integer.

Input

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

N

Output

Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of . and #.
The j-th character of S_i (1 \leq j \leq 3^N) should be # if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and . if it is white.


Sample Input 1

1

Sample Output 1

###
#.#
###

A level-1 carpet is a 3 \times 3 grid as follows:

When output according to the specified format, it looks like the sample output.


Sample Input 2

2

Sample Output 2

#########
#.##.##.#
#########
###...###
#.#...#.#
###...###
#########
#.##.##.#
#########

A level-2 carpet is a 9 \times 9 grid.

D - 88888888

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

正整数 N に対して、NN 個つなげた整数を V_N とします。
より厳密には、N を文字列とみなしたものを N 個連結し、 それを再度整数とみなしたものを V_N とします。
例えば、V_3=333, V_{10}=10101010101010101010 です。

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

制約

  • 1\leq N\leq 10^{18}
  • N は整数

入力

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

N

出力

V_N998244353 で割った余りを出力せよ。


入力例 1

5

出力例 1

55555

V_5=55555998244353 で割った余りは 55555 です。


入力例 2

9

出力例 2

1755646

V_9=999999999998244353 で割った余りは 1755646 です。


入力例 3

10000000000

出力例 3

468086693

入力が 32 bit 整数型に収まらない可能性があることに注意してください。

Score : 350 points

Problem Statement

For a positive integer N, let V_N be the integer formed by concatenating N exactly N times.
More precisely, consider N as a string, concatenate N copies of it, and treat the result as an integer to get V_N.
For example, V_3=333 and V_{10}=10101010101010101010.

Find the remainder when V_N is divided by 998244353.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer.

Input

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

N

Output

Print the remainder when V_N is divided by 998244353.


Sample Input 1

5

Sample Output 1

55555

The remainder when V_5=55555 is divided by 998244353 is 55555.


Sample Input 2

9

Sample Output 2

1755646

The remainder when V_9=999999999 is divided by 998244353 is 1755646.


Sample Input 3

10000000000

Sample Output 3

468086693

Note that the input may not fit into a 32-bit integer type.

E - Reachability in Functional Graph

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

頂点に 1 から N の番号がついた N 頂点 N 辺の有向グラフがあります。
全ての頂点の出次数は 1 で、頂点 i から出ている辺は頂点 a_i へ伸びています。
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を求めてください。

ここで、頂点 u から頂点 v へ到達可能であるとは、ある長さ K+1 の頂点の列 w_0, w_1, \dots, w_K であって次の条件を全て満たすものが存在することをいいます。特に、u = v の時は常に到達可能です。

  • w_0 = u
  • w_K = v
  • 0 \leq i \lt K を満たす全ての i について、頂点 w_i から頂点 w_{i+1} へ伸びる辺が存在する。

制約

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

入力

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

N
a_1 a_2 \dots a_N

出力

頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を出力せよ。


入力例 1

4
2 1 1 4

出力例 1

8

頂点 1 から到達可能である頂点は頂点 1, 2 です。
頂点 2 から到達可能である頂点は頂点 1, 2 です。
頂点 3 から到達可能である頂点は頂点 1, 2, 3 です。
頂点 4 から到達可能である頂点は頂点 4 のみです。
よって、頂点の組 (u, v) であって頂点 u から頂点 v へ到達可能であるものの個数は 8 個です。
頂点 4 から出ている辺は自己ループ、すなわち頂点 4 自身へ伸びている辺である点に注意してください。


入力例 2

5
2 4 3 1 2

出力例 2

14

入力例 3

10
6 10 4 1 5 9 8 6 5 1

出力例 3

41

Score : 450 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and N edges.
The out-degree of every vertex is 1, and the edge from vertex i points to vertex a_i.
Count the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.

Here, vertex v is reachable from vertex u if there exists a sequence of vertices w_0, w_1, \dots, w_K of length K+1 that satisfies the following conditions. In particular, if u = v, it is always reachable.

  • w_0 = u.
  • w_K = v.
  • For every 0 \leq i \lt K, there is an edge from vertex w_i to vertex w_{i+1}.

Constraints

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

Print the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.


Sample Input 1

4
2 1 1 4

Sample Output 1

8

The vertices reachable from vertex 1 are vertices 1, 2.
The vertices reachable from vertex 2 are vertices 1, 2.
The vertices reachable from vertex 3 are vertices 1, 2, 3.
The vertex reachable from vertex 4 is vertex 4.
Therefore, the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u is 8.
Note that the edge from vertex 4 is a self-loop, that is, it points to vertex 4 itself.


Sample Input 2

5
2 4 3 1 2

Sample Output 2

14

Sample Input 3

10
6 10 4 1 5 9 8 6 5 1

Sample Output 3

41
F - Two Sequence Queries

Time Limit: 5 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。
Q 個のクエリが与えられるので、順に処理してください。

クエリは次の 3 種類です。

  • 1 l r x : A_l, A_{l+1}, \ldots, A_rx を加える。
  • 2 l r x : B_l, B_{l+1}, \ldots, B_rx を加える。
  • 3 l r : \displaystyle\sum_{i=l}^r (A_i\times B_i)998244353 で割った余りを出力する。

制約

  • 1\leq N,Q\leq 2\times 10^5
  • 0\leq A_i,B_i\leq 10^9
  • 1\leq l\leq r\leq N
  • 1\leq x\leq 10^9
  • 入力はすべて整数
  • 3 種類目のクエリが 1 つ以上存在する。

入力

入力は以下の形式で標準入力から与えられる。ここで、\mathrm{query}_i (1\leq i\leq Q)i 番目に処理するクエリである。

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1 l r x
2 l r x
3 l r

出力

3 種類目のクエリが K 個あるとき、K 行出力せよ。
i 行目 (1\leq i\leq K) には、i 個目の 3 種類目のクエリに対する出力を出力せよ。


入力例 1

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

出力例 1

16
25
84

最初、A=(1,3,5,6,8), B=(3,1,2,1,2) です。クエリは次の順で処理されます。

  • 1 個目のクエリでは (1\times 3)+(3\times 1)+(5\times 2)=16998244353 で割った余りである 16 を出力します。
  • 2 個目のクエリでは A_2,A_3,A_4,A_53 を加えます。A=(1,6,8,9,11) となります。
  • 3 個目のクエリでは (1\times 3)+(6\times 1)+(8\times 2)=25998244353 で割った余りである 25 を出力します。
  • 4 個目のクエリでは A_1,A_2,A_31 を加えます。A=(2,7,9,9,11) となります。
  • 5 個目のクエリでは B_52 を加えます。B=(3,1,2,1,4) となります。
  • 6 個目のクエリでは (2\times 3)+(7\times 1)+(9\times 2)+(9\times 1)+(11\times 4)=84998244353 で割った余りである 84 を出力します。

よって、1, 2, 3 行目にはそれぞれ 16, 25, 84 を出力します。


入力例 2

2 3
1000000000 1000000000
1000000000 1000000000
3 1 1
1 2 2 1000000000
3 1 2

出力例 2

716070898
151723988

3 種類目のクエリでは 998244353 で割った余りを出力することに注意してください。

Score : 550 points

Problem Statement

You are given sequences of length N, A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
You are also given Q queries to process in order.

There are three types of queries:

  • 1 l r x : Add x to each of A_l, A_{l+1}, \ldots, A_r.
  • 2 l r x : Add x to each of B_l, B_{l+1}, \ldots, B_r.
  • 3 l r : Print the remainder of \displaystyle\sum_{i=l}^r (A_i\times B_i) when divided by 998244353.

Constraints

  • 1\leq N,Q\leq 2\times 10^5
  • 0\leq A_i,B_i\leq 10^9
  • 1\leq l\leq r\leq N
  • 1\leq x\leq 10^9
  • All input values are integers.
  • There is at least one query of the third type.

Input

The input is given from Standard Input in the following format. Here, \mathrm{query}_i (1\leq i\leq Q) is the i-th query to be processed.

N Q
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in one of the following formats:

1 l r x
2 l r x
3 l r

Output

If there are K queries of the third type, print K lines.
The i-th line (1\leq i\leq K) should contain the output for the i-th query of the third type.


Sample Input 1

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

Sample Output 1

16
25
84

Initially, A=(1,3,5,6,8) and B=(3,1,2,1,2). The queries are processed in the following order:

  • For the first query, print (1\times 3)+(3\times 1)+(5\times 2)=16 modulo 998244353, which is 16.
  • For the second query, add 3 to A_2,A_3,A_4,A_5. Now A=(1,6,8,9,11).
  • For the third query, print (1\times 3)+(6\times 1)+(8\times 2)=25 modulo 998244353, which is 25.
  • For the fourth query, add 1 to A_1,A_2,A_3. Now A=(2,7,9,9,11).
  • For the fifth query, add 2 to B_5. Now B=(3,1,2,1,4).
  • For the sixth query, print (2\times 3)+(7\times 1)+(9\times 2)+(9\times 1)+(11\times 4)=84 modulo 998244353, which is 84.

Thus, the first, second, and third lines should contain 16, 25, and 84, respectively.


Sample Input 2

2 3
1000000000 1000000000
1000000000 1000000000
3 1 1
1 2 2 1000000000
3 1 2

Sample Output 2

716070898
151723988

Make sure to print the sum modulo 998244353 for the third type of query.

G - Stair-like Grid

Time Limit: 6 sec / Memory Limit: 1024 MiB

配点 : 650

問題文

N 行の特殊な形をしたグリッドがあります。(N は偶数) 上から i 行目にはマスが左端から \left \lceil \frac{i}{2} \right \rceil \times 2 マス並んでいます。
例えば N = 6 の時は下図のような形をしています。

image

上から i 行目、左から j 列目のマスを (i, j) と表します。
各マスは空きマスと壁マスのいずれかです。壁マスは M 個あり、i 個目の壁マスは (a_i, b_i) にあります。ただし (1, 1)(N, N) は空きマスです。
(1, 1) を出発して、右または下に隣り合う空きマスへの移動を繰り返して (N, N) へ辿り着く方法は何通りありますか?答えを 998244353 で割った余りを求めてください。

制約

  • 2 \leq N \leq 2.5 \times 10^5
  • N は偶数
  • 0 \leq M \leq 50
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq \left \lceil \frac{a_i}{2} \right \rceil \times 2
  • (a_i, b_i) \neq (1, 1) かつ (a_i, b_i) \neq (N, N)
  • i \neq j ならば (a_i, b_i) \neq (a_j, b_j)
  • 入力される値は全て整数

入力

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

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

出力

(1, 1) を出発して、右または下に隣り合う空きマスへの移動を繰り返して (N, N) へ辿り着く方法の個数を 998244353 で割った余りを出力せよ。


入力例 1

4 2
2 1
4 2

出力例 1

2

問題文の条件を満たす経路は次の 2 通りです。

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

入力例 2

6 3
2 1
3 3
4 2

出力例 2

0

入力例 3

100 10
36 9
38 5
38 30
45 1
48 40
71 52
85 27
86 52
92 34
98 37

出力例 3

619611437

入力例 4

100000 10
552 24
4817 255
7800 954
23347 9307
28028 17652
39207 11859
48670 22013
74678 53158
75345 45891
88455 4693

出力例 4

175892766

Score : 650 points

Problem Statement

There is a special grid with N rows. (N is even.) The i-th row from the top has \left \lceil \frac{i}{2} \right \rceil \times 2 cells from the left end.
For example, when N = 6, the grid looks like the following:

image

Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
Each cell is either an empty cell or a wall cell. There are M wall cells, and the i-th wall cell is (a_i, b_i). Here, (1, 1) and (N, N) are empty.
Starting from (1, 1), how many ways are there to reach (N, N) by repeatedly moving right or down to an adjacent empty cell? Find the count modulo 998244353.

Constraints

  • 2 \leq N \leq 2.5 \times 10^5
  • N is even.
  • 0 \leq M \leq 50
  • 1 \leq a_i \leq N
  • 1 \leq b_i \leq \left \lceil \frac{a_i}{2} \right \rceil \times 2
  • (a_i, b_i) \neq (1, 1) and (a_i, b_i) \neq (N, N).
  • (a_i, b_i) \neq (a_j, b_j) if i \neq j.
  • All input values are integers.

Input

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

N M
a_1 b_1
a_2 b_2
\vdots
a_M b_M

Output

Print the number of ways to reach (N, N) from (1, 1) by repeatedly moving right or down to an adjacent empty cell, modulo 998244353.


Sample Input 1

4 2
2 1
4 2

Sample Output 1

2

There are two paths that satisfy the conditions of the problem:

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

Sample Input 2

6 3
2 1
3 3
4 2

Sample Output 2

0

Sample Input 3

100 10
36 9
38 5
38 30
45 1
48 40
71 52
85 27
86 52
92 34
98 37

Sample Output 3

619611437

Sample Input 4

100000 10
552 24
4817 255
7800 954
23347 9307
28028 17652
39207 11859
48670 22013
74678 53158
75345 45891
88455 4693

Sample Output 4

175892766