A - Floor, Ceil - Decomposition

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

黒板にひとつの整数 X が書かれています。あなたは次の操作を、何度でも行うことができます(一度も行わなくてもよいです)。

  • 黒板に書かれている整数 x をひとつ選ぶ。
  • x をひとつ黒板から消去し、新たに \lfloor \frac{x}{2}\rfloor\lceil \frac{x}{2}\rceil をひとつずつ黒板に書く。

操作後の黒板に書かれている整数すべての積としてありうる最大値を、998244353 で割った余りを答えてください。

\lfloor \frac{x}{2}\rfloor\lceil \frac{x}{2}\rceil とは?

実数 x に対して,x 以下の最大の整数を \lfloor x\rfloorx 以上の最小の整数を \lceil x\rceil と書きます。したがって例えば以下が成り立ちます。

  • x = 2 のとき、\lfloor \frac{x}{2}\rfloor = 1, \lceil \frac{x}{2}\rceil = 1
  • x = 3 のとき、\lfloor \frac{x}{2}\rfloor = 1, \lceil \frac{x}{2}\rceil = 2

制約

  • 1\leq X \leq 10^{18}

入力

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

X

出力

操作後の黒板に書かれている整数すべての積としてありうる最大値を、998244353 で割った余りを出力してください。


入力例 1

15

出力例 1

192

例えば次のように操作を行うことで、黒板に書かれている整数すべての積を 192 にすることが可能です。

  • はじめ、黒板は次の状態です:(15)
  • x = 15 として操作を行うことで、黒板は次の状態に変化します:(7, 8)
  • x = 7 として操作を行うことで、黒板は次の状態に変化します:(8, 3, 4)
  • x = 4 として操作を行うことで、黒板は次の状態に変化します:(8, 3, 2, 2)
  • x = 8 として操作を行うことで、黒板は次の状態に変化します:(3, 2, 2, 4, 4)。 このとき、黒板に書かれている整数すべての積は 3\times 2\times 2\times 4\times 4 = 192 です。

入力例 2

3

出力例 2

3

操作を一度も行わないことで、黒板に書かれている整数すべての積を 3 にすることが可能です。


入力例 3

100

出力例 3

824552442

操作後の黒板に書かれている整数すべての積としてありうる最大値は、5856458868470016 です。これを 998244353 で割った余りを出力します。

Score : 300 points

Problem Statement

There is an integer X written on a blackboard. You can do the operation below any number of times (possibly zero).

  • Choose an integer x written on the blackboard.
  • Erase one x from the blackboard and write two new integers \lfloor \frac{x}{2}\rfloor and \lceil \frac{x}{2}\rceil.

Find the maximum possible product of the integers on the blackboard after your operations, modulo 998244353.

What are \lfloor \frac{x}{2}\rfloor and \lceil \frac{x}{2}\rceil?

For a real number x, \lfloor x\rfloor denotes the largest integer not greater than x, and \lceil x\rceil denotes the smallest integer not less than x. For example, the following holds.

  • For x = 2, we have \lfloor \frac{x}{2}\rfloor = 1 and \lceil \frac{x}{2}\rceil = 1.
  • For x = 3, we have \lfloor \frac{x}{2}\rfloor = 1, \lceil \frac{x}{2}\rceil = 2.

Constraints

  • 1\leq X \leq 10^{18}

Input

Input is given from Standard Input from the following format:

X

Output

Print the maximum possible product of the integers on the blackboard after your operations, modulo 998244353.


Sample Input 1

15

Sample Output 1

192

Here is a sequence of operations that makes the product of the integers on the blackboard 192.

  • The initial state of the blackboard is (15).
  • An operation with x = 15 changes it to (7, 8).
  • An operation with x = 7 changes it to (8, 3, 4).
  • An operation with x = 4 changes it to (8, 3, 2, 2).
  • An operation with x = 8 changes it to (3, 2, 2, 4, 4).

