A - T-shirt

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

あるプログラミングコンテストでは、以下のルールに従って参加者に T シャツをプレゼントします。

  • 上位 A 位までの参加者は、必ず T シャツが貰える。
  • 加えて、上位 A+1 位から B 位までの参加者のうち C 人が一様ランダムに選ばれ、選ばれた参加者は T シャツを貰える。

コンテストには 1000 人が参加し、全ての参加者が相異なる順位を取りました。
このコンテストの参加者であるいろはちゃんは、X 位を取りました。
このとき、いろはちゃんが T シャツを貰える確率を求めてください。

制約

  • 入力はすべて整数
  • 1 \le A < B \le 1000
  • 1 \le C \le B-A
  • 1 \le X \le 1000

入力

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

A B C X

出力

答えを出力せよ。 なお、想定解との絶対誤差または相対誤差が 10^{−6} 以下であれば、正解として扱われる。


入力例 1

30 500 20 103

出力例 1

0.042553191489

いろはちゃんは 103 位を取りました。
31 位から 500 位までの 470 人の参加者の中から 20 人が一様ランダムに選ばれ、ここで選ばれるといろはちゃんは T シャツを貰えます。この確率は \frac{20}{470}=0.04255319\dots です。


入力例 2

50 500 100 1

出力例 2

1.000000000000

いろはちゃんは 1 位を取りました。この入力において、いろはちゃんは確実に T シャツを貰えます。


入力例 3

1 2 1 1000

出力例 3

0.000000000000

いろはちゃんは 1000 位を取りました。この入力において、いろはちゃんが T シャツを貰えることはありません。

Score : 100 points

Problem Statement

In a certain programming contest, T-shirts are awarded to participants according to the following rules.

  • All participants who ranked A-th or higher get a T-shirt.
  • Additionally, from the participants who ranked between (A+1)-th and B-th (inclusive), C participants chosen uniformly at random get a T-shirt.

There were 1000 participants in this contest, and all of them got different ranks.
Iroha-chan, who participated in this contest, ranked X-th.
Find the probability that she gets a T-shirt.

Constraints

  • All values in input are integers.
  • 1 \le A < B \le 1000
  • 1 \le C \le B-A
  • 1 \le X \le 1000

Input

Input is given from Standard Input in the following format:

A B C X

Output

Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{−6}.


Sample Input 1

30 500 20 103

Sample Output 1

0.042553191489

Iroha-chan ranked 103-rd.
She will get a T-shirt if she is among the 20 participants chosen uniformly at random from the 470 participants who ranked between 31-st and 500-th, which happens with probability \frac{20}{470}=0.04255319\dots.


Sample Input 2

50 500 100 1

Sample Output 2

1.000000000000

Iroha-chan ranked 1-st. This time, she is guaranteed to get a T-shirt.


Sample Input 3

1 2 1 1000

Sample Output 3

0.000000000000

Iroha-chan ranked 1000-th. This time, she will never get a T-shirt.

B - Minimize Ordering

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

文字列 S が与えられます。S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力してください。

なお、相異なる 2 つの文字列 s = s_1 s_2 \ldots s_nt = t_1 t_2 \ldots t_m について、それらが以下の条件のいずれかを満たすとき、辞書順で s \lt t であるとします。

  • ある整数 i\ (1 \leq i \leq \min(n,m)) が存在し、s_i \lt t_i かつすべての整数 j\ (1 \leq j \lt i) について s_j=t_j
  • すべての整数 i\ (1 \leq i \leq \min(n,m)) について s_i = t_i かつ、n \lt m

制約

  • S は英小文字のみからなる長さ 1 以上 2 \times 10^5 以下の文字列

入力

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

S

出力

S の各文字を並び替えて得られる文字列 S' のうち、辞書順で最小のものを出力せよ。


入力例 1

aba

出力例 1

aab

S= aba を並び替えて得られる文字列は以下の 3 つが考えられます。

  • aba
  • aab
  • baa

