A - Apple

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

果物屋さんでりんごが売られています。
あなたは次の操作を好きな順で好きなだけ繰り返すことができます。

  • X 円を払ってりんごを 1 個手に入れる。
  • Y 円を払ってりんごを 3 個手に入れる。

りんごをちょうど N 個手に入れるには最低何円必要ですか?

制約

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • 入力される値はすべて整数

入力

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

X Y N

出力

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


入力例 1

10 25 10

出力例 1

85

25 円払って 3 個のりんごを手に入れる操作を 3 回繰り返した後、10 円払って 1 個のりんごを手に入れると丁度 10 個のりんごを手に入れられます。このときあなたは 85 円を消費します。
これより少ない金額でちょうど 10 個のりんごを手に入れることはできないので、答えは 85 円になります。


入力例 2

10 40 10

出力例 2

100

10 円払って 1 個のりんごを手に入れる操作を 10 回繰り返すのが最適です。


入力例 3

100 100 2

出力例 3

200

100 円を払って 1 個のりんごを手に入れる操作を 2 回繰り返す以外に ちょうど 2 個のりんごを手に入れる方法はありません。


入力例 4

100 100 100

出力例 4

3400

Score : 100 points

Problem Statement

A fruit store sells apples.
You may perform the following operations as many times as you want in any order:

  • Buy one apple for X yen (the currency in Japan).
  • Buy three apples for Y yen.

How much yen do you need to pay to obtain exactly N apples?

Constraints

  • 1 \leq X \leq Y \leq 100
  • 1 \leq N \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y N

Output

Print the answer as an integer.


Sample Input 1

10 25 10

Sample Output 1

85

Buy three apples for 25 yen three times and one apple for 10 yen, and you will obtain exactly 10 apples for a total of 85 yen.
You cannot obtain exactly 10 apples for a lower cost, so the answer is 85 yen.


Sample Input 2

10 40 10

Sample Output 2

100

It is optimal to buy an apple for 10 yen 10 times.


Sample Input 3

100 100 2

Sample Output 3

200

The only way to obtain exactly 2 apples is to buy an apple for 100 yen twice.


Sample Input 4

100 100 100

Sample Output 4

3400
B - Explore

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君はゲームの中で洞窟を探索しています。

洞窟は N 個の部屋が一列に並んだ構造であり、入り口から順に部屋 1,2,\ldots,N と番号がついています。

最初、高橋君は部屋 1 におり、持ち時間T です。
1 \leq i \leq N-1 について、持ち時間を A_i 消費することで、部屋 i から部屋 i+1 へ移動することができます。これ以外に部屋を移動する方法はありません。 また、持ち時間が 0 以下になるような移動は行うことができません。

洞窟内には M 個のボーナス部屋があります。i 番目のボーナス部屋は部屋 X_i であり、この部屋に到達すると持ち時間が Y_i 増加します。

高橋君は部屋 N にたどりつくことができますか?

制約

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq N-2
  • 1 \leq T \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 1 < X_1 < \ldots < X_M < N
  • 1 \leq Y_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

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

N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

高橋君が部屋 N にたどりつくことができるなら Yes を、できないなら No を出力せよ。


入力例 1

4 1 10
5 7 5
2 10

出力例 1

Yes
  • 高橋君は最初、部屋 1 にいて持ち時間は 10 です。
  • 持ち時間を 5 消費して部屋 2 に移動します。持ち時間は 5 になります。その後、持ち時間が 10 増え 15 になります。
  • 持ち時間を 7 消費して部屋 3 に移動します。持ち時間は 8 になります。
  • 持ち時間を 5 消費して部屋 4 に移動します。持ち時間は 3 になります。

入力例 2

4 1 10
10 7 5
2 10

出力例 2

No

部屋 1 から部屋 2 へ移動することができません。

Score : 200 points

Problem Statement

Takahashi is exploring a cave in a video game.

The cave consists of N rooms arranged in a row. The rooms are numbered Room 1,2,\ldots,N from the entrance.

Takahashi is initially in Room 1, and the time limit is T.
For each 1 \leq i \leq N-1, he may consume a time of A_i to move from Room i to Room (i+1). There is no other way to move between rooms. He cannot make a move that makes the time limit 0 or less.

There are M bonus rooms in the cave. The i-th bonus room is Room X_i; when he arrives at the room, the time limit increases by Y_i.

Can Takahashi reach Room N?

Constraints

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq N-2
  • 1 \leq T \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 1 < X_1 < \ldots < X_M < N
  • 1 \leq Y_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

