A - Reversi 3

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

配点 : 700

問題文

01 からなる長さ N の文字列 A,B が与えられます.Ai 文字目を A_i とします.

あなたは以下の操作を 0 回以上好きな回数行うことができます.

  • A_{i-1}=A_{i+1} を満たす整数 i\ (2\leq i\leq N-1) を選び,A_i を反転する(1 ならば 0 に,0 ならば 1 にする).

操作を繰り返すことで AB に一致させることができるか判定し,可能ならば一致させるために必要な操作回数の最小値を求めてください.

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

制約

  • 1\leq T\leq 2\times 10^5
  • 3\leq N\leq 10^6
  • A,B01 からなる長さ N の文字列
  • T,N は整数
  • 全てのテストケースに対する N の総和は 10^6 以下

入力

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

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

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

N
A
B

出力

\mathrm{case}_1,\mathrm{case}_2,\ldots,\mathrm{case}_T に対する答えを以下の形式で出力せよ.

AB に一致させることができない場合,-1 と出力せよ.

一致させることができる場合,必要な操作回数の最小値を出力せよ.


入力例 1

4
4
0001
0111
6
101101
011100
5
10101
10101
10
0101000101
0011100111

出力例 1

2
-1
0
6

1 つ目のテストケースについて,以下のように 2 回の操作を行うことで AB に一致させることができます.

  1. i = 2 を選ぶ.A0101 となる.
  2. i = 3 を選ぶ.A0111 となる.

2 つ目のテストケースについて,どのように操作しても AB に一致させることはできません.

Score : 700 points

Problem Statement

You are given strings A and B of length N consisting of 0 and 1. Let A_i denote the i-th character of A.

You can perform the following operation any number of times, possibly zero.

  • Choose an integer i\ (2 \leq i \leq N-1) satisfying A_{i-1} = A_{i+1}, and flip A_i (change 1 to 0 or 0 to 1).

Determine whether you can make A equal to B by repeating the operation, and if so, find the minimum number of operations required.

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

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 3 \leq N \leq 10^6
  • A and B are strings of length N consisting of 0 and 1.
  • T and N are integers.
  • The sum of N over all test cases is at most 10^6.

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
B

Output

Output the answers for \mathrm{case}_1, \mathrm{case}_2, \ldots, \mathrm{case}_T in the following format.

If it is impossible to make A equal to B, print -1.

If it is possible, print the minimum number of operations required.


Sample Input 1

4
4
0001
0111
6
101101
011100
5
10101
10101
10
0101000101
0011100111

Sample Output 1

2
-1
0
6

For the first test case, you can make A equal to B in two operations as follows.

  1. Choose i = 2. A becomes 0101.
  2. Choose i = 3. A becomes 0111.

For the second test case, it is impossible to make A equal to B no matter how you operate.

B - Range Mex Sum

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

配点 : 700

問題文

正整数 N および整数列 A=(A_1,A_2,\ldots,A_N) が与えられます.A_i-1 以上 N-1 以下であり,1\leq i\lt j\leq N に対し,A_i\neq -1 かつ A_j\neq -1 ならば A_i\neq A_j です.

次のクエリを Q 回解いてください.

  • 整数 l,r\ (1\leq l\leq r\leq N) が与えられる.
    (0,1,\ldots,N-1) の順列 P=(P_1,P_2,\ldots,P_N) であって,A_i\neq -1\implies P_i=A_i を満たすようなもの全てに対する \mathrm{mex}(\lbrace P_l,P_{l+1}, \ldots,P_r\rbrace) の総和を 998244353 で割った余りを求めよ.
\mathrm{mex}(S) とは 非負整数からなる集合 S に対して,S に含まれない最小の非負整数を \mathrm{mex}(S) と定義します.

制約

  • 1\leq N\leq 5000
  • 1\leq Q\leq 5\times 10^5
  • -1\leq A_i\leq N-1
  • 1\leq i\lt j\leq N に対し,A_i\neq -1 かつ A_j\neq -1 ならば A_i\neq A_j
  • 各クエリにおいて,1\leq l\leq r\leq N
  • 入力は全て整数

入力

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは以下の形式で与えられる.

l r

出力

Q 行出力せよ.i 行目には \mathrm{query}_i に対する答えを出力せよ.


入力例 1

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

出力例 1

2
0
3
6

