zoukankan      html  css  js  c++  java
  • BZOJ 4517: [Sdoi2016]排列计数 错排+逆元

    4517: [Sdoi2016]排列计数


    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

     
    题解:
      
      蒟蒻什么都不懂
      错排公式
      http://blog.csdn.net/liwen_7/article/details/7646451
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 1e6+20, M = 1e6+10, mod = 1e9+7, inf = 1e9+1000;
    typedef long long ll;
    
    ll f[N],fac[N];
    ll inv(ll x,ll mo)
    {
        ll y=mo-2,ans=1;
        while (y)
        {
            if (y&1) ans=ans*x%mo;
            x=x*x%mo;
            y>>=1;
        }
        return ans;
    }
    void init() {
        fac[0] = 1;
        for(int i=1;i<=M;i++) {
            fac[i] = (fac[i-1]*i)%mod;
        }
        f[0] = 1;
        f[1] = 0;
        f[2] = 1;
        for(ll i=3;i<=M;i++) {
            f[i] = (f[i-1]+f[i-2])%mod*(i-1ll)%mod;
        }
    }
    int main() {
        init();
        int T;
        scanf("%d",&T);
        while(T--) {
            int n,m;
            scanf("%d%d",&n,&m);
            printf("%lld
    ",(f[n-m]*fac[n]%mod*inv(fac[n-m],mod)%mod*inv(fac[m],mod)%mod)%mod);
        }
        return 0;
    }
  • 相关阅读:
    Understand 学习
    2021年1月
    查看所有请求
    DB2日期和时间函数汇总
    .getClass和.class
    继承和实现接口的区别
    java8 stream
    Lambda表达式详解
    SQL语句小知识---XML文件中的 CDATA语法
    Java--mapper.xml中常用SQL标签
  • 原文地址:https://www.cnblogs.com/zxhl/p/5447813.html
Copyright © 2011-2022 走看看