其他分享
首页 > 其他分享> > 2018-2019 ACM-ICPC, Asia Shenyang Regional Contest C. Insertion Sort(组合计数)

2018-2019 ACM-ICPC, Asia Shenyang Regional Contest C. Insertion Sort(组合计数)

作者:互联网

题目链接
思路:
对于排列\(1,2,3...k,k+1...n\)分三种情况:

code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <ctime>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
using ll = long long;

int n;
ll fac[maxn];

int main() {
    int t, n, k, mod, q = 0;
    scanf("%d", &t);
    while(t --){
        scanf("%d%d%d", &n, &k, &mod);
        fac[0] = 1;
        for(int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;

        ll temp = 1, a = 1, ans;
        for(int i = 1; i <= n; i++) {
            temp = temp * (++ a) % mod;
            if(k == i){
                ll ret = (n * fac[i] % mod * (n - i - 1) % mod + mod) % mod;
                printf("Case #%d: %lld\n", ++ q, (ret + temp) % mod);
                break;
            }
        }
        if(k > n) printf("Case #%d: %lld\n", ++ q, fac[n]);
    }
    return 0;
}

标签:Sort,d%,int,Regional,ACM,数为,long,fac,include
来源: https://www.cnblogs.com/lniiwuw/p/15568506.html