A - Reverse, Recurse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 700 点

問題文

(1,2,\cdots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます。1\le L\le R\le N を満たす整数 L,R に対し、手順 f(L,R) を、以下の 2 ステップを順に行うものと定義します。

  • P_L > P_R ならば、P_L, P_{L+1}, \cdots,P_R を反転する。つまり、P_R, P_{R-1},\cdots, P_L に置き換える。
  • L<R ならば、f(L,R-1) を実行したのちに f(L+1,R) を実行する。

f(1,N) を実行した場合に反転が行われる回数を 998244353 で割った余りを求めてください。

制約

  • 2 \le N \le 3 \times 10^5
  • 1 \le P_i \le N
  • P は (1,2,\cdots,N) の順列
  • 入力される値はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

答えを出力してください。


入力例 1

3
3 1 2

出力例 1

2

初め、P=(3,1,2) です。手順は以下の順に呼び出されます。

  • f(1,3):P_1>P_3 なので反転し、P=(2,1,3) となります。
    • f(1,2):P_1>P_2 なので反転し、P=(1,2,3) となります。
      • f(1,1):P_1>P_1 が成り立たないため、反転は行われません。
      • f(2,2):P_2>P_2 が成り立たないため、反転は行われません。
    • f(2,3):P_2>P_3 が成り立たないため、反転は行われません。
      • f(2,2):P_2>P_2 が成り立たないため、反転は行われません。
      • f(3,3):P_3>P_3 が成り立たないため、反転は行われません。

反転は合計 2 回行われるため、答えは 2 です。


入力例 2

2
1 2

出力例 2

0

入力例 3

40
4 23 40 37 2 35 33 5 3 16 34 13 14 15 11 10 18 7 26 1 22 21 32 25 12 39 8 19 28 20 17 24 38 31 30 29 36 9 27 6

出力例 3

143250188

Score : 700 points

Problem Statement

You are given a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\cdots,N). For integers L,R satisfying 1\leq L\leq R\leq N, define the procedure f(L,R) as performing the following two steps in order.

  • If P_L > P_R, reverse P_L, P_{L+1}, \cdots,P_R. That is, replace them with P_R, P_{R-1},\cdots, P_L.
  • If L<R, execute f(L,R-1) and then execute f(L+1,R).

Find the number of reversals performed when f(1,N) is executed, modulo 998244353.

Constraints

  • 2 \le N \le 3 \times 10^5
  • 1 \le P_i \le N
  • P is a permutation of (1,2,\cdots,N).
  • All input values are integers.

Input

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

N
P_1 P_2 \ldots P_N

Output

Output the answer.


Sample Input 1

3
3 1 2

Sample Output 1

2

Initially, P=(3,1,2). The procedures are called in the following order.

  • f(1,3): Since P_1>P_3, a reversal is performed, resulting in P=(2,1,3).
    • f(1,2): Since P_1>P_2, a reversal is performed, resulting in P=(1,2,3).
      • f(1,1): Since P_1>P_1 does not hold, no reversal is performed.
      • f(2,2): Since P_2>P_2 does not hold, no reversal is performed.
    • f(2,3): Since P_2>P_3 does not hold, no reversal is performed.
      • f(2,2): Since P_2>P_2 does not hold, no reversal is performed.
      • f(3,3): Since P_3>P_3 does not hold, no reversal is performed.

A total of 2 reversals are performed, so the answer is 2.


Sample Input 2

2
1 2

Sample Output 2

0

Sample Input 3

40
4 23 40 37 2 35 33 5 3 16 34 13 14 15 11 10 18 7 26 1 22 21 32 25 12 39 8 19 28 20 17 24 38 31 30 29 36 9 27 6

Sample Output 3

143250188
B - Three Vertices

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 800 点

問題文

N 頂点 M 辺の連結な単純無向グラフが与えられます。頂点には 1 から N までの番号が付いており、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。

