A - 12435

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

(1,2,3,4,5) を並び替えた整数列 A=(A_1,A_2,A_3,A_4,A_5) が与えられます。

A の隣り合う 2 つの項を入れ替える操作を ちょうど 1 行うことで A を昇順にすることができるか判定してください。

制約

  • A(1,2,3,4,5) を並び替えてできる長さ 5 の整数列

入力

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

A_1 A_2 A_3 A_4 A_5

出力

ちょうど 1 回の操作で A を昇順にすることができるならば Yes を、できないならば No を出力せよ。


入力例 1

1 2 4 3 5

出力例 1

Yes

A_3A_4 を入れ替えることで A=(1,2,3,4,5) となり、 A を昇順に並び替えることができます。したがって、 Yes を出力してください。


入力例 2

5 3 2 4 1

出力例 2

No

どのような操作をしても A を昇順に並び替えることはできません。


入力例 3

1 2 3 4 5

出力例 3

No

ちょうど 1 回操作をする必要があります。


入力例 4

2 1 3 4 5

出力例 4

Yes

Score : 150 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,A_3,A_4,A_5) obtained by permuting (1,2,3,4,5).

Determine whether A can be sorted in ascending order by performing exactly one operation of swapping two adjacent elements in A.

Constraints

  • A is an integer sequence of length 5 obtained by permuting (1,2,3,4,5).

Input

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

A_1 A_2 A_3 A_4 A_5

Output

If A can be sorted in ascending order by exactly one operation, print Yes; otherwise, print No.


Sample Input 1

1 2 4 3 5

Sample Output 1

Yes

By swapping A_3 and A_4, A becomes (1,2,3,4,5), so it can be sorted in ascending order. Therefore, print Yes.


Sample Input 2

5 3 2 4 1

Sample Output 2

No

No matter what operation is performed, it is impossible to sort A in ascending order.


Sample Input 3

1 2 3 4 5

Sample Output 3

No

You must perform exactly one operation.


Sample Input 4

2 1 3 4 5

Sample Output 4

Yes
B - Geometric Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

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

A が等比数列であるか判定してください。

制約

  • 2\leq N\leq 100
  • 1\leq A_i\leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A が 等比数列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

5
3 6 12 24 48

出力例 1

Yes

A=(3,6,12,24,48) です。
A は初項 3, 公比 2, 項数 5 の等比数列となっています。
よって、Yes を出力します。


入力例 2

3
1 2 3

出力例 2

No

A=(1,2,3) です。
A_1:A_2=1:2\neq 2:3=A_2:A_3 より、A は等比数列ではありません。
よって、No を出力します。


入力例 3

2
10 8

出力例 3

Yes

A は初項 10, 公比 0.8, 項数 2 の等比数列となっています。
よって、Yes を出力します。

Score : 200 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\ldots,A_N) of positive integers.

Determine whether A is a geometric progression.

Constraints

  • 2 \leq N \leq 100
  • 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
A_1 A_2 \ldots A_N

Output

If A is a geometric progression, print Yes; otherwise, print No.


Sample Input 1

5
3 6 12 24 48

Sample Output 1

Yes

A=(3,6,12,24,48).
A is a geometric progression with first term 3, common ratio 2, and five terms.
Therefore, print Yes.


Sample Input 2

3
1 2 3

Sample Output 2

No

A=(1,2,3).
Since A_1 : A_2 = 1 : 2 \neq 2 : 3 = A_2 : A_3, A is not a geometric progression.
Therefore, print No.


Sample Input 3

2
10 8

Sample Output 3

Yes

A is a geometric progression with first term 10, common ratio 0.8, and two terms.
Therefore, print Yes.

C - Paint to make a rectangle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

HW 列のマス目が与えられます。
以下、上から i 行目 (1\leq i\leq H) かつ左から j 列目 (1\leq j\leq W) のマスをマス (i,j) で表します。
マス目の状態は H 個の長さ W の文字列 S_1,S_2, \ldots, S_H によって以下のように表されます。

  • S_ij 文字目が # のとき、マス (i,j) は黒く塗られている。
  • S_ij 文字目が . のとき、マス (i,j) は白く塗られている。
  • S_ij 文字目が ? のとき、マス (i,j) は塗られていない。

