A - Replace C or Swap AB

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

配点 : 400

問題文

A, B, C からなる長さ N の文字列 X, Y が与えられます.

X に対して次の 3 種の操作を(0 回を含め)何回でも行えるとき,XY と一致させることが可能であるか否かを判定してください.

  • 操作 (1)X に含まれる文字 C をひとつ選び, A で置き換える.
  • 操作 (2)X に含まれる文字 C をひとつ選び, B で置き換える.
  • 操作 (3)X に含まれる部分文字列 AB をひとつ選び, BA で置き換える.より形式的には,X のうち i 文字目が A であり (i+1) 文字目が B であるような i を選び,Xi 文字目を B で,(i+1) 文字目を A で置き換える.

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

制約

  • 1\leq T\leq 2\times 10^5
  • 1\leq N\leq 2\times 10^5
  • X, YA, B, C からなる長さ N の文字列である.
  • 1 つの入力に含まれるテストケースについて,N の総和は 2\times 10^5 以下である.

入力

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

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

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

N X Y

出力

T 行出力してください.i 行目には i 番目のテストケースについて,XY と一致させることが可能ならば Yes,不可能ならば No を出力してください.


入力例 1

6
3 ABC ABC
1 C B
1 B C
2 AB BA
2 BA AB
3 CCB ABA

出力例 1

Yes
Yes
No
Yes
No
Yes
  • 1 番目のテストケースについて: 0 回の操作により XY と一致させることが出来ます.
  • 2 番目のテストケースについて: 1 回の操作 (2) により XY と一致させることが出来ます.
  • 4 番目のテストケースについて: 1 回の操作 (3) により XY と一致させることが出来ます.
  • 6 番目のテストケースについて: 例えば操作 (1), 操作 (3), 操作 (1) をこの順に適切な位置に対して行うと,XCCBCABCBAABA と変化して,Y と一致します.

入力例 2

7
5 ABABA BABAB
5 ABCBC BBABA
5 CCCCC CBABC
5 BBAAA AAABB
5 AAABB BBAAA
5 ACACB BAACB
5 ACACB BBACA

出力例 2

No
Yes
Yes
No
Yes
Yes
No

Score : 400 points

Problem Statement

You are given strings X and Y of length N each, consisting of A, B, and C.

Determine if it is possible to make X coincide with Y by performing the following three kinds of operations on X any number of times, possibly zero.

  • Operation (1):Choose a character C in X and replace it with A.
  • Operation (2):Choose a character C in X and replace it with B.
  • Operation (3):Choose a substring AB in X and replace it with BA. More formally, choose an i such that the i-th and (i+1)-th characters of X are A and B, respectively, and replace the former with B and the latter with A.

You have T test cases to solve.

Constraints

  • 1\leq T\leq 2\times 10^5
  • 1\leq N\leq 2\times 10^5
  • Each of X and Y is a string of length N consisting of A, B, and C.
  • The sum of N over the test cases in a single input is at most 2\times 10^5.

Input

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

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

Each test case is given in the following format:

N X Y

Output

Print T lines. The i-th line should contain Yes if it is possible to make X coincide with Y for the i-th test case, and No otherwise.


Sample Input 1

6
3 ABC ABC
1 C B
1 B C
2 AB BA
2 BA AB
3 CCB ABA

Sample Output 1

Yes
Yes
No
Yes
No
Yes
  • For the first test case, you can perform the operations zero times to make X coincide with Y.
  • For the second test case, you can perform Operation (2) once to make X coincide with Y.
  • For the fourth test case, you can perform Operation (3) once to make X coincide with Y.
  • For the sixth test case, you can perform Operations (1), (3), (1) in this order at appropriate positions, for example, to make X change as CCBCABCBAABA and coincide with Y.

Sample Input 2

7
5 ABABA BABAB
5 ABCBC BBABA
5 CCCCC CBABC
5 BBAAA AAABB
5 AAABB BBAAA
5 ACACB BAACB
5 ACACB BBACA