Here, the product of the integers on the blackboard is 3\times 2\times 2\times 4\times 4 = 192.


Sample Input 2

3

Sample Output 2

3

Doing zero operations makes the product of the integers on the blackboard 3.


Sample Input 3

100

Sample Output 3

824552442

The maximum possible product of the integers on the blackboard after your operations is 5856458868470016, which should be printed modulo 998244353.

B - Sum of Three Terms

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

項数 N の整数列 S = (S_1, \ldots, S_N) が与えられます。 項数 N+2 の整数列 A = (A_1, \ldots, A_{N+2}) であって、次の条件を満たすものが存在するか否かを判定してください。

  • 任意の i (1\leq i\leq N+2) に対して 0\leq A_i が成り立つ。
  • 任意の i (1\leq i\leq N) に対して、S_i = A_{i} + A_{i+1} + A_{i+2} が成り立つ。

存在する場合には、そのようなものをひとつ出力してください。

制約

  • 1\leq N\leq 3\times 10^5
  • 0\leq S_i\leq 10^9

入力

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

N
S_1 \ldots S_N

出力

問題の条件を満たす整数列 A が存在するならば Yes を、そうでなければ No を出力してください。 Yes の場合には、2 行目にそのような整数列 A の各要素を、空白で区切って 1 行で出力してください。

A_1 \ldots A_{N+2}

条件を満たす整数列が複数存在する場合は、どれを出力しても正解となります。


入力例 1

5
6 9 6 6 5

出力例 1

Yes
0 4 2 3 1 2 2

以下のように、任意の i (1\leq i\leq N) に対して S_i = A_i + A_{i+1} + A_{i+2} が成り立つことが確認できます。

  • 6 = 0 + 4 + 2
  • 9 = 4 + 2 + 3
  • 6 = 2 + 3 + 1
  • 6 = 3 + 1 + 2
  • 5 = 1 + 2 + 2

入力例 2

5
0 1 2 1 0

出力例 2

No

入力例 3

1
10

出力例 3

Yes
0 0 10

Score : 500 points

Problem Statement

You are given a sequence of N integers S = (S_1, \ldots, S_N). Determine whether there is a sequence of N+2 integers A = (A_1, \ldots, A_{N+2}) that satisfies the conditions below.

  • 0\leq A_i for every i (1\leq i\leq N+2).
  • S_i = A_{i} + A_{i+1} + A_{i+2} for every i (1\leq i\leq N).

If it exists, print one such sequence.

Constraints

  • 1\leq N\leq 3\times 10^5
  • 0\leq S_i\leq 10^9

Input

Input is given from Standard Input from the following format:

N
S_1 \ldots S_N

Output

If there is a sequence A that satisfies the conditions, print Yes; otherwise, print No. In the case of Yes, print an additional line containing the elements of such a sequence A, separated by spaces.

A_1 \ldots A_{N+2}

If there are multiple sequences satisfying the conditions, you may print any of them.


Sample Input 1

5
6 9 6 6 5

Sample Output 1

Yes
0 4 2 3 1 2 2

We can verify that S_i = A_i + A_{i+1} + A_{i+2} for every i (1\leq i\leq N), as follows.

  • 6 = 0 + 4 + 2.
  • 9 = 4 + 2 + 3.
  • 6 = 2 + 3 + 1.
  • 6 = 3 + 1 + 2.
  • 5 = 1 + 2 + 2.

Sample Input 2

5
0 1 2 1 0

Sample Output 2

No

Sample Input 3

1
10

Sample Output 3

Yes
0 0 10
C - XOR to All

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

非負整数列 A = (A_1, \ldots, A_N) が与えられます。 あなたは次の操作を、何度でも行うことができます(一度も行わなくてもよいです)。

  • 非負整数 x\in \{A_1,\ldots,A_N\} をひとつ選ぶ。
  • すべての i に対して、A_iA_i\oplus x に置き換える(\oplus はビット単位 \mathrm{XOR} 演算を表します)。

