P4859 已经没有什么好害怕的了(二项式反演+dp)

题录:P4859 已经没有什么好害怕的了 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

代码:

 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
#define  LL long long 
const int N = 2e3 + 100;
const long long mod = 1e9+9;
LL n, k;
LL a[N], b[N], F[N][N], f[N], g[N],fc[N],c[N];
LL quick(LL a, LL b, LL mod)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1) ans = ans * a % mod;
        b = b >> 1;
        a = a * a % mod;
    }
    return ans;
}
void into()
{
    c[0] = 1;
    for (int i = 1; i <= n; i++) c[i] = (LL)c[i - 1] * i % mod;
    for (int i = 0; i <= n; i++) fc[i] = quick(c[i], mod - 2, mod);
}
int seek(int num,int x)
{
    int l = 1, r = n, ans=0,mid;
    while (l <= r)
    {
         mid = (l + r) >> 1;
        if (a[num] > b[mid])
            ans = mid, l = mid + 1;
        else
            r = mid - 1;
    }
    return ans - x < 0 ? 0 : ans - x;
}
int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    into();
    if ((n + k)%2)
    {
        cout << 0 << endl;
        return 0;
    }
    F[0][0] = 1;
    sort(a + 1, a + 1 + n);
    sort(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++)
    {
        F[i][0] = 1;
        for (int j = 1; j <= i; j++)
        {
            int pos = seek(i,j-1);
            F[i][j] = (F[i - 1][j] + F[i - 1][j - 1] * pos)%mod;
        }
    }
    LL ans = 0;
    int now = (n + k) >> 1;
    for (int j = now; j <= n; j++)
    {
        LL res = (LL)F[n][j] * c[n - j]%mod;
        res = (LL)res * c[j] % mod * fc[now] % mod * fc[j - now] % mod;
        if ((j-now) % 2) res *= -1;
        ans = (ans + res) % mod;
    }
    cout << (ans%mod+mod)%mod << endl;
    return 0;
}

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值