A - Merge and Increment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700

問題文

次の条件を満たす整数列を 良い数列 と呼びます。

  • 次の マージ操作0 回以上自由な回数繰り返して、数列の長さを 1 にすることができる。
    • 値の等しい要素が 2 個連続している箇所を選ぶ。その要素の値を x とする。2 個の要素を両方取り除き、要素があった場所に x+1 を挿入する。
      例えば (1, 3, 3, 5) という数列の前から 2 番目と 3 番目の要素を選んでマージ操作を行うと、操作後の数列は (1, 4, 5) になる。

長さ N の整数列 A = (A_1, A_2, \dots, A_N) があります。
あなたは以下の 挿入操作0 回以上自由な回数繰り返すことができます。

  • 整数 x を自由に選ぶ。A の自由な位置に x1 個挿入する。(位置は先頭および末尾でもよい)

A を良い数列にするには最小で何回挿入操作を行う必要がありますか?なお、制約を満たす任意の N および A に対して、有限回の挿入操作で A を良い数列にすることが可能です。

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

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 全てのテストケースに対する N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 番目のテストケースを意味する。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

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

N
A_1 A_2 \dots A_N

出力

T 行出力せよ。i 行目には i 番目のテストケースの答えを出力せよ。
各テストケースでは、A を良い数列にするために必要な最小の挿入操作の回数を出力せよ。


入力例 1

4
3
4 2 2
1
1
5
1 3 5 4 2
10
766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 122410709 549392045

出力例 1

1
0
6
1889132222

1 番目のテストケースについて、A の末尾に 3 を挿入してできる数列 (4, 2, 2, 3) は以下の手順でマージ操作を行うことで数列の長さを 1 にできるので良い数列です。

  • 前から 2 番目と 3 番目の 2 を取り除き、2+1=3 を挿入する。数列は (4, 3, 3) になる。
  • 前から 2 番目と 3 番目の 3 を取り除き、3+1=4 を挿入する。数列は (4, 4) になる。
  • 前から 1 番目と 2 番目の 4 を取り除き、4+1=5 を挿入する。数列は (5) になる。

1 回未満の挿入操作で A を良い数列にすることはできないので、答えは 1 回です。

Score : 700 points

Problem Statement

An integer sequence that satisfies the following condition is called a good sequence.

  • It is possible to perform the following merge operation zero or more times to make the sequence length 1.
    • Choose a location where two consecutive elements have equal values. Let x be the value of those elements. Remove both elements and insert x+1 at the location where the elements were.
      For example, if we perform a merge operation on the 2nd and 3rd elements of the sequence (1, 3, 3, 5), the sequence after the operation will be (1, 4, 5).

There is a length-N integer sequence A = (A_1, A_2, \dots, A_N).
You can perform the following insertion operation zero or more times.

  • Choose any integer x. Insert one x at any position in A (possibly the beginning or end).

What is the minimum number of insertion operations needed to make A a good sequence? For any N and A satisfying the constraints, it is possible to make A a good sequence with a finite number of insertion operations.

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

Constraints

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

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i means the i-th test case.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
A_1 A_2 \dots A_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, output the minimum number of insertion operations needed to make A a good sequence.


Sample Input 1

4
3
4 2 2
1
1
5
1 3 5 4 2
10
766294630 440423914 59187620 725560241 585990757 965580536 623321126 550925214 122410709 549392045

Sample Output 1

1
0
6
1889132222

For the 1st test case, the sequence (4, 2, 2, 3) obtained by inserting 3 at the end of A is a good sequence because it can be reduced to length 1 by performing merge operations in the following steps.

  • Remove the 2s at the 2nd and 3rd positions and insert 2+1=3. The sequence becomes (4, 3, 3).
  • Remove the 3s at the 2nd and 3rd positions and insert 3+1=4. The sequence becomes (4, 4).
  • Remove the 4s at the 1st and 2nd positions and insert 4+1=5. The sequence becomes (5).

It is impossible to make A a good sequence with fewer than one insertion operation, so the answer is 1.

B - Japanese "Knight's Tour"

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800

問題文

H マス、横 W マスの将棋盤と 1 個の桂馬の駒があります。将棋盤の上から i 行目、左から j 列目にあるマスを (i-1, j-1) と表します。(値が 1 ずれている点に注意してください。)
この将棋盤はトーラス、すなわち上と下および左と右がつながっています。(i, j) にある桂馬は ((i-2) \bmod H, (j-1) \bmod W) または ((i-2) \bmod H, (j+1) \bmod W) に動かすことができます。

