A - Divide Grid

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

N \times N のグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と呼びます。

各マスに 0,1 のいずれかを書き込む方法に対して、以下のように f(X) を定めます。

  • X が書かれているマスから、1 回以上「右に隣接するマスに移動するか、下に隣接するマスに移動する」という操作を繰り返して X が書かれている別のマスへ移動する方法の個数

f(0) = f(1) を満たす書き込み方を 1 個構築してください。この問題の制約下において解が存在することが保証されます。

制約

  • 1 \le N \le 500

入力

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

N

出力

N 行出力せよ。i(1 \le i \le N) 行目には (i,1),(i,2),\dots,(i,N) に書き込む数字をこの順で空白を入れずに出力せよ。


入力例 1

2

出力例 1

01
01

f(0) の条件を満たす操作方法は以下の 1 通りなので、f(0) = 1 です。

  • (1,1) から始め、(2,1) に移動する。

f(1) の条件を満たす操作方法は以下の 1 通りなので、f(1) = 1 です。

  • (1,2) から始め、(2,2) に移動する。

f(0) = f(1) であるため、条件を満たします。


入力例 2

6

出力例 2

100111
101000
100010
011101
010000
110001

Score : 600 points

Problem Statement

There is an N \times N grid. The cell at the i-th row from the top and j-th column from the left is called (i,j).

For each way of writing 0 or 1 in each cell, define f(X) as follows:

  • The number of ways to move from a cell with X written on it to another cell with X written on it by repeating the operation "move to the right adjacent cell or move to the down adjacent cell" one or more times.

Construct one way of writing that satisfies f(0) = f(1). It is guaranteed that a solution exists under the constraints of this problem.

Constraints

  • 1 \le N \le 500

Input

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

N

Output

Output N lines. The i-th line (1 \le i \le N) should contain the digits to be written in (i,1),(i,2),\dots,(i,N) in this order without spaces.


Sample Input 1

2

Sample Output 1

01
01

There is one way, shown below, to perform operations satisfying the condition for f(0), so f(0) = 1.

  • Start from (1,1) and move to (2,1).

There is one way, shown below, to perform operations satisfying the condition for f(1), so f(1) = 1.

  • Start from (1,2) and move to (2,2).

Since f(0) = f(1), the condition is satisfied.


Sample Input 2

6

Sample Output 2

100111
101000
100010
011101
010000
110001
B - Traffic Light

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 800

問題文

ある信号があります。この信号は常に赤になっていますが、PCT 君が P 円払ってボタンを押すと X 秒間緑になります。ただし、PCT 君は以下のルールを守る必要があります。

  • ボタンを押すことが出来るのは整数 t に対して時刻 t と表すことが出来るタイミングのみ。(t として負の整数を選ぶことも出来ます。)
  • 時刻 t にボタンを押した場合、時刻 t+Y になるまでボタンを押すことはできない。(時刻 t+Y にボタンを押すことは可能です。)

この信号を N 人の人が渡ろうとしています。i 人目の人は時刻 A_i + 0.5 にこの信号を渡ろうとしていて、時刻 A_i + 0.5 に信号が緑だと PCT 君に C_i 円を払います。

(PCT 君の貰ったお金 - ボタンを押すためにかかった費用) としてあり得る最大値を求めてください。

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

制約

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 2 \times 10^5
  • 1 \le P \le 10^9
  • 1 \le X \le Y \le 10^9
  • 1 \le A_i \le 10^{18}
  • 1 \le C_i \le 10^9
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

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

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

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

N P X Y
A_1 A_2 \dots A_N
C_1 C_2 \dots C_N

出力

T 行出力せよ。i(1 \le i \le T) 行目には \mathrm{case}_i の答えを出力せよ。


入力例 1

