From 74f9851e31c92af982399b5745fc3658cf2f0a16 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 17 Jul 2022 19:35:44 +0800 Subject: [PATCH] =?UTF-8?q?P3807=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=8D=A2=E5=8D=A1=E6=96=AF=E5=AE=9A=E7=90=86/Lucas=20=E5=AE=9A?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/79996740 --- Luogu/P3807/P3807.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Luogu/P3807/P3807.cpp diff --git a/Luogu/P3807/P3807.cpp b/Luogu/P3807/P3807.cpp new file mode 100644 index 00000000..c6d11618 --- /dev/null +++ b/Luogu/P3807/P3807.cpp @@ -0,0 +1,48 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int t, n, m, p, fac[N], inv[N]; + +inline int C(int n, int m, int p) { + if (m > n) return 0; + return 1ll * fac[n] * inv[m] % p * inv[n - m] % p; +} + +int lucas(int n, int m, int p) { + if (!m) return 1; + return static_cast(C(n % p, m % p, p)) * lucas(n / p, m / p, p) % p; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> t; + + while (t--) { + cin >> n >> m >> p; + + fac[0] = 1; + for (int i = 1; i <= 100000; i++) { + fac[i] = 1ll * fac[i - 1] * i % p; + } + + inv[0] = inv[1] = 1; + for (int i = 2; i <= 100000; i++) { + inv[i] = static_cast(p - (p / i)) * inv[p % i] % p; + } + + for (int i = 2; i <= 100000; i++) { + inv[i] = static_cast(inv[i - 1]) * inv[i] % p; + } + + cout << lucas(n + m, n, p) << endl; + } + + return 0; +}