次の条件を満たすように桂馬を将棋盤の上で動かすことを ツアー と呼びます。

  • はじめ、(0, 0) に桂馬を置く。その後、桂馬が全てのマスにちょうど 1 回ずつ移動するように H \times W 回桂馬を動かす。そして最終的に桂馬が (0, 0) に戻ってくる。

ツアーが何通りあるかを 998244353 で割った余りを求めてください。ただし、2 つのツアーはある整数 i (1 \leq i \leq H\times W) が存在して i 回目の移動先が異なる場合に異なるとみなします。

制約

  • 3 \leq H \leq 2 \times 10^5
  • 3 \leq W \leq 2 \times 10^5
  • 入力される値は全て整数

入力

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

H W

出力

ツアーが何通りあるかを 998244353 で割った余りを出力せよ。


入力例 1

3 3

出力例 1

6

例えば (0,0) \to (1,1) \to (2,2) \to (0,1) \to (1,2) \to (2,0) \to (0,2) \to (1,0) \to (2,1) \to (0,0) という移動はツアーとして条件を満たします。
ツアーは全部で 6 通りあります。


入力例 2

123 45

出力例 2

993644157

入力例 3

6789 200000

出力例 3

152401277

Score : 800 points

Problem Statement

There is a shogi (Japanese chess) board with H rows and W columns, and one knight piece. The square at the i-th row from the top and j-th column from the left of the board is represented as (i-1, j-1). (Note that the values are shifted by 1.)
This board is a torus, meaning the top and bottom are connected, and the left and right are connected. A knight at (i, j) can move to ((i-2) \bmod H, (j-1) \bmod W) or ((i-2) \bmod H, (j+1) \bmod W). (Note that this is different from the knight in Standard chess.)

Moving the knight on the board to satisfy the following condition is called a tour.

  • Initially, place the knight at (0, 0). Then, move the knight H \times W times so that the knight visits every square exactly once. Finally, the knight returns to (0, 0).

Find the number of tours modulo 998244353. Two tours are considered different if and only if there exists an integer i (1 \leq i \leq H\times W) such that the destination of the i-th move is different.

Constraints

  • 3 \leq H \leq 2 \times 10^5
  • 3 \leq W \leq 2 \times 10^5
  • All input values are integers.

Input

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

H W

Output

Output the number of tours modulo 998244353.


Sample Input 1

3 3

Sample Output 1

6

For example, the movement (0,0) \to (1,1) \to (2,2) \to (0,1) \to (1,2) \to (2,0) \to (0,2) \to (1,0) \to (2,1) \to (0,0) satisfies the conditions as a tour.
There are six tours in total.


Sample Input 2

123 45

Sample Output 2

993644157

Sample Input 3

6789 200000

Sample Output 3

152401277
C - Repunits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 900

問題文

正整数 n に対して R_n を「1n 個並べてできる文字列を 10 進表記された整数と解釈したもの」として定義します。例えば R_3 = 111 です。

正整数列 A = (A_1, A_2, \dots, A_N) が与えられます。
k = 1, 2, \dots, N について \mathrm{LCM}(R_{A_1}, R_{A_2}, \dots, R_{A_k}) \bmod 998244353 を計算してください。ここで \mathrm{LCM} は最小公倍数を計算する関数とします。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

N 行出力せよ。k 行目には \mathrm{LCM}(R_{A_1}, R_{A_2}, \dots, R_{A_k}) \bmod 998244353 を出力せよ。


入力例 1

3
2 4 6

出力例 1

11
1111
11222211

k=1 について、\mathrm{LCM}(11) \bmod 998244353 = 11 です。
k=2 について、\mathrm{LCM}(11,1111) \bmod 998244353= 1111 です。
k=3 について、\mathrm{LCM}(11,1111,111111) \bmod 998244353= 11222211 です。


入力例 2

1
200000

出力例 2

202819780

入力例 3

10
47718 21994 98917 104184 160670 190107 125377 29127 7017 177076

出力例 3

429620650
844699313
355160870
608402385
858856681
605347397
566966598
429324494
370941155
567238109

Score : 900 points

Problem Statement

For a positive integer n, define R_n as "the integer obtained by interpreting a string of n consecutive 1s as a decimal number". For example, R_3 = 111.

