东莞网站网络推广公司/网络媒体推广报价
https://codeforces.com/blog/entry/94721
题意:n个人每个人有ai个问题,每轮若自己还有问题,则问,否则跳过,问不存在同一个人连续问两次的排列有多少种,答案对998244353取模?
// Decline is inevitable
// Romance will last forever
// CF1569C
#include <bits/stdc++.h>
#define mst(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
#define P 998244353
#define int long long
using namespace std;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
int Fac[maxn];
void solve() {int n;cin >> n;vector<int> a(n);a.clear();for(int i = 0; i < n; i++) {int x;cin >> x;a.push_back(x);}sort(a.begin(), a.end());int mx = a[n-1];int mxx = a[n-2];if(mx - mxx >= 2) {cout << 0 << endl;return;}if(mx == mxx) {cout << Fac[n] << endl;return;}int k = count(a.begin(), a.end(), mx - 1);int sub = 1;for(int i = 1; i <= n; i++) {if(i != k + 1)sub = sub * i % P;}cout << (Fac[n] - sub + P) % P << endl;
}
signed main() {Fac[0] = 1;for(int i = 1; i <= maxn-10; i++) {Fac[i] = Fac[i-1] * i % P;}int t;cin >> t;while(t--)solve();return 0;
}
思路:按ai从小到大排序,显然,若最大值不止一个,则所有排列方式均可,即为n!,若最大值和次大值相差大于1,则没有排列可行。下面考虑最大值有且仅有一个且次大值仅比最大值小1的情况。
统计次大值的个数,设为k个,则除去最大值和次大值之外还有n-k-1个数,这n-k-1个数排列到n个数里有种方式,考虑余下k+1个数,最大值在这些数的第一个位置的方案数为k!,