5
3 4 5 12
3 5 11
6 2 9
4 2 1 1
1 2 3 4
100 100 100 1
7 37 21 41
80 136 88 102 161 91 115
61 1 11 86 17 59 91
10 2763 2591 3369
2735 7773 36286 43737 9840 21707 19921 45759 44170 45287
3287 8050 3485 6845 3784 4565 3345 9297 2010 5550
15 408094769 859747485 886452311
1760053960 6327322912 490407029 1899495636 1174752626 84645660 6211206638 270040559 6098433044 4316510828 6601842919 5644655565 243073507 3150320792 4408022586
872391155 333542193 911310678 545186339 707811244 197915352 984885281 162966691 939525712 161385193 129092590 700676660 546046296 128723014 931663769

出力例 1

7
294
164
36403
6362927487

1 個目のテストケースについて、PCT 君の行動例として、以下のようなものがあります。

  • 4 円払い、時刻 -1 にボタンを押す。これにより時刻 -1 から時刻 4 の間信号が緑になる。
  • 時刻 3.5 に人 1 が来る。信号が緑なので PCT 君に 6 円を払う。
  • 時刻 5.5 に人 2 が来る。信号が赤なので何もしない。
  • 4 円払い、時刻 11 にボタンを押す。これにより時刻 11 から時刻 16 の間信号が緑になる。
  • 時刻 11.5 に人 3 が来る。信号が緑なので PCT 君に 9 円を払う。

この場合、(PCT 君の貰ったお金 - ボタンを押すためにかかった費用) は 7 円となり、かつ 7 円が最大値であることが証明できます。

2 個目のテストケースについて、PCT 君の行動例として、以下のようなものがあります。

  • 2 円払い、時刻 1 にボタンを押す。これにより時刻 1 から時刻 2 の間信号が緑になる。
  • 時刻 1.5 に人 1 が来る。信号が緑なので PCT 君に 100 円を払う。
  • 2 円払い、時刻 2 にボタンを押す。これにより時刻 2 から時刻 3 の間信号が緑になる。
  • 時刻 2.5 に人 1 が来る。信号が緑なので PCT 君に 100 円を払う。
  • 2 円払い、時刻 3 にボタンを押す。これにより時刻 3 から時刻 4 の間信号が緑になる。
  • 時刻 3.5 に人 1 が来る。信号が緑なので PCT 君に 100 円を払う。

この場合、(PCT 君の貰ったお金 - ボタンを押すためにかかった費用) は 294 円となり、かつ 294 円が最大値であることが証明できます。

PCT 君は負の時刻にもボタンを押すことが出来ることに注意してください。

Score : 800 points

Problem Statement

There is a traffic signal. This signal is always red, but when PCT-kun pays P yen and presses a button, it becomes green for X seconds. However, he must follow the following rules:

  • The button can only be pressed at timings that can be expressed as time t for an integer t. (Negative integers can also be chosen as t.)
  • If the button is pressed at time t, it cannot be pressed until time t+Y. (It can be pressed at time t+Y.)

There are N people crossing this signal. The i-th person tries to cross this signal at time A_i + 0.5, and if the signal is green at time A_i + 0.5, they pay C_i yen to PCT-kun.

Find the maximum possible value of money received by PCT-kun minus the cost of pressing the button.

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

Constraints

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 2 \times 10^5
  • 1 \le P \le 10^9
  • 1 \le X \le Y \le 10^9
  • 1 \le A_i \le 10^{18}
  • 1 \le C_i \le 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:

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

Each case is given in the following format:

N P X Y
A_1 A_2 \dots A_N
C_1 C_2 \dots C_N

Output

Output T lines. The i-th line (1 \le i \le T) should contain the answer for \mathrm{case}_i.


Sample Input 1

5
3 4 5 12
3 5 11
6 2 9
4 2 1 1
1 2 3 4
100 100 100 1
7 37 21 41
80 136 88 102 161 91 115
61 1 11 86 17 59 91
10 2763 2591 3369
2735 7773 36286 43737 9840 21707 19921 45759 44170 45287
3287 8050 3485 6845 3784 4565 3345 9297 2010 5550
15 408094769 859747485 886452311
1760053960 6327322912 490407029 1899495636 1174752626 84645660 6211206638 270040559 6098433044 4316510828 6601842919 5644655565 243073507 3150320792 4408022586
872391155 333542193 911310678 545186339 707811244 197915352 984885281 162966691 939525712 161385193 129092590 700676660 546046296 128723014 931663769

