A - A*B*C

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

正の整数 K が与えられます。正の整数の 3 つ組 (A,B,C) であって、ABC\leq K なるものの個数を求めてください。 ただし、A,B,C の順番が異なるだけの組も異なる組として数えます。

制約

  • 1\leq K\leq 2\times 10^5
  • K は整数である

入力

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

K

出力

正の整数の 3 つ組 (A,B,C) であって、ABC\leq K なるものの個数を出力せよ。


入力例 1

2

出力例 1

4

(1,1,1),(1,1,2),(1,2,1),(2,1,1) が条件を満たします。


入力例 2

10

出力例 2

53

入力例 3

31415

出力例 3

1937281

Score : 300 points

Problem Statement

Given a positive integer K, find the number of triples of positive integers (A, B, C) such that ABC \leq K. Two triples that only differ in the order of numbers are also distinguished.

Constraints

  • 1\leq K\leq 2\times 10^5
  • K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of triples of positive integers (A, B, C) such that ABC \leq K.


Sample Input 1

2

Sample Output 1

4

We have the following triples: (1,1,1),(1,1,2),(1,2,1),(2,1,1).


Sample Input 2

10

Sample Output 2

53

Sample Input 3

31415

Sample Output 3

1937281
B - A^B^C

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正の整数 A,B,C が与えられます。A^{B^C}10 進法での 1 の位を求めてください。

制約

  • 1\leq A,B,C \leq 10^9
  • A,B,C は整数である

入力

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

A B C

出力

A^{B^C}10 進法での 1 の位を出力せよ。


入力例 1

4 3 2

出力例 1

4

4^{3^2}=4^9=2621441 の位は 4 です。


入力例 2

1 2 3

出力例 2

1

入力例 3

3141592 6535897 9323846

出力例 3

2

Score : 400 points

Problem Statement

Given positive integers A, B, C, find the digit at the ones place in the decimal notation of A^{B^C}.

Constraints

  • 1\leq A,B,C \leq 10^9
  • A,B,C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the digit at the one's place in the decimal notation of A^{B^C}.


Sample Input 1

4 3 2

Sample Output 1

4

The ones digit in the decimal notation of 4^{3^2}=4^9=262144 is 4.


Sample Input 2

1 2 3

Sample Output 2

1

Sample Input 3

3141592 6535897 9323846

Sample Output 3

2
C - String Invasion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の文字列 S が与えられます。Si 文字目を s_i で表します。以下の操作を繰り返せる回数の最大値を求めてください。

  • 連続する 3 文字 s_i,s_{i+1},s_{i+2}\quad (1\leq i\leq |S|-2) であって、s_i=s_{i+1}\neq s_{i+2} であるものを選ぶ。s_{i+2}s_i で置き換える。

制約

  • 3 \leq |S| \leq 2\times 10^5
  • S は英小文字からなる

入力

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

S

出力

操作を繰り返せる回数の最大値を出力せよ。


入力例 1

accept

出力例 1

3

以下のように 3 回の操作を行うことができます。

  • i=2 に対して操作を行う。操作後の文字列は acccpt になる。
  • i=3 に対して操作を行う。操作後の文字列は acccct になる。
  • i=4 に対して操作を行う。操作後の文字列は accccc になる。

入力例 2

atcoder

出力例 2

0

入力例 3

anerroroccurred

出力例 3

16

Score : 500 points

Problem Statement

Given is a string S of length N. Let s_i denote the i-th character of S. Find the maximum number of times the following operation can be done.

  • Choose three consecutive characters in S, s_i,s_{i+1},s_{i+2}\quad (1\leq i\leq |S|-2), such that s_i=s_{i+1}\neq s_{i+2}, and replace s_{i+2} with s_i.

Constraints

  • 3 \leq |S| \leq 2\times 10^5
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of times the operation can be done.


Sample Input 1

accept

Sample Output 1

3

We can do the operation three times, as follows:

  • do it with i=2, changing the string to acccpt;
  • do it with i=3, changing the string to acccct;
  • do it with i=4, changing the string to accccc.

Sample Input 2

atcoder

Sample Output 2

0

Sample Input 3

anerroroccurred

Sample Output 3

16
D - Sky Reflector

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N マス横 M マスのマス目の各マスに 1 以上 K 以下の整数をひとつずつ書き込み、列 A,B を以下のように定義します。

  • i=1,\dots, N に対し、A_ii 行目のマスに書かれた整数の最小値
  • j=1,\dots, M に対し、B_jj 列目のマスに書かれた整数の最大値

N,M,K が与えられるので、列対 (A,B) としてありうる相異なるものの個数を 998244353 で割った余りを求めてください。

制約

  • 1 \leq N,M,K \leq 2\times 10^5
  • 入力はすべて整数である

入力

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

N M K

出力