この中で辞書順で最小のものは、aab です。


入力例 2

zzzz

出力例 2

zzzz

Score : 200 points

Problem Statement

You are given a string S. Find the lexicographically smallest string S' obtained by permuting the characters of S.

Here, for different two strings s = s_1 s_2 \ldots s_n and t = t_1 t_2 \ldots t_m, s \lt t holds lexicographically when one of the conditions below is satisfied.

  • There is an integer i\ (1 \leq i \leq \min(n,m)) such that s_i \lt t_i and s_j=t_j for all integers j\ (1 \leq j \lt i).
  • s_i = t_i for all integers i\ (1 \leq i \leq \min(n,m)), and n \lt m.

Constraints

  • S is a string of length between 1 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest string S' obtained by permuting the characters in S.


Sample Input 1

aba

Sample Output 1

aab

Three strings can be obtained by permuting aba:

  • aba
  • aab
  • baa

The lexicographically smallest among them is aab.


Sample Input 2

zzzz

Sample Output 2

zzzz
C - 1111gal password

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 N が与えられるので、以下の条件を全て満たす整数 X の個数を 998244353 で割った余りを求めてください。

  • N 桁の正整数である。
  • X の各桁を上の位から順に X_1,X_2,\dots,X_N とする。このとき以下の条件を全て満たす。
    • 全ての整数 1 \le i \le N に対して、 1 \le X_i \le 9
    • 全ての整数 1 \le i \le N-1 に対して、 |X_i-X_{i+1}| \le 1

制約

  • N は整数
  • 2 \le N \le 10^6

入力

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

N

出力

答えを整数として出力せよ。


入力例 1

4

出力例 1

203

4 桁の整数として、例えば 1111,1234,7878,6545 が問題文中の条件を満たします。


入力例 2

2

出力例 2

25

入力例 3

1000000

出力例 3

248860093

998244353 で割った余りを求めることに注意してください。

Score : 300 points

Problem Statement

Given an integer N, find the number of integers X that satisfy all of the following conditions, modulo 998244353.

  • X is an N-digit positive integer.
  • Let X_1,X_2,\dots,X_N be the digits of X from top to bottom. They satisfy all of the following:
    • 1 \le X_i \le 9 for all integers 1 \le i \le N;
    • |X_i-X_{i+1}| \le 1 for all integers 1 \le i \le N-1.

Constraints

  • N is an integer.
  • 2 \le N \le 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

4

Sample Output 1

203

Some of the 4-digit integers satisfying the conditions are 1111,1234,7878,6545.


Sample Input 2

2

Sample Output 2

25

Sample Input 3

1000000

Sample Output 3

248860093

Be sure to find the count modulo 998244353.

D - ABC Transform

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

A, B, C のみからなる文字列 S が与えられます。

S^{(0)}:=S とし、i=1,2,3,\ldots について S^{(i)}S^{(i-1)} の各文字を ABC, BCA, CAB と同時に置き換えたものと定義します。

以下の Q 個のクエリに答えてください。i 個目のクエリの内容は以下の通りです。

  • S^{(t_i)} の先頭から k_i 文字目を出力せよ。

制約

  • SA, B, C のみからなる長さ 1 以上 10^5 以下の文字列
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18},\ S^{(t_i)} の長さ)
  • Q, t_i, k_i は整数

入力

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

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

出力

Q 個のクエリを添字の昇順に、すなわち与えられる順に処理し、出力ごとに改行せよ。


入力例 1

ABC
4
0 1
1 1
1 3
1 6

出力例 1

A
B
C
B

S^{(0)}=ABC, S^{(1)}=BCCAAB です。

よって各クエリへの答えは順に A, B, C, B となります。


入力例 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

出力例 2

A
A
C
A
A

Score : 400 points

Problem Statement

You are given a string S consisting of A, B, C.

Let S^{(0)}:=S. For i=1,2,3,\ldots, let S^{(i)} be the result of simultaneously replacing the characters of S^{(i-1)} as follows: ABC, BCA, CAB.