各頂点は白または黒に塗られています。初期状態は長さ N の文字列 S で表され、S_i が 0 のとき頂点 i は白、1 のとき黒です。

以下の操作を好きな回数(0 回でもよい)行ってすべての頂点を白にできる状態を、良い状態 と呼びます。

  • 相異なる 3 頂点 a,b,c であって、a と b、b と c がそれぞれ辺で結ばれているものを選び、これら 3 頂点の色をそれぞれ反転する。すなわち、白なら黒に、黒なら白にする。

なお、頂点 a と頂点 c が辺で結ばれていても構いません。

Q 個のクエリを順に処理してください。i 番目のクエリは以下の通りです。

  • 整数 x_i が与えられる。頂点 x_i の色を反転する。その後、グラフが良い状態にあるかを判定する。

クエリによる色の変更は、以降のクエリにも引き継がれます。

制約

  • 3 \le N \le 2 \times 10^5
  • N-1 \le M \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • S は 0, 1 からなる長さ N の文字列
  • 1 \le u_i,v_i \le N
  • 1 \le x_i \le N
  • 与えられるグラフは連結な単純無向グラフ
  • 入力される数値はすべて整数

入力

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

N M Q
S
u_1 v_1
u_2 v_2
\vdots
u_M v_M
x_1
x_2
\vdots
x_Q

出力

Q 行出力してください。i 行目には、i 番目のクエリを処理した後の状態が良ければ Yes、そうでなければ No を出力してください。


入力例 1

3 3 5
100
1 2
2 3
3 1
1
1
2
3
2

出力例 1

Yes
No
No
Yes
No

各クエリを処理した直後の色を文字列で表すと、順に 000、100、110、111、101 です。 このグラフではどの操作でも 3 頂点すべての色が反転するため、色がそろっている 1 番目と 4 番目のクエリの答えが Yes です。


入力例 2

4 3 4
1000
1 2
2 3
3 4
4
2
3
4

出力例 2

Yes
No
No
Yes

1 番目のクエリで頂点 4 の色を反転すると 1001 になります。 (a,b,c)=(1,2,3),(2,3,4) の順に操作すると 0111 を経て 0000 になるため、答えは Yes です。

2 番目のクエリでは 1001 から頂点 2 の色を反転するので、状態は 1101 になります。

Score : 800 points

Problem Statement

You are given a connected simple undirected graph with N vertices and M edges. The vertices are numbered from 1 to N, and the i-th edge connects vertices u_i and v_i.

Each vertex is painted white or black. The initial state is represented by a string S of length N: vertex i is white if S_i is 0, and black if it is 1.

A coloring is good if all vertices can be made white by performing the following operation any number of times, possibly zero.

  • Choose three distinct vertices a,b,c such that a and b are connected by an edge and b and c are connected by an edge, and flip the color of each of these three vertices. That is, a white vertex becomes black, and a black vertex becomes white.

Here, vertices a and c may also be connected by an edge.

Process Q queries in the given order. The i-th query is as follows.

  • Given an integer x_i, flip the color of vertex x_i. Then, determine whether the resulting coloring is good.

The color change in each query carries over to subsequent queries.

Constraints

  • 3 \le N \le 2 \times 10^5
  • N-1 \le M \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1 \le u_i,v_i \le N
  • 1 \le x_i \le N
  • The given graph is a connected simple undirected graph.
  • All input numbers are integers.

Input

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

N M Q
S
u_1 v_1
u_2 v_2
\vdots
u_M v_M
x_1
x_2
\vdots
x_Q

Output

Output Q lines. On the i-th line, print Yes if the coloring after processing the i-th query is good, and No otherwise.


Sample Input 1

3 3 5
100
1 2
2 3
3 1
1
1
2
3
2

Sample Output 1

Yes
No
No
Yes
No

