E - Flavors

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

配点 : 300

問題文

N カップのアイスクリームがあります。
i カップ目の味は F_i 、美味しさは S_i ( S_i は偶数 ) です。

あなたは、 N 個のカップの中から 2 つを選んで食べることにしました。
このときの満足度は次のように定義されます。

  • 食べたアイスクリームの美味しさを s,t ( 但し、 s \ge t ) とする。
    • 2 つのカップの味が異なるなら、満足度は \displaystyle s+t である。
    • そうでないなら、満足度は \displaystyle s + \frac{t}{2} である。

満足度として達成可能な最大値を求めてください。

制約

  • 入力は全て整数
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i は偶数

入力

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

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

出力

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


入力例 1

4
1 4
2 10
2 8
3 6

出力例 1

16

2 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 2 カップ目の味は 2 、美味しさは 10 です。
  • 4 カップ目の味は 3 、美味しさは 6 です。
  • 両者の味は異なるので、満足度は 10+6=16 です。

以上より、満足度 16 を達成できます。
満足度を 16 より大きくすることはできません。


入力例 2

4
4 10
3 2
2 4
4 12

出力例 2

17

1 カップ目と 4 カップ目のアイスを食べることを考えます。

  • 1 カップ目の味は 4 、美味しさは 10 です。
  • 4 カップ目の味は 4 、美味しさは 12 です。
  • 両者の味は同じなので、満足度は 12+\frac{10}{2}=17 です。

以上より、満足度 17 を達成できます。
満足度を 17 より大きくすることはできません。

Score : 300 points

Problem Statement

We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are F_i and S_i, respectively (S_i is an even number).

You will choose and eat two of the N cups.
Your satisfaction here is defined as follows.

  • Let s and t (s \ge t) be the deliciousness of the eaten cups.
    • If the two cups have different flavors, your satisfaction is \displaystyle s+t.
    • Otherwise, your satisfaction is \displaystyle s + \frac{t}{2}.

Find the maximum achievable satisfaction.

Constraints

  • All input values are integers.
  • 2 \le N \le 3 \times 10^5
  • 1 \le F_i \le N
  • 2 \le S_i \le 10^9
  • S_i is even.

Input

Input is given from Standard Input in the following format:

N
F_1 S_1
F_2 S_2
\vdots
F_N S_N

Output

Print the answer as an integer.


Sample Input 1

4
1 4
2 10
2 8
3 6

Sample Output 1

16

Consider eating the second and fourth cups.

  • The second cup has a flavor of 2 and deliciousness of 10.
  • The fourth cup has a flavor of 3 and deliciousness of 6.
  • Since they have different flavors, your satisfaction is 10+6=16.

Thus, you can achieve the satisfaction of 16.
You cannot achieve a satisfaction greater than 16.


Sample Input 2

4
4 10
3 2
2 4
4 12

Sample Output 2

17

Consider eating the first and fourth cups.

  • The first cup has a flavor of 4 and deliciousness of 10.
  • The fourth cup has a flavor of 4 and deliciousness of 12.
  • Since they have the same flavor, your satisfaction is 12+\frac{10}{2}=17.

Thus, you can achieve the satisfaction of 17.
You cannot achieve a satisfaction greater than 17.

F - PC on the Table

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

配点 : 300

問題文

高橋君は部屋に PC を沢山置こうとしています。そこで最大何台の PC を部屋に置けるか調べるプログラムを書くことにしました。

H 個の長さ W., T からなる文字列 S_1,S_2,\ldots,S_H が与えられます。

高橋君は以下の操作を 0 回以上何回でも行うことができます。

  • 1\leq i \leq H, 1 \leq j \leq W-1 を満たす整数であって、 S_ij 番目の文字も j+1 番目の文字も T であるようなものを選ぶ。 S_ij 番目の文字を P で置き換え、S_ij+1 番目の文字を C で置き換える。

高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を出力してください。

制約

  • 1\leq H \leq 100
  • 2\leq W \leq 100
  • HW は整数である
  • S_i., T からなる長さ W の文字列

入力

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

H W 
S_1
S_2
\vdots
S_H

出力

高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を改行区切りで出力せよ。

解が複数存在する場合、どれを出力しても正答とみなされる。


入力例 1

2 3
TTT
T.T

出力例 1

PCT
T.T