If Takahashi can reach Room N, print Yes; otherwise, print No.


Sample Input 1

4 1 10
5 7 5
2 10

Sample Output 1

Yes
  • Takahashi is initially in Room 1, and the time limit is 10.
  • He consumes a time of 5 to move to Room 2. Now the time limit is 5. Then, the time limit increases by 10; it is now 15.
  • He consumes a time of 7 to move to Room 3. Now the time limit is 8.
  • He consumes a time of 5 to move to Room 4. Now the time limit is 3.

Sample Input 2

4 1 10
10 7 5
2 10

Sample Output 2

No

He cannot move from Room 1 to Room 2.

C - Belt Conveyor

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j}U, D, L, R のいずれかです。

あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。

あなたは現在 (i,j) にいるとする。
G_{i,j}U であり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j}D であり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j}L であり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j}R であり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。

操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1 を出力してください。

制約

  • 1 \leq H, W \leq 500
  • G_{i,j}U, D, L, R のいずれかである。
  • H, W は整数である。

入力

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

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

出力

操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。

i j

また、無限に動き続ける場合は -1 を出力せよ。


入力例 1

2 3
RDU
LRU

出力例 1

1 3

あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。


入力例 2

2 3
RRD
ULL

出力例 2

-1

あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1 を出力します。


入力例 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

出力例 3

9 5

Score : 300 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U, D, L, or R.

You are initially at (1,1). You repeat the following operation until you cannot make a move.

Let (i,j) be the square you are currently at.
If G_{i,j} is U and i \neq 1, move to (i-1,j).
If G_{i,j} is D and i \neq H, move to (i+1,j).
If G_{i,j} is L and j \neq 1, move to (i,j-1).
If G_{i,j} is R and j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.

Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.

Constraints

  • 1 \leq H, W \leq 500
  • G_{i,j} is U, D, L, or R.
  • H and W are integers.

Input

Input is given from Standard Input in the following format:

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

Output

If you end up at (i, j), print it in the following format:

i j

If you indefinitely repeat moving, print -1.


Sample Input 1

2 3
RDU
LRU

Sample Output 1

1 3

You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).


Sample Input 2

2 3
RRD
ULL

Sample Output 2

-1

You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1 should be printed in this case.


Sample Input 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

Sample Output 3

9 5
D - Iroha and Haiku (New ABC Edition)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

長さ N の数列 A=(A_0,\ldots,A_{N-1}) があります。
次の条件を全て満たす整数の組 (x,y,z,w) が存在するか判定してください。

  • 0 \leq x < y < z < w \leq N
  • A_x + A_{x+1} + \ldots + A_{y-1} = P
  • A_y + A_{y+1} + \ldots + A_{z-1} = Q
  • A_z + A_{z+1} + \ldots + A_{w-1} = R

制約

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq P,Q,R \leq 10^{15}
  • 入力に含まれる値は全て整数である

入力

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

N P Q R
A_0 A_1 \ldots A_{N-1}

出力

条件を満たす組が存在するなら Yes、存在しないなら No を出力せよ。


入力例 1

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

出力例 1

Yes

(x,y,z,w)=(1,3,6,8) が条件を満たします。


入力例 2

9 100 101 100
31 41 59 26 53 58 97 93 23

出力例 2

No

入力例 3

7 1 1 1
1 1 1 1 1 1 1

出力例 3

Yes

Score : 400 points

Problem Statement

There is a sequence A=(A_0,\ldots,A_{N-1}) of length N.
Determine if there exists a tuple of integers (x,y,z,w) that satisfies all of the following conditions:

  • 0 \leq x < y < z < w \leq N
  • A_x + A_{x+1} + \ldots + A_{y-1} = P
  • A_y + A_{y+1} + \ldots + A_{z-1} = Q
  • A_z + A_{z+1} + \ldots + A_{w-1} = R

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq P,Q,R \leq 10^{15}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N P Q R
A_0 A_1 \ldots A_{N-1}

Output

If there exists a tuple that satisfies the conditions, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

(x,y,z,w)=(1,3,6,8) satisfies the conditions.


Sample Input 2

9 100 101 100
31 41 59 26 53 58 97 93 23

Sample Output 2

No

Sample Input 3

7 1 1 1
1 1 1 1 1 1 1

Sample Output 3

Yes
E - Warp

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