The colorings immediately after each query are 000, 100, 110, 111, and 101, in that order. Every operation on this graph flips all three vertices, so the coloring is good exactly when all three vertices have the same color. The answers to the first and fourth queries are therefore Yes.


Sample Input 2

4 3 4
1000
1 2
2 3
3 4
4
2
3
4

Sample Output 2

Yes
No
No
Yes

Flipping the color of vertex 4 in the first query gives 1001. Performing the operations with (a,b,c)=(1,2,3),(2,3,4) in this order gives 0111 and then 0000, so the answer is Yes.

The second query starts from 1001 and flips the color of vertex 2, giving 1101.

C - Blindfolded Cubing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 900 点

問題文

この問題は output-only です。入力は与えられません。

長さ 4 の整数列 A=(A_1,A_2,A_3,A_4) があり、初期状態は (N,1,0,0) です。N の値はあなたには知らされません。 あなたの目標は、最終的に A のいずれかの要素を N^3 にすることです。

以下の 2 種類の操作を、合計 2026 回以下行えます。

  • - i j k(1\le i,j,k\le4):A_i を \max(0,A_j-A_k) に置き換える。
  • ^ i j(1\le i,j\le4):A_i を A_j^2 に置き換える。

添字はいずれも整数で、互いに等しくても構いません。どちらの操作でも、操作直前の数列 A を使って置き換え後の値を計算し、A_i のみを変更します。

操作の途中でいずれかの値が N^4 を超えると失敗です。

制約を満たすすべての N に対して目標を達成する、共通の操作列を出力してください。 ジャッジは各 N について A=(N,1,0,0) から操作列を実行し、途中で失敗せず、全操作終了後にいずれかの要素が N^3 となることを確認します。

制約

  • 2\le N\le2000
  • N は整数

入力

入力は与えられません。

出力

1 行目に操作回数 Q(0\le Q\le2026)を出力してください。 続く Q 行に、各操作を実行順に - i j k または ^ i j の形式で出力してください。


出力例

4
^ 3 1
^ 4 3
- 4 4 3
- 4 4 3

この例は出力形式を示すもので、正解ではありません。

N=2 の場合、初期状態は A=(2,1,0,0) です。各操作後の状態は次の通りです。

操作 操作後の A
^ 3 1 (2,1,4,0)
^ 4 3 (2,1,4,16)
- 4 4 3 (2,1,4,12)
- 4 4 3 (2,1,4,8)

N=2 では目標を達成しますが、N=3 では最後に A=(3,1,9,63) となり、どの要素も N^3=27 になりません。

Score : 900 points

Problem Statement

This problem is output-only. No input is given.

There is an integer sequence of length 4, A=(A_1,A_2,A_3,A_4), whose initial state is (N,1,0,0). The value of N is not revealed to you. Your goal is to make some element of A equal to N^3 in the end.

You can perform the following two types of operations, at most 2026 times in total.

  • - i j k (1\le i,j,k\le4): Replace A_i with \max(0,A_j-A_k).
  • ^ i j (1\le i,j\le4): Replace A_i with A_j^2.

All indices are integers and may be equal to each other. The new value is computed from the values just before the operation, and only A_i is changed.

If any value exceeds N^4 during the operations, you fail.

Output a single sequence of operations that achieves the goal for every N satisfying the constraints. For each N, the judge executes the sequence of operations starting from A=(N,1,0,0), and checks that it does not fail along the way and that some element equals N^3 after all operations are finished.

Constraints

  • 2\le N\le2000
  • N is an integer.

Input

No input is given.

Output

On the first line, output the number of operations Q (0\le Q\le2026). On the following Q lines, output the operations in the order they are performed, each in the format - i j k or ^ i j.


Sample Output

4
^ 3 1
^ 4 3
- 4 4 3
- 4 4 3

This example illustrates the output format and is not a correct answer.

When N=2, the initial state is A=(2,1,0,0). The state after each operation is as follows.

