Mocha and Stars
time limit per test: 2 seconds
memory limit per test: 256 megabytes
Articles
Mocha wants to be an astrologer. There are stars which can be seen in Zhijiang, and the brightness of the star is .
Mocha considers that these stars form a constellation, and she uses to show its state. A state is called mathematical if all of the following three conditions are satisfied:
- For all , is an integer in the range .
- .
- .
Here, denotes the greatest common divisor (GCD) of integers .
Mocha is wondering how many different mathematical states of this constellation exist. Because the answer may be large, you must find it modulo .
Two states and are considered different if there exists such that .
Input
The first line contains two integers and — the number of stars and the upper bound of the sum of the brightness of stars.
Each of the next lines contains two integers and — the range of the brightness of the star.
Output
Print a single integer — the number of different mathematical states of this constellation, modulo .
Examples
input
1 | 2 4 |
output
1 | 4 |
input
1 | 5 10 |
output
1 | 251 |
input
1 | 5 100 |
output
1 | 47464146 |
Note
In the first example, there are 44 different mathematical states of this constellation:
- .
- .
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 by Knapsack DP optimized by prefix-sums. So we can compute it in .
Code
1 |
|