Sample Output 1

7
294
164
36403
6362927487

For the first test case, here is an example of PCT-kun's actions:

  • Pay 4 yen and press the button at time -1. This makes the signal green from time -1 to time 4.
  • Person 1 comes at time 3.5. Since the signal is green, they pay 6 yen to him.
  • Person 2 comes at time 5.5. Since the signal is red, nothing happens.
  • Pay 4 yen and press the button at time 11. This makes the signal green from time 11 to time 16.
  • Person 3 comes at time 11.5. Since the signal is green, they pay 9 yen to him.

In this case, the money received by him minus the cost of pressing the button is 7 yen, and it can be proved that 7 yen is the maximum value.

For the second test case, here is an example of his actions:

  • Pay 2 yen and press the button at time 1. This makes the signal green from time 1 to time 2.
  • Person 1 comes at time 1.5. Since the signal is green, they pay 100 yen to him.
  • Pay 2 yen and press the button at time 2. This makes the signal green from time 2 to time 3.
  • Person 2 comes at time 2.5. Since the signal is green, they pay 100 yen to him.
  • Pay 2 yen and press the button at time 3. This makes the signal green from time 3 to time 4.
  • Person 3 comes at time 3.5. Since the signal is green, they pay 100 yen to him.

In this case, the money received by him minus the cost of pressing the button is 294 yen, and it can be proved that 294 yen is the maximum value.

Note that he can press the button at negative times.

C - Minimization of Divide

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 1200

問題文

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) が与えられます。

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) に対して、P のコストを \sum_{i=1}^{N} \left \lfloor \frac{A_{P_i}}{2^{B_i}} \right \rfloor と定義します。

コストの最小値を達成する P の個数を 998244353 で割った余りを求めてください。

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

制約

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 0 \le B_i \le 30
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下

入力

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

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

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

N
A_1\ A_2\ \dots\ A_N
B_1\ B_2\ \dots\ B_N

出力

T 行出力せよ。i(1 \le i \le T) 行目には \mathrm{case}_i の答えを出力せよ。


入力例 1

7
2
5 9
0 1
3
3 4 10
0 1 2
4
1 2 3 4
0 0 1 1
5
1000000000 1000000000 1000000000 1000000000 1000000000
0 0 0 0 0
11
19 68 97 62 99 52 57 19 43 80 96
5 3 3 2 3 4 3 2 3 4 3
14
86 49 83 31 5 43 7 46 98 36 60 4 69 59
3 1 4 4 4 0 1 3 0 4 3 5 1 2
10
907139744 237517128 852012347 350549430 62876062 196019710 263351472 184393437 281593038 753973502
23 12 1 26 29 24 0 7 10 4

出力例 1

1
2
4
120
8640
15552
1

1 個目のテストケースについては、各 P に対するコストは以下のようになります。

  • P=(1,2) のとき、\left \lfloor \frac{A_{1}}{2^{B_1}} \right \rfloor + \left \lfloor \frac{A_{2}}{2^{B_2}} \right \rfloor = \left \lfloor \frac{5}{1} \right \rfloor + \left \lfloor \frac{9}{2} \right \rfloor = 9
  • P=(2,1) のとき、\left \lfloor \frac{A_{2}}{2^{B_1}} \right \rfloor + \left \lfloor \frac{A_{1}}{2^{B_2}} \right \rfloor = \left \lfloor \frac{9}{1} \right \rfloor + \left \lfloor \frac{5}{2} \right \rfloor = 11

よって、コストの最小値は 9 であり、それを達成する P(1,2)1 個です。

2 個目のテストケースについては、P = (1,2,3),(2,1,3) がコストの最小値 7 を達成します。

3 個目のテストケースについては、P = (1,2,3,4),(2,1,3,4),(1,2,4,3),(2,1,4,3) がコストの最小値 6 を達成します。