高橋君はまだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにしたいです。
より具体的には、ある 4 つの整数の組 (a,b,c,d) (1\leq a\leq b\leq H, 1\leq c\leq d\leq W) が存在して、次が成り立つようにしたいです。

マス (i,j) (1\leq i\leq H, 1\leq j\leq W) は、 a\leq i\leq b かつ c\leq j\leq d をみたすとき、黒く塗られている。
そうでないとき、白く塗られている。

そのようなことが可能か判定してください。

制約

  • 1\leq H,W\leq 1000
  • H, W は整数
  • S_i#, ., ? のみからなる長さ W の文字列
  • 黒く塗られたマスが 1 つ以上存在する。

入力

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

H W
S_1
S_2
\vdots
S_H

出力

まだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにできるならば Yes を、そうでないならば No を出力せよ。


入力例 1

3 5
.#?#.
.?#?.
?...?

出力例 1

Yes

マス目は以下の状態になっています。? のマスがまだ塗られていないマスです。

マス (1,3), (2,2), (2,4) を黒く塗り、マス (3,1), (3,5) を白く塗ることで、 以下のように黒マス全体が長方形をなすようにできます。

よって、Yes を出力します。


入力例 2

3 3
?##
#.#
##?

出力例 2

No

黒マス全体が長方形をなすためには、少なくともマス (2,2) を黒く塗る必要がありますがすでに白く塗られています。
よって、黒マス全体が長方形をなすようにマス目を塗ることはできないため、No を出力します。


入力例 3

1 1
#

出力例 3

Yes

Score : 300 points

Problem Statement

You are given a grid of H rows and W columns.
Let (i,j) denote the cell at row i (1 \leq i \leq H) from the top and column j (1 \leq j \leq W) from the left.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W, as follows:

  • If the j-th character of S_i is #, cell (i,j) is painted black.
  • If the j-th character of S_i is ., cell (i,j) is painted white.
  • If the j-th character of S_i is ?, cell (i,j) is not yet painted.

Takahashi wants to paint each not-yet-painted cell white or black so that all the black cells form a rectangle.
More precisely, he wants there to exist a quadruple of integers (a,b,c,d) (1 \leq a \leq b \leq H, 1 \leq c \leq d \leq W) such that:

For each cell (i,j) (1 \leq i \leq H, 1 \leq j \leq W), if a \leq i \leq b and c \leq j \leq d, the cell is black;
otherwise, the cell is white.

Determine whether this is possible.

Constraints

  • 1 \leq H, W \leq 1000
  • H and W are integers.
  • Each S_i is a string of length W consisting of #, ., ?.
  • There is at least one cell that is already painted black.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

If it is possible to paint all the not-yet-painted cells so that the black cells form a rectangle, print Yes; otherwise, print No.


Sample Input 1

3 5
.#?#.
.?#?.
?...?

Sample Output 1

Yes

The grid is in the following state. ? indicates a cell that are not yet painted.

By painting cells (1,3), (2,2), and (2,4) black and cells (3,1) and (3,5) white, the black cells can form a rectangle as follows:

Therefore, print Yes.


Sample Input 2

3 3
?##
#.#
##?

Sample Output 2

No

To form a rectangle with all black cells, you would need to paint cell (2,2) black, but it is already painted white.
Therefore, it is impossible to make all black cells form a rectangle, so print No.


Sample Input 3

1 1
#

Sample Output 3

Yes
D - Stone XOR

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

1, 袋 2, \ldots, 袋 N と番号づけられた N 個の袋があります。
i (1\leq i\leq N) には A_i 個の石が入っています。

高橋君は次の操作を好きなだけ(0 回でも良い)繰り返すことができます。

2 つの袋 A, B を選び、袋 A に入っている石を すべて 袋 B に入れる。

操作を繰り返した後の状態における次の値としてあり得るものが何個あるか求めてください。

  • i に入っている石の個数を B_i として、B_1\oplus B_2\oplus\cdots\oplus B_N の値。
    ただし、\oplus は排他的論理和を表す。
排他的論理和とは 非負整数 a,b の排他的論理和 a \oplus b は、以下のように定義されます。
a \oplus b を二進表記した際の 2^k (k \geq 0) の位の数は、 a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1 、 そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります(二進表記すると 011 \oplus 101 = 110)。
一般に k 個の非負整数 x_1, x_2, \ldots, x_k の排他的論理和 x_1\oplus x_2\oplus\cdots\oplus x_k(\cdots((x_1 \oplus x_2) \oplus x_3) \oplus \cdots \oplus x_k) と定義され、 これは x_1, x_2, \ldots, x_k の順番によらないことが証明できます。

