设
首先介绍一个性质:当
当
当
到现在,就可以根据上面的性质得出,答案
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
const int N = 1e7;
int T, PYZ, m, n, f[N + 5], fac[N + 5];
bool prm[N + 5];
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (1ll * res * a) % PYZ;
a = (1ll * a * a) % PYZ;
b >>= 1;
}
return res;
}
int main() {
T = read(); PYZ = read();
int i, j; prm[0] = prm[1] = f[0] = fac[0] = 1;
for (i = 2; i * i <= N; i++) {
if (prm[i]) continue;
for (j = i * i; j <= N; j += i)
prm[j] = 1;
}
for (i = 1; i <= N; i++)
f[i] = 1ll * f[i - 1] * (prm[i] ? i : i - 1) % PYZ,
fac[i] = 1ll * fac[i - 1] * i % PYZ;
while (T--) {
n = read(); m = read();
int res = 1ll * f[m] * fac[n] % PYZ;
res = 1ll * res * qpow(fac[m], PYZ - 2) % PYZ;
printf("%d\n", res);
}
return 0;
}