可能な操作回数の最大値は 1 です。

例えば、 (i,j)=(1,1) として操作を行うと、S_1PCT に変化します。


入力例 2

3 5
TTT..
.TTT.
TTTTT

出力例 2

PCT..
.PCT.
PCTPC

Score : 300 points

Problem Statement

Planning to place many PCs in his room, Takahashi has decided to write a code that finds how many PCs he can place in his room.

You are given H strings S_1,S_2,\ldots,S_H, each of length W, consisting of . and T.

Takahashi may perform the following operation any number of times (possibly zero):

  • Choose integers satisfying 1\leq i \leq H and 1 \leq j \leq W-1 such that the j-th and (j+1)-th characters of S_i are both T. Replace the j-th character of S_i with P, and (j+1)-th with C.

He tries to maximize the number of times he performs the operation. Find possible resulting S_1,S_2,\ldots,S_H.

Constraints

  • 1\leq H \leq 100
  • 2\leq W \leq 100
  • H and W are integers.
  • S_i is a string of length W consisting of . and T.

Input

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

H W 
S_1
S_2
\vdots
S_H

Output

Print a sequence of strings, S_1,S_2,\ldots,S_H, separated by newlines, possibly resulting from maximizing the number of times he performs the operation.

If multiple solutions exist, print any of them.


Sample Input 1

2 3
TTT
T.T

Sample Output 1

PCT
T.T

He can perform the operation at most once.

For example, an operation with (i,j)=(1,1) makes S_1 PCT.


Sample Input 2

3 5
TTT..
.TTT.
TTTTT

Sample Output 2

PCT..
.PCT.
PCTPC
G - Linear Probing

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

配点 : 400

問題文

N = 2^{20} 項からなる数列 A = (A_0, A_1, \dots, A_{N - 1}) があります。はじめ、全ての要素は -1 です。

Q 個のクエリを順番に処理してください。i \, (1 \leq i \leq Q) 個目のクエリは t_i = 1 または t_i = 2 を満たす整数 t_i および整数 x_i で表され、内容は以下の通りです。

  • t_i = 1 のとき、以下の処理を順番に行う。
    1. 整数 hh = x_i で定める。
    2. A_{h \bmod N} \neq -1 である間、h の値を 1 増やすことを繰り返す。この問題の制約下でこの操作が有限回で終了することは証明できる。
    3. A_{h \bmod N} の値を x_i で書き換える。
  • t_i = 2 のとき、その時点での A_{x_i \bmod N} の値を出力する。

なお、整数 a, b に対し、ab で割った余りを a \bmod b と表します。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
  • 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
  • t_i = 2 であるような i \, (1 \leq i \leq Q)1 つ以上存在する。
  • 入力は全て整数である。

入力

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

Q
t_1 x_1
\vdots
t_{Q} x_{Q}

出力

t_i = 2 であるようなクエリに対し、それぞれ答えを 1 行に出力せよ。そのようなクエリが 1 つ以上存在することは保証される。


入力例 1

4
1 1048577
1 1
2 2097153
2 3

出力例 1

1048577
-1

x_1 \bmod N = 1 であるので、1 番目のクエリによって A_1 = 1048577 となります。

2 番目のクエリにおいて、はじめ h = x_2 ですが、A_{h \bmod N} = A_{1} \neq -1 であるので h の値を 1 増やします。すると A_{h \bmod N} = A_{2} = -1 となるので、このクエリによって A_2 = 1 となります。

3 番目のクエリにおいて、A_{x_3 \bmod N} = A_{1} = 1048577 を出力します。

4 番目のクエリにおいて、A_{x_4 \bmod N} = A_{3} = -1 を出力します。

この問題において N = 2^{20} = 1048576 は定数であり、入力では与えられないことに注意してください。

Score : 400 points

Problem Statement

There is a sequence A = (A_0, A_1, \dots, A_{N - 1}) with N = 2^{20} terms. Initially, every term is -1.

Process Q queries in order. The i-th query (1 \leq i \leq Q) is described by an integer t_i such that t_i = 1 or t_i = 2, and another integer x_i, as follows.

  • If t_i = 1, do the following in order.
    1. Define an integer h as h = x_i.
    2. While A_{h \bmod N} \neq -1, keep adding 1 to h. We can prove that this process ends after finite iterations under the Constraints of this problem.
    3. Replace the value of A_{h \bmod N} with x_i.
  • If t_i = 2, print the value of A_{x_i \bmod N} at that time.

