码迷,mamicode.com
首页 > 其他好文 > 详细

E. Placing Rooks (组合数学,经典容斥,思维)

时间:2020-05-02 14:38:11      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:bsp   com   style   while   pac   back   size   tar   bit   

题目:传送门

 

博一

博二

 

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF 0x3f3f3f3f
#define inf LLONG_MAX
#define PI acos(-1)
#define fir first
#define sec second
#define lb(x) ((x) & (-(x)))
using namespace std;

const int N = 1e6 + 5;
const LL mod = 998244353;

LL ksm(LL a, LL b) {
    LL res = 1LL;
    while(b) {
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

LL fac[N];

LL C(LL n, LL m) {
    return fac[n] * ksm(fac[m] * fac[n - m] % mod, mod - 2) % mod;
}

void solve() {

    LL n, k;
    scanf("%lld %lld", &n, &k);

    fac[0] = 1LL;

    for(LL i = 1LL; i <= n; i++) fac[i] = fac[i - 1] * i % mod;

    if(k == 0LL) {

        printf("%lld\n", fac[n]);

        return ;

    }

    if(k >= n) {
        puts("0");
        return ;
    }

    LL ans = 0LL;

    LL flag = 1LL;

    rep(i, 0, n - k) {
        ans = (ans + flag * C(n - k, n - k - i) * ksm(n - k - i, n) % mod + mod) % mod;
        flag = -flag;
    }

    ans = 2LL * C(n, n - k) * ans % mod;

    printf("%lld\n", ans);

}

int main() {

//    int _; scanf("%d", &_);
//    while(_--) solve();

    solve();

    return 0;
}

 

E. Placing Rooks (组合数学,经典容斥,思维)

标签:bsp   com   style   while   pac   back   size   tar   bit   

原文地址:https://www.cnblogs.com/Willems/p/12817829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!