From 09e9e78b0840be9b2cc244af890b58f58a9d87bc Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 8 Feb 2023 08:48:33 +0800 Subject: [PATCH] =?UTF-8?q?P4859=20=E5=B7=B2=E7=BB=8F=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BB=80=E4=B9=88=E5=A5=BD=E5=AE=B3=E6=80=95=E7=9A=84=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/101676924 --- Luogu/P4859/P4859.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Luogu/P4859/P4859.cpp diff --git a/Luogu/P4859/P4859.cpp b/Luogu/P4859/P4859.cpp new file mode 100644 index 00000000..98be5b1e --- /dev/null +++ b/Luogu/P4859/P4859.cpp @@ -0,0 +1,80 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 2005; +const int mod = 1e9 + 9; + +int n, k, inv[N], fac[N], inv_fac[N], a[N], b[N], l[N], f[N][N], g[N], ans; + +inline int C(int n, int m) { + return static_cast(fac[n]) * inv_fac[m] % mod * inv_fac[n - m] % mod; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + fac[0] = 1; + for (int i = 1; i < N; i++) { + fac[i] = static_cast(fac[i - 1]) * i % mod; + } + + inv[0] = inv[1] = 1; + for (int i = 2; i < N; i++) { + inv[i] = static_cast(mod - (mod / i)) * inv[mod % i] % mod; + } + + inv_fac[0] = inv_fac[1] = 1; + for (int i = 2; i < N; i++) { + inv_fac[i] = static_cast(inv_fac[i - 1]) * inv[i] % mod; + } + + cin >> n >> k; + + if ((n - k) % 2 == 1) { + cout << 0 << endl; + + exit(0); + } + + std::copy_n(std::istream_iterator(cin), n, a + 1); + std::copy_n(std::istream_iterator(cin), n, b + 1); + + std::sort(a + 1, a + 1 + n); + std::sort(b + 1, b + 1 + n); + std::transform(a + 1, a + 1 + n, l + 1, [&](int x) { + return std::distance(b + 1, std::lower_bound(b + 1, b + 1 + n, x)); + }); + + f[0][0] = 1; + + for (int i = 1; i <= n; i++) { + f[i][0] = f[i - 1][0]; + + for (int j = 1; j <= i; j++) { + f[i][j] = (static_cast(f[i - 1][j]) + static_cast(f[i - 1][j - 1]) * (l[i] - j + 1) % mod) % mod; + } + } + + for (int i = 0; i <= n; i++) { + g[i] = static_cast(f[n][i]) * fac[n - i] % mod; + } + + int w = (n + k) >> 1; + for (int i = w; i <= n; i++) { + if ((i - w) & 1) { + ans = (static_cast(ans) - static_cast(C(i, w)) * g[i] % mod + mod) % mod; + } else { + ans = (static_cast(ans) + static_cast(C(i, w)) * g[i] % mod) % mod; + } + } + + cout << ans << endl; + + return 0; +}