Answer Q queries. The i-th query is as follows.

  • Print the k_i-th character from the beginning of S^{(t_i)}.

Constraints

  • S is a string of length between 1 and 10^5 (inclusive) consisting of A, B, C.
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18}, the length of S^{(t_i)})
  • Q, t_i, k_i are integers.

Input

Input is given from Standard Input in the following format:

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

Output

Process the Q queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.


Sample Input 1

ABC
4
0 1
1 1
1 3
1 6

Sample Output 1

A
B
C
B

We have S^{(0)}=ABC, S^{(1)}=BCCAAB.

Thus, the answers to the queries are A, B, C, B in the given order.


Sample Input 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

Sample Output 2

A
A
C
A
A
E - (∀x∀)

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

T 個のテストケースについて、次の問題を解いてください。

整数 N と文字列 S が与えられるので、以下の条件を全て満たす文字列 X の数を 998244353 で割った余りを求めてください。

  • X は英大文字のみからなる長さ N の文字列
  • X は回文
  • 辞書順で X \le S
    • すなわち、 X=S であるか、辞書順で XS より前に来る

制約

  • 1 \le T \le 250000
  • N1 以上 10^6 以下の整数
  • ひとつの入力について、含まれるテストケースの N の総和は 10^6 を超えない
  • S は英大文字のみからなる長さ N の文字列

入力

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

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

ただし、 \mathrm{case}_ii 個目のテストケースを表す。

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

N
S

出力

全体で T 行出力せよ。
i 行目には i 個目のテストケースに対する答えを整数として出力せよ。


入力例 1

5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW

出力例 1

24
29
212370247
36523399
231364016

この入力には 5 個のテストケースが含まれます。

1 個目のテストケース:
問題文中の条件を満たす文字列は AAA, ABA, ACA,..., AXA24 個です。

2 個目のテストケース:
S が回文であるとは限りません。

3 個目のテストケース:
998244353 で割った余りを求めることに注意してください。

Score : 500 points

Problem Statement

Solve the following problem for T test cases.

Given an integer N and a string S, find the number of strings X that satisfy all of the conditions below, modulo 998244353.

  • X is a string of length N consisting of uppercase English letters.
  • X is a palindrome.
  • X \le S in lexicographical order.
    • That is, X=S or X is lexicographically smaller than S.

Constraints

  • 1 \le T \le 250000
  • N is an integer between 1 and 10^6 (inclusive).
  • In a single input, the sum of N over the test cases is at most 10^6.
  • S is a string of length N consisting of uppercase English letters.

Input

Input is given from Standard Input in the following format:

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

Here, \mathrm{case}_i represents the i-th test case.

Each test case is in the following format:

N
S

Output

Print T lines. The i-th line should contain the answer for the i-th test case as an integer.


Sample Input 1

5
3
AXA
6
ABCZAZ
30
QWERTYUIOPASDFGHJKLZXCVBNMQWER
28
JVIISNEOXHSNEAAENSHXOENSIIVJ
31
KVOHEEMSOZZASHENDIGOJRTJVMVSDWW

Sample Output 1

24
29
212370247
36523399
231364016

This input contains five test cases.

Test case #1:
The 24 strings satisfying the conditions are AAA, ABA, ACA,..., AXA.

Test case #2:
S may not be a palindrome.

Test case #3:
Be sure to find the count modulo 998244353.

F - Black and White Rooks

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 行、横 M 列のマス目に、黒い飛車の駒 B 個と白い飛車の駒 W 個を設置することを考えましょう。

以下の条件をすべて満たす設置の仕方を いい配置 と呼びます。

  • B+W 個の駒すべてが設置されている。
  • 1 つのマスに置かれている駒の数は高々 1 つである。
  • ある白い駒と黒い駒の組であって、互いが互いを攻撃しているようなものが存在しない。すなわち、ある白い駒と黒い駒の組であって、一方が 1 手の移動によってもう片方が置かれているマスに到達できるようなものが存在しない。