列対 (A,B) としてありうる相異なるものの個数を 998244353 で割った余りを出力せよ。


入力例 1

2 2 2

出力例 1

7

(A_1,A_2,B_1,B_2) としてありうるものは、(1,1,1,1),(1,1,1,2),(1,1,2,1),(1,1,2,2),(1,2,2,2),(2,1,2,2),(2,2,2,2)7 通りです。


入力例 2

1 1 100

出力例 2

100

入力例 3

31415 92653 58979

出力例 3

469486242

Score : 600 points

Problem Statement

In a grid with N horizontal rows and M vertical columns of squares, we will write an integer between 1 and K (inclusive) on each square and define sequences A, B as follows:

  • for each i=1,\dots, N, A_i is the minimum value written on a square in the i-th row;
  • for each j=1,\dots, M, B_j is the maximum value written on a square in the j-th column.

Given N, M, K, find the number of different pairs of sequences that can be (A, B), modulo 998244353.

Constraints

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

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the number of different pairs of sequences that can be (A, B), modulo 998244353.


Sample Input 1

2 2 2

Sample Output 1

7

(A_1,A_2,B_1,B_2) can be (1,1,1,1), (1,1,1,2), (1,1,2,1), (1,1,2,2), (1,2,2,2), (2,1,2,2), or (2,2,2,2) - there are seven candidates.


Sample Input 2

1 1 100

Sample Output 2

100

Sample Input 3

31415 92653 58979

Sample Output 3

469486242
E - Rvom and Rsrev

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

ab からなる文字列 S が与えられます。S に以下の操作を 0 回以上繰り返してできる辞書順最大の文字列を求めてください。

  • 同一の文字である S2 箇所の文字を選ぶ。それらの間の文字列を前後反転させ、選んだ 2 文字を削除する。すなわち、Si 文字目を s_i と表すことにすれば、s_i=s_j なる i < j を選んで Ss_1\dots s_{i-1}s_{j-1}\dots s_{i+1}s_{j+1}\dots s_{|S|} で置き換える。

なお、この問題ではテストケースが T 個与えられます。i 個目のテストケースは文字列 S_i で表され、S=S_i に対して上記の問題を解く問題です。

制約

  • 1 \leq T \leq 2\times 10^5
  • 1 \leq |S_i|\quad (i=1,\dots, T)
  • 1 \leq |S_1| + \dots + |S_T| \leq 2\times 10^5
  • S_iab からなる

入力

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

T
S_1
\vdots
S_T

出力

T 行出力せよ。i 行目には、S_i に操作を 0 回以上繰り返してできる辞書順最大の文字列を出力せよ。


入力例 1

20
abbaa
babbb
aabbabaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbabaaaaabbaababaaabbabbbbbaaaaa
babbbaaaabaababbbabaabaaaaababaa
bbaababababbbaaabaabababaabbabab
abaabbabaabaaaaabaaaabbaabaaaaab
aabababbabbabbabbaaaabbabbbabaab
aabababbabbbbaaaabbaaaaabbaaaabb
abbbbaabaaabababaaaababababbaabb
aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb
aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbb
abababaaababaaabbbbbaaaaaaabbbbb
aabbaaaaababaabbbbbbbbbaabaaabbb
babababbababbbababbbbbababbbabbb
bbbbababbababbbabababbabbabbbbbb
aaaaaaaaaaaaaaaaababababbbabbbbb
aabababbabbabababababababbbbabbb

出力例 1

bba
bba
bbba
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbaaaaaaaa
bbbbbbbbbbbbbaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbbbbbbbbab
bbbbbbbbbbbbbb
bbbbbbbbbbbbbabb
abbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbaaaaaaaaa
bbbbbbbbbbbbbbbaaaaa
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbba
bbbbbbbbbaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbba
  • 1 個目のテストケースは、1 文字目と 4 文字目に対して操作を行うことで Sbba にできます。
  • 2 個目のテストケースは、1 文字目と 5 文字目に対して操作を行うことで Sbba にできます。
  • 3 個目のテストケースは、1 文字目と 2 文字目に対して操作を行うことで Sbbabaa にでき、その後 3 文字目と 5 文字目に対して操作を行うことで Sbbba にできます。

Score : 800 points

Problem Statement

Given is a string S consisting of a and b. Find the lexicographically greatest string that can be obtained by applying the following operation to S zero or more times:

  • Choose two characters of S that are the same letter. Reverse the string between them and delete the chosen two characters. In other words: let s_i denote the i-th character of S. Choose i < j such that s_i = s_j and replace S with s_1\dots s_{i-1}s_{j-1}\dots s_{i+1}s_{j+1}\dots s_{|S|}.

In this problem, you are given T test cases. The i-th test case is represented as S_i and asks you to solve the problem above for S = S_i.