Operation A after the operation
^ 3 1 (2,1,4,0)
^ 4 3 (2,1,4,16)
- 4 4 3 (2,1,4,12)
- 4 4 3 (2,1,4,8)

The goal is achieved for N=2, but for N=3, the final state is A=(3,1,9,63), and no element equals N^3=27.

D - Greedy Balancer

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 900 点

問題文

赤い皿と青い皿が 1 枚ずつあります。また、1 から N までの番号が付いた N 個の石があり、石 i の重さは W_i です。

各石をどちらか一方の皿に乗せた状態を初期状態とします。空の皿があっても構いません。

あなたは、赤い皿に乗っている石の重さの和と、青い皿に乗っている石の重さの和が等しくなるまで、以下の操作を繰り返します。

  • 重さの和が大きい方の皿から、最も軽い石を 1 つ選び、もう一方の皿に移す。最も軽い石が複数ある場合は、どれを選んでも構わない。

この操作が有限回(0 回を含む)で終了する初期状態の個数を 998244353 で割った余りを求めてください。 ただし、2 つの初期状態は、乗っている皿が異なる石が存在するときに区別します。番号が異なる石は区別します。

制約

  • 2 \le N \le 500
  • 1 \le W_i \le 500
  • 入力される値はすべて整数

入力

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

N
W_1 W_2 \ldots W_N

出力

答えを出力してください。


入力例 1

5
4 5 6 7 8

出力例 1

16
  • 初期状態で赤い皿に石 1,2 のみが乗っている場合、青い皿から石 3 を移すと両方の重さの和が 15 となり、操作が終了します。
  • 初期状態で赤い皿に石 2,4 のみが乗っている場合、石 1 が両方の皿を往復し続けるため、操作は終了しません。

条件を満たす初期状態は 16 通りです。


入力例 2

4
2 2 2 2

出力例 2

16

どの初期状態からも、両方の皿に石が 2 個ずつ乗った状態になり、操作が終了します。重さが等しくても番号の異なる石は区別するため、答えは 2^4=16 です。


入力例 3

2
1 3

出力例 3

0

入力例 4

20
17 4 23 8 15 4 29 11 6 19 8 25 3 14 21 6 27 12 9 31

出力例 4

557536

Score : 900 points

Problem Statement

There is one red plate and one blue plate. There are also N stones numbered from 1 to N, and stone i has weight W_i.

An initial state is a state in which each stone is placed on one of the two plates. A plate may be empty.

You repeat the following operation until the total weight of the stones on the red plate equals the total weight of the stones on the blue plate.

  • From the plate with the larger total weight, choose one of the lightest stones and move it to the other plate. If there are multiple lightest stones, you may choose any of them.

Find the number, modulo 998244353, of initial states for which this process terminates after a finite number of operations (possibly zero). Here, two initial states are distinguished when there is a stone that is on different plates in them. Stones with different numbers are distinguished.

Constraints

  • 2 \le N \le 500
  • 1 \le W_i \le 500
  • All input values are integers.

Input

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

N
W_1 W_2 \ldots W_N

Output

Output the answer.


Sample Input 1

5
4 5 6 7 8

Sample Output 1

16
  • If only stones 1,2 are on the red plate in the initial state, moving stone 3 from the blue plate makes both total weights 15, and the process terminates.
  • If only stones 2,4 are on the red plate in the initial state, stone 1 keeps going back and forth between the two plates, so the process does not terminate.

There are 16 initial states that satisfy the condition.


Sample Input 2

4
2 2 2 2

Sample Output 2

16

From any initial state, the process reaches a state with two stones on each plate and terminates. Since stones with different numbers are distinguished even if they have the same weight, the answer is 2^4=16.


Sample Input 3

2
1 3

Sample Output 3

0

Sample Input 4

20
17 4 23 8 15 4 29 11 6 19 8 25 3 14 21 6 27 12 9 31

Sample Output 4

557536