F - Two Exams Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

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

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

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

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

制約

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

入力

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

NN KK
P1P_1 P2P_2 \dots PNP_N
Q1Q_1 Q2Q_2 \dots QNQ_N

出力

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


入力例 1Copy

Copy
4 2
2 4 3 1
2 1 4 3

出力例 1Copy

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

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


入力例 2Copy

Copy
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

出力例 2Copy

Copy
168558757

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


入力例 3Copy

Copy
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

出力例 3Copy

Copy
23

Score : 500500 points

Problem Statement

In the Kingdom of Takahashi, NN citizens numbered 11 to NN took an examination of competitive programming.
There were two tests, and Citizen ii ranked PiP_i-th in the first test and QiQ_i-th in the second test.
There were no ties in either test. That is, each of the sequences PP and QQ is a permutation of (1,2,...,N)(1, 2, ..., N).

Iroha, the president of the kingdom, is going to select KK 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)(x, y) where Citizen xx is selected and Citizen yy is not selected such that Px>PyP_x > P_y and Qx>QyQ_x > Q_y.
    • In other words, if Citizen yy got a rank smaller than that of Citizen xx in both tests, it is not allowed to select Citizen xx while not selecting Citizen yy.

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

Constraints

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

Input

Input is given from Standard Input in the following format:

NN KK
P1P_1 P2P_2 \dots PNP_N
Q1Q_1 Q2Q_2 \dots QNQ_N

Output

Print the answer as an integer.


Sample Input 1Copy

Copy
4 2
2 4 3 1
2 1 4 3

Sample Output 1Copy

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

The final answer is 33.


Sample Input 2Copy

Copy
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 2Copy

Copy
168558757

All (3316)=1166803110\binom{33}{16} = 1166803110 ways of selecting 1616 from the 3333 citizens satisfy the requirement.
Therefore, we should print 11668031101166803110 modulo 998244353998244353, that is, 168558757168558757.


Sample Input 3Copy

Copy
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 3Copy

Copy
23


2025-03-29 (Sat)
20:56:27 +00:00