Sample Output 2

No
Yes
Yes
No
Yes
Yes
No
B - Make Multiples

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

配点 : 400

問題文

整数列 A=(A_1,\ldots,A_N) および,正整数 a,b,c が与えられます.

あなたはこの数列に対して,以下の操作を(0 回を含め)何回でも行うことができます.

  • 1\leq i\leq N となる整数 i をひとつ選ぶ.A_iA_i+1 で置き換える.

あなたの目的は,数列 A の中に,a の倍数,b の倍数,c の倍数がいずれもひとつ以上存在するようにすることです. 目的を達成するために必要な操作回数の最小値を求めてください.

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq a, b, c \leq 10^6
  • 1\leq A_i\leq 10^{18}

入力

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

N a b c
A_1 \cdots A_N

出力

目的を達成するために必要な操作回数の最小値を出力してください.


入力例 1

3 3 4 5
8 9 11

出力例 1

2

操作を 2 回行い A = (8,10,12) とすることで目的を達成できます.


入力例 2

3 3 4 5
14 11 59

出力例 2

1

操作を 1 回行い A = (14,11,60) とすることで目的を達成できます.


入力例 3

6 10 20 30
8 17 5 28 39 13

出力例 3

3

操作を 3 回行い A = (8,17,5,30,40,13) とすることで目的を達成できます.


入力例 4

1 999997 999998 999999
123456789123456789

出力例 4

876537210887543205

操作を 876537210887543205 回行い A = (999994000010999994) とすることで目的を達成できます.

Score : 400 points

Problem Statement

You are given an integer sequence A=(A_1,\ldots,A_N) and positive integers a, b, and c.

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

  • Choose an integer i such that 1\leq i\leq N. Replace A_i with A_i+1.

Your objective is to make the sequence A contain at least one multiple of a, at least one multiple of b, and at least one multiple of c. Find the minimum number of operations required to achieve this objective.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq a, b, c \leq 10^6
  • 1\leq A_i\leq 10^{18}

Input

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

N a b c
A_1 \cdots A_N

Output

Print the minimum number of operations required to achieve the objective.


Sample Input 1

3 3 4 5
8 9 11

Sample Output 1

2

You can perform the operation twice so that A = (8,10,12) to achieve the objective.


Sample Input 2

3 3 4 5
14 11 59

Sample Output 2

1

You can perform the operation once so that A = (14,11,60) to achieve the objective.


Sample Input 3

6 10 20 30
8 17 5 28 39 13

Sample Output 3

3

You can perform the operation three times so that A = (8,17,5,30,40,13) to achieve the objective.


Sample Input 4

1 999997 999998 999999
123456789123456789

Sample Output 4

876537210887543205

You can perform the operation 876537210887543205 times so that A = (999994000010999994) to achieve the objective.

C - LU / RD Marking

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

配点 : 600

問題文

H 行,横 W 列のグリッドがあります.

このグリッドには縦向きの辺が H(W+1) 個,横向きの辺が W(H+1) 個,合計で H(W+1) + W(H+1) 個の辺があります(入出力例の図も参考にしてください).これらの辺に対して,次の 2 種の操作によって印をつけることを考えます.

  • 操作 (1):操作を行う時点で左側の辺と上側の辺に印がついていないようなマスをひとつ選ぶ.そのマスの左側の辺と上側の辺に印をつける.
  • 操作 (2):操作を行う時点で右側の辺と下側の辺に印がついていないようなマスをひとつ選ぶ.そのマスの右側の辺と下側の辺に印をつける.

操作 (1) と操作 (2) を(0 回を含め)何回でも行えるとき,最終的に印がついている辺の集合としてありうるものの個数を 998244353 で割った余りを求めてください.

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

制約

  • 1\leq T\leq 2\times 10^5
  • 1\leq H, W\leq 10^6