操作後の \sum_{i=1}^N A_i としてありうる最大値を求めてください。

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR}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)。

制約

  • 1\leq N \leq 3\times 10^{5}
  • 0\leq A_i < 2^{30}

入力

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

N
A_1 \ldots A_N

出力

操作後の \sum_{i=1}^N A_i としてありうる最大値を出力してください。


入力例 1

5
1 2 3 4 5

出力例 1

19

例えば次のように操作を行うことで、\sum_{i=1}^N A_i19 にすることが可能です。

  • はじめ、数列 A は次の状態です:(1,2,3,4,5)
  • x = 1 として操作を行うと、数列 A は次の状態に変化します:(0,3,2,5,4)
  • x = 5 として操作を行うと、数列 A は次の状態に変化します:(5,6,7,0,1)。このとき \sum_{i=1}^N A_i = 5 + 6 + 7 + 0 + 1 = 19 です。

入力例 2

5
10 10 10 10 10

出力例 2

50

操作を一度も行わないことで、\sum_{i=1}^N A_i50 にすることが可能です。


入力例 3

5
3 1 4 1 5

出力例 3

18

Score : 500 points

Problem Statement

You are given a sequence of non-negative integers A = (A_1, \ldots, A_N). You can do the operation below any number of times (possibly zero).

  • Choose an integer x\in \{A_1,\ldots,A_N\}.
  • Replace A_i with A_i\oplus x for every i (\oplus denotes the bitwise \mathrm{XOR} operation).

Find the maximum possible value of \sum_{i=1}^N A_i after your operations.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, A \oplus B, is defined as follows:

  • When A \oplus B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110).

Constraints

  • 1\leq N \leq 3\times 10^{5}
  • 0\leq A_i < 2^{30}

Input

Input is given from Standard Input from the following format:

N
A_1 \ldots A_N

Output

Print the maximum possible value of \sum_{i=1}^N A_i after your operations.


Sample Input 1

5
1 2 3 4 5

Sample Output 1

19

Here is a sequence of operations that achieves \sum_{i=1}^N A_i = 19.

  • Initially, the sequence A is (1,2,3,4,5).
  • An operation with x = 1 changes it to (0,3,2,5,4).
  • An operation with x = 5 changes it to (5,6,7,0,1), where \sum_{i=1}^N A_i = 5 + 6 + 7 + 0 + 1 = 19.

Sample Input 2

5
10 10 10 10 10

Sample Output 2

50

Doing zero operations achieves \sum_{i=1}^N A_i = 50.


Sample Input 3

5
3 1 4 1 5

Sample Output 3

18
D - Add to Square

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

H\times W のマス目があり、各マスに整数がひとつずつ書き込まれています。 1\leq i\leq H, 1\leq j\leq W に対して、i 行目・j 列目のマスに書き込まれている整数を A_{i,j} で表します。

あなたは次の操作を、何度でも行うことができます(一度も行わなくてもよいです)。

  • 1\leq i\leq H - 1 かつ 1\leq j\leq W - 1 を満たす整数 i, j を選ぶ。
  • 整数 x をひとつ選ぶ。
  • A_{i,j}, A_{i,j+1}, A_{i+1,j}, A_{i+1,j+1}x を加える。

操作後の \sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| が最小となるように操作を行うとき、操作後の \sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| の値および、そのときのマス目の状態を出力してください。

制約

  • 2\leq H, W \leq 500
  • |A_{i,j}|\leq 10^9

入力

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

H W
A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}

出力

H + 1 行出力してください。 1 行目には、\sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| の値を出力してください。 2 行目から H+1 行目には、マス目の状態を以下の形式で出力してください。

A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}

条件を満たすマス目の状態が複数存在する場合は、どれを出力しても正解となります。


入力例 1

2 3
1 2 3
4 5 6

出力例 1

9
0 -3 -1
3 0 2

