【Atcoder】 [AGC058D] Yet Another ABC String

题目链接

Atcoder方向
Luogu方向

题目解法

考虑容斥长度至少为 3 的不合法串至少出现了 x x x
考虑起点与终点都不固定,所以考虑钦定 x x x 个不合法串的起点,终点不固定(因为是容斥)
因为起点一定,所以不合法串不能往左边延伸(有两种可能,左边可能真的不能延伸或者左边是另一个不合法串)
考虑第 1 个串的起点可能有 2 种情况

  1. 左边没有数,答案为 (                          x − 1 a + b + c − 3 x + 1 + x − 1 − 1 ) ∗ 2 x − 1 ∗ 3 (^{a+b+c-3x+1+x-1-1}_{\;\;\;\;\;\;\;\;\;\;\;\;x-1})*2^{x-1}*3 (x1a+b+c3x+1+x11)2x13
  2. 左边有数,答案为 (                        x a + b + c − 3 x + x − 1 ) ∗ 2 x (^{a+b+c-3x+x-1}_{\;\;\;\;\;\;\;\;\;\;\;x})*2^x (xa+b+c3x+x1)2x

最后需要将 a − x a-x ax A A A b − x b-x bx B B B c − x c-x cx C C C 排列以下,方案数即为 ( a − x n − 3 x ) ∗ (              b − x n − 3 x − ( a − x ) ) (^{n-3x}_{a-x})*(^{n-3x-(a-x)}_{\;\;\;\;\;\;b-x}) (axn3x)(bxn3x(ax))

时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

#include <bits/stdc++.h>
using namespace std;
const int N(3000100),P(998244353);
int a,b,c,fac[N],inv[N]; 
inline int read(){
	int FF=0,RR=1;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') RR=-1;
	for(;isdigit(ch);ch=getchar()) FF=(FF<<1)+(FF<<3)+ch-48;
	return FF*RR;
}
int qmi(int a,int b){
	int res=1;
	for(;b;b>>=1){
		if(b&1) res=1ll*res*a%P;
		a=1ll*a*a%P;
	}
	return res;
}
int C(int a,int b){ return 1ll*fac[a]*inv[b]%P*inv[a-b]%P;}
int main(){
	a=read(),b=read(),c=read();
	fac[0]=inv[0]=1;
	for(int i=1;i<N;i++){
		fac[i]=1ll*fac[i-1]*i%P;
		inv[i]=1ll*inv[i-1]*qmi(i,P-2)%P;
	}
	int n=a+b+c;
	//容斥长度>=3的串至少有i个 
	int mx=min(min(a,b),c),ans=0;
	for(int i=0;i<=mx;i++){
		//还剩下a-i个A,b-i个B,c-i个C 
		int res=0;
		//put the first
		if(i) res=1ll*C(n-3*i+i-1,i-1)*qmi(2,i-1)*3%P;
		//do not put the first
		res=(res+1ll*C(n-3*i+i-1,i)*qmi(2,i))%P;
		//排列剩余的n-3i个字母 
		res=1ll*res*C(n-3*i,a-i)%P*C(n-3*i-(a-i),b-i)%P;
//		cout<<res<<'\n';
		if(i&1) ans=(ans-res+P)%P;
		else ans=(ans+res)%P;
	}
	printf("%d",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值