0%

Mocha and Stars

Mocha and Stars

time limit per test: 2 seconds

memory limit per test: 256 megabytes

Articles

Mocha wants to be an astrologer. There are nn stars which can be seen in Zhijiang, and the brightness of the ithi^{th} star is aia_i.

Mocha considers that these nn stars form a constellation, and she uses (a1,a2,,an)(a_1,a_2,…,a_n) to show its state. A state is called mathematical if all of the following three conditions are satisfied:

  • For all ii (1in)(1≤i≤n), aia_i is an integer in the range li,ril_i,r_i.
  • i=1naim∑_{i=1}^na_i≤m.
  • gcd(a1,a2,,an)=1gcd(a_1,a_2,…,a_n)=1.

Here, gcd(a1,a2,,an)gcd(a_1,a_2,…,a_n) denotes the greatest common divisor (GCD) of integers a1,a2,,ana_1,a_2,…,a_n.

Mocha is wondering how many different mathematical states of this constellation exist. Because the answer may be large, you must find it modulo 998244353998244353.

Two states (a1,a2,,an)(a_1,a_2,…,a_n) and (b1,b2,,bn)(b_1,b_2,…,b_n) are considered different if there exists ii (1in)(1≤i≤n) such that aibia_i≠b_i.

Input

The first line contains two integers nn and mm (2n50,1m105)(2≤n≤50, 1≤m≤10^5) — the number of stars and the upper bound of the sum of the brightness of stars.

Each of the next nn lines contains two integers lil_i and rir_i (1lirim)(1≤l_i≤r_i≤m) — the range of the brightness of the ithi^{th} star.

Output

Print a single integer — the number of different mathematical states of this constellation, modulo 998244353998244353.

Examples

input

1
2
3
2 4
1 3
1 2

output

1
4

input

1
2
3
4
5
6
5 10
1 10
1 10
1 10
1 10
1 10

output

1
251

input

1
2
3
4
5
6
5 100
1 94
1 96
1 91
4 96
6 97

output

1
47464146

Note

In the first example, there are 44 different mathematical states of this constellation:

  • a1=1,a2=1.a_1=1, a_2=1.
  • a1=1,a2=2.a_1=1, a_2=2.
  • a1=2,a2=1a_1=2, a_2=1.
  • a1=3,a2=1a_1=3, a_2=1.

Tutorial

\large \begin{align*}\label{2} &\sum_{a_1=l_1}^{r_1}\sum_{a_2=l_2}^{r_2}\cdots\sum_{a_n=l_n}^{r_n}[\sum a_i \leq m][gcd(a_1,a_2,\cdots,a_n)=1] \\ =&\sum_{a_1=l_1}^{r_1}\sum_{a_2=l_2}^{r_2}\cdots\sum_{a_n=l_n}^{r_n}[\sum a_i \leq m]\sum_{d|gcd(a_1,\cdots,a_n)}\mu(d) \\ =&\sum_{d=1}^{m}\sum_{a_1=\lceil \frac{l_1}{d} \rceil}^{\lfloor \frac{r_1}{d} \rfloor}\sum_{a_2=\lceil \frac{l_2}{d} \rceil}^{\lfloor \frac{r_2}{d} \rfloor}\cdots\sum_{a_n=\lceil \frac{l_n}{d} \rceil}^{\lfloor \frac{r_n}{d} \rfloor}[\sum d\cdot a_i\leq m]\\ =&\sum_{d=1}^{m}\sum_{a_1=\lceil \frac{l_1}{d} \rceil}^{\lfloor \frac{r_1}{d} \rfloor}\sum_{a_2=\lceil \frac{l_2}{d} \rceil}^{\lfloor \frac{r_2}{d} \rfloor}\cdots\sum_{a_n=\lceil \frac{l_n}{d} \rceil}^{\lfloor \frac{r_n}{d} \rfloor}[\sum a_i\leq \lfloor \frac{m}{d} \rfloor]\\ \end{align*}

For each d, we can compute it in O(nmd)O(n\frac{m}{d}) by Knapsack DP optimized by prefix-sums. So we can compute it in O(nd=1mmd)=O(nmlogm)O(n∑_{d=1}^{m}⌊\frac{m}{d}⌋)=O(nmlogm).

Code

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>

using namespace std;

const int mod = 998244353;
const int maxn = 1e5+1;

int l[maxn], r[maxn];
long long dp[51][maxn];
long long prime[maxn], tot, mu[maxn];
bool vis[maxn];

void get_prime(int n) {
mu[1] = 1;
vis[1] = true;
for(int i = 2; i <= n; ++ i) {
if (!vis[i]) prime[++tot] = i, mu[i] = -1;
for(int j = 1; i * prime[j] <= n; ++ j) {
vis[i*prime[j]] = true;
if (i % prime[j] == 0) break;
mu[i*prime[j]] = mu[i] * mu[prime[j]];
}
}
}

int main() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++ i) {
cin >> l[i] >> r[i];
}
for(int i = 0; i <= m; ++ i) {
dp[0][i] = 1;
}
get_prime(m);
long long ans = 0;
for(int d = 1; d <= m; ++ d) {
int M = m/d, flag = 1;
if (mu[d] == 0 || M == 0) continue;
for(int i = 1; i <= n; ++ i) {
int x = (l[i]+d-1)/d, y = r[i]/d;
if (x > y) {
flag = 0;
break;
}
for(int j = 1; j <= M; ++ j) {
dp[i][j] = 0;
if (x <= j) dp[i][j] = (dp[i][j] + dp[i-1][j-x]) % mod;
if (y < j) dp[i][j] = (mod + (dp[i][j] - dp[i-1][j-y-1])%mod) %mod;
dp[i][j] = (dp[i][j] + dp[i][j-1]) % mod;
}
}
if (flag) ans = (ans + mod + mu[d] * dp[n][M]) % mod;
}
cout << ans << "\n";
}
-------------本文结束感谢您的阅读-------------