ABC266G Yet Another RGB Sequence

题目传送门:ABC266G Yet Another RGB Sequence

考虑二项式反演,设 \(f_i\) 表示钦定 \(i\) 个 RG 的方案数。

考虑一个经典的组合问题,\(n\) 个位置选 \(m\) 个位置两两不相邻的位置的方案数,那么可以先放 \(m\) 组,每组 \(2\) 个数,这样不会相邻,然后剩下 \(n-2m\) 个数有 \(m+1\) 个位置能放,且可以为空,那么即为 \(\binom{n-2m+m+1-1}{m+1-1}=\binom{n-m}{m}\)

那么 \(f_i=\binom{R+G+B-i}{i}\binom{R+G+B-2i}{R-i}\binom{G+B-i}{G-i}\),即先放 RG 然后放 R 然后放 G 剩下的位置放 B。

最后答案直接二项式反演 \(ans=\sum_{i=k}^n (-1)^{i-k}\binom{i}{k} f_i\) 即可。

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
inline int read(){
	char c=getchar();
	int f=1,ans=0;
	while(c<48||c>57) f=(c==45?f=-1:1),c=getchar();
	while(c>=48&&c<=57) ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
	return ans*f;
}
const int N=3e6+10,mod=998244353;
int fac[N],faci[N],R,G,B;
void exgcd(int a,int b,int &x,int &y){
	if (b==0) x=1,y=0;
	else exgcd(b,a%b,y,x),y-=a/b*x; 
}
inline int inv(int a){
	int x,y;
	exgcd(a,mod,x,y);
	return (x%mod+mod)%mod;
}
inline int C(int n,int m){
	return fac[n]*faci[m]%mod*faci[n-m]%mod;
}
main(){
	int R=read(),G=read(),B=read(),k=read();
	fac[0]=faci[0]=1;
	for (int i=1;i<=R+G+B;i++) fac[i]=fac[i-1]*i%mod,faci[i]=faci[i-1]*inv(i)%mod;
	int ans=0;
	for (int i=k;i<=min(R,G);i++){
		int tmp=C(R+G+B-i,i)*C(R+G+B-2*i,R-i)%mod*C(G+B-i,G-i)%mod*C(i,k)%mod;
		if ((i-k)&1) ans=(ans-tmp+mod)%mod;
		else ans=(ans+tmp)%mod;
	}
	cout <<ans; 
    return 0;
}
posted @ 2026-01-22 22:58  OTn53_qwq  阅读(0)  评论(0)    收藏  举报