BZOJ4517——[Sdoi2016]排列计数

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

在uoj群里问逆元,被神犇们裱了一通,好在是学会了
n个数中有m个数是稳定的,那么确定这m个数有C(n,m)种方法
那么剩下的n-m个数的要求就是第i个数不是i
思考了一波没想出来。。。。
然后问了一下发现有错排公式。。就是这个问题,这就很尴尬了。。
然后有个问题就是要注意 $$D[0] = 1$$ ,另外还有 $$ine[1] = 1$$
求n!的逆元除了暴力exgcd以外还有一种方法就是 $$ine[n!] = ine[(n+1)!]*(n+1)$$
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define for1(i, x, y) for(int i = (x); i <= (y); i ++)
#define for2(i, x, y) for(int i = (x); i >= (y); i --)
#define LL long long
#define inf 2147483647
#define MOD 1000000007
#define eps 1e-7

inline LL read(){
    char ch = getchar(); LL x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline LL exgcd(LL &x, LL &y, LL a, LL b){
    if(b == 0){
        x = 1; y = 0;
        return a;
    }
    LL ret = exgcd(x, y, b, a % b);
    LL t = x; x = y; y = t - a / b * x;
    return ret;
}

inline LL inv(LL a, LL p){
    LL x, y; LL d = exgcd(x, y, a, p);
    if(d == 1) return (x + p) % p;
}

LL fac[1000010], ine[1000010], D[1000010];

int main(){
    LL T = read();
    //C(n,m)*D(n-m)
    fac[0] = 1; for(LL i = 1; i <= 1000000; i ++) fac[i] = fac[i - 1] * (LL)i % MOD;
    LL o = fac[1000000]; ine[1000000] = inv(fac[1000000], MOD);
    ine[0] = 1; for(LL i = 999999; i >= 1; i --) ine[i] = ine[i + 1] * (i + 1) % MOD;
    D[0] = 1;D[1] = 0; D[2] = 1;
    for(LL i = 3; i <= 1000000; i ++) D[i] = (i - 1) * (D[i - 2] + D[i - 1]) % MOD;
    while(T --){
        LL n = read(), m = read();
        LL ans = fac[n] * ine[n - m] % MOD * ine[m] % MOD;
        ans = ans * D[n - m] % MOD;
        printf("%lld\n", ans);
    }
    return 0;
}