0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-11 22:46:28 +00:00

P2155 [SDOI2008] 沙拉公主的困惑

https://www.luogu.com.cn/record/96680376
This commit is contained in:
Baoshuo Ren 2022-12-06 14:47:46 +08:00
parent 55e9b45de1
commit 7b40b909b2
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 76 additions and 0 deletions

70
Luogu/P2155/P2155.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e7 + 5;
int t, mod, p, primes[N], fac[N], fac_phi[N];
bool not_prime[N];
int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = static_cast<long long>(res) * a % mod;
a = static_cast<long long>(a) * a % mod;
b >>= 1;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t >> mod;
for (int i = 2; i < N; i++) {
if (!not_prime[i]) primes[++p] = i;
for (int j = 1; j <= p && i * primes[j] < N; j++) {
not_prime[i * primes[j]] = true;
if (i % primes[j] == 0) break;
}
}
fac[0] = 1;
for (int i = 1; i < N; i++) {
if (i % mod) {
fac[i] = static_cast<long long>(fac[i - 1]) * i % mod;
} else {
fac[i] = fac[i - 1];
}
}
fac_phi[1] = 1;
for (int i = 2; i < N; i++) {
if (not_prime[i]) {
fac_phi[i] = static_cast<long long>(fac_phi[i - 1]) * i % mod;
} else {
fac_phi[i] = static_cast<long long>(fac_phi[i - 1]) * (i - 1) % mod;
}
}
while (t--) {
int n, m;
cin >> n >> m;
cout << (n / mod > m / mod ? 0 : static_cast<long long>(fac[n]) * fac_phi[m] % mod * binpow(fac[m], mod - 2) % mod) << endl;
}
return 0;
}

BIN
Luogu/P2155/data/P2155_2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Luogu/P2155/data/P2155_2.out (Stored with Git LFS) Normal file

Binary file not shown.