You are given a positive integer sequence A = (A_1, A_2, \dots, A_N).
For k = 1, 2, \dots, N, calculate \mathrm{LCM}(R_{A_1}, R_{A_2}, \dots, R_{A_k}) \bmod 998244353. Here, \mathrm{LCM} is the function that calculates the least common multiple.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 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 N lines. The k-th line should contain \mathrm{LCM}(R_{A_1}, R_{A_2}, \dots, R_{A_k}) \bmod 998244353.


Sample Input 1

3
2 4 6

Sample Output 1

11
1111
11222211

For k=1, \mathrm{LCM}(11) \bmod 998244353 = 11.
For k=2, \mathrm{LCM}(11,1111) \bmod 998244353= 1111.
For k=3, \mathrm{LCM}(11,1111,111111) \bmod 998244353= 11222211.


Sample Input 2

1
200000

Sample Output 2

202819780

Sample Input 3

10
47718 21994 98917 104184 160670 190107 125377 29127 7017 177076

Sample Output 3

429620650
844699313
355160870
608402385
858856681
605347397
566966598
429324494
370941155
567238109
D - King

Time Limit: 15 sec / Memory Limit: 1024 MiB

配点 : 1000

問題文

H マス、横 W マスの将棋盤と 1 個の王将の駒があります。将棋盤の上から i 行目、左から j 列目にあるマスを (i, j) と表します。
はじめ、王将は (A, B) に置かれています。あなたは次の操作をちょうど T 回行います。

  • 王将を現在あるマスの 8 近傍のマスに動かす。厳密に言うと、王将があるマスを (i, j) としたとき、(i+1,j+1), (i+1,j), (i+1,j-1), (i,j+1), (i,j-1), (i-1,j+1), (i-1,j), (i-1,j-1) のいずれかに王将を動かす。ただし、盤の外に出るような移動はできない。

T 回の操作を全て終了した時点で王将が (C, D) にあるような操作列の個数を 998244353 で割った余りを求めてください。ただし、2 つの操作列はある整数 i (1 \leq i \leq T) が存在して i 回目の操作を終了した時点での王将があるマスが異なる場合に異なるとみなします。

制約

  • 2 \leq H \leq 3 \times 10^5
  • 2 \leq W \leq 3 \times 10^5
  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq A \leq H
  • 1 \leq B \leq W
  • 1 \leq C \leq H
  • 1 \leq D \leq W
  • 入力される値は全て整数

入力

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

H W T A B C D

出力

T 回の操作を全て終了した時点で王将が (C, D) にあるような操作列の個数を 998244353 で割った余りを出力せよ。


入力例 1

3 4 3 2 1 3 4

出力例 1

5

問題文の条件を満たす操作列は全部で 5 個あります。各操作列に対応する王将の移動は次の通りです。

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

入力例 2

202 123 456 20 25 7 20

出力例 2

167373259

Score : 1000 points

Problem Statement

There is a shogi (Japanese chess) board with H rows and W columns, and one king piece. The square at the i-th row from the top and j-th column from the left of the board is represented as (i, j).
Initially, the king is placed at (A, B). You perform the following operation exactly T times.

  • Move the king to one of the eight adjacent squares of the current square. More precisely, if the king is at square (i, j), move the king to one of (i+1,j+1), (i+1,j), (i+1,j-1), (i,j+1), (i,j-1), (i-1,j+1), (i-1,j), (i-1,j-1). The king cannot leave the board.

Find the number, modulo 998244353, of operation sequences such that the king is at (C, D) after completing all T operations. Two operation sequences are considered different if and only if there exists an integer i (1 \leq i \leq T) such that the king's position after the i-th operation is different.

Constraints

  • 2 \leq H \leq 3 \times 10^5
  • 2 \leq W \leq 3 \times 10^5
  • 1 \leq T \leq 3 \times 10^5
  • 1 \leq A \leq H
  • 1 \leq B \leq W
  • 1 \leq C \leq H
  • 1 \leq D \leq W
  • All input values are integers.

Input

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

H W T A B C D

Output

Output the number, modulo 998244353, of operation sequences such that the king is at (C, D) after completing all T operations.


Sample Input 1

3 4 3 2 1 3 4

Sample Output 1

5

There are five operation sequences that satisfy the conditions in the problem statement. The king's movements corresponding to each operation sequence are as follows.

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

Sample Input 2

202 123 456 20 25 7 20

Sample Output 2

167373259