例えば次のように操作を行うと、出力例のマス目の状態になります。

  • (i, j, x) = (1, 1, -1) として操作を行う。
  • (i, j, x) = (1, 2, -4) として操作を行う。

このとき、\sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| = 0 + 3 + 1 + 3 + 0 + 2 = 9 です。


入力例 2

2 2
1000000000 -1000000000
-1000000000 1000000000

出力例 2

4000000000
2000000000 0
0 2000000000

|A_{i,j}| > 10^9 となるような操作も認められています。


入力例 3

3 4
0 2 0 -2
-3 -1 2 0
-3 -3 2 2

出力例 3

0
0 0 0 0
0 0 0 0
0 0 0 0

Score : 700 points

Problem Statement

We have an H \times W grid, where each square has one integer written on it. For 1\leq i\leq H and 1\leq j\leq W, let A_{i,j} denote the integer written on the square at the i-th row and j-th column.

You can do the operation below any number of times (possibly zero).

  • Choose integers i and j such that 1\leq i\leq H - 1 and 1\leq j\leq W - 1.
  • Choose another integer x.
  • Add x to each of A_{i,j}, A_{i,j+1}, A_{i+1,j}, and A_{i+1,j+1}.

Print the minimum possible value of \sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| after your operations, and the integers on the grid when that value is achieved.

Constraints

  • 2\leq H, W \leq 500
  • |A_{i,j}|\leq 10^9

Input

Input is given from Standard Input from the following format:

H W
A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}

Output

Print H + 1 lines. The 1-st line should contain the value of \sum_{i=1}^H \sum_{j=1}^W |A_{i,j}|. The 2-nd through (H+1)-th lines should contain the integers on the grid, in the following format:

A_{1,1} \ldots A_{1,W}
\vdots
A_{H,1} \ldots A_{H,W}

If there are multiple solutions, you may print any of them.


Sample Input 1

2 3
1 2 3
4 5 6

Sample Output 1

9
0 -3 -1
3 0 2

Here is a sequence of operations that produces the grid in the Sample Output.

  • Do the operation with (i, j, x) = (1, 1, -1).
  • Do the operation with (i, j, x) = (1, 2, -4).

Here, we have \sum_{i=1}^H \sum_{j=1}^W |A_{i,j}| = 0 + 3 + 1 + 3 + 0 + 2 = 9.


Sample Input 2

2 2
1000000000 -1000000000
-1000000000 1000000000

Sample Output 2

4000000000
2000000000 0
0 2000000000

It is fine if |A_{i,j}| > 10^9 after your operations.


Sample Input 3

3 4
0 2 0 -2
-3 -1 2 0
-3 -3 2 2

Sample Output 3

0
0 0 0 0
0 0 0 0
0 0 0 0
E - Sequence of Multiples

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 800

問題文

整数 N, X が与えられます。整数列 A = (A_1, \ldots, A_N) が次の条件をすべて満たすとします。

  • A_1 = X
  • 任意の i (1\leq i\leq N) に対して、A_ii の倍数である。
  • A は狭義単調増加である。つまり、A_1 < \cdots < A_N が成り立つ。

\sum_{i=1}^N A_i として考えられる最小値を、998244353 で割った余りを求めてください。

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

制約

  • 1\leq T\leq 10
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq 10^{18}

入力

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

T
\text{case}_1
\vdots
\text{case}_T

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

N X

出力

T 行出力してください。i 行目には、\text{case}_i に対する答えを出力してください。


入力例 1

5
5 100
1 10
10 1
1000000000000000000 1
100 100

出力例 1

525
10
55
75433847
61074

はじめの 3 つのテストケースについて、例えば次の A\sum_{i=1}^N A_i の最小値を与えます:

  • 1 番目のテストケース:A = (100, 102, 105, 108, 110)
  • 2 番目のテストケース:A = (10)
  • 3 番目のテストケース:A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Score : 800 points