A_i\neq -1\implies P_i=A_i であるような順列 P(0, 1, 2)(0, 2, 1)2 つです.したがって,各クエリの答えは以下のように求まります.

  • 1 つ目のクエリの答えは \mathrm{mex}(\lbrace 0\rbrace)+\mathrm{mex}(\lbrace 0\rbrace)=1+1=2 です.
  • 2 つ目のクエリの答えは \mathrm{mex}(\lbrace 1\rbrace)+\mathrm{mex}(\lbrace 2\rbrace)=0+0=0 です.
  • 3 つ目のクエリの答えは \mathrm{mex}(\lbrace 0,1\rbrace)+\mathrm{mex}(\lbrace 0,2\rbrace)=2+1=3 です.
  • 4 つ目のクエリの答えは \mathrm{mex}(\lbrace 0,1,2\rbrace)+\mathrm{mex}(\lbrace 0,2,1\rbrace)=3+3=6 です.

入力例 2

5 3
-1 2 -1 -1 1
1 4
3 5
1 5

出力例 2

6
8
30

入力例 3

15 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
1 15

出力例 3

612227903

Score : 700 points

Problem Statement

You are given a positive integer N and a sequence of integers A = (A_1, A_2, \ldots, A_N). Each A_i satisfies -1 \leq A_i \leq N-1, and for 1 \leq i < j \leq N, if A_i \neq -1 and A_j \neq -1, then A_i \neq A_j.

Answer the following query Q times.

  • You are given integers l, r\ (1 \leq l \leq r \leq N).
    Find the sum, modulo 998244353, of \mathrm{mex}(\lbrace P_l, P_{l+1}, \ldots, P_r \rbrace) over all permutations P = (P_1, P_2, \ldots, P_N) of (0, 1, \ldots, N-1) satisfying A_i \neq -1 \implies P_i = A_i.
What is \mathrm{mex}(S)? For a set S of non-negative integers, \mathrm{mex}(S) is defined as the smallest non-negative integer not contained in S.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq Q \leq 5 \times 10^5
  • -1 \leq A_i \leq N-1
  • For 1 \leq i < j \leq N, if A_i \neq -1 and A_j \neq -1, then A_i \neq A_j.
  • For each query, 1 \leq l \leq r \leq N.
  • All input values are integers.

Input

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

N Q
A_1 A_2 \ldots A_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

l r

Output

Output Q lines. The i-th line should contain the answer to \mathrm{query}_i.


Sample Input 1

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

Sample Output 1

2
0
3
6

The permutations P satisfying A_i \neq -1 \implies P_i = A_i are (0, 1, 2) and (0, 2, 1). Thus, the answer to each query is computed as follows.

  • The answer to the first query is \mathrm{mex}(\lbrace 0 \rbrace) + \mathrm{mex}(\lbrace 0 \rbrace) = 1 + 1 = 2.
  • The answer to the second query is \mathrm{mex}(\lbrace 1 \rbrace) + \mathrm{mex}(\lbrace 2 \rbrace) = 0 + 0 = 0.
  • The answer to the third query is \mathrm{mex}(\lbrace 0, 1 \rbrace) + \mathrm{mex}(\lbrace 0, 2 \rbrace) = 2 + 1 = 3.
  • The answer to the fourth query is \mathrm{mex}(\lbrace 0, 1, 2 \rbrace) + \mathrm{mex}(\lbrace 0, 2, 1 \rbrace) = 3 + 3 = 6.

Sample Input 2

5 3
-1 2 -1 -1 1
1 4
3 5
1 5

Sample Output 2

6
8
30

Sample Input 3

15 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
1 15

Sample Output 3

612227903
C - Count Power of 2

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

配点 : 800

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます.

2^{A_l}+2^{A_{l+1}}+\dots+2^{A_r}2 べきとなるような整数の組 (l, r)\ (1\leq l\leq r\leq N) の個数を求めてください.ただし,2 べきとはある非負整数 k を用いて 2^k と表される数を言います.

制約

  • 1\leq N\leq 2\times 10^5
  • 0\leq A_i\leq 2\times 10^5
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ.


入力例 1

4
0 1 0 2

出力例 1

6

条件を満たす (l,r)(1,1),(1,3),(1,4),(2,2),(3,3),(4,4)6 つです.


入力例 2

4
100 100 100 100

