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

linkkkkk

题意:

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

思路:

题意是比较难读的,考虑怎么样才是不合法的方案。
答案可以直接用总的方案数减去不合法的方案数
拿题目中的第四个样例来举例:3 4 2 1 3 3
排序后的序列为1 2 3 3 3 4
其中最大值 m a x x = 4 maxx=4 maxx=4,最大值的个数为 c n t = 1 cnt=1 cnt=1
比最大值小 1 1 1的数存在,个数 t o t = 3 tot=3 tot=3
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 2 2
那么这样的方案数为 5 ! 5! 5!
还有什么是不合法的呢:

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

这些的特点就是说比 m a x x − 1 maxx-1 maxx1小的数都可以放到 m a x x maxx maxx的后面。
具体分析一下也是这样的,如果将比 m a x x − 1 maxx-1 maxx1小的数放到 m a x x maxx maxx后面,并且等于 m a x x − 1 maxx-1 maxx1的数始终在 m a x x maxx maxx前面,就说明最后两次一定是 m a x x maxx maxx进行的操作。
如果将 m a x x − 1 maxx-1 maxx1的数也放到 m a x x maxx maxx后面,那么 m a x x maxx maxx不会执行两次连续的操作,会被 m a x x − 1 maxx-1 maxx1间隔开,可以自己模拟下。
针对这些特点,推一个组合数的式子就可以了。
首先,是总的方案数, n n n个元素可以任意排列,记作 n ! n! n!
其次,是不合法的方案数。我们可以向 m a x x maxx maxx后面放的数量为 n − c n t − t o t n-cnt-tot ncnttot,其中 c n t cnt cnt表示 m a x x maxx maxx的个数, t o t tot tot表示 m a x x − 1 maxx-1 maxx1的个数;那么组合的方案数就是:
∑ i = 0 n − c n t − t o t ∗ i ! ∗ c u l ( n − c n t − t o t , i ) ∗ ( n − c n t − i ) ! ∗ c n t ! \sum_{i=0}^{n-cnt-tot}*i!*cul(n-cnt-tot,i)*(n-cnt-i)!*cnt! i=0ncnttoti!cul(ncnttot,i)(ncnti)!cnt!
一点点解释:
i i i枚举的是可以放到 m a x x maxx maxx后面的数的个数
i ! i! i!说明放到 m a x x maxx maxx后面的数是可以任意排列的
c u l ( n − c n t − t o t , i ) cul(n-cnt-tot,i) cul(ncnttot,i)是计算组合数的,表示在比 m a x x − 1 maxx-1 maxx1小的数里选 i i i个的方案数
n − c n t − i n-cnt-i ncnti表示除了 m a x x maxx maxx和放到 m a x x maxx maxx后面的数,其余的数都是可以任意排列的
c n t ! cnt! cnt!表示值是 m a x x maxx maxx的任意排列数。
最后要注意一下边界:
如果说 c n t > = 2 cnt>=2 cnt>=2,所有方案都合法;
如果说没有和 m a x x − 1 maxx-1 maxx1相等的数,所有方案都不合法。因为无论怎么排列, m a x x maxx maxx一定会重复两次或两次以上。

预处理下阶乘和逆元~

代码:

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值