跳到正文

题解

题解:AT_arc156_b [ARC156B] Mex on Blackboard

zxh_qwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年4月8日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前

AT_arc156_b 题解

我们可以尝试去枚举 mex⁡\operatorname{mex}。

设你需要 pip_i 次写下 ii 这个数(ii 为序列的最大值),写下这个数之后操作就变成了 (k−pi)(k-p_i) 次操作写下剩余的数。

那么在 (k−pi)(k-p_i) 次操作中,你可以插入 0∼i0 \sim i 中的所有数。枚举每个 ii 求和,答案就出来了。结果应为:

ans=∑i=1kCk+iians = \sum\limits_{i=1}^{k} C_{k+i}^{i}

从而代码就出来了。

代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=998244353;
int a[214514],jc[414514],iv[414514],h[214514];
bool v[414514];
int q(int x,int y){
	int ans=1;
	while(y){
		if(y&1){
			ans*=x;
			ans%=mod;
		}
		x*=x;
		x%=mod;
		y>>=1;
	}
	return ans;
}
int main(){
	int n,k; 
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		v[a[i]]=1;
	}
	jc[0]=iv[0]=1;
	for(int i=1;i<=4e5+1;i++){
		jc[i]=jc[i-1]*i%mo;
		iv[i]=q(jc[i],mo-2)%mo;
	}
	int now;
	for(int i=1;i<=k;i++){
		while(v[now]) now++;
		h[i]=now;
		now++;
	}
	int ans=1;
	for(int i=1;i<=k;i++){
		ans+=jc[h[i]+k-i]*iv[k-i+1]%mo*iv[h[i]-1]%mo;
		ans%=mo; 
	}
	cout<<ans;
	return 0;
}
原文