Here, for integers a and b, a \bmod b denotes the remainder when a is divided by b.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
  • 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
  • There is at least one i (1 \leq i \leq Q) such that t_i = 2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
t_1 x_1
\vdots
t_{Q} x_{Q}

Output

For each query with t_i = 2, print the response in one line. It is guaranteed that there is at least one such query.


Sample Input 1

4
1 1048577
1 1
2 2097153
2 3

Sample Output 1

1048577
-1

We have x_1 \bmod N = 1, so the first query sets A_1 = 1048577.

In the second query, initially we have h = x_2, for which A_{h \bmod N} = A_{1} \neq -1, so we add 1 to h. Now we have A_{h \bmod N} = A_{2} = -1, so this query sets A_2 = 1.

In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577.

In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1.

Note that, in this problem, N = 2^{20} = 1048576 is a constant and not given in input.

H - Digit Sum Divisible 2

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

配点 : 500

問題文

正整数 n の桁和を、n10 進法で表したときの各桁の和と定義します。例えば 2025 の桁和は 2 + 0 + 2 + 5 = 9 です。
正整数 nn の桁和で割り切れる時、n良い整数 と呼びます。例えば 2025 はその桁和である 9 で割り切れるので良い整数です。
正整数の組 (a, a+1) であって aa+1 が共に良い整数であるものを 双子の良い整数 と呼びます。例えば (2024, 2025) は双子の良い整数です。

正整数 N が与えられます。N \leq a かつ a + 1 \leq 2N であるような双子の良い整数 (a, a + 1) を発見してください。そのような (a, a + 1) が存在しない場合はそのことを報告してください。

制約

  • N1 以上 10^{100000} 未満の整数

入力

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

N

出力

問題文の条件を満たす (a, a+1) が存在する場合は a を、存在しない場合は -1 を出力せよ。a は leading-zeros を省略した表現で出力する必要がある点に注意せよ。
条件を満たす (a, a+1) が複数存在する場合はどれを出力してもよい。


入力例 1

5

出力例 1

8

(8, 9) は問題文の条件を満たす双子の良い整数です。他にも (5, 6), (6, 7), (7, 8), (9, 10) が条件を満たします。


入力例 2

21

出力例 2

-1

問題文の条件を満たす双子の良い整数は存在しません。


入力例 3

1234

出力例 3

2024

(2024, 2025) は問題文の条件を満たす双子の良い整数です。


入力例 4

1234567890123456789012345678901234567890

出力例 4

1548651852734633803438094164372911259190

Score : 500 points

Problem Statement

The digit sum of a positive integer n is defined as the sum of its digits when n is written in decimal. For example, the digit sum of 2025 is 2 + 0 + 2 + 5 = 9.
A positive integer n is called a good integer if it is divisible by its digit sum. For example, 2025 is a good integer because it is divisible by its digit sum of 9.
A pair of positive integers (a, a+1) is called twin good integers if both a and a+1 are good integers. For example, (2024, 2025) is twin good integers.

You are given a positive integer N. Find a pair of twin good integers (a, a + 1) such that N \leq a and a + 1 \leq 2N. If no such pair exists, report that fact.

Constraints

  • N is an integer at least 1 and less than 10^{100000}.

Input

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

N

Output

If there exists such a pair (a, a+1), output a. Otherwise, output -1. Do not print leading zeros.
If there are multiple such pairs, you may print any.


Sample Input 1

5

Sample Output 1

8

(8, 9) is a valid pair of twin good integers satisfying the conditions. Other examples include (5, 6), (6, 7), (7, 8), (9, 10).


Sample Input 2

21

Sample Output 2

-1

No pair of twin good integers satisfies the conditions.


Sample Input 3

1234

Sample Output 3

2024

(2024, 2025) is a valid pair of twin good integers.


Sample Input 4

1234567890123456789012345678901234567890

Sample Output 4

1548651852734633803438094164372911259190
I - Score of Permutations

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

配点 : 500

問題文