なお、問題の制約下において、操作を繰り返した後の状態における上記の値としてあり得るものが有限個しかないことが証明できます。

制約

  • 2\leq N \leq 12
  • 1\leq A_i \leq 10^{17}
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

高橋君が操作を繰り返した後の状態における、問題文中で定義された値としてあり得るものの個数を出力せよ。


入力例 1

3
2 5 7

出力例 1

3

例えば、高橋君が袋 A, B として袋 1, 3 を選び、操作を行ったとすると、袋 1,2,3 に入っている石はそれぞれ 0,5,9 となります。
ここで高橋君が操作を終了したとき、この状態における、袋に入っている石の個数の排他的論理和は 0\oplus 5\oplus 9=12 となります。

他に、高橋君が操作を繰り返した後の状態における、袋に入っている石の個数の排他的論理和としてあり得るものは 0,14 があります。
よって、あり得る値は 0,12,143 個であるため、3 を出力します。


入力例 2

2
100000000000000000 100000000000000000

出力例 2

2

入力例 3

6
71 74 45 34 31 60

出力例 3

84

Score : 400 points

Problem Statement

There are N bags, labeled bag 1, bag 2, \ldots, bag N.
Bag i (1 \leq i \leq N) contains A_i stones.

Takahashi can perform the following operation any number of times, possibly zero:

Choose two bags A and B, and move all stones from bag A into bag B.

Find the number of different possible values for the following after repeating the operation.

  • B_1 \oplus B_2 \oplus \cdots \oplus B_N, where B_i is the final number of stones in bag i.
    Here, \oplus denotes bitwise XOR.
About bitwise XOR For non-negative integers a and b, the bitwise XOR a \oplus b is defined as follows:
In the binary representation of a \oplus b, the digit in the 2^k place (k \ge 0) is 1 if and only if exactly one of the digits in the 2^k place of a and b is 1; otherwise, it is 0.
For example, 3 \oplus 5 = 6 (in binary, 011 \oplus 101 = 110).
In general, for k non-negative integers x_1, x_2, \ldots, x_k, their bitwise XOR x_1 \oplus x_2 \oplus \cdots \oplus x_k is defined as (\cdots((x_1 \oplus x_2) \oplus x_3) \oplus \cdots) \oplus x_k, which does not depend on the order of x_1, x_2, \ldots, x_k.

It can be proved that under the constraints of this problem, the number of possible values is finite.

Constraints

  • 2 \leq N \leq 12
  • 1 \leq A_i \leq 10^{17}
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the number of different possible values for B_1 \oplus B_2 \oplus \cdots \oplus B_N after repeating the operation.


Sample Input 1

3
2 5 7

Sample Output 1

3

For example, if Takahashi chooses bags 1 and 3 for the operation, then the numbers of stones in bags 1, 2, 3 become 0, 5, 9.
If he stops at this point, the XOR is 0 \oplus 5 \oplus 9 = 12.

The other possible XOR values after repeating the operation are 0 and 14.
Therefore, the possible values are 0, 12, 14; there are three values, so the output is 3.


Sample Input 2

2
100000000000000000 100000000000000000

Sample Output 2

2

Sample Input 3

6
71 74 45 34 31 60

Sample Output 3

84
E - Vitamin Balance

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

N 個の食べ物があり、それぞれの食べ物にはビタミン 1,2,3 のうちちょうど 1 つのみが含まれています。
具体的には、i 個目の食べ物を食べると、ビタミン V_iA_i だけ摂取でき、またカロリーが C_i だけ摂取されます。

高橋君は、摂取するカロリーが合計で X 以下となるように、N 個の食べ物のうちいくつか(0 個でも良い)を選んで食べることができます。
このとき、「ビタミン 1,2,3 のうちもっとも摂取量が少ないものの摂取量」としてあり得る最大の値を求めてください。

制約

  • 1\leq N \leq 5000
  • 1\leq X \leq 5000
  • 1\leq V_i\leq 3
  • 1\leq A_i\leq 2\times 10^5
  • 1\leq C_i\leq X
  • 入力はすべて整数

