• BZOJ 4517: [Sdoi2016]排列计数 错排公式


    4517: [Sdoi2016]排列计数

    题目连接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=4517

    Description

    求有多少种长度为 n 的序列 A,满足以下条件:
    1 ~ n 这 n 个数在序列中各出现了一次
    若第 i 个数 A[i] 的值为 i,则称 i 是稳定的。序列恰好有 m 个数是稳定的
    满足条件的序列可能很多,序列数对 10^9+7 取模。

    Input

    第一行一个数 T,表示有 T 组数据。
    接下来 T 行,每行两个整数 n、m。
    T=500000,n≤1000000,m≤1000000

    Output

    输出 T 行,每行一个数,表示求出的序列数

    Sample Input

    5

    1 0

    1 1

    5 2

    100 50

    10000 5000

    Sample Output

    0

    1

    20

    578028887

    60695423

    Hint

    题意

    题解:

    其实就是错排啦,选择其中C(n,m)来正常排,剩下的错排

    就是C(n,m)*f(n-m),f(n-m)就是n-m个人错误排序的方案数

    错排这个东西用容斥或者反演,很容易推出来

    f[i] = (f[i-1]+f[i-2])(i-1)

    代码

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1000005;
    const int mod = 1e9+7;
    ll fac[maxn];
    ll f[maxn];
    ll qpow(ll a,ll b)
    {
        ll ans=1;a%=mod;
        for(ll i=b;i;i>>=1,a=a*a%mod)
            if(i&1)ans=ans*a%mod;
        return ans;
    }
    ll C(ll n,ll m)
    {
        if(m>n||m<0)return 0;
        ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
        return s1*qpow(s2,mod-2)%mod;
    }
    int main()
    {
        fac[0]=1,f[0]=1,f[1]=0;
        for(int i=1;i<maxn;i++)fac[i]=fac[i-1]*i%mod;
        for(int i=2;i<maxn;i++)f[i]=(f[i-1]+f[i-2])*(i-1)%mod;
        int t;scanf("%d",&t);
        while(t--)
        {
            int x,y;scanf("%d%d",&x,&y);
            printf("%lld
    ",C(x,y)*f[x-y]%mod);
        }
    }
  • 相关阅读:
    自己定义Actionbar
    创建自己的Repo Server
    AI案例
    贝叶斯定理,从白袜到飞机失事再到人工智能
    jupyter notebook 安装代码提示功能
    Jupyter notebook 自动补全
    27 个Jupyter Notebook的小提示与技巧
    Windows下的Jupyter Notebook 安装与自定义启动
    以太坊联盟链 parity 节点搭建
    ubuntu上面Parity 安装
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5440040.html
Copyright © 2020-2023  润新知