E - K Swap

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

配点 : 300

問題文

長さ N の数列 A=(a_1,\ldots,a_N) があります。また、整数 K が与えられます。

あなたは次の操作を 0 回以上何度でも行えます。

  • 1 \leq i \leq N-K を満たす整数 i を選び、a_ia_{i+K} の値を入れ替える。

A を昇順に並べ替えることが出来るかどうかを判定してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N-1
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
a_1 \ldots a_N

出力

A を昇順に並び替えることが出来るならば Yes と、出来ないならば No と出力せよ。


入力例 1

5 2
3 4 1 3 4

出力例 1

Yes

次のように操作をすることで A を昇順に並び替えることが出来ます。

  • i=1 とし、a_1a_3 の値を入れ替える。数列は (1,4,3,3,4) となる。
  • i=2 とし、a_2a_4 の値を入れ替える。数列は (1,3,3,4,4) となる。

入力例 2

5 3
3 4 1 3 4

出力例 2

No

入力例 3

7 5
1 2 3 4 5 5 10

出力例 3

Yes

操作を行う必要が無い場合もあります。

Score : 300 points

Problem Statement

We have a sequence of length N: A=(a_1,\ldots,a_N). Additionally, you are given an integer K.

You can perform the following operation zero or more times.

  • Choose an integer i such that 1 \leq i \leq N-K, then swap the values of a_i and a_{i+K}.

Determine whether it is possible to sort A in ascending order.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq N-1
  • 1 \leq a_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 \ldots a_N

Output

If it is possible to sort A in ascending order, print Yes; otherwise, print No.


Sample Input 1

5 2
3 4 1 3 4

Sample Output 1

Yes

The following sequence of operations sorts A in ascending order.

  • Choose i=1 to swap the values of a_1 and a_3. A is now (1,4,3,3,4).
  • Choose i=2 to swap the values of a_2 and a_4. A is now (1,3,3,4,4).

Sample Input 2

5 3
3 4 1 3 4

Sample Output 2

No

Sample Input 3

7 5
1 2 3 4 5 5 10

Sample Output 3

Yes

No operations may be needed.

F - 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.

G - Add One Edge

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

配点 : 400

問題文

N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。

  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結

次の操作をちょうど 1 回行います。

  • 1 \leq u \leq N_1 を満たす整数 uN_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する

操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。

操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。

連結とは? 無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。

制約

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結
  • 入力はすべて整数

入力

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

5

u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。


入力例 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

出力例 2

4

Score : 400 points

Problem Statement

We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:

  • Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.

Consider performing the following operation exactly once:

  • choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.

We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).

Find the maximum possible d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.

Constraints

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • (a_i,b_i) \neq (a_j,b_j) if i \neq j.
  • Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.
  • All input values are integers.

Input

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

5

If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.


Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4
H - Playlist

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

配点 : 450

問題文

高橋君は N 曲からなるプレイリストを持っています。 曲 i (1 \leq i \leq N) の長さは T_i 秒です。
高橋君は時刻 0 にプレイリストのランダム再生を開始しました。

ランダム再生では、N 曲の中から等確率で 1 つを選びその曲を最後まで再生することが繰り返されます。 ここで、曲の再生は休みなく行われ、1 つの曲が終わったらすぐに次に選ばれた曲が始まります。 また、同じ曲が連続して選ばれる事もあります。

時刻 0 から (X+0.5) 秒後に曲 1 が再生されている確率を \text{mod}998244353 で求めてください。

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 2 \leq N\leq 10^3
  • 0 \leq X\leq 10^4
  • 1 \leq T_i\leq 10^4
  • 入力はすべて整数

入力

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

N X
T_1 T_2 \ldots T_N

出力

時刻 0 から (X+0.5) 秒後にプレイリストの 1 番目の曲が再生されている確率を \text{mod}998244353 で出力せよ。


入力例 1

3 6
3 5 6

出力例 1

369720131

時刻 0 から 6.5 秒後に曲 1 が流れているパターンとしてあり得るのは、

  • 1 \to1 \to1
  • 2 \to1
  • 3 \to1

の順で音楽が再生された場合であり、これらのいずれかが起こる確率は \frac{7}{27} となります。
369720131\times 27\equiv 7 \pmod{998244353} であるため、369720131 を出力します。


入力例 2

5 0
1 2 1 2 1

出力例 2

598946612

時刻 0 から 0.5 秒後には最初に再生された曲が再生されているため、求める確率は \frac{1}{5} となります。
同じ長さの異なる曲が存在することがあることに注意してください。


入力例 3

5 10000
1 2 3 4 5

出力例 3

586965467

Score : 450 points

Problem Statement

Takahashi has a playlist with N songs. Song i (1 \leq i \leq N) lasts T_i seconds.
Takahashi has started random play of the playlist at time 0.

Random play repeats the following: choose one song from the N songs with equal probability and play that song to the end. Here, songs are played continuously: once a song ends, the next chosen song starts immediately. The same song can be chosen consecutively.

Find the probability that song 1 is being played (X + 0.5) seconds after time 0, modulo 998244353.

How to print a probability modulo 998244353

It can be proved that the probability to be found in this problem is always a rational number. Also, the constraints of this problem guarantee that when the probability to be found is expressed as an irreducible fraction \frac{y}{x}, x is not divisible by 998244353.

Then, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 2 \leq N\leq 10^3
  • 0 \leq X\leq 10^4
  • 1 \leq T_i\leq 10^4
  • All input values are integers.

Input

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

N X
T_1 T_2 \ldots T_N

Output

Print the probability, modulo 998244353, that the first song in the playlist is being played (X+0.5) seconds after time 0.