入力

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

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

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

H W

出力

T 行出力してください.i 行目には i 番目のテストケースについて,最終的に印がついている辺の集合としてありうるものの個数を 998244353 で割った余りを出力してください.


入力例 1

2
1 1
2 3

出力例 1

4
800

(H, W)=(1,1) の場合には,最終的に印がついている辺の集合としてありうるのは次の 4 通りです.印がついている辺を太線で表しています.

(H, W)=(2,3) の場合には,例えば次のような辺の集合がありえます

一方で,次のような辺の集合はありえません


入力例 2

3
123 456
654 321
1000000 1000000

出力例 2

60549740
298307903
656009181

Score : 600 points

Problem Statement

There is a grid with H rows and W columns.

This grid has H(W+1) vertical edges and W(H+1) horizontal edges, for a total of H(W+1) + W(H+1) (see also the figures at Sample Input/Output). Consider marking these edges by the following two kinds of operations.

  • Operation (1): Choose a square whose left and upper edges are unmarked when performing this operation. Mark the left and upper edges of that square.
  • Operation (2): Choose a square whose right and lower edges are unmarked when performing this operation. Mark the right and lower edges of that square.

Find the number, modulo 998244353, of possible sets of edges that are eventually marked when Operations (1) and (2) can be performed any number of times, possibly zero.

You have T test cases to solve.

Constraints

  • 1\leq T\leq 2\times 10^5
  • 1\leq H, W\leq 10^6

Input

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

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

Each test case is given in the following format:

H W

Output

Print T lines. The i-th line should contain the number, modulo 998244353, of possible sets of edges that are eventually marked for the i-th test case.


Sample Input 1

2
1 1
2 3

Sample Output 1

4
800

For the case (H, W)=(1,1), there are four possible sets of edges that are eventually marked, as shown below. The marked edges are drawn in bold.

For the case (H, W)=(2,3), the following sets of marked edges are possible:

On the other hand, the following sets of marked edges are impossible:


Sample Input 2

3
123 456
654 321
1000000 1000000

Sample Output 2

60549740
298307903
656009181
D - Interval Counts

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

配点 : 600

問題文

x_1 < \cdots < x_N を満たす正整数 x_1, \ldots, x_N および,正整数 y_1, \ldots, y_N が与えられます.

(M, L_1, R_1, \ldots, L_M, R_M) であって,以下の条件を全て満たすものを考えます:

  • M は正整数である.
  • j \ (1\leq j\leq M) に対して,L_j, R_jL_j\leq R_j を満たす整数である.
  • i \ (1\leq i\leq N) に対して,L_j\leq x_i\leq R_j を満たす j \ (1\leq j\leq M) がちょうど y_i 個存在する.

このような組は必ず存在することが証明できます.そのような組に対する \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace としてありうる最大値を求めてください.ただし,最大値が存在しない場合には -1 を出力してください.

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq x_1 < \cdots < x_N \leq 10^9
  • 1\leq y_i \leq 10^9

入力

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

N
x_1 \cdots x_N
y_1 \cdots y_N

出力

条件を満たす組に対する \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace としてありうる最大値を出力してください.ただし,最大値が存在しない場合には -1 を出力してください.


入力例 1

3
1 3 5
1 3 1

出力例 1

2

例えば組 (3, 1, 4, 2, 4, 3, 5) に対して \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace = 2 が成り立ちます.


入力例 2

3
1 10 100
2 3 2

出力例 2

-1

例えば組 (3, -1000, 10, -1000, 1000, 10, 1000) に対して \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace = 990 が成り立ちます.\min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace の最大値は存在しません.


入力例 3

7
10 31 47 55 68 73 90
3 7 4 6 3 4 4

出力例 3

56

Score : 600 points

Problem Statement

You are given positive integers x_1, \ldots, x_N such that x_1 < \cdots < x_N, and positive integers y_1, \ldots, y_N.

