BZOJ4517 [Sdoi2016][排列计数]

结题报告:
选n-m个数出来错排
f(n-m)*c(n,n-m)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 ;
#define LL long long
const LL Mod = 1e9 + 7 ;

LL f [N+10] , fac [N+10] , inv [N+10] ;

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

LL getinv ( LL a , LL n ){
    LL d , x , y ;
    exgcd ( a , n , d , x , y ) ;
    return ( x+n ) % n ;
}

LL C ( int n , int m ) {
    if ( n == 0 || m > n ) return 0 ;
    return ( fac [n] * inv [m] ) % Mod * inv [n-m] % Mod ;
}
void init (){
    fac [0] = 1 ;
    for ( int i = 1 ; i <= N + 2 ; ++ i )
        fac [i] = ( fac [i-1] * i ) % Mod ;
    inv [0] = 1 ;
    for ( int i = 1 ; i <= N + 2 ; ++ i )
        inv [i] = getinv ( fac [i] , Mod ) ;
    f [0] = 1 ;
    f [1] = 0 , f [2] = 1;
    for ( int i = 3 ; i <= N + 2 ; ++ i ){
        f [i] = ( ( f [i-1] + f [i-2] ) % Mod ) * (i-1) % Mod ;
    }
}

int main () {
    init ();
    int T ;
    scanf ( "%d" , &T );
    while ( T-- ) {
        LL a , b ;
        scanf ( "%lld%lld" , &a , &b ) ;
        printf ( "%lld\n" , C(a,b)*f[a-b]%Mod );
    }
    return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值