0
点赞
收藏
分享

微信扫一扫

Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)


​​linkkkkk​​

题意:

个人,每个人有个任务。
假设开始的顺序为,按照以下顺序执行直到所有人的任务都完成:
每次从头开始遍历,如果这个人还有任务没有完成,就;否则跳过这个人;
定义一个好的排列为:没有一个人能够连续完成自己两个及两个以上任务

思路:

题意是比较难读的,考虑怎么样才是不合法的方案。
答案可以直接用总的方案数减去不合法的方案数
拿题目中的第四个样例来举例:3 4 2 1 3 3
排序后的序列为1 2 3 3 3 4
其中最大值,最大值的个数为
比最大值小的数存在,个数
1 2 3 3 3 4就是不合法的方案,因为序列的执行如下:

1 2 3 3 3 4
0 1 2 2 2 3
0 0 1 1 1 2
0 0 0 0 0 1
0 0 0 0 0 0

在最后的过程中,一直是最后一个数在减
那么这样的方案数为
还有什么是不合法的呢:

2 3 3 3 4 1
1 3 3 3 4 2
3 3 3 4 1 2
3 3 3 4 2 1

这些的特点就是说比小的数都可以放到的后面。
具体分析一下也是这样的,如果将比小的数放到后面,并且等于的数始终在前面,就说明最后两次一定是进行的操作。
如果将的数也放到后面,那么不会执行两次连续的操作,会被间隔开,可以自己模拟下。
针对这些特点,推一个组合数的式子就可以了。
首先,是总的方案数,个元素可以任意排列,记作
其次,是不合法的方案数。我们可以向后面放的数量为,其中表示的个数,表示的个数;那么组合的方案数就是:

一点点解释:
枚举的是可以放到后面的数的个数
说明放到后面的数是可以任意排列的
是计算组合数的,表示在比小的数里选个的方案数
表示除了和放到后面的数,其余的数都是可以任意排列的
表示值是的任意排列数。
最后要注意一下边界:
如果说,所有方案都合法;
如果说没有和相等的数,所有方案都不合法。因为无论怎么排列,一定会重复两次或两次以上。

预处理下阶乘和逆元~

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}

inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}

#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)

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

const int maxn=2e5+100,inf=0x3f3f3f3f;

const ll mod=998244353;

ll a[maxn];
ll fact[maxn];//阶乘
ll infact[maxn];//逆元
void init(){
fact[0]=1;
infact[0]=1;
for(int i=1;i<maxn;i++){
fact[i]=fact[i-1]*i%mod;
infact[i]=infact[i-1]*ksm(i,mod-2,mod)%mod;
}
}

ll cul(ll a,ll b){
return fact[a]%mod*infact[b]%mod*infact[a-b]%mod;
}

int main(){
init();
int _=read;
while(_--){
ll n=read;
ll ans=fact[n];
for(ll i=1;i<=n;i++) a[i]=read;
sort(a+1,a+1+n);
ll cnt=1,tot=0;
for(int i=n-1;i;i--){
if(a[i]==a[n]) cnt++;
if(a[i]==a[n]-1) tot++;
}
if(a[n]==a[n-1]){
cout<<ans<<endl;
continue;
}
if(a[n]!=a[n-1]+1){
cout<<"0"<<endl;
continue;
}
//cout<<ans<<endl;
//cout<<cnt<<" "<<tot<<endl;
for(int i=0;i<=n-cnt-tot;i++){
ll tmp=fact[i]*cul(n-cnt-tot,i)*fact[cnt]%mod*fact[n-cnt-i]%mod;
//cout<<i<<" "<<tmp<<endl;
ans=(ans-tmp+mod)%mod;
}

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






return 0;
}


举报

相关推荐

0 条评论