2 次元平面の原点に高橋君がいます。
高橋君はこれから、ワープを N 回繰り返します。各ワープでは、以下の 3 つのうちいずれか 1 つを行います。

  • 現在いる座標 (x,y) から (x+A,y+B) に移動する
  • 現在いる座標 (x,y) から (x+C,y+D) に移動する
  • 現在いる座標 (x,y) から (x+E,y+F) に移動する

平面上の M 箇所 (X_1,Y_1),\ldots,(X_M,Y_M) には障害物があり、これらの座標に移動することはできません。

N 回のワープによる移動経路として考えられるものは何通りですか? 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N \leq 300
  • 0 \leq M \leq 10^5
  • -10^9 \leq A,B,C,D,E,F \leq 10^9
  • (A,B),(C,D),(E,F) は相異なる
  • -10^9 \leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq(0,0)
  • (X_i,Y_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N M
A B C D E F
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

出力

答えを出力せよ。


入力例 1

2 2
1 1 1 2 1 3
1 2
2 2

出力例 1

5

以下の 5 通りが考えられます。

  • (0,0)\to(1,1)\to(2,3)
  • (0,0)\to(1,1)\to(2,4)
  • (0,0)\to(1,3)\to(2,4)
  • (0,0)\to(1,3)\to(2,5)
  • (0,0)\to(1,3)\to(2,6)

入力例 2

10 3
-1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000
-1000000000 -1000000000
1000000000 1000000000
-1000000000 1000000000

出力例 2

0

入力例 3

300 0
0 0 1 0 0 1

出力例 3

292172978

Score : 500 points

Problem Statement

Takahashi is at the origin of a two-dimensional plane.
Takahashi will repeat teleporting N times. In each teleportation, he makes one of the following moves:

  • Move from the current coordinates (x,y) to (x+A,y+B)
  • Move from the current coordinates (x,y) to (x+C,y+D)
  • Move from the current coordinates (x,y) to (x+E,y+F)

There are obstacles on M points (X_1,Y_1),\ldots,(X_M,Y_M) on the plane; he cannot teleport to these coordinates.

How many paths are there resulting from the N teleportations? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 300
  • 0 \leq M \leq 10^5
  • -10^9 \leq A,B,C,D,E,F \leq 10^9
  • (A,B), (C,D), and (E,F) are distinct.
  • -10^9 \leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq(0,0)
  • (X_i,Y_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A B C D E F
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M

Output

Print the answer.


Sample Input 1

2 2
1 1 1 2 1 3
1 2
2 2

Sample Output 1

5

The following 5 paths are possible:

  • (0,0)\to(1,1)\to(2,3)
  • (0,0)\to(1,1)\to(2,4)
  • (0,0)\to(1,3)\to(2,4)
  • (0,0)\to(1,3)\to(2,5)
  • (0,0)\to(1,3)\to(2,6)

Sample Input 2

10 3
-1000000000 -1000000000 1000000000 1000000000 -1000000000 1000000000
-1000000000 -1000000000
1000000000 1000000000
-1000000000 1000000000

Sample Output 2

0

Sample Input 3

300 0
0 0 1 0 0 1

Sample Output 3

292172978
F - Manhattan Cafe

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 次元空間上の 2x=(x_1, x_2, \dots, x_N), y = (y_1, y_2, \dots, y_N) のマンハッタン距離 d(x,y) は次の式で定義されます。

\displaystyle d(x,y)=\sum_{i=1}^n \vert x_i - y_i \vert

また、座標成分 x_1, x_2, \dots, x_N がすべて整数であるような点 x=(x_1, x_2, \dots, x_N) を格子点と呼びます。

N 次元空間上の格子点 p=(p_1, p_2, \dots, p_N), q = (q_1, q_2, \dots, q_N) が与えられます。
d(p,r) \leq D かつ d(q,r) \leq D であるような格子点 r としてあり得るものは全部で何個ありますか?答えを 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N \leq 100
  • 0 \leq D \leq 1000
  • -1000 \leq p_i, q_i \leq 1000
  • 入力される値はすべて整数

入力

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

N D 
p_1 p_2 \dots p_N
q_1 q_2 \dots q_N

出力

答えを出力せよ。


入力例 1

1 5
0
3

出力例 1

8

N=1 の場合は 1 次元空間、すなわち数直線上の点に関する問題になります。
条件を満たす点は -2,-1,0,1,2,3,4,58 個です。


入力例 2

3 10
2 6 5
2 1 2

出力例 2

632

入力例 3

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

出力例 3

145428186

Score : 500 points

Problem Statement

In an N-dimensional space, the Manhattan distance d(x,y) between two points x=(x_1, x_2, \dots, x_N) and y = (y_1, y_2, \dots, y_N) is defined by:

\displaystyle d(x,y)=\sum_{i=1}^n \vert x_i - y_i \vert.

A point x=(x_1, x_2, \dots, x_N) is said to be a lattice point if the components x_1, x_2, \dots, x_N are all integers.

You are given lattice points p=(p_1, p_2, \dots, p_N) and q = (q_1, q_2, \dots, q_N) in an N-dimensional space.
How many lattice points r satisfy d(p,r) \leq D and d(q,r) \leq D? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq D \leq 1000
  • -1000 \leq p_i, q_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D 
p_1 p_2 \dots p_N
q_1 q_2 \dots q_N

Output

Print the answer.


Sample Input 1

1 5
0
3

Sample Output 1

8

When N=1, we consider points in a one-dimensional space, that is, on a number line.
8 lattice points satisfy the conditions: -2,-1,0,1,2,3,4,5.


Sample Input 2

3 10
2 6 5
2 1 2

Sample Output 2

632

Sample Input 3

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

Sample Output 3

145428186
G - 012 Inversion

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

各要素が 0,1,2 のいずれかである長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。
Q 個のクエリを順に処理してください。各クエリは以下の 2 種類のいずれかです。

  • 1 L R:数列 (A_L,\ldots,A_R) の転倒数を出力する
  • 2 L R S T UL\leq i \leq R を満たす各 i について、A_i0 なら S に、1 なら T に、2 なら U に置き換える
転倒数とは? 数列 B = (B_1, \ldots, B_M) の転倒数とは、整数の組 (i, j) (1 \leq i < j \leq M) であって B_i > B_j を満たすものの個数です。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 2
  • 1\leq Q\leq 10^5
  • 各クエリにおいて、1\leq L \leq R \leq N
  • 2 種類目のクエリにおいて、0\leq S,T,U \leq 2
  • 入力に含まれる値は全て整数である

入力

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

N Q
A_1 A_2 \ldots A_N
\rm Query_1
\rm Query_2
\vdots
\rm Query_Q

ここで i 番目のクエリを表す \rm Query_i は以下のいずれかの形式で与えられる。

1 L R
2 L R S T U

出力

1 種類目のクエリに対する答えを順に改行区切りで出力せよ。


入力例 1

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

出力例 1

3
4

最初 A=(2,0,2,1,0) です。

  • 1 番目のクエリにおいて、(A_2,A_3,A_4,A_5)=(0,2,1,0) の転倒数 3 を出力します。
  • 2 番目のクエリを処理すると、A=(2,2,0,1,0) となります。
  • 3 番目のクエリにおいて、(A_2,A_3,A_4,A_5)=(2,0,1,0) の転倒数 4 を出力します。

入力例 2

3 3
0 1 2
1 1 1
2 1 3 0 0 0
1 1 3

出力例 2

0
0

Score : 600 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N. Each element is 0, 1, or 2.
Process Q queries in order. Each query is of one of the following kinds:

  • 1 L R: print the inversion number of the sequence (A_L,\ldots,A_R).
  • 2 L R S T U: for each i such that L\leq i \leq R, if A_i is 0, replace it with S; if A_i is 1, replace it with T; if A_i is 2, replace it with U.
What is the inversion number? The inversion number of a sequence B = (B_1, \ldots, B_M) is the number of pairs of integers (i, j) (1 \leq i < j \leq M) such that B_i > B_j.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 2
  • 1\leq Q\leq 10^5
  • In each query, 1\leq L \leq R \leq N.
  • In each query of the second kind, 0\leq S,T,U \leq 2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \ldots A_N
\rm Query_1
\rm Query_2
\vdots
\rm Query_Q

\rm Query_i denotes the i-th query, which is in one of the following formats:

1 L R
2 L R S T U

Output

Print the responses to the queries of the first kind in the given order, separated by newlines.


Sample Input 1

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

Sample Output 1

3
4

Initially, A=(2,0,2,1,0).

  • In the 1-st query, print the inversion number 3 of (A_2,A_3,A_4,A_5)=(0,2,1,0).
  • The 2-nd query makes A=(2,2,0,1,0).
  • In the 3-rd query, print the inversion number 4 of (A_2,A_3,A_4,A_5)=(2,0,1,0).

Sample Input 2

3 3
0 1 2
1 1 1
2 1 3 0 0 0
1 1 3

Sample Output 2

0
0
Ex - No-capture Lance Game

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 600

問題文

縦横 H \times W マスの将棋盤と 2 \times H 枚の将棋の香車の駒があります。これらを用いて次のようなゲームを考えます。
ゲームは先手と後手の二人によって行われ、以下に示す手順で進行します。

  • 初期状態では、先手の香車と後手の香車がすべての行に 1 枚ずつ配置されている。
  • 先手から始めて先手と後手で交互に自分の駒を動かしていく。ただし相手の駒を取る(盤面から取り除く)ことはできない
  • 先に全ての自分の駒を動かせなくなった方が負けとなり、そうでない方は勝ちになる。

香車の動かせる場所は次のように定めます。ここで (i,j) を上から i 行目、左から j 列目のマスとします。

  • k \lt j かつ (i,k),(i,k+1),\dots,(i,j-1) に双方の駒が存在しないとき、(i,j) にある先手の香車を (i,k) に動かすことが出来る。
  • k \gt j かつ (i,j+1),(i,j+2),\dots,(i,k) に双方の駒が存在しないとき、(i,j) にある後手の香車を (i,k) に動かすことが出来る。

例えば下の図では、3\times 9 の将棋盤の (1,7),(2,1),(3,4) に先手の香車が、(1,3),(2,7),(3,5) に後手の香車が置かれています。
(1,7) の先手の香車は (1,4),(1,5),(1,6) のいずれかのマスへ、(3,4) の先手の香車は (3,1),(3,2),(3,3) のいずれかのマスへ動かすことができます。(2,1) の先手の香車は動かすことができません。
将棋を知っている人は、通常の将棋に存在する「取る」「成る」などのルールは適用されないことに注意してください。

今、将棋盤の上には何もありません。先手の香車と後手の香車を、双方の香車が同じマスに置かれないように各行に 1 枚ずつ置く方法は \left\lbrace W(W-1)\right\rbrace^H 通りあります。そのうち次の条件を満たす配置は何通りありますか?答えを 998244353 で割ったあまりを求めてください。

  • その配置を初期配置としてゲームを開始して双方が最適にゲームを進めた時に先手が勝つことができる。

制約

  • 1 \leq H \leq 8000
  • 2 \leq W \leq 30
  • H, W は整数

入力

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

H W

出力

答えを出力せよ。


入力例 1

1 3

出力例 1

2

先手が勝てる配置は次の 2 通りです。

  • 先手の香車を (1, 3) に、後手の香車を (1, 1) に配置した場合。
  • 先手の香車を (1, 2) に、後手の香車を (1, 3) に配置した場合。

入力例 2

9 9

出力例 2

583962987

入力例 3

265 30

出力例 3

366114675

Score : 600 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and (2 \times H) pieces. We consider the following game using them.
Two players take alternating turns. The game progresses as follows.

  • In the initial state, every row contains one piece of the first player facing left and one piece of the second player facing right.
  • The two players alternately advance one of their pieces.
  • The player who is first to be unable to make a move loses, and the other player wins.

Let (i, j) denote the square at the i-th row from the top and j-th column from the left. The following moves are allowed:

  • The first player can move a piece at (i,j) to (i,k) if k \lt j and none of (i,k),(i,k+1),\dots,(i,j-1) contains a piece of either player.
  • The second player can move a piece at (i,j) to (i,k) if k \gt j and none of (i,j+1),(i,j+2),\dots,(i,k) contains a piece of either player.

For example, in the figure below, on a 3\times 9 grid, the first player's pieces are at (1,7),(2,1),(3,4), and the second player's pieces are at (1,3),(2,7),(3,5).
The first player can move the piece at (1,7) to (1,4),(1,5), or (1,6), and the piece at (3,4) to (3,1),(3,2), or (3,3). The first player cannot move the piece at (2,1).

Currently, there is no piece on the grid. There are \left\lbrace W(W-1)\right\rbrace^H ways to place one piece of the first player and one piece of the second player in each row, so that no two pieces are placed at the same square. How many of them satisfy the following condition? Find the count modulo 998244353.

  • When they play the game optimally from that initial state, the first player wins.

Constraints

  • 1 \leq H \leq 8000
  • 2 \leq W \leq 30
  • H and W are integers.

Input

Input is given from Standard Input in the following format:

H W

Output

Print the answer.


Sample Input 1

1 3

Sample Output 1

2

The first player can win if:

  • the first player's piece is placed at (1, 3), and the second player's piece is placed at (1, 1); or
  • the first player's piece is placed at (1, 2), and the second player's piece is placed at (1, 3).

Sample Input 2

9 9

Sample Output 2

583962987

Sample Input 3

265 30

Sample Output 3

366114675