2018-2019 ACM-ICPC, Asia Shenyang Regional Contest C. Insertion Sort(组合计数)
作者:互联网
题目链接
思路:
对于排列\(1,2,3...k,k+1...n\)分三种情况:
- 对前\(k\)个全排列,后\(n-k\)个形成最长递增子序列长度\(>=n-k-1\)的序列,方案数为\(k![(n-k)(n-k-2)-2]\)。
- 在前\(k\)个数中选一个数与\(k+1\)互换时,方案数为\(k![k*(n-k)]\)。
- 在\([k+2,n]\)中选一个数与\([1,k]\)互换时,方案数为\(k!(n-k-1)\)。
总方案数为\(k![(n-k)*(n-1)+1]\)。
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