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 ;
}
發佈了190 篇原創文章 · 獲贊 19 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章