Consider a tuple (M, L_1, R_1, \ldots, L_M, R_M) that satisfies all of the following conditions.

  • M is a positive integer.
  • For each j \ (1\leq j\leq M), L_j and R_j are integers such that L_j\leq R_j.
  • For each i \ (1\leq i\leq N), exactly y_i integers j \ (1\leq j\leq M) satisfy L_j\leq x_i\leq R_j.

It can be proved that such a tuple always exists. Find the maximum value of \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace for such a tuple. If there is no maximum value, print -1.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq x_1 < \cdots < x_N \leq 10^9
  • 1\leq y_i \leq 10^9

Input

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

N
x_1 \cdots x_N
y_1 \cdots y_N

Output

Print the maximum value of \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace for a tuple that satisfies the conditions, or -1 if there is no maximum value.


Sample Input 1

3
1 3 5
1 3 1

Sample Output 1

2

For example, we have \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace = 2 for the tuple (3, 1, 4, 2, 4, 3, 5).


Sample Input 2

3
1 10 100
2 3 2

Sample Output 2

-1

For example, we have \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace = 990 for the tuple (3, -1000, 10, -1000, 1000, 10, 1000). There is no maximum value of \min \lbrace R_j-L_j\mid 1\leq j\leq M\rbrace.


Sample Input 3

7
10 31 47 55 68 73 90
3 7 4 6 3 4 4

Sample Output 3

56
E - Fizz Buzz Difference

実行時間制限: 10 sec / メモリ制限: 1024 MB

配点 : 800

問題文

正整数 n, a, b であって,a<b を満たすものが与えられます.

1\leq L\leq R を満たす整数の組 (L,R)良い組であるとは,次の条件が成り立つことをいいます.

  • L 以上 R 以下の整数のうちで a の倍数であるものの個数を n_ab の倍数であるものの個数を n_b とするとき,n_a - n_b = n が成り立つ.

良い組は必ず存在することが証明できます.良い組のうち,R-L が最大であるものを答えてください.そのような組が複数存在する場合には,さらにそのうちで L が最小であるものを答えてください(1\leq L より L が最小のものが存在し,答えるべき (L, R) は一意に定まります).

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

制約

  • 1\leq T\leq 2\times 10^5
  • 1\leq n \leq 10^6
  • 1\leq a < b \leq 10^6

入力

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

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

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

n a b

出力

T 行出力してください.i 行目には i 番目のテストケースについて,答えとなる組 (L,R) を次の形式で出力してください.

L R

入力例 1

1
3 3 5

出力例 1

4 35

(L,R)=(4,35) は,n_a=10, n_b=7 より良い組です.

他には例えば (1,26), (10,41) などの良い組があります.このうち (1,26)R-L が最大ではないので答えではありません.また (10,41)R-L は最大ですが,L が最小ではないので答えではありません.


入力例 2

5
4 3 5
6 2 4
1 1 2
123 456 789
9876 54 321

出力例 2

10 50
3 29
2 4
5473 140447
163 641411

Score : 800 points

Problem Statement

You are given positive integers n, a, and b such that a<b.

An integer pair (L,R) such that 1\leq L\leq R is said to be a good pair when the following condition holds.

  • Let n_a and n_b be respectively the number of multiples of a and the number of multiples of b among the integers between L and R, inclusive. Then, n_a - n_b = n.

It can be proved that a good pair always exists. Report the good pair with the largest value of R-L. If multiple such pairs exist, report the one with the smallest L (from 1\leq L, the sought (L, R) with the smallest L exists and is unique).

You have T test cases to solve.

Constraints

  • 1\leq T\leq 2\times 10^5
  • 1\leq n \leq 10^6
  • 1\leq a < b \leq 10^6

Input

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

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

Each test case is given in the following format:

n a b

Output

Print T lines. The i-th line should contain the sought pair (L,R) for the i-th test case in the following format:

L R

