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

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

linkkkkk

题意:

有n个人,每个人有a i个任务。

假设开始的顺序为p,按照以下顺序执行直到所有人的任务都完成:

每次从头开始遍历,如果这个人还有任务没有完成,就a i − −;否则跳过这个人;

定义一个好的排列为:没有一个人能够连续完成自己两个及两个以上任务

思路:

题意是比较难读的,考虑怎么样才是不合法的方案。

答案可以直接用总的方案数减去不合法的方案数

拿题目中的第四个样例来举例:3 4 2 1 3 3

排序后的序列为1 2 3 3 3 4

其中最大值m a x x = 4,最大值的个数为c n t = 1

比最大值小1的数存在,个数t o t = 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

那么这样的方案数为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小的数都可以放到m a x x的后面。

具体分析一下也是这样的,如果将比m a x x − 1 maxx-1maxx−1小的数放到m a x x后面,并且等于m a x x − 1的数始终在m a x x前面,就说明最后两次一定是m a x x进行的操作。

如果将m a x x − 1 的数也放到m a x x 后面,那么m a x x 不会执行两次连续的操作,会被m a x x − 1间隔开,可以自己模拟下。

针对这些特点,推一个组合数的式子就可以了。

首先,是总的方案数,n个元素可以任意排列,记作n !

其次,是不合法的方案数。我们可以向m a x x后面放的数量为n − c n t − t o t ,其中c n t表示m a x x的个数,t o t 表示m a x x − 1的个数;那么组合的方案数就是:

∑ 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枚举的是可以放到m a x x后面的数的个数

i !说明放到m a x x后面的数是可以任意排列的

c u l ( n − c n t − t o t , i )是计算组合数的,表示在比m a x x − 1小的数里选i个的方案数

n − c n t − i表示除了m a x x和放到m a x x后面的数,其余的数都是可以任意排列的

c n t !表示值是m a x x的任意排列数。

最后要注意一下边界:

如果说c n t > = 2,所有方案都合法;

如果说没有和m a x x − 1相等的数,所有方案都不合法。因为无论怎么排列,m a x x一定会重复两次或两次以上。

预处理下阶乘和逆元~

代码:

#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;
}
目录
相关文章
|
9月前
|
机器学习/深度学习 人工智能
Educational Codeforces Round 113 (Rated for Div. 2)C. Jury Meeting
Educational Codeforces Round 113 (Rated for Div. 2)C. Jury Meeting
40 0
|
机器学习/深度学习 人工智能 BI
Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)
Educational Codeforces Round 115 (Rated for Div. 2) D. Training Session(组合数学 思维)
88 0
|
人工智能
Educational Codeforces Round 113 (Rated for Div. 2) B. Chess Tournament(思维 构造)
Educational Codeforces Round 113 (Rated for Div. 2) B. Chess Tournament(思维 构造)
73 0
|
机器学习/深度学习 Java
codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题
刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。
93 0