luogu#P8322. 『JROI-4』少女幻葬

『JROI-4』少女幻葬

Background

Original background of this problem

"Girl's Necro-Fantasy" is the theme song of Yakumo Ran. It is also the battle music for the boss of the extra stage in Touhou Youyoumu.

It is one of the most classic pieces among the music using the "ZUN trumpet" (ZUN 号). The piano seems to depict a powerful and beautiful beast, and the bright ZUN trumpet, together with Ran's gorgeous bullet patterns, brings the theme of death to its peak.

Problem Description

Given a sequence aa of length nn, and the value range [li,ri][l_i,r_i] for the ii-th number aia_i. Now Ran wants to know how many sequences aa satisfy, for a given constant kk, that:

  • The greatest common divisor of any two adjacent numbers is not kk.

  • The greatest common divisor of any three adjacent numbers is exactly kk.

Since the answer may be very large, output its value mod 998244353\bmod \space998244353.

Input Format

The first line contains two integers n,kn,k.

In the next nn lines, each line contains two integers, representing li,ril_i,r_i.

Output Format

One line: the answer modulo 998244353998244353.

3 1
1 6
2 6
3 6
3
4 1
11 45
19 81
31 53
7 28
6295

Hint

[Sample Explanation]

For sample 11, the valid sequences are: [2,6,3],[3,6,4],[4,6,3][2,6,3],[3,6,4],[4,6,3].

[Constraints and Conventions]

  • Subtask 1 (7 pts): 3n53 \leq n \leq 5, 1m101 \leq m \leq 10.
  • Subtask 2 (23 pts): 3n1003 \leq n \leq 100, 1m1001 \leq m \leq 100.
  • Subtask 3 (25 pts): 3n10003 \leq n \leq 1000, 1m10001 \leq m \leq 1000.
  • Subtask 4 (45 pts): no special restrictions.

For 100%100\% of the data, 3n20003 \leq n \leq 2000, 1liri50001 \leq l_i \leq r_i \leq 5000, 1k50001 \leq k \leq 5000.

Here, m=maxi=1nrim=\max_{i=1}^{n}r_i.

Translated by ChatGPT 5