D - Cards Touching with the Same Number Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

N 枚のカードが左から順に並んでいます。左から i 枚目のカードの表には整数 A_i、裏には整数 B_i が書かれています。

それぞれのカードについて、表と裏のどちらを上にするかを選びます。カードの並び順は変えません。

隣り合うカードの上面に書かれた整数が等しい箇所が、ちょうど K 箇所になるような表裏の選び方は何通りありますか。答えを 998244353 で割った余りを求めてください。

表と裏に同じ整数が書かれているカードについても、表を上にする選び方と裏を上にする選び方は区別します。

制約

  • 1 \leq N \leq 2000
  • 0 \leq K \leq N-1
  • 1 \leq A_i,B_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

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

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • 1 行目にはカードの枚数 N と指定する箇所数 K が与えられます。
  • 続く N 行のうち i 行目には、左から i 枚目のカードの整数 A_i,B_i が与えられます。

出力

条件を満たす表裏の選び方の数を 998244353 で割った余りを出力してください。


入力例 1

3 1
1 2
1 2
1 2

出力例 1

4

入力例 2

2 1
1 1
2 2

出力例 2

0

入力例 3

6 2
1 2
2 3
3 1
1 1
2 1
2 3

出力例 3

38

入力例 4

10 4
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2

出力例 4

252

入力例 5

1 0
1000000000 1000000000

出力例 5

2

Score : 366 pts

Problem Statement

There are N cards arranged in a row from left to right. The i-th card from the left has an integer A_i written on the front and an integer B_i written on the back.

For each card, you choose whether to place it front-side up or back-side up. The order of the cards does not change.

How many ways are there to choose which side faces up for each card such that there are exactly K positions where adjacent cards have the same integer written on their top faces? Find the count modulo 998244353.

Even if a card has the same integer written on both the front and back, placing it front-side up and placing it back-side up are distinguished.

Constraints

  • 1 \leq N \leq 2000
  • 0 \leq K \leq N-1
  • 1 \leq A_i,B_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

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

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N
  • The first line contains the number of cards N and the specified number of positions K.
  • The i-th of the following N lines contains the integers A_i, B_i of the i-th card from the left.

Output

Print the number of ways to choose the card orientations satisfying the condition, modulo 998244353.


Sample Input 1

3 1
1 2
1 2
1 2

Sample Output 1

4

Sample Input 2

2 1
1 1
2 2

Sample Output 2

0

Sample Input 3

6 2
1 2
2 3
3 1
1 1
2 1
2 3

Sample Output 3

38

Sample Input 4

10 4
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2

Sample Output 4

252

Sample Input 5

1 0
1000000000 1000000000

Sample Output 5

2