出力例 2

8

入力例 3

10
3 2 2 3 2 4 1 1 0 0

出力例 3

19

Score : 800 points

Problem Statement

You are given a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) of length N.

Find the number of pairs of integers (l, r)\ (1 \leq l \leq r \leq N) such that 2^{A_l} + 2^{A_{l+1}} + \dots + 2^{A_r} is a power of 2. Here, a power of 2 is a number expressible as 2^k for some non-negative integer k.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \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 \ldots A_N

Output

Print the answer.


Sample Input 1

4
0 1 0 2

Sample Output 1

6

The pairs (l, r) satisfying the condition are (1,1), (1,3), (1,4), (2,2), (3,3), (4,4), totaling six pairs.


Sample Input 2

4
100 100 100 100

Sample Output 2

8

Sample Input 3

10
3 2 2 3 2 4 1 1 0 0

Sample Output 3

19
D - GCD of Product of Arithmetic Progression

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

配点 : 1000

問題文

正整数 N, B, C, D が与えられます.

非負整数 k に対し,整数 a_k を初項 Bk+C,公差 D,項数 N の等差数列の全項の積,すなわち a_k=(Bk+C)(Bk+C+D)(Bk+C+2D)\dots(Bk+C+(N-1)D) で定義します.

a_0,a_1,a_2,\ldots,a_N の最大公約数を 998244353 で割った余りを求めてください.

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

制約

  • 1\leq T\leq 10^5
  • 1\leq N,B,C,D\leq 10^6
  • 入力は全て整数

入力

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

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

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

N B C D

出力

T 行出力せよ.i 行目には \mathrm{case}_i に対する答えを出力せよ.


入力例 1

3
3 1 1 1
4 2 2 6
2026 3 22 216

出力例 1

6
128
114347907

1 つ目のテストケースについて,(a_0, a_1, a_2, a_3) = (1 \times 2 \times 3, 2 \times 3 \times 4, 3 \times 4 \times 5, 4 \times 5 \times 6) = (6, 24, 60, 120) であり,これらの最大公約数は 6 です.

Score : 1000 points

Problem Statement

You are given positive integers N, B, C, D.

For a non-negative integer k, define the integer a_k as the product of all terms of an arithmetic sequence with first term Bk + C, common difference D, and N terms; that is, a_k = (Bk+C)(Bk+C+D)(Bk+C+2D)\dots(Bk+C+(N-1)D).

Find the greatest common divisor, modulo 998244353, of a_0, a_1, a_2, \ldots, a_N.

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

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N, B, C, D \leq 10^6
  • 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 test case is given in the following format:

N B C D

Output

Output T lines. The i-th line should contain the answer to \mathrm{case}_i.


Sample Input 1

3
3 1 1 1
4 2 2 6
2026 3 22 216

Sample Output 1

6
128
114347907

For the first test case, (a_0, a_1, a_2, a_3) = (1 \times 2 \times 3, 2 \times 3 \times 4, 3 \times 4 \times 5, 4 \times 5 \times 6) = (6, 24, 60, 120), and their greatest common divisor is 6.

E - Swap or Reverse

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

配点 : 1000

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) および整数の組の集合 S=\lbrace (x_1, y_1), (x_2,y_2),\ldots,(x_M,y_M)\rbrace が与えられます.

あなたは以下の 2 種類の操作を好きな順番で何度でも行うことができます.

  • (P_l,P_r)\in S または (P_r,P_l)\in S であるような (l,r)\ (1\leq l\lt r\leq N) を選び,Pl 番目の要素と r 番目の要素を入れ替える.すなわち,P(P_1,\ldots,P_{l-1},P_r,P_{l+1},\ldots,P_{r-1},P_l,P_{r+1},\ldots,P_N) で置き換える.
  • (P_l,P_r)\in S または (P_r,P_l)\in S であるような (l,r)\ (1\leq l\lt r\leq N) を選び,Pl 番目から r 番目までの要素を反転する.すなわち,P(P_1,\ldots,P_{l-1},P_r,P_{r-1},\ldots,P_{l+1},P_l,P_{r+1},\ldots,P_N) で置き換える.

操作によって得られる順列のうち,辞書順最小のものを求めてください.

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