Problem Statement

You are given integers N and X. Assume that an integer sequence A = (A_1, \ldots, A_N) satisfies all of the conditions below.

  • A_1 = X.
  • For every i (1\leq i\leq N), A_i is a multiple of i.
  • A is strictly increasing. In other words, A_1 < \cdots < A_N holds.

Find the minimum possible value of \sum_{i=1}^N A_i, modulo 998244353.

There are T test cases, each of which should be solved.

Constraints

  • 1\leq T\leq 10
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq 10^{18}

Input

Input is given from Standard Input from the following format:

T
\text{case}_1
\vdots
\text{case}_T

Each case is in the following format:

N X

Output

Print T lines. The i-th line should contain the answer for \text{case}_i.


Sample Input 1

5
5 100
1 10
10 1
1000000000000000000 1
100 100

Sample Output 1

525
10
55
75433847
61074

Here is a sequence A that minimizes \sum_{i=1}^N A_i for each of the first three test cases.

  • The first test case: A = (100, 102, 105, 108, 110).
  • The second test case: A = (10).
  • The third test case: A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).
F - Delete 1, 4, 7, ...

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

整数 N が与えられます。整数列 A = (1, 2, \ldots, N) に対して、次の操作をちょうど K 回行います:

  • 操作を行う時点での A の項数を n とする。1\leq i \leq n かつ i\equiv 1\pmod{3} となるすべての i に対して、Ai 番目の項を削除する。この操作はすべての i について同時に行う。

K 回の操作後の A の項の総和を 998244353 で割った余りを求めてください。

制約

  • 1\leq N\leq 10^{14}
  • 1\leq K\leq 100

入力

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

N K

出力

K 回の操作後の A の項の総和を 998244353 で割った余りを出力してください。


入力例 1

10 2

出力例 1

25
  • はじめ、A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) です。
  • 1 回目の操作を行うと、A = (2, 3, 5, 6, 8, 9) となります。
  • 2 回目の操作を行うと、A = (3, 5, 8, 9) となります。
  • このとき A の項の総和は 3 + 5 + 8 + 9 = 25 です。

入力例 2

10 10

出力例 2

0
  • 2 回目の操作を行うと、A = (3, 5, 8, 9) となります(入力例 1 と同様です)。
  • 3 回目の操作を行うと、A = (5, 8) となります。
  • 4 回目の操作を行うと、A = (8) となります。
  • 5 回目の操作を行うと、A は空になります。
  • 6 回目以降の操作では、A は空のままで、その項の総和は 0 です。

入力例 3

10000 10

出力例 3

862816

Score : 1000 points

Problem Statement

You are given an integer N. On an integer sequence A = (1, 2, \ldots, N), let us do the operation below exactly K times.

  • Let n be the current number of terms in A. For all i such that 1\leq i \leq n and i\equiv 1\pmod{3}, delete the i-th term of A, simultaneously.

Find the sum of the terms of A after K operations, modulo 998244353.

Constraints

  • 1\leq N\leq 10^{14}
  • 1\leq K\leq 100

Input

Input is given from Standard Input from the following format:

N K

Output

Print the sum of the terms of A after K operations, modulo 998244353.


Sample Input 1

10 2

Sample Output 1

25
  • Initially, we have A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).
  • After the first operation, we have A = (2, 3, 5, 6, 8, 9).
  • After the second operation, we have A = (3, 5, 8, 9).
  • The sum of the terms here is 3 + 5 + 8 + 9 = 25.

Sample Input 2

10 10

Sample Output 2

0
  • After the second operation, we have A = (3, 5, 8, 9) (same as Sample Input 1).
  • After the third operation, we have A = (5, 8).
  • After the fourth operation, we have A = (8).
  • After the fifth operation, A is empty.
  • In the sixth and subsequent operations, A remains empty, where the sum of the terms is 0.

Sample Input 3

10000 10

Sample Output 3

862816