A - ABA and BAB

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

配点 : 400

問題文

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

あなたは以下の 2 種類の操作を好きな順序で 0 回以上繰り返すことができます.

  • S の中で ABA となっている (連続した) 部分を選び,それを A で置き換える.
  • S の中で BAB となっている (連続した) 部分を選び,それを B で置き換える.

操作後の S としてあり得る文字列の個数を 10^9+7 で割ったあまりを求めてください.

制約

  • 1 \leq N \leq 250000
  • SA, B からなる長さ N の文字列

入力

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

N
S

出力

答えを出力せよ.


入力例 1

4
ABAB

出力例 1

2

操作後の S としてあり得るのは以下の 2 種類の文字列です.

  • ABAB: 0 回の操作を行うことでこの文字列を得ることができます.
  • AB: S=ABAB1 文字目から 3 文字目までが ABA となっています.これを A で置き換えると S=AB となります.

なお,S=ABAB2 文字目から 4 文字目までが BAB となっているので,これを B に置き換える操作も可能です. ただし,その結果得られる AB は重複して数えないことに注意してください.


入力例 2

1
A

出力例 2

1

操作を 1 度も行うことができません.


入力例 3

17
BBABABAABABAAAABA

出力例 3

18

入力例 4

100
ABAABAABABBABAABAABAABABBABBABBABBABBABBABBABBABBABBABBABBABBABBABAABABAABABBABBABABBABAABAABAABAABA

出力例 4

415919090

10^9+7 で割ったあまりを求めるのを忘れないようにしてください.

Score : 400 points

Problem Statement

You are given a string S of length N consisting of characters A and B.

You can perform the following two types of operations zero or more times in any order:

  • Choose a (contiguous) substring ABA in S and replace it with A.
  • Choose a (contiguous) substring BAB in S and replace it with B.

Find, modulo 10^9+7, the number of possible strings S after performing the operations.

Constraints

  • 1 \leq N \leq 250000
  • S is a string of length N consisting of characters A and B.

Input

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

N
S

Output

Print the answer.


Sample Input 1

4
ABAB

Sample Output 1

2

The two possible strings S after the operations are as follows:

  • ABAB: This string can be obtained by performing zero operations.
  • AB: The 1st to 3rd characters of S=ABAB are ABA. Replacing them with A results in S=AB.

Here, the 2nd to 4th characters of S=ABAB are BAB, so you can also replace them with B. Note, however, that the resulting AB does not count multiple times.


Sample Input 2

1
A

Sample Output 2

1

No operations can be performed.


Sample Input 3

17
BBABABAABABAAAABA

Sample Output 3

18

Sample Input 4

100
ABAABAABABBABAABAABAABABBABBABBABBABBABBABBABBABBABBABBABBABBABBABAABABAABABBABBABABBABAABAABAABAABA

Sample Output 4

415919090

Remember to take the result modulo 10^9+7.

B - Improve Inversions

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

配点 : 600

問題文

(1,2,\cdots,N) の順列 P=(P_1,P_2,\cdots,P_N) が与えられます. また整数 K も与えられます.

あなたはこれから以下の操作を 0 回以上行います.

  • 整数 l,r (1 \leq l < r \leq N) を選ぶ.ただしここで (l,r) は以下の条件をすべて満たす必要がある.
    • K \leq r-l
    • 操作を行う段階で P_l>P_r である.
    • 同じ組 (l,r) を今までに選んだことが一度もない.
  • そして,P_lP_r の値を入れ替える.

あなたは操作回数を最大化したいです. その方法を一つ求めてください.

制約

  • 2 \leq N \leq 500
  • 1 \leq K \leq N-1
  • (P_1,P_2,\cdots,P_N)(1,2,\cdots,N) の順列
  • 入力される値はすべて整数

入力

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

N K
P_1 P_2 \cdots P_N

出力

以下の形式で答えを出力せよ.

m
l_1 r_1
l_2 r_2
\vdots
l_m r_m

ただしここで m は操作回数の最大値であり,l_i,r_ii 回目の操作で選んだ l,r の値である. 複数の解がある場合は,どれを出力してもよい.