制約

  • 1\leq T\leq 3\times 10^4
  • 2\leq N\leq 2\times 10^5
  • 1\leq M\leq 2\times 10^5
  • P(1,2,\ldots,N) の順列
  • 1\leq x_i\lt y_i\leq N
  • i\neq j ならば (x_i,y_i)\neq (x_j,y_j)
  • 全てのテストケースに対する N の総和は 2\times 10^5 以下
  • 全てのテストケースに対する M の総和は 2\times 10^5 以下
  • 入力は全て整数

入力

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

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

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

N M
P_1 P_2 \ldots P_N
x_1 y_1
x_2 y_2
\vdots
x_M y_M

出力

T 行出力せよ.i 行目には \mathrm{case}_i に対する答えを出力せよ.


入力例 1

2
6 2
1 3 2 5 4 6
1 4
2 5
8 5
7 3 2 8 6 5 1 4
1 8
2 5
3 4
7 8
5 6

出力例 1

1 2 5 3 4 6
1 2 3 7 5 6 8 4

1 つ目のテストケースについて,以下のように操作をすることで P=(1,2,5,3,4,6) とすることができます.

  • はじめ P=(1,3,2,5,4,6) である.
  • (l,r)=(1,5)\ ((P_l,P_r)=(1,4)\in S) として 1 番目から 5 番目までの要素を反転する.P=(4,5,2,3,1,6) となる.
  • (l,r)=(2,3)\ ((P_r,P_l)=(2,5)\in S) として 2 番目の要素と 3 番目の要素を入れ替える.P=(4,2,5,3,1,6) となる.
  • (l,r)=(1,5)\ ((P_r,P_l)=(1,4)\in S) として 1 番目の要素と 5 番目の要素を入れ替える.P=(1,2,5,3,4,6) となる.

これが操作によって得られる辞書順最小の順列です.

Score : 1000 points

Problem Statement

You are given a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and a set of pairs of integers S = \lbrace (x_1, y_1), (x_2, y_2), \ldots, (x_M, y_M) \rbrace.

You can perform the following two types of operations any number of times in any order.

  • Choose (l, r)\ (1 \leq l < r \leq N) such that (P_l, P_r) \in S or (P_r, P_l) \in S, and swap the l-th and r-th elements of P. That is, replace P with (P_1, \ldots, P_{l-1}, P_r, P_{l+1}, \ldots, P_{r-1}, P_l, P_{r+1}, \ldots, P_N).
  • Choose (l, r)\ (1 \leq l < r \leq N) such that (P_l, P_r) \in S or (P_r, P_l) \in S, and reverse the elements from the l-th to the r-th of P. That is, replace P with (P_1, \ldots, P_{l-1}, P_r, P_{r-1}, \ldots, P_{l+1}, P_l, P_{r+1}, \ldots, P_N).

Find the lexicographically smallest permutation obtainable by the operations.

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

Constraints

  • 1 \leq T \leq 3 \times 10^4
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • P is a permutation of (1, 2, \ldots, N).
  • 1 \leq x_i < y_i \leq N
  • (x_i, y_i) \neq (x_j, y_j) if i \neq j.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • The sum of M 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 test case is given in the following format:

N M
P_1 P_2 \ldots P_N
x_1 y_1
x_2 y_2
\vdots
x_M y_M

Output

Output T lines. The i-th line should contain the answer to \mathrm{case}_i.


Sample Input 1

2
6 2
1 3 2 5 4 6
1 4
2 5
8 5
7 3 2 8 6 5 1 4
1 8
2 5
3 4
7 8
5 6

Sample Output 1

1 2 5 3 4 6
1 2 3 7 5 6 8 4

For the first test case, you can obtain P = (1, 2, 5, 3, 4, 6) by performing the operations as follows.

  • Initially, P = (1, 3, 2, 5, 4, 6).
  • Reverse the elements from the first to the fifth with (l, r) = (1, 5)\ ((P_l, P_r) = (1, 4) \in S). P becomes (4, 5, 2, 3, 1, 6).
  • Swap the second and third elements with (l, r) = (2, 3)\ ((P_r, P_l) = (2, 5) \in S). P becomes (4, 2, 5, 3, 1, 6).
  • Swap the first and fifth elements with (l, r) = (1, 5)\ ((P_r, P_l) = (1, 4) \in S). P becomes (1, 2, 5, 3, 4, 6).

This is the lexicographically smallest permutation obtainable by the operations.