ここで、飛車の駒は、今いる位置から上、下、右、左のいずれかの方向に伸びる直線上にあり、かつ他の駒を飛び越えずに到達できるマスに 1 手で移動することができます。

いい配置としてあり得るものは何通りありますか?答えは非常に大きくなることがあるので、998244353 で割ったあまりを出力してください。

同じ色の駒同士は区別しないものとします。

制約

  • 1 \leq N,M \leq 50
  • 1 \leq B,W \leq 2500
  • B+W \leq N \times M
  • 入力はすべて整数

入力

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

N M B W

出力

答えを 998244353 で割ったあまりを出力せよ。


入力例 1

2 2 1 1

出力例 1

4

いい配置としてあり得るものは以下の 4 通りです。


入力例 2

1 2 1 1

出力例 2

0

いい配置としてあり得るものが存在しない場合もあります。


入力例 3

40 40 30 30

出力例 3

467620384

998244353 で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

Consider placing B black rooks and W white rooks on a grid with N horizontal rows and M vertical columns.

A placement of the rooks satisfying all of the following conditions is called a good placement.

  • All B+W rooks are placed on the grid.
  • At most one rook is placed in the same square.
  • There is no pair of white rook and black rook attacking each other. That is, there is no pair of white rook and black rook such that one of them can reach the square in which the other is placed in one move.

Here, in one move, a rook can reach any square that is on a horizontal or vertical line from its current position and is reachable without jumping over another rook.

How many good placements are there? Since this count can be enormous, print it modulo 998244353.

Rooks in the same color are not distinguished.

Constraints

  • 1 \leq N,M \leq 50
  • 1 \leq B,W \leq 2500
  • B+W \leq N \times M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M B W

Output

Print the count modulo 998244353.


Sample Input 1

2 2 1 1

Sample Output 1

4

There are four good placements as follows.


Sample Input 2

1 2 1 1

Sample Output 2

0

There may be no good placement.


Sample Input 3

40 40 30 30

Sample Output 3

467620384

Be sure to print the count modulo 998244353.

G - Range Pairing Query

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1,2,\dots,N と番号付けられた人が並んでおり、人 i は色 A_i の服を着ています。

以下の形式で表される Q 個のクエリに答えてください。

  • 整数 l,r が与えられる。 人 l,l+1,\dots,r だけに着目したとき、同じ色の服を着た 2 人からなるペアは最大何組作れるか答えよ。

制約

  • 入力は全て整数
  • 1 \le N \le 10^5
  • 1 \le Q \le 10^6
  • 1 \le A_i \le N
  • 各クエリについて、 1 \le l \le r \le N

入力

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

N
A_1 A_2 \dots A_N
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

ただし、 \mathrm{Query}_ii 個目のクエリを表す。

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

l r

出力

全体で Q 行出力せよ。
i 行目には i 個目のクエリに対する答えを整数として出力せよ。
なお、入出力が大きくなる場合があるので、高速な方法で入出力を行うことを推奨する。


入力例 1

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

出力例 1

2
2
1
0
3
4

A=(1,2,3,2,3,1,3,1,2,3) です。この入力には 6 個のクエリが含まれます。

1 個目のクエリは (l, r) = (6, 10) です。人 6 と人 8 、人 7 と人 10 を組にすることで、同じ色の服を着たペアを 2 組作ることができます。

2 個目のクエリは (l, r) = (5, 8) です。人 5 と人 7 、人 6 と人 8 を組にすることで、同じ色の服を着たペアを 2 組作ることができます。

l=r であるようなクエリも与えられます。

Score : 600 points

Problem Statement

N people numbered 1,2,\dots,N are standing in a row. Person i wears Color A_i.

Answer Q queries of the format below.

  • You are given integers l and r. Considering only Person l,l+1,\dots,r, how many pairs of people wearing the same color can be formed at most?