入力

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

N X
V_1 A_1 C_1
V_2 A_2 C_2
\vdots
V_N A_N C_N

出力

高橋君が摂取カロリーの合計が X 以下となるように食べ物を食べたとき、「ビタミン 1,2,3 のうちもっとも摂取量が少ないものの摂取量」としてあり得る最大の値を出力せよ。


入力例 1

5 25
1 8 5
2 3 5
2 7 10
3 2 5
3 3 10

出力例 1

3

それぞれの食べ物を食べると高橋君は次のものを摂取します。

  • 1 個目の食べ物を食べると、ビタミン 18 とカロリーを 5 だけ摂取する。
  • 2 個目の食べ物を食べると、ビタミン 23 とカロリーを 5 だけ摂取する。
  • 3 個目の食べ物を食べると、ビタミン 27 とカロリーを 10 だけ摂取する。
  • 4 個目の食べ物を食べると、ビタミン 32 とカロリーを 5 だけ摂取する。
  • 5 個目の食べ物を食べると、ビタミン 33 とカロリーを 10 だけ摂取する。

高橋君が 1, 2, 4, 5 個目の食べ物を食べると、 高橋君はビタミン 18、ビタミン 23、ビタミン 35、そしてカロリーを 25 だけ摂取します。
このとき、「ビタミン 1,2,3 のうちもっとも摂取量が少ないもの」はビタミン 2 であり、その摂取量は 3 です。

どのように食べ物を選んで食べても、摂取カロリーの合計が 25 以下となるようにビタミン 1,2,3 すべてを 4 以上摂取することはできないため、3 を出力します。


入力例 2

2 5000
1 200000 1
2 200000 1

出力例 2

0

Score : 450 points

Problem Statement

There are N foods, each containing exactly one of vitamins 1, 2, and 3.
Specifically, eating the i-th food gives you A_i units of vitamin V_i, and C_i calories.

Takahashi can choose any subset of these N foods as long as the total calorie consumption does not exceed X.
Find the maximum possible value of this: the minimum intake among vitamins 1, 2, and 3.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq X \leq 5000
  • 1 \leq V_i \leq 3
  • 1 \leq A_i \leq 2 \times 10^5
  • 1 \leq C_i \leq X
  • All input values are integers.

Input

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

N X
V_1 A_1 C_1
V_2 A_2 C_2
\vdots
V_N A_N C_N

Output

Print the maximum possible value of "the minimum intake among vitamins 1, 2, and 3" when the total calories consumed is at most X.


Sample Input 1

5 25
1 8 5
2 3 5
2 7 10
3 2 5
3 3 10

Sample Output 1

3

Each food provides the following if eaten:

  • 1st food: 8 units of vitamin 1, and 5 calories
  • 2nd food: 3 units of vitamin 2, and 5 calories
  • 3rd food: 7 units of vitamin 2, and 10 calories
  • 4th food: 2 units of vitamin 3, and 5 calories
  • 5th food: 3 units of vitamin 3, and 10 calories

Eating the 1st, 2nd, 4th, and 5th foods gives 8 units of vitamin 1, 3 units of vitamin 2, 5 units of vitamin 3, and 25 calories.
In this case, the minimum among the three vitamin intakes is 3 (vitamin 2).

It is impossible to get 4 or more units of each vitamin without exceeding 25 calories, so the answer is 3.


Sample Input 2

2 5000
1 200000 1
2 200000 1

Sample Output 2

0
F - Double Sum 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

1\le L\le R\le N を満たす整数の組 (L,R) に対し f(L,R) を以下のように定義します。

  • 何も書かれていない黒板に R-L+1 個の整数 A_L,A_{L+1},\ldots,A_R を順に書き込む。
  • 以下の操作を黒板に書かれた整数が全て消えるまで繰り返す。
    • 整数 l,r を選ぶ。ただし、 l\le r かつ黒板に l 以上 r 以下の整数が全て 1 つ以上書かれているように l,r を選ぶ必要がある。その後、黒板に書かれた l 以上 r 以下の整数を全て消す。
  • 黒板に書かれた整数が全て消えるまでに必要な操作回数の最小値を f(L,R) とする。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R) を求めてください。

制約

  • 1\le N\le 3\times 10^5
  • 1\le A_i\le N
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
1 3 1 4

