#P2155. [SDOI2008] 沙拉公主的困惑

    ID: 1133 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>数学2008各省省选山东素数判断,质数,筛法逆元

[SDOI2008] 沙拉公主的困惑

Description

Due to inflation and rampant counterfeiting, the government of the Monopoly Kingdom decides on a new policy: the serial numbers of existing banknotes range from 11 to N!N!, but the government will issue only banknotes whose serial numbers are coprime to M!M!. Princess Shala, the largest real estate holder, wants to estimate the total number of genuine banknotes now. Please help Princess Shala solve this problem. Since the number can be very large, you only need to compute the answer modulo RR.

Input Format

The first line contains two integers TT and RR, where TT is the number of testdata in this set, and RR is the modulus.

The next TT lines each contain a pair of integers NN and MM, as described above.

Output Format

Output TT lines. For each pair NN and MM, output the number of integers in [1,N!][1, N!] that are coprime to M!M!, taken modulo RR.

1 11
4 2
1

Hint

For 100%100\% of the testdata, 1MN1071 \le M \le N \le 10^7, 1T1041 \le T \le 10^4, 2R109+102 \le R \le 10^9+10, and RR is prime.

Translated by ChatGPT 5