链接 C. Jury Meeting
题意
给出一组序列,每个元素依次减一,如果没有一个元素连续两次或者以上次数减一就说明这个序列是好的,求这个序列满足条件的序列数量;
思路
首先对于这个序列所有情况为
n
!
n!
n!;
每个序列对于最大值进行三种情况的讨论:
- 如果最大值个数大于 1 1 1 ,那这个序列就都是好的序列,直接输出 n ! n! n!;
- 如果序列中没有 m a x − 1 max-1 max−1 这个数,那么直接输出 0 0 0,因为无论如何最大的数都会连续多次减一;
- 如果最大值个数为 1 1 1,并且存在 m a x − 1 max - 1 max−1 这个数,个数为 r e s res res ,那么当把 m a x − 1 max - 1 max−1 放在最大值前边,然后对于剩余的数进行插入,那么不满足的情况就是 r e s ! ∗ ( r e s + 2 ) ∗ ( r e s + 3 ) . . . res! * (res + 2)*(res+3)... res!∗(res+2)∗(res+3)...;
AC代码
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int N = 200010;
const ll mod = 998244353;
int T, n;
ll a[N];
ll b[N];
int main()
{
b[1] = 1;
for (int i = 2; i <= 2e5; i ++) {
b[i] = b[i - 1] * i;
b[i] %= mod;
}
cin >> T;
while (T --){
cin >> n;
ll ans = 1;
for (int i = 1;i <= n ;i ++) cin >> a[i];
sort(a + 1, a + n + 1);
bool flag = false;
int cnt = 0, res = 0;
for (int i = n; i >= 1; i --){
if (a[i] == a[n]) cnt ++;
if (a[i] == a[n] - 1) res ++;
}
if (cnt != 1) {
cout << b[n] << endl;
continue;
}
if (!res) cout << 0 << endl;
else {
ll ans = b[n];
ll q = b[res];
ll y = res + 2;
for (int i = 1; i <= n - 1 - res; i ++)
{
q *= y;
y ++;
q %= mod;
}
ans += mod;
ans -= q;
ans %= mod;
cout << ans << endl;
}
}
}