A - Max Mod Min

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

あなたは以下の操作を A の長さが 1 になるまで繰り返します。

  • 操作を行う時点での A の長さを k とする。\max(\{A_1,A_2,\dots,A_{k}\})=A_i,\min(\{A_1,A_2,\dots,A_{k}\})=A_j かつ i \neq j を満たす整数の組 (i,j) を選び、A_i(A_i \bmod A_j) で置き換える。このとき、A_i=0 となったのであれば A から A_i を削除する。

どのように操作を行っても操作回数は一定であることが証明できます。操作回数を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
2 3 6

出力例 1

3

以下のように操作を行うことになります。操作回数は 3 回です。

  • i=3,j=1 を選ぶ。A_3=0 となるため、A から A_3 を削除する。A=(2,3) となる。
  • i=2,j=1 を選ぶ。A_2=1 となる。A=(2,1) となる。
  • i=1,j=2 を選ぶ。A_1=0 となるため、A から A_1 を削除する。A=(1) となる。A の長さが 1 になったため、操作を終了する。

入力例 2

6
1232 452 23491 34099 57341 21488

出力例 2

12

Score : 300 points

Problem Statement

You are given a sequence of N positive integers: A=(A_1,A_2,\dots,A_N).

You will repeat the following operation until the length of A becomes 1.

  • Let k be the length of A before this operation. Choose integers i and j such that \max(\{A_1,A_2,\dots,A_{k}\})=A_i,\min(\{A_1,A_2,\dots,A_{k}\})=A_j, and i \neq j. Then, replace A_i with (A_i \bmod A_j). If A_i becomes 0 at this moment, delete A_i from A.

Find the number of operations that you will perform. We can prove that, no matter how you choose i,j in the operations, the total number of operations does not change.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
2 3 6

Sample Output 1

3

You will perform 3 operations as follows:

  • Choose i=3,j=1. You get A_3=0, and delete A_3 from A. Now you have A=(2,3).
  • Choose i=2,j=1. You get A_2=1. Now you have A=(2,1).
  • Choose i=1,j=2. You get A_1=0, and delete A_1 from A. Now you have A=(1), and terminate the process because the length of A becomes 1.

Sample Input 2

6
1232 452 23491 34099 57341 21488

Sample Output 2

12
B - Swap to Sort

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が与えられます。あなたは以下の 2 種類のどちらかの操作を行うことを繰り返すことで P を昇順に並び替えたいです。

  • 操作 A1 \leq i \leq N-1 を満たす整数 i を選び、P_iP_{i+1} を入れ替える

  • 操作 B1 \leq i \leq N-2 を満たす整数 i を選び、P_iP_{i+2} を入れ替える

操作 A の回数が最小となり、かつ操作回数の合計が 10^5 回以下であるような操作の手順を 1 つ示してください。

なお、この問題の制約のもとで、条件を満たす解が存在することが証明できます。

制約

  • 2 \leq N \leq 400
  • 1 \leq P_i \leq N \,(1 \leq i \leq N)
  • P_i \neq P_j \,(1 \leq i < j \leq N)
  • 入力はすべて整数

入力

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

N
P_1 P_2 \ldots P_N

出力

操作回数の合計を S\,(0 \leq S \leq 10^5) 回としたとき、S+1 行出力せよ。

1 行目には S を出力せよ。

s+1\,(1 \leq s \leq S) 行目には、

  • s 回目の操作が操作 A で、選ぶ整数が i\,(1 \leq i \leq N-1) の場合、A i

  • s 回目の操作が操作 B で、選ぶ整数が i\,(1 \leq i \leq N-2) の場合、B i

出力せよ。

複数の解がある場合は、どれを答えてもよい。


入力例 1

4
3 2 4 1

出力例 1

4
A 3
B 1
B 2
B 2

この出力例では、P(3,2,4,1) \rightarrow (3,2,1,4) \rightarrow (1,2,3,4) \rightarrow (1,4,3,2) \rightarrow (1,2,3,4) の順に変わります。

操作回数の合計は最小でなくてもよいということに注意してください。


入力例 2

3
1 2 3

出力例 2

0

入力例 3

6
2 1 4 3 6 5

出力例 3

3
A 1
A 3
A 5

Score : 500 points

Problem Statement

You are given a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).

You can repeat the following two kinds of operations in any order to make P sorted in increasing order.

  • Operation A: Choose an integer i such that 1 \leq i \leq N-1, and swap P_i and P_{i+1}.

  • Operation B: Choose an integer i such that 1 \leq i \leq N-2, and swap P_i and P_{i+2}.

Find a sequence of operations with the following property:

  • The number of Operations A is the minimum possible.

  • The total number of operations is not larger than 10^5.

Under the Constraints of this problem, we can prove that a solution always exists.

