AT_arc188_a [ARC188A] ABC Symmetry 题解
容易发现一个串是好的的充要条件是:A
,B
,C
出现次数的奇偶性都相同。因此我们也可以将所有的串分为四类:好的,只有 A
和其他两个的奇偶性不同,只有 B
和其他两个的奇偶性不同,只有 C
和其他两个的奇偶性不同。
大于 \(k\) 的不好统计,可以直接用总数减去小于 \(k\) 的总和。
设 $ f_{i,x,g,a,b,c} $ 为到第 \(i\) 个时,已经凑成了 \(x\) 个好的字串,此时所有后缀中,上面说的四类串分别有 \(g,a,b,c\) 个。那么转移方程就显而易见了。
当第 \(i\) 个字符给定且为 A
时:
\[f_{i,x,g,a,b,c} \rightarrow f_{i+1,x+a,g+1,c,b}
\]
当第 \(i\) 个字符给定且为 B
时:
\[f_{i,x,g,a,b,c} \rightarrow f_{i+1,x+b,c,g+1,a}
\]
当第 \(i\) 个字符给定且为 C
时:
\[f_{i,x,g,a,b,c} \rightarrow f_{i+1,x+c,b,a,g+1}
\]
当第 \(i\) 个字符为 ?
时,上面三种转移都进行。
时间复杂度 \(O(n^7)\) 难以通过。
显然 $ g = i - a - b - c$ 所以可以省掉一维,时间复杂度变为 $ O(n^6) $。
直觉上不能过,但你滚动数组之后就直接过了。因为首先 h好串数量一定小于 \(\dfrac{n^2}{4}\),毕竟如果 \(s_{l \dots r}\) 是好串,那么 $ s_{l-1 \dots r} $ 就一定不是。其次 $ a + b + c \le n$,相当于有带了 \(\dfrac{1}{6}\) 的小常数。所以算一下就是对的。
#include<bits/stdc++.h>
using namespace std;
inline void rd(){}
template<typename T,typename ...U>
inline void rd(T &x,U &...args){
char ch=getchar();
T f=1;x=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x*=f;rd(args...);
}
const int N=55,mod=998244353;
int f[2][N*N/4][N][N][N],n,k;
char s[N];
inline int KSM(int x,int n){
int ans=1;
while(n){
if(n&1)ans=1ll*ans*x%mod;
x=1ll*x*x%mod;
n>>=1;
}return ans;
}
signed main(){
rd(n,k);
if(k>n*(n+10)/4){
printf("0\n");
return 0;
}
scanf("%s",s+1);
int t=0,cnt=0;
for(int i=0;i<n;i++){
t^=1;
if(s[i+1]=='?')++cnt;
if(!i)f[t][0][0][0][0]=1;
for(int x=0;x<=min(k,i*(i+1)/2);x++){
for(int A=0;A<=i;A++){
for(int B=0;B+A<=i;B++){
for(int C=0;A+B+C<=i;C++){
int G=i-A-B-C;
if(s[i+1]=='A'){
f[t^1][x+A][G+1][C][B]+=f[t][x][A][B][C];
if(f[t^1][x+A][G+1][C][B]>=mod)f[t^1][x+A][G+1][C][B]-=mod;
}else if(s[i+1]=='B'){
f[t^1][x+B][C][G+1][A]+=f[t][x][A][B][C];
if(f[t^1][x+B][C][G+1][A]>=mod)f[t^1][x+B][C][G+1][A]-=mod;
}else if(s[i+1]=='C'){
f[t^1][x+C][B][A][G+1]+=f[t][x][A][B][C];
if(f[t^1][x+C][B][A][G+1]>=mod)f[t^1][x+C][B][A][G+1]-=mod;
}else{
f[t^1][x+A][G+1][C][B]+=f[t][x][A][B][C];
if(f[t^1][x+A][G+1][C][B]>=mod)f[t^1][x+A][G+1][C][B]-=mod;
f[t^1][x+B][C][G+1][A]+=f[t][x][A][B][C];
if(f[t^1][x+B][C][G+1][A]>=mod)f[t^1][x+B][C][G+1][A]-=mod;
f[t^1][x+C][B][A][G+1]+=f[t][x][A][B][C];
if(f[t^1][x+C][B][A][G+1]>=mod)f[t^1][x+C][B][A][G+1]-=mod;
}
f[t][x][A][B][C]=0;
}
}
}
}
}
t^=1;
int ans=0;
for(int i=0;i<k;i++){
for(int A=0;A<=n;A++)
for(int B=0;B+A<=n;B++)
for(int C=0;A+B+C<=n;C++)(ans+=f[t][i][A][B][C])%=mod;
}
printf("%d\n",(KSM(3,cnt)-ans+mod)%mod);
return 0;
}
本文作者:KIreteria
本文链接:https://www.cnblogs.com/11-twentythree/p/18593369
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步