zoukankan      html  css  js  c++  java
  • bzoj4517: [Sdoi2016]排列计数

    错排问题。。正经的公式是这个 D(n) = n! [(-1)^2/2! + … + (-1)^(n-1)/(n-1)! + (-1)^n/n!] 一个供参考的简化后的公式是D(n) = [n!/e+0.5] 

    有个递推式是f[n]=(f[n-1]+f[n-2])*(i-1)

    那么答案就是C(n,m)*f[n-m]

    C预处理阶乘和阶乘的逆元就行

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    const LL mod=1e9+7;
    
    LL f[1100000];
    void initf()
    {
        f[0]=1;f[1]=0;
        for(int i=2;i<=1000000;i++)f[i]=(f[i-1]+f[i-2])*(i-1)%mod;
    }
    
    LL quick_pow(LL A,LL p)
    {
        LL ret=1;
        while(p!=0)
        {
            if(p%2==1)ret=(ret*A)%mod;
            A=(A*A)%mod;p/=2;
        }
        return ret;
    }
    LL ml[1100000],inv[1100000];
    void initforC()
    {
        ml[0]=1;inv[0]=1;
        for(int i=1;i<=1000000;i++)
            ml[i]=(ml[i-1]*i)%mod, inv[i]=quick_pow(ml[i],mod-2);
    }
    
    //-----------init--------------
    
    LL getC(LL n,LL m)
    {
        return ((ml[n]*inv[n-m]%mod)*inv[m])%mod;
    }
    
    int main()
    {
        initf();
        initforC();
        
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            printf("%lld
    ",(getC(n,m)*f[n-m])%mod);
        }
        return 0;
    }
  • 相关阅读:
    全球疫情可视化
    ListView(1)
    《浪潮之巅》阅读笔记02
    Intern Day12
    Intern Day11
    Intern Day10
    Intern Day10
    Intern Day10
    PTA1065
    Intern Day10
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/9411283.html
Copyright © 2011-2022 走看看