4 個目のテストケースについては、(1,2,3,4,5) の順列全てがコストの最小値 5000000000 を達成します。

Score : 1200 points

Problem Statement

You are given two length-N sequences of non-negative integers: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).

For a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N), define the cost of P as \sum_{i=1}^{N} \left \lfloor \frac{A_{P_i}}{2^{B_i}} \right \rfloor.

Find the number, modulo 998244353, of permutations P that achieve the minimum cost.

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

Constraints

  • 1 \le T \le 2 \times 10^5
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 0 \le B_i \le 30
  • The sum of N over all test cases is at most 2 \times 10^5.

Input

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

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
B_1\ B_2\ \dots\ B_N

Output

Output T lines. The i-th line (1 \le i \le T) should contain the answer for \mathrm{case}_i.


Sample Input 1

7
2
5 9
0 1
3
3 4 10
0 1 2
4
1 2 3 4
0 0 1 1
5
1000000000 1000000000 1000000000 1000000000 1000000000
0 0 0 0 0
11
19 68 97 62 99 52 57 19 43 80 96
5 3 3 2 3 4 3 2 3 4 3
14
86 49 83 31 5 43 7 46 98 36 60 4 69 59
3 1 4 4 4 0 1 3 0 4 3 5 1 2
10
907139744 237517128 852012347 350549430 62876062 196019710 263351472 184393437 281593038 753973502
23 12 1 26 29 24 0 7 10 4

Sample Output 1

1
2
4
120
8640
15552
1

For the first test case, the cost for each P is as follows:

  • For P=(1,2), \left \lfloor \frac{A_{1}}{2^{B_1}} \right \rfloor + \left \lfloor \frac{A_{2}}{2^{B_2}} \right \rfloor = \left \lfloor \frac{5}{1} \right \rfloor + \left \lfloor \frac{9}{2} \right \rfloor = 9
  • For P=(2,1), \left \lfloor \frac{A_{2}}{2^{B_1}} \right \rfloor + \left \lfloor \frac{A_{1}}{2^{B_2}} \right \rfloor = \left \lfloor \frac{9}{1} \right \rfloor + \left \lfloor \frac{5}{2} \right \rfloor = 11

Thus, the minimum cost is 9, and there is one permutation P = (1,2) that achieves it.

For the second test case, P = (1,2,3),(2,1,3) achieve the minimum cost of 7.

For the third test case, P = (1,2,3,4),(2,1,3,4),(1,2,4,3),(2,1,4,3) achieve the minimum cost of 6.

For the fourth test case, all permutations of (1,2,3,4,5) achieve the minimum cost of 5000000000.

D - Max Prod Plus

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 1200

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) に対し、以下で f(A) を定めます。

  • 1 \le i < j < k \le N を満たす整数の組 (i,j,k) に対する A_iA_j + A_k の最大値

長さ N かつ全要素が 1 以上 M 以下である正整数列 A のうち、f(A) \le K となるものの個数を 998244353 で割った余りを求めてください。

制約

  • 3 \le N \le 10^9
  • 1 \le M,K \le 10^4

入力

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

N M K

出力

答えを出力せよ。


入力例 1

3 5 4

出力例 1

9

条件を満たす数列は (1,1,1),(1,1,2),(1,2,1),(2,1,1),(1,1,3),(1,2,2),(2,1,2),(1,3,1),(3,1,1)9 個です。


入力例 2

4 5 7

出力例 2

66

入力例 3

123 456 789

出力例 3

436486661

入力例 4

1000000000 10000 10000

出力例 4

855626126

Score : 1200 points

Problem Statement

For a length-N sequence of positive integers A=(A_1,A_2,\dots,A_N), define f(A) as follows:

  • The maximum value of A_iA_j + A_k over all triples of integers (i,j,k) satisfying 1 \le i < j < k \le N.

Find the number, modulo 998244353, of length-N sequences A of positive integers with all elements between 1 and M, inclusive, such that f(A) \le K.