Constraints

  • 1 \leq T \leq 2\times 10^5
  • 1 \leq |S_i|\quad (i=1,\dots, T)
  • 1 \leq |S_1| + \dots + |S_T| \leq 2\times 10^5
  • S_i consists of a and b.

Input

Input is given from Standard Input in the following format:

T
S_1
\vdots
S_T

Output

Print T lines. The i-th line should contain the lexicographically greatest string that can be obtained by applying the operation to S_i zero or more times.


Sample Input 1

20
abbaa
babbb
aabbabaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbabaaaaabbaababaaabbabbbbbaaaaa
babbbaaaabaababbbabaabaaaaababaa
bbaababababbbaaabaabababaabbabab
abaabbabaabaaaaabaaaabbaabaaaaab
aabababbabbabbabbaaaabbabbbabaab
aabababbabbbbaaaabbaaaaabbaaaabb
abbbbaabaaabababaaaababababbaabb
aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb
aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbb
abababaaababaaabbbbbaaaaaaabbbbb
aabbaaaaababaabbbbbbbbbaabaaabbb
babababbababbbababbbbbababbbabbb
bbbbababbababbbabababbabbabbbbbb
aaaaaaaaaaaaaaaaababababbbabbbbb
aabababbabbabababababababbbbabbb

Sample Output 1

bba
bba
bbba
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbaaaaaaaa
bbbbbbbbbbbbbaaaaaaa
bbbbbbbbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbbbbbbbbab
bbbbbbbbbbbbbb
bbbbbbbbbbbbbabb
abbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbaaaaaaaaa
bbbbbbbbbbbbbbbaaaaa
bbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbba
bbbbbbbbbaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbba
  • In the 1-st test case, we can do the operation for the 1-st and 4-th characters to make S bba.
  • In the 2-nd test case, we can do the operation for the 1-st and 5-th characters to make S bba.
  • In the 3-rd test case, we can do the operation for the 1-st and 2-nd characters to make S bbabaa, then do it for the 3-rd and 5-th characters to make S bbba.
F - Social Distance

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

長さ N+1 の整数列 X_0,X_1,\ldots,X_N が与えられます。 ここで、0=X_0 < X_1 < \ldots < X_N です。

これから、1 から N までの番号のついた N 人の人が、数直線上に現れます。 人 i は、区間 [X_{i-1},X_i] の中から一様ランダムに選ばれた実数座標に出現します。

人と人の距離の最小値の期待値を \bmod 998244353 で求めてください。

期待値 \bmod 998244353 の定義

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

制約

  • 2 \leq N \leq 20
  • 0=X_0 < X_1 < \cdots < X_N \leq 10^6

入力

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

N
X_0 X_1 \ldots X_N

出力

人と人の距離の最小値の期待値を \bmod 998244353 で出力せよ。


入力例 1

2
0 1 3

出力例 1

499122178

人が二人しかいないので、人と人の距離の最小値の期待値は、人 1 と人 2 の距離の期待値と同じです。 答えは 3/2 です。


入力例 2

5
0 3 4 8 9 14

出力例 2

324469854

答えは 196249/172800 です。


入力例 3

20
0 38927 83112 125409 165053 204085 246405 285073 325658 364254 406395 446145 485206 525532 563762 605769 644863 683453 722061 760345 798556

出力例 3

29493181

Score : 1000 points

Problem Statement

Given is an integer sequence of length N+1: X_0,X_1,\ldots,X_N, where 0=X_0 < X_1 < \ldots < X_N holds.

Now, N people numbered 1 through N will appear on a number line. Person i will appear at a real coordinate chosen uniformly at random from the interval [X_{i-1},X_i].

Find the expected value of the smallest distance between two people, modulo 998244353.

Definition of the expected value modulo 998244353

We can prove that the expected value in question is always a rational number. We can also prove that, under the constraints of this problem, if we express the expected value as an irreducible fraction \frac{P}{Q}, we have Q \neq 0 \pmod{998244353}. Thus, there uniquely exists an integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Report this R.

Constraints

  • 2 \leq N \leq 20
  • 0=X_0 < X_1 < \cdots < X_N \leq 10^6

Input

Input is given from Standard Input in the following format:

N
X_0 X_1 \ldots X_N

Output

Print the expected value of the smallest distance between two people, modulo 998244353.


Sample Input 1

2
0 1 3

Sample Output 1

499122178

There are just two people, so the expected value of the smallest distance between two people is just the expected value of the distance between Person 1 and Person 2. The answer is 3/2.


Sample Input 2

5
0 3 4 8 9 14

Sample Output 2

324469854

The answer is 196249/172800.


Sample Input 3

20
0 38927 83112 125409 165053 204085 246405 285073 325658 364254 406395 446145 485206 525532 563762 605769 644863 683453 722061 760345 798556

Sample Output 3

29493181