Sample Input 1

3 6
3 5 6

Sample Output 1

369720131

Song 1 will be playing 6.5 seconds after time 0 if songs are played in one of the following orders.

  • Song 1 \to Song 1 \to Song 1
  • Song 2 \to Song 1
  • Song 3 \to Song 1

The probability that one of these occurs is \frac{7}{27}.
We have 369720131\times 27\equiv 7 \pmod{998244353}, so you should print 369720131.


Sample Input 2

5 0
1 2 1 2 1

Sample Output 2

598946612

0.5 seconds after time 0, the first song to be played is still playing, so the sought probability is \frac{1}{5}.
Note that different songs may have the same length.


Sample Input 3

5 10000
1 2 3 4 5

Sample Output 3

586965467
I - Two Exams

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

配点 : 500

問題文

高橋王国にて、 1 から N までの番号のついた N 人の国民が競技プログラミングの試験に参加しました。
試験は 2 回からなり、人 i1 回目の試験で P_i 位、 2 回目の試験で Q_i 位となりました。
なお、どちらの試験においても、複数人が同じ順位になることはありませんでした。すなわち、順位を表す数列 P,Q はそれぞれ (1,2,...,N) の順列です。

高橋王国の大統領であるいろはちゃんは、この試験の結果に基づいて、 N 人の中から競技プログラミングの世界大会に出場する K 人の代表を決めることになりました。
代表を決めるにあたって、以下が成立していなければなりません。

  • x が代表であり、人 y が代表でないような人の組 (x,y) であって、 P_x > P_y かつ Q_x > Q_y であるようなものが存在してはならない。
    • 言い換えると、 2 回の試験の双方で人 y が人 x よりも小さい順位を取っているにも拘らず、人 x が代表で人 y が代表でないということがあってはならない。

いろはちゃんは、ひとまず上記の条件を満たす代表の選び方の数を知りたいので、この数を求めてください。
ただし、この数は非常に大きくなる場合もあるので、 998244353 で割った余りを出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 300
  • 1 \le K \le N
  • P,Q(1,2,...,N) の順列である。

入力

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

N K
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

出力

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


入力例 1

4 2
2 4 3 1
2 1 4 3

出力例 1

3
  • 1 と人 2 を代表にすることは問題ありません。
  • 1 と人 3 を代表にすると、双方の試験で人 4 が人 3 よりも小さい順位を取っているため、 (x,y)=(3,4) に対して問題文中の条件に違反します。
  • 1 と人 4 を代表にすることは問題ありません。
  • 2 と人 3 を代表にすると、 (x,y)=(3,1) に対して問題文中の条件に違反します。
  • 2 と人 4 を代表にすることは問題ありません。
  • 3 と人 4 を代表にすると、 (x,y)=(3,1) に対して問題文中の条件に違反します。

結局、求める答えは 3 通りです。


入力例 2

33 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

出力例 2

168558757

33 人から 16 人を選ぶ \binom{33}{16} = 1166803110 通りの全てにおいて、問題文中の条件を満たします。
よって、 1166803110998244353 で割った余りである 168558757 を出力することとなります。


入力例 3

15 7
4 9 7 5 6 13 2 11 3 1 12 14 15 10 8
4 14 9 12 7 15 1 2 8 11 3 5 13 6 10

出力例 3

23

Score : 500 points

Problem Statement

In the Kingdom of Takahashi, N citizens numbered 1 to N took an examination of competitive programming.
There were two tests, and Citizen i ranked P_i-th in the first test and Q_i-th in the second test.
There were no ties in either test. That is, each of the sequences P and Q is a permutation of (1, 2, ..., N).

Iroha, the president of the kingdom, is going to select K citizens for the national team at the coming world championship of competitive programming.
The members of the national team must be selected so that the following is satisfied.

  • There should not be a pair of citizens (x, y) where Citizen x is selected and Citizen y is not selected such that P_x > P_y and Q_x > Q_y.
    • In other words, if Citizen y got a rank smaller than that of Citizen x in both tests, it is not allowed to select Citizen x while not selecting Citizen y.

To begin with, Iroha wants to know the number of ways to select citizens for the national team that satisfy the condition above. Find it to help her.
Since this number can be enormous, print it modulo 998244353.

Constraints

  • All values in input are integers.
  • 1 \le N \le 300
  • 1 \le K \le N
  • Each of P and Q is a permutation of (1,2,...,N).

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \dots P_N
Q_1 Q_2 \dots Q_N

Output

Print the answer as an integer.


Sample Input 1

4 2
2 4 3 1
2 1 4 3

Sample Output 1

3
  • It is fine to select Citizen 1 and Citizen 2 for the team.
  • If Citizen 1 and Citizen 3 are selected, Citizen 4 ranked higher than Citizen 3 did in both tests, so the pair (x,y)=(3,4) would violate the condition in the Problem Statement.
  • It is fine to select Citizen 1 and Citizen 4.
  • If Citizen 2 and Citizen 3 are selected, the pair (x,y)=(3,1) would violate the condition.
  • It is fine to select Citizen 2 and Citizen 4.
  • If Citizen 3 and Citizen 4 are selected, the pair (x,y)=(3,1) would violate the condition.

The final answer is 3.


Sample Input 2

33 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Sample Output 2

168558757

All \binom{33}{16} = 1166803110 ways of selecting 16 from the 33 citizens satisfy the requirement.
Therefore, we should print 1166803110 modulo 998244353, that is, 168558757.


Sample Input 3

15 7
4 9 7 5 6 13 2 11 3 1 12 14 15 10 8
4 14 9 12 7 15 1 2 8 11 3 5 13 6 10

Sample Output 3

23