入力例 1

3 1
3 2 1

出力例 1

3
2 3
1 3
1 2

この例では操作回数の最大値は 3 です. 出力例の操作の様子は以下のとおりです.

  • 1 回目の操作: (l,r)=(2,3) を選ぶ.1 \leq 3-2,\ P_2>P_3 かつ (2,3) を選んだことはないので条件は満たされている.P_2,P_3 の値を入れ替え,P=(3,1,2) になる.
  • 2 回目の操作: (l,r)=(1,3) を選ぶ.1 \leq 3-1,\ P_1>P_3 かつ (1,3) を選んだことはないので条件は満たされている.P_1,P_3 の値を入れ替え,P=(2,1,3) になる.
  • 3 回目の操作: (l,r)=(1,2) を選ぶ.1 \leq 2-1,\ P_1>P_2 かつ (1,2) を選んだことはないので条件は満たされている.P_1,P_2 の値を入れ替え,P=(1,2,3) になる.

入力例 2

5 4
1 4 3 2 5

出力例 2

0

入力例 3

4 2
4 1 2 3

出力例 3

2
1 4
1 3

入力例 4

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

出力例 4

15
3 8
2 8
3 10
3 9
1 8
2 10
2 9
2 7
1 10
5 10
1 9
4 10
4 9
1 7
1 6

Score : 600 points

Problem Statement

You are given a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N). Additionally, you are given an integer K.

You can perform the following operation zero or more times:

  • Choose integers l and r (1 \leq l < r \leq N). Here, the pair (l,r) must satisfy all of the following conditions:
    • K \leq r-l.
    • P_l > P_r at the time of the operation.
    • The pair (l,r) has never been chosen before.
  • Then, swap the values of P_l and P_r.

You want to maximize the number of operations. Find one way to achieve this.

Constraints

  • 2 \leq N \leq 500
  • 1 \leq K \leq N-1
  • (P_1,P_2,\cdots,P_N) 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 K
P_1 P_2 \cdots P_N

Output

Print the answer in the following format:

m
l_1 r_1
l_2 r_2
\vdots
l_m r_m

Here, m is the maximum number of operations, and l_i and r_i are the values of l and r chosen in the i-th operation, respectively. If multiple solutions exist, you can print any of them.


Sample Input 1

3 1
3 2 1

Sample Output 1

3
2 3
1 3
1 2

In this example, the maximum number of operations is 3. The operations in the sample output proceed as follows:

  • 1st operation: Choose (l,r)=(2,3). We have 1 \leq 3-2 and P_2 > P_3, and (2,3) has not been chosen before, so the conditions are satisfied. Swap P_2 and P_3, resulting in P=(3,1,2).
  • 2nd operation: Choose (l,r)=(1,3). We have 1 \leq 3-1 and P_1 > P_3, and (1,3) has not been chosen before, so the conditions are satisfied. Swap P_1 and P_3, resulting in P=(2,1,3).
  • 3rd operation: Choose (l,r)=(1,2). We have 1 \leq 2-1 and P_1 > P_2, and (1,2) has not been chosen before, so the conditions are satisfied. Swap P_1 and P_2, resulting in P=(1,2,3).

Sample Input 2

5 4
1 4 3 2 5

Sample Output 2

0

Sample Input 3

4 2
4 1 2 3

Sample Output 3

2
1 4
1 3

Sample Input 4

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

Sample Output 4

15
3 8
2 8
3 10
3 9
1 8
2 10
2 9
2 7
1 10
5 10
1 9
4 10
4 9
1 7
1 6
C - Subsequence and Prefix Sum

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

配点 : 600

問題文

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

あなたは以下の操作をちょうど 1 回行います.

  • A の (連続とは限らない) 非空な部分列を選び,それを累積和で置き換える. より正確に述べれば,まず 1 \leq i_1 < i_2 < \cdots < i_k \leq N を満たす添字の列 (i_1,i_2,\cdots,i_k) を選ぶ. 列の長さ k (1 \leq k \leq N) も自由に選べる. その後,各 j (1 \leq j \leq k) について,A_{i_j} の値を \sum_{1 \leq x \leq j} A_{i_x} で置き換える. この置き換えはすべて同時に行う.