Constraints

  • All values in input are integers.
  • 1 \le N \le 10^5
  • 1 \le Q \le 10^6
  • 1 \le A_i \le N
  • 1 \le l \le r \le N in each query.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
Q
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

Here, \mathrm{Query}_i represents the i-th query.

Each query is in the following format:

l r

Output

Print Q lines. The i-th line should contain the answer for the i-th query as an integer. The use of fast input and output methods is recommended because of potentially large input and output.


Sample Input 1

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

Sample Output 1

2
2
1
0
3
4

We have A=(1,2,3,2,3,1,3,1,2,3). This input contains six queries.

The first query is (l, r) = (6, 10). By pairing Person 6, 8 and paring Person 7, 10, we can form two pairs of people wearing the same color.

The second query is (l, r) = (5, 8). By pairing Person 5, 7 and paring Person 6, 8, we can form two pairs of people wearing the same color.

There can be a query where l=r.

Ex - Random Painting

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号が振られた N 個のマスがあり、始めすべてのマスは白く塗られています。

また、箱の中に 1 から M までの番号が振られた M 個のボールが入っています。

以下の操作を、N 個のマスがすべて黒く塗られるまで繰り返します。

  1. 箱から 1 つボールを取り出す。取り出し方は完全ランダムであり、各ボールは等確率で選ばれる。
  2. 取り出したボールの番号を x として、マス L_x, L_x+1, \ldots, R_x を黒く塗る。
  3. 取り出したボールを箱に戻す。

操作回数の期待値を \text{mod } 998244353 で求めてください(注記参照)。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 1 \leq N,M \leq 400
  • 1 \leq L_i \leq R_i \leq N
  • すべてのマス i についてある整数 j が存在し、L_j \leq i \leq R_j
  • 入力はすべて整数

入力

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

N M
L_1 R_1
L_2 R_2
\hspace{0.5cm}\vdots
L_M R_M

出力

求めた期待値を \text{mod } 998244353 で出力せよ。


入力例 1

3 3
1 1
1 2
2 3

出力例 1

499122180

求める期待値は \frac{7}{2} です。

499122180 \times 2 \equiv 7\pmod{998244353} なので、499122180 を出力します。


入力例 2

13 10
3 5
5 9
3 12
1 13
9 11
12 13
2 4
9 12
9 11
7 11

出力例 2

10

入力例 3

100 11
22 43
84 93
12 71
49 56
8 11
1 61
13 80
26 83
23 100
80 85
9 89

出力例 3

499122193

Score : 600 points

Problem Statement

There are N squares numbered 1 to N. Initially, all squares are painted white.

Additionally, there are M balls numbered 1 to M in a box.

We repeat the procedure below until all squares are painted black.

  1. Pick up a ball from a box uniformly at random.
  2. Let x be the index of the ball. Paint Squares L_x, L_x+1, \ldots, R_x black.
  3. Return the ball to the box.

Find the expected value of the number of times the procedure is done, modulo 998244353 (see Notes).

Notes

It can be proved that the sought expected value is always a rational number. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q, it can be proved that there uniquely exists an integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. You should find this R.

Constraints

  • 1 \leq N,M \leq 400
  • 1 \leq L_i \leq R_i \leq N
  • For every square i, there is an integer j such that L_j \leq i \leq R_j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\hspace{0.5cm}\vdots
L_M R_M

Output

Print the sought expected value modulo 998244353.


Sample Input 1

3 3
1 1
1 2
2 3

Sample Output 1

499122180

The sought expected value is \frac{7}{2}.

We have 499122180 \times 2 \equiv 7\pmod{998244353}, so 499122180 should be printed.


Sample Input 2

13 10
3 5
5 9
3 12
1 13
9 11
12 13
2 4
9 12
9 11
7 11

Sample Output 2

10

Sample Input 3

100 11
22 43
84 93
12 71
49 56
8 11
1 61
13 80
26 83
23 100
80 85
9 89

Sample Output 3

499122193