Constraints

  • 2 \leq N \leq 400
  • 1 \leq P_i \leq N \,(1 \leq i \leq N)
  • P_i \neq P_j \,(1 \leq i < j \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Let S be the number of operations in your answer. Print S+1 lines.

The first line should contain S.

The (s+1)-th (1 \leq s \leq S) line should contain the following:

  • A i if the s-th operation is Operation A, and the integer chosen in this operation is i.

  • B i if the s-th operation is Operation B, and the integer chosen in this operation is i.

If there are multiple solutions satisfying the condition, printing any of them will be accepted.


Sample Input 1

4
3 2 4 1

Sample Output 1

4
A 3
B 1
B 2
B 2

In this Sample Output, P changes like this: (3,2,4,1) \rightarrow (3,2,1,4) \rightarrow (1,2,3,4) \rightarrow (1,4,3,2) \rightarrow (1,2,3,4).

Note that you don't have to minimize the total number of operations.


Sample Input 2

3
1 2 3

Sample Output 2

0

Sample Input 3

6
2 1 4 3 6 5

Sample Output 3

3
A 1
A 3
A 5
C - Min Diff Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1,2,\ldots ,N の番号のついた N 人の人を数直線上に並べます。人 i\,(1 \leq i \leq N) がいる地点の座標を x_i としたとき、 x_iL_i 以上 R_i 以下の整数である必要があります。複数の人が同じ座標にいても構いません。

ここで、並べ方の不満度を以下の式で定義します。

\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^{N}|x_j-x_i|

不満度としてあり得る値の最小値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq L_i \leq R_i \leq 10^7 \,(1 \leq i \leq N)
  • 入力はすべて整数

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えを出力せよ。


入力例 1

3
1 3
2 4
5 6

出力例 1

4

x_1=3,x_2=4,x_3=5 とすると、不満度は 4 です。不満度を 3 以下にすることはできないので、4 を出力します。


入力例 2

3
1 1
1 1
1 1

出力例 2

0

入力例 3

6
1 5
2 4
1 1
4 4
3 6
3 3

出力例 3

15

Score : 600 points

Problem Statement

N people, numbered 1,2,\ldots ,N, are going to stand on the number line. Let's denote by x_i the coordinate the Person i stands at. Then, x_i should be an integer satisfying L_i \leq x_i \leq R_i. Multiple people can occupy the same coordinate.

We define the dissatisfaction level as the following formula:

\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^{N}|x_j-x_i|

Find the minimum possible value of the dissatisfaction level.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq L_i \leq R_i \leq 10^7 \,(1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the answer.


Sample Input 1

3
1 3
2 4
5 6

Sample Output 1

4

If we let x_1=3,x_2=4,x_3=5, we get the dissatisfaction level of 4. We cannot make it 3 or less, so the answer is 4.


Sample Input 2

3
1 1
1 1
1 1

Sample Output 2

0

Sample Input 3

6
1 5
2 4
1 1
4 4
3 6
3 3

Sample Output 3

15
D - Sets Scores

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

長さ N の整数の集合の列 S=(S_1,S_2,\dots,S_N) のうち、以下の条件を全て満たすものを「素晴らしい集合の列」と呼びます。

  • S_i1 以上 M 以下の整数のみからなる集合(空集合でもよい)である。(1 \le i \le N)
  • S_iS_{i+1} のうち、ちょうど片方にのみ含まれる要素の個数は 1 個である。(1 \le i \le N-1)

ここで、素晴らしい集合の列 S のスコアを \displaystyle \prod_{i=1}^{M} (S_1,S_2,\dots,S_N のうち、i を含む集合の個数 ) と定義します。

全ての素晴らしい集合の列に対するスコアの総和を 998244353 で割ったあまりを求めてください。

制約

  • 1 \le N,M \le 2 \times 10^5
  • 入力は全て整数である。

入力

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

N M

出力

答えを出力せよ。


入力例 1

2 3

出力例 1

24

素晴らしい集合の列のうち、スコアが正であるものは以下の 6 個です。

  • S_1=\{1,2\},S_2=\{1,2,3\}
  • S_1=\{1,3\},S_2=\{1,2,3\}
  • S_1=\{2,3\},S_2=\{1,2,3\}
  • S_1=\{1,2,3\},S_2=\{1,2\}
  • S_1=\{1,2,3\},S_2=\{1,3\}
  • S_1=\{1,2,3\},S_2=\{2,3\}

全てスコアは 4 であるため、解は 24 です。


入力例 2

12 34

出力例 2

786334067

Score : 700 points

Problem Statement

Consider a sequence of integer sets of length N: S=(S_1,S_2,\dots,S_N). We call a sequence brilliant if it satisfies all of the following conditions:

  • S_i is a (possibly empty) integer set, and its elements are in the range [1,M]. (1 \le i \le N)

  • The number of integers that is included in exactly one of S_i and S_{i+1} is 1. (1 \le i \le N-1)

We define the score of a brilliant sequence S as \displaystyle \prod_{i=1}^{M} ( the number of sets among S_1,S_2,\dots,S_N that include i.).

Find, modulo 998244353, the sum of the scores of all possible brilliant sequences.

Constraints

  • 1 \le N,M \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

2 3

Sample Output 1

24

Among all possible brilliant sequences, the following 6 have positive scores.

  • S_1=\{1,2\},S_2=\{1,2,3\}
  • S_1=\{1,3\},S_2=\{1,2,3\}
  • S_1=\{2,3\},S_2=\{1,2,3\}
  • S_1=\{1,2,3\},S_2=\{1,2\}
  • S_1=\{1,2,3\},S_2=\{1,3\}
  • S_1=\{1,2,3\},S_2=\{2,3\}

All of them have a score of 4, so the sum of them is 24.


Sample Input 2

12 34

Sample Output 2

786334067
E - Examination

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

1,2,\ldots,N の番号のついた N 人の生徒が試験を受けました。人 i\,(1 \leq i \leq N) の点数は A_i でしたが、B_i 点以上取らないと留年です。そこで誰も留年しないように、ある 2 人の点数を入れ替える、という操作を任意の回数行うことにしました。

これが可能かを判定し、もし可能ならば一度も点数を入れ替えなかった人数の最大値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i,B_i \leq 10^9\,(1 \leq i \leq N)
  • 入力はすべて整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

誰も留年しないように操作を行うことが可能な場合、一度も点数を入れ替えなかった人数の最大値を出力せよ。

不可能な場合は -1 を出力せよ。


入力例 1

3
1 2
3 1
3 3

出力例 1

1

1 と人 2 の点数を入れ替えると、誰も留年しません。このとき、一度も点数を入れ替えなかった人は人 3 だけなので、1 を出力します。


入力例 2

2
100 1
100 1

出力例 2

2

入力例 3

6
3 2
1 6
4 5
1 3
5 5
9 8

出力例 3

-1

入力例 4

6
3 1
4 5
5 2
2 3
5 4
5 1

出力例 4

3

Score : 800 points

Problem Statement

N students, numbered 1,2,\ldots,N, took an examination. Student i\,(1 \leq i \leq N) had to score at least B_i points to graduate, where they actually scored A_i points.

You can repeat the following operation any number of times (possibly zero):

  • Choose two students, and swap their scores.

Your goal is to make everyone graduate. Determine whether it is possible. If it is possible, find the maximum number of students whose scores do not change during the process.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i,B_i \leq 10^9\,(1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If it is possible to make everyone graduate, print the maximum number of students whose scores do not change during the process.

Otherwise, print -1.


Sample Input 1

3
1 2
3 1
3 3

Sample Output 1

1

If you swap scores of Student 1 and 2, everyone can graduate. Here, the number of students whose scores do not change is 1 (only Student 3).


Sample Input 2

2
100 1
100 1

Sample Output 2

2

Sample Input 3

6
3 2
1 6
4 5
1 3
5 5
9 8

Sample Output 3

-1

Sample Input 4

6
3 1
4 5
5 2
2 3
5 4
5 1

Sample Output 4

3
F - Again ABC String

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

長さ NA,B,C からなる文字列 S のうち、以下の条件を満たすものの個数を 2 で割ったあまり を求めてください。

  • S1 文字目から i 文字目までからなる文字列を S_i とする。S_i に含まれる A,B,C の個数をそれぞれ A_i,B_i,C_i とする。このとき、1 \le i \le N を満たす任意の整数 i に対し、以下が成り立つ。
    • A_i-B_i \le X
    • B_i-C_i \le Y
    • C_i-A_i \le Z

この問題は、T ケース与えられます。

制約

  • 1 \le T \le 10
  • 1 \le N \le 10^9
  • 0 \le X,Y,Z \le 10^9
  • 入力は全て整数である。

入力

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

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

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

N X Y Z

出力

各ケースについて、答えを出力せよ。


入力例 1

1
3 2 1 0

出力例 1

0

条件を満たす文字列は、AAB,AAC,ABA,ABC,ACA,ACB,BAA,BAC8 個があります。よって、解は 0 です。


入力例 2

10
1 22 9 92
14 7 74 39
23 50 8 6
93 40 9 60
68 8 47 64
11 68 18 24
3 26 54 8
46 17 90 86
86 76 45 55
80 68 79 62

出力例 2

1
0
0
0
1
1
1
0
1
0

Score : 1100 points

Problem Statement

Consider strings of length N consisting of A, B, and C. Among them, find the number of strings that satisfy the following condition, modulo 2:

  • Let S_i be the string formed by the first i characters of S. Also let A_i, B_i, and C_i be the numbers of A's, B's, and C's in S_i, respectively. For all i such that 1 \le i \le N, the following holds:
    • A_i-B_i \le X
    • B_i-C_i \le Y
    • C_i-A_i \le Z

You have T test cases to solve.

Constraints

  • 1 \le T \le 10
  • 1 \le N \le 10^9
  • 0 \le X,Y,Z \le 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Each case is in the following format:

N X Y Z

Output

For each case, print the answer.


Sample Input 1

1
3 2 1 0

Sample Output 1

0

8 strings satisfy the condition: AAB,AAC,ABA,ABC,ACA,ACB,BAA,BAC. Therefore the answer is 0.


Sample Input 2

10
1 22 9 92
14 7 74 39
23 50 8 6
93 40 9 60
68 8 47 64
11 68 18 24
3 26 54 8
46 17 90 86
86 76 45 55
80 68 79 62

Sample Output 2

1
0
0
0
1
1
1
0
1
0