操作後の A としてあり得る数列の個数を 10^9+7 で割ったあまりを求めてください.

制約

  • 1 \leq N \leq 100
  • -10 \leq A_i \leq 10
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ.


入力例 1

3
1 1 2

出力例 1

4

操作後の A としてありうるのは以下の 4 通りです.

  • A=(1,1,2): k=1, (i_1)=(1) とすれば達成できます.
  • A=(1,2,2): k=2, (i_1,i_2)=(1,2) とすれば達成できます.
  • A=(1,1,3): k=2, (i_1,i_2)=(1,3) とすれば達成できます.
  • A=(1,2,4): k=3, (i_1,i_2,i_3)=(1,2,3) とすれば達成できます.

入力例 2

4
1 -1 1 -1

出力例 2

8

入力例 3

5
0 0 0 0 0

出力例 3

1

入力例 4

40
2 -2 1 3 -3 -1 -2 -3 0 -1 -2 0 -3 0 0 2 0 -1 2 -2 -2 -1 3 -2 -2 -2 2 3 2 -3 0 -2 2 1 3 0 -1 0 -2 -3

出力例 4

420429545

Score : 600 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N.

You will perform the following operation exactly once:

  • Choose a non-empty subsequence of A (not necessarily contiguous) and replace it with its cumulative sums. More precisely, first choose a sequence of indices (i_1,i_2,\cdots,i_k) such that 1 \leq i_1 < i_2 < \cdots < i_k \leq N. The length of the sequence k (1 \leq k \leq N) can be chosen freely. Then, for each j (1 \leq j \leq k), replace the value of A_{i_j} with \sum_{1 \leq x \leq j} A_{i_x}. This replacement is done simultaneously for all chosen indices.

Find, modulo 10^9+7, the number of possible sequences A after the operation.

Constraints

  • 1 \leq N \leq 100
  • -10 \leq A_i \leq 10
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

3
1 1 2

Sample Output 1

4

The possible sequences A after the operation are as follows:

  • A=(1,1,2): This can be achieved with k=1 and (i_1)=(1).
  • A=(1,2,2): This can be achieved with k=2 and (i_1,i_2)=(1,2).
  • A=(1,1,3): This can be achieved with k=2 and (i_1,i_2)=(1,3).
  • A=(1,2,4): This can be achieved with k=3 and (i_1,i_2,i_3)=(1,2,3).

Sample Input 2

4
1 -1 1 -1

Sample Output 2

8

Sample Input 3

5
0 0 0 0 0

Sample Output 3

1

Sample Input 4

40
2 -2 1 3 -3 -1 -2 -3 0 -1 -2 0 -3 0 0 2 0 -1 2 -2 -2 -1 3 -2 -2 -2 2 3 2 -3 0 -2 2 1 3 0 -1 0 -2 -3

Sample Output 4

420429545
D - Division into 3

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

配点 : 700

問題文

長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます. 以下の Q 個のクエリに答えてください.

  • i 番目のクエリ: 整数 L_i,R_i が与えられる. B=(A_{L_i},A_{L_i+1},\cdots,A_{R_i}) に対して次の問題を解け.
    • B3 つの非空な連続部分列に分割する.各連続部分列についてその要素の最大値を求める.これらの値の総和としてあり得る最小値を求めよ. なお,問題の制約から B の長さは 3 以上になるため,3 つの非空な連続部分列に分割する方法は必ず 1 つ以上存在する.

制約

  • 3 \leq N \leq 250000
  • 1 \leq Q \leq 250000
  • 1 \leq A_i \leq 10^8
  • 1 \leq L_i \leq R_i \leq N
  • R_i-L_i \geq 2
  • 入力される値はすべて整数

入力

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

N Q
A_1 A_2 \cdots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

出力

Q 行出力せよ.i 行目には i 番目のクエリに対する答えを出力せよ.


