「雜題亂刷」AT_abc359_d

wangmarui發表於2024-06-23

狀壓板子。

\(dp_{i,j}\) 表示考慮到第 \(i\) 為狀態為 \(j\) 的方案數。

時間複雜度 \(O(n \times 2^k)\)

程式碼:

點選檢視程式碼
/*
Tips:
你陣列開小了嗎?
你MLE了嗎?
你覺得是貪心,是不是該想想dp?
一個小時沒調出來,是不是該考慮換題?
打 cf 不要用 umap!!!

記住,rating 是身外之物。

該衝正解時衝正解!

Problem:

演算法:

思路:

*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
string s;
ll n,m,ans;
ll dp[1010][1<<11],mod=998244353;
bool pd(ll num,ll wz)
{
	if(wz<m)
		return 1;
	string s;
	ll len=0;
	while(num)
	{
		len++;
		if(num%2)
			s+='1';
		else
			s+='0';
		num/=2;
	}
	while(s.size()!=m)
		s=s+'0';
	string ss=s;
	reverse(ss.begin(),ss.end());
	return s!=ss;
}
void solve()
{
	cin>>n>>m>>s;
	s=' '+s;
	forl(i,1,n)
	{
		if(s[i]=='?')
			s[i]='2';
		else
			s[i]=(s[i]=='A')+'0';
	}
	dp[0][0]=1;
//	cout<<s;
	forl(i,1,n)
	{
		forl(time,0,(1ll<<m)-1)
		{
			if(s[i]=='1')
			{
				ll now=time*2;
				now%=1<<m;
				now|=1;
				if(pd(now,i))
				{
					dp[i][now]+=dp[i-1][time];
					dp[i][now]%=mod;
				}			
			}
			else if(s[i]=='0')
			{
				ll now=time*2;
				now%=1<<m;
				now|=0;
				now%=1<<m;
				if(pd(now,i))
				{
					dp[i][now]+=dp[i-1][time];
					dp[i][now]%=mod;
				}			
			}
			else
			{
				ll now=time*2;
				now%=1<<m;
				now|=0;
				now%=1<<m;
				if(pd(now,i))
				{
					dp[i][now]+=dp[i-1][time];
					dp[i][now]%=mod;
				}
				now=time*2;
				now%=1<<m;
				now|=1;
				now%=1<<m;
				if(pd(now,i))
				{
					dp[i][now]+=dp[i-1][time];
					dp[i][now]%=mod;
				}				
			}	
		}
	}
	forl(i,0,(1<<m)-1)
		if(pd(i,n))
			ans+=dp[n][i],ans%=mod;//,cout<<i<<":"<<dp[n][i]<<endl;
	cout<<ans<<endl;
}
int main()
{
	IOS;
	t=1;
 //	cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}

相關文章