出力例 1

16

例えば (L,R)=(1,4) の場合、以下の手順で f(L,R) を計算することができます。

  • 黒板に 1,3,1,4 が書かれている。
  • (l,r)=(1,1) を選び、黒板に書かれた 1 を全て消す。黒板には 3,4 が書かれた状態になる。
  • (l,r)=(3,4) を選び、黒板に書かれた 3,4 を全て消す。黒板は何も書かれていない状態になる。
  • 2 回未満の操作で黒板の整数を全て消すことはできないので、f(1,4)=2 である。

同様の計算で、例えば f(2,4)=2, f(1,1)=1 なども分かります。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R)=16 なので、 16 を出力してください。


入力例 2

5
3 1 4 2 4

出力例 2

23

入力例 3

10
5 1 10 9 2 5 6 9 1 6

出力例 3

129

Score : 525 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

For each integer pair (L,R) with 1 \le L \le R \le N, define f(L,R) as follows:

  • Start with an empty blackboard. Write the R-L+1 integers A_L, A_{L+1}, \ldots, A_R on the blackboard in order.
  • Repeat the following operation until all integers on the blackboard are erased:
    • Choose integers l, r with l \le r such that every integer from l through r appears at least once on the blackboard. Then, erase all integers from l through r that are on the blackboard.
  • Let f(L,R) be the minimum number of such operations needed to erase all the integers from the blackboard.

Find \displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R).

Constraints

  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le N
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
1 3 1 4

Sample Output 1

16

For example, in the case of (L,R)=(1,4):

  • The blackboard has 1,3,1,4.
  • Choose (l,r)=(1,1) and erase all occurrences of 1. The blackboard now has 3,4.
  • Choose (l,r)=(3,4) and erase all occurrences of 3 and 4. The blackboard becomes empty.
  • It cannot be done in fewer than two operations, so f(1,4) = 2.

Similarly, you can find f(2,4)=2, f(1,1)=1, etc.

\displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R) = 16, so print 16.


Sample Input 2

5
3 1 4 2 4

Sample Output 2

23

Sample Input 3

10
5 1 10 9 2 5 6 9 1 6

Sample Output 3

129
G - Permutation Concatenation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

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

長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) に対し f(A) を次の手順で得られる整数とします。

  • S を空文字列とする。
  • i=1,2,\ldots,N の順番で以下の操作を行う。
    • A_i を先頭に余分な 0 を付けない十進数の文字列としてみたものを T とする。
    • S の末尾に T を連結する。
  • S を十進数の整数としてみた値を f(A) とする。

例えば A=(1,20,34) に対し f(A)=12034 です。

(1,2,\ldots,N) の順列 P は全部で N! 通りありますが、それら全てに対する f(P) の総和を 998244353 で割ったあまりを求めてください。

制約

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

入力

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

N

出力

答えを出力せよ。


入力例 1

3

出力例 1

1332

長さ 3 の順列は P=(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1)6 通りありますが、それらに対する f(P) の値はそれぞれ f(P)=123,132,213,231,312,321 です。よって、 123+132+213+231+312+321=1332 を出力してください。


入力例 2

390

出力例 2

727611652

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


入力例 3

79223

出力例 3

184895744

Score : 600 points

Problem Statement

You are given a positive integer N.

For an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. Let f(A) be the integer obtained as follows:

  • Let S be an empty string.
  • For i=1,2,\ldots,N in this order:
    • Let T be the decimal representation of A_i without leading zeros.
    • Append T to the end of S.
  • Interpret S as a decimal integer, and let that be f(A).

For example, if A=(1,20,34), then f(A)=12034.

There are N! permutations P of (1,2,\ldots,N). Find the sum, modulo 998244353, of f(P) over all such permutations P.

Constraints

  • 1 \le N \le 2 \times 10^5
  • All input values are integers.

Input

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

N

Output

Print the sum, modulo 998244353, of f(P) over all permutations P of (1,2,\ldots,N).


Sample Input 1

3

Sample Output 1

1332

The six permutations of (1,2,3) are (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1). Their f(P) values are 123,132,213,231,312,321. Therefore, print 123+132+213+231+312+321 = 1332.


Sample Input 2

390

Sample Output 2

727611652

Print the sum modulo 998244353.


Sample Input 3

79223

Sample Output 3

184895744