入力例 1

7 5
4 3 1 1 4 5 2
1 7
2 4
3 5
1 5
4 7

出力例 1

10
5
6
9
8

1 つめのクエリについて説明します. B=(4,3,1,1,4,5,2) です. これを (4,3),(1,1),(4,5,2) と分解すると,各連続部分列の最大値は 4,1,5 となり,その総和は 10 になります. この総和が 10 より小さくなる方法は存在しないので,このクエリの答えは 10 になります.


入力例 2

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

出力例 2

16
14
12
11
17
17
19
14
19
14
17
17
12
16
16

Score : 700 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N. Answer the following Q queries.

  • The i-th query: You are given integers L_i and R_i. Solve the following problem for B=(A_{L_i},A_{L_i+1},\cdots,A_{R_i}).
    • Divide B into three non-empty contiguous subsequences. For each contiguous subsequence, let us find the maximum value of its elements. Find the minimum possible sum of these maximum values. Here, the constraints of the problem force the length of B to be at least 3, so there is always at least one way to divide it into three non-empty contiguous subsequences.

Constraints

  • 3 \leq N \leq 250000
  • 1 \leq Q \leq 250000
  • 1 \leq A_i \leq 10^8
  • 1 \leq L_i \leq R_i \leq N
  • R_i - L_i \geq 2
  • All input values are integers.

Input

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

N Q
A_1 A_2 \cdots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

7 5
4 3 1 1 4 5 2
1 7
2 4
3 5
1 5
4 7

Sample Output 1

10
5
6
9
8

Let us explain the first query. We have B=(4,3,1,1,4,5,2). If you divide it into (4,3),(1,1),(4,5,2), the maximum values of the contiguous subsequences are 4,1,5, respectively, and their sum is 10. There is no way to make this sum smaller, so the answer to this query is 10.


Sample Input 2

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

Sample Output 2

16
14
12
11
17
17
19
14
19
14
17
17
12
16
16
E - LIS and Inversion

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

配点 : 800

問題文

長さ N の整数列 A=(A_1,A_2,\cdots,A_N) が与えられます. ここで,各 i について 0 \leq A_i <i が保証されます.

(1,2,\cdots,N) の順列 P=(P_1,P_2,\cdots,P_N)スコアコストを次のように定義します.

  • P のスコアは,P の最長増加部分列の長さである.
  • P のコストは,以下の条件を満たす整数 i (1 \leq i \leq N) の個数である.
    • j < i かつ P_j > P_i を満たす整数 j の個数が A_i 未満.

k=1,2,\cdots,N について次の問題を解いてください.

  • スコアが k 以上の順列 P の最小コストを求めよ.

制約

  • 1 \leq N \leq 250000
  • 0 \leq A_i < i
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

k=1,2,\cdots,N に対する答えをこの順に出力せよ.


入力例 1

4
0 1 2 1

出力例 1

0 0 1 3

k について,解となる P は以下のとおりです.

  • k=1: P=(4,2,1,3) とすれば,P のスコアは 2 でコストは 0 になります.
  • k=2: P=(4,3,1,2) とすれば,P のスコアは 2 でコストは 0 になります.
  • k=3: P=(4,1,2,3) とすれば,P のスコアは 3 でコストは 1 になります.
  • k=4: P=(1,2,3,4) とすれば,P のスコアは 4 でコストは 3 になります.

入力例 2

3
0 0 0

出力例 2

0 0 0

入力例 3

5
0 1 2 3 4

出力例 3

0 1 2 3 4

入力例 4

11
0 0 2 3 4 5 3 7 8 2 10

出力例 4

0 0 0 1 2 3 4 5 7 8 9

Score : 800 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N. Here, it is guaranteed that 0 \leq A_i < i for each i.

The score and cost of a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N) are defined as follows:

  • The score of P is the length of a longest increasing subsequence of P.
  • The cost of P is the number of integers i (1 \leq i \leq N) that satisfy the following condition:
    • There are fewer than A_i integers j such that j < i and P_j > P_i.