Constraints

  • 3 \le N \le 10^9
  • 1 \le M,K \le 10^4

Input

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

N M K

Output

Output the answer.


Sample Input 1

3 5 4

Sample Output 1

9

The sequences satisfying the condition are (1,1,1),(1,1,2),(1,2,1),(2,1,1),(1,1,3),(1,2,2),(2,1,2),(1,3,1),(3,1,1), which is nine sequences.


Sample Input 2

4 5 7

Sample Output 2

66

Sample Input 3

123 456 789

Sample Output 3

436486661

Sample Input 4

1000000000 10000 10000

Sample Output 4

855626126
E - Many Optimization Problems

実行時間制限: 5 sec / メモリ制限: 1024 MiB

配点 : 1600

問題文

Optimization Problem

長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。また、長さ N+1 の非負整数列 X=(0,0,\dots,0) があります。

あなたは今から i=1,2,\dots,N について以下の操作を行います。

  • 0 \le v \le A_i を満たす整数 v を選び、X_i,X_{i+1} にそれぞれ v,A_i-v を加算する。

操作終了時の X の最大値としてあり得る値の最小値を求めてください。

正整数 N,M と素数 P が与えられます。長さ N かつ総和が M であるような非負整数列 A=(A_1,A_2,\dots,A_N) 全てに対する Optimization Problem の答えの総和を P で割った余りを求めてください。

制約

  • 1 \le N \le 100
  • 1 \le M \le 10^{18}
  • 9 \times 10^8 \le P \le 10^9
  • P は素数

入力

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

N M P

出力

Optimization Problem の答えの総和を P で割った余りを出力せよ。


入力例 1

3 3 998244353

出力例 1

13

例えば、A=(1,0,2) について Optimization Problem を解きます。例えば、操作列の一例として以下のようなものがあります。

  • i = 1 では v = 1 とする。X=(1,0,0,0) となる。
  • i = 2 では v = 0 とする。X=(1,0,0,0) となる。
  • i = 3 では v = 1 とする。X=(1,0,1,1) となる。

X の最大値を 0 以下にすることは出来ないため、答えは 1 です。

これ以外に答えが 1 となる数列が 6 個、答えが 2 となる数列が 3 個あるためその総和は 13 です。


入力例 2

10 135 998244353

出力例 2

679877945

入力例 3

100 1000000000000000000 924844033

出力例 3

789282612

Score : 1600 points

Problem Statement

Optimization Problem

You are given a length-N sequence of non-negative integers A=(A_1,A_2,\dots,A_N). Also, there is a length-N+1 sequence of non-negative integers X=(0,0,\dots,0).

You will now perform the following operation for i=1,2,\dots,N:

  • Choose an integer v satisfying 0 \le v \le A_i, and add v and A_i-v to X_i and X_{i+1}, respectively.

Find the minimum possible value of the maximum value of X at the end of the operations.

You are given positive integers N, M, and a prime number P. Find the sum, modulo P, of the answers to the Optimization Problem for all sequences A=(A_1,A_2,\dots,A_N) of non-negative integers with length N and sum M.

Constraints

  • 1 \le N \le 100
  • 1 \le M \le 10^{18}
  • 9 \times 10^8 \le P \le 10^9
  • P is prime.

Input

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

N M P

Output

Output the sum, modulo P, of the answers to the Optimization Problem.


Sample Input 1

3 3 998244353

Sample Output 1

13

For example, we solve the Optimization Problem for A=(1,0,2). Here is a possible sequence of operations:

  • For i = 1, let v = 1. X becomes (1,0,0,0).
  • For i = 2, let v = 0. X remains (1,0,0,0).
  • For i = 3, let v = 1. X becomes (1,0,1,1).

Since the maximum value of X cannot be made 0 or less, the answer is 1.

There are six other sequences with answer 1 and three sequences with answer 2, so the sum is 13.


Sample Input 2

10 135 998244353

Sample Output 2

679877945

Sample Input 3

100 1000000000000000000 924844033

Sample Output 3

789282612