(1,2, \dots, N) を並び替えた長さ N の順列 P = (p_1, p_2, \dots, p_N) に対して、 P のスコア S(P) を次のように定めます。

  • N 人の人とすぬけ君がいて、N 人の人には 1,2,\dots,N の番号がついています。はじめ、人 i (1 \leq i \leq N) はボール i を持っています。
    すぬけ君が叫ぶたびに、i \neq p_i であるようなすべての人 i は人 p_i に持っているボールを同時に渡します。
    すぬけ君は、1 回以上叫んだ後にすべての人 i がボール i を持っている状態になると叫ぶのをやめます。
    すぬけ君が叫ぶのをやめるまでに叫んだ回数が順列のスコアとなります。ここでスコアは有限の値を取ることが保証されます。

P としてあり得るものは N! 通りありますが、それらのスコアを K 乗した値の総和を 998244353 で割ったあまりを計算してください。

  • 厳密に言い換えると、(1,2, \dots, N) を並び替えた長さ N の順列全体の集合を S_N として

    \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}

    を計算してください。

制約

  • 2 \leq N \leq 50
  • 1 \leq K \leq 10^4
  • 入力はすべて整数である。

入力

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

N K

出力

\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353} を出力せよ。


入力例 1

2 2

出力例 1

5

N = 2 のとき P としてあり得る順列は (1,2),(2,1)2 つです。

順列 (1,2) のスコアは次のように決まります。

  • はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
    すぬけ君が 1 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
    このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
    よってスコアは 1 となります。

順列 (2,1) のスコアは次のように決まります。

  • はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
    すぬけ君が 1 回目に叫んだ後に、人 1 はボール 2 を、人 2 はボール 1 を持っています。
    すぬけ君が 2 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
    このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
    よってスコアは 2 となります。

よって 1^2 + 2^2 = 5 がこの問題の答えになります。


入力例 2

3 3

出力例 2

79

すべての順列とスコアの組を列挙すると以下のようになります。

  • 順列 : (1,2,3), スコア : 1
  • 順列 : (1,3,2), スコア : 2
  • 順列 : (2,1,3), スコア : 2
  • 順列 : (2,3,1), スコア : 3
  • 順列 : (3,1,2), スコア : 3
  • 順列 : (3,2,1), スコア : 2

よって 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79 を出力します。


入力例 3

50 10000

出力例 3

77436607

Score : 500 points

Problem Statement

For a permutation P = (p_1, p_2, \dots, p_N) of (1,2, \dots, N), let us define the score S(P) of P as follows.

  • There are N people, numbered 1,2,\dots,N. Additionally, Snuke is there. Initially, Person i (1 \leq i \leq N) has Ball i.
    Each time Snuke screams, every Person i such that i \neq p_i gives their ball to Person p_i simultaneously.
    If, after screaming at least once, every Person i has Ball i, Snuke stops screaming.
    The score is the number of times Snuke screams until he stops. Here, it is guaranteed that the score will be a finite value.

There are N! permutations P of (1,2, \dots, N). Find the sum, modulo 998244353, of the scores of those permutations, each raised to the K-th power.

  • Formally, let S_N be the set of the permutations of (1,2, \dots, N). Compute the following: \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.

Constraints

  • 2 \leq N \leq 50
  • 1 \leq K \leq 10^4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.


Sample Input 1

2 2

Sample Output 1

5

When N = 2, there are two possible permutations P: (1,2),(2,1).

The score of the permutation (1,2) is found as follows.

  • Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
    After Snuke's first scream, Person 1 has Ball 1, and Person 2 has Ball 2.
    Here, every Person i has Ball i, so he stops screaming.
    Thus, the score is 1.

The score of the permutation (2,1) is found as follows.

  • Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
    After Snuke's first scream, Person 1 has Ball 2, and Person 2 has Ball 1.
    After Snuke's second scream, Person 1 has Ball 1, and Person 2 has Ball 2.
    Here, every Person i has Ball i, so he stops screaming.
    Thus, the score is 2.

Therefore, the answer in this case is 1^2 + 2^2 = 5.


Sample Input 2

3 3

Sample Output 2

79

All permutations and their scores are listed below.

  • (1,2,3): The score is 1.
  • (1,3,2): The score is 2.
  • (2,1,3): The score is 2.
  • (2,3,1): The score is 3.
  • (3,1,2): The score is 3.
  • (3,2,1): The score is 2.

Thus, we should print 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79.


Sample Input 3

50 10000

Sample Output 3

77436607