Sample Input 1

1
3 3 5

Sample Output 1

4 35

(L,R)=(4,35) is a good pair since n_a=10, n_b=7.

Some other good pairs are (1,26) and (10,41). (1,26) is not the answer because it does not have the largest R-L. (10,41) is not the answer because, although it has the largest R-L, it does not have the smallest L.


Sample Input 2

5
4 3 5
6 2 4
1 1 2
123 456 789
9876 54 321

Sample Output 2

10 50
3 29
2 4
5473 140447
163 641411
F - Tangent Addition Formula

実行時間制限: 10 sec / メモリ制限: 1024 MB

配点 : 1100

問題文

素数 p および,非負整数 a, b が与えられます.

長さ無限の非負整数列 t = \bigl(t(0), t(1), t(2), \ldots) であって,以下の条件を全て満たすものが存在するか否かを判定してください.

  • 任意の非負整数 x に対して 0\leq t(x) < p
  • 任意の非負整数 x, y に対して t(x+y)\bigl(1-t(x)t(y)\bigr)\equiv t(x)+t(y)\pmod{p}
  • t(a)=b

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

制約

  • 1\leq T\leq 2\times 10^5
  • p1\leq p\leq 10^9 を満たす素数.
  • 0\leq a\leq 10^{9}
  • 0\leq b < p

入力

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

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

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

p a b

出力

T 行出力してください.i 行目には i 番目のテストケースについて,条件を満たす非負整数列 t が存在するならば Yes を,存在しないならば No を出力してください.


入力例 1

4
11 1 0
11 1 1
11 1 3
11 1 5

出力例 1

Yes
No
No
Yes
  • p=11, a=1, b=0 の場合:定数列 t = (0,0,0,0,\ldots) が条件を満たします.
  • p=11, a=1, b=5 の場合:周期 3 の数列 t = (0,5,6,0,5,6,\ldots) が条件を満たします.

入力例 2

5
5 0 0
5 1 1
5 2 2
5 3 3
5 4 4

出力例 2

Yes
No
Yes
Yes
No

入力例 3

7
2 3 1
2 5 0
5 0 1
5 0 2
7 1 4
11 12345 5
13 12345 5

出力例 3

Yes
Yes
No
Yes
No
No
Yes

Score : 1100 points

Problem Statement

You are given a prime number p and non-negative integers a and b.

Determine if there is an infinite sequence of non-negative integers t = \bigl(t(0), t(1), t(2), \ldots) that satisfies all of the following conditions.

  • 0\leq t(x) < p for every non-negative integer x.
  • t(x+y)\bigl(1-t(x)t(y)\bigr)\equiv t(x)+t(y)\pmod{p} for all non-negative integers x and y.
  • t(a)=b.

You have T test cases to solve.

Constraints

  • 1\leq T\leq 2\times 10^5
  • p is a prime number such that 1\leq p\leq 10^9.
  • 0\leq a\leq 10^{9}
  • 0\leq b < p

Input

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

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

Each test case is given in the following format:

p a b

Output

Print T lines. The i-th line should contain Yes if there is a sequence of non-negative integers that satisfies all of the conditions, and No otherwise.


Sample Input 1

4
11 1 0
11 1 1
11 1 3
11 1 5

Sample Output 1

Yes
No
No
Yes
  • For p=11, a=1, b=0, a constant sequence t = (0,0,0,0,\ldots) satisfies the conditions.
  • For p=11, a=1, b=5, a sequence t = (0,5,6,0,5,6,\ldots) of period 3 satisfies the conditions.

Sample Input 2

5
5 0 0
5 1 1
5 2 2
5 3 3
5 4 4

Sample Output 2

Yes
No
Yes
Yes
No

Sample Input 3

7
2 3 1
2 5 0
5 0 1
5 0 2
7 1 4
11 12345 5
13 12345 5

Sample Output 3

Yes
Yes
No
Yes
No
No
Yes