For each k=1,2,\cdots,N, solve the following problem:

  • Find the minimum cost of a permutation P whose score is at least k.

Constraints

  • 1 \leq N \leq 250000
  • 0 \leq A_i < i
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the answers for k=1,2,\cdots,N in this order.


Sample Input 1

4
0 1 2 1

Sample Output 1

0 0 1 3

For each k, a solution P is as follows:

  • k=1: If P=(4,2,1,3), then P has the score of 2 and the cost of 0.
  • k=2: If P=(4,3,1,2), then P has the score of 2 and the cost of 0.
  • k=3: If P=(4,1,2,3), then P has the score of 3 and the cost of 1.
  • k=4: If P=(1,2,3,4), then P has the score of 4 and the cost of 3.

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

Sample Input 3

5
0 1 2 3 4

Sample Output 3

0 1 2 3 4

Sample Input 4

11
0 0 2 3 4 5 3 7 8 2 10

Sample Output 4

0 0 0 1 2 3 4 5 7 8 9
F - Yet Another Expected Value

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

配点 : 1100

問題文

整数 N,A が与えられます.

あなたはこれから以下の操作を行います.

  • 0 以上 1 以下の実数をランダムに N 個生成する. すべての生成は独立であり,かつ乱数は一様であるものとする.
  • 生成した N 個の実数を小さい順に x_1,x_2,\cdots,x_N と呼ぶ. つまり,0 \leq x_1 \leq x_2 \leq \cdots \leq x_N \leq 1 である.
  • ここで,あなたのスコアは次の式の値になる.

\displaystyle \prod_{i=1}^{N} \left(1+\sum_{j=i+1}^N x_j^A \right)

スコアの期待値を \pmod{10^9+7} で求めてください.

期待値 \pmod{10^9+7} の定義

求める期待値は必ず有理数になることが証明できます.また,この問題の制約のもとでは,その値を既約分数 \frac{P}{Q} で表した時,Q \neq 0 \pmod{10^9+7} となることも証明できます. よって,R \times Q \equiv P \pmod{10^9+7}, 0 \leq R < 10^9+7 を満たす整数 R が一意に定まります. この R を答えてください.

制約

  • 1 \leq N \leq 10^4
  • 1 \leq A \leq 5 \times 10^4
  • 入力される値はすべて整数

入力

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

N A

出力

答えを出力せよ.


入力例 1

2 1

出力例 1

666666673

スコアの期待値は 5/3 です.


入力例 2

1 1

出力例 2

1

入力例 3

2 2

出力例 3

500000005

入力例 4

3 2

出力例 4

142857147

入力例 5

5 3

出力例 5

758371066

入力例 6

10000 12345

出力例 6

32201773

Score : 1100 points

Problem Statement

You are given integers N and A.

You will perform the following operations:

  • Generate N real numbers uniformly at random between 0 and 1, inclusive. All generations are independent, and the random numbers are uniformly distributed.
  • Call the generated N real numbers x_1, x_2, \cdots, x_N in ascending order. That is, 0 \leq x_1 \leq x_2 \leq \cdots \leq x_N \leq 1.
  • Your score is given by the following formula:

\displaystyle \prod_{i=1}^{N} \left(1+\sum_{j=i+1}^N x_j^A \right)

Calculate the expected value, modulo 10^9+7, of the score.

Definition of expected value modulo 10^9+7

It can be proved that the sought expected value is always rational. Furthermore, under the constraints of this problem, it can be proved that if the expected value is expressed as an irreducible fraction \frac{P}{Q}, then Q \neq 0 \pmod{10^9+7}. Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{10^9+7}, 0 \leq R < 10^9+7. Report this R.

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq A \leq 5 \times 10^4
  • All input values are integers.

Input

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

N A

Output

Print the answer.


Sample Input 1

2 1

Sample Output 1

666666673

The expected value of the score is 5/3.


Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

2 2

Sample Output 3

500000005

Sample Input 4

3 2

Sample Output 4

142857147

Sample Input 5

5 3

Sample Output 5

758371066

Sample Input 6

10000 12345

Sample Output 6

32201773