AT_abc465_e 题解
板子数位 DP。嘟嘟嘟。
我的数位 DP 是递推型的,可以先看一下这篇文章对我的这种数位 DP 类型做一个基本的了解 qwq。顺便求赞喵。
首先你看这个要维护点什么信息。事实上,我们只需要各个数码之和(判断是否为
但是我们还需要上下界标记,所以最后定义出来的状态是
初值是
最后求和枚举所有
时刻注意取模!!
::::success[code && submission]
#include<bits/stdc++.h>
#define LL long long
#define UInt unsigned int
#define ULL unsigned long long
#define LD long double
#define pii pair<int,int>
#define pLL pair<LL,LL>
#define pDD pair<LD,LD>
#define fr first
#define se second
#define pb push_back
#define isr insert
#define _i128 __int128
using namespace std;
const int N = 505;
const int K = (1<<10)+5;
const LL MOD = 998244353;
int n;
LL dp[N][5][K][2][2],Ans;
string str;
int read(){
int su=0,pp=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')pp=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){su=su*10+ch-'0';ch=getchar();}
return su*pp;
}
int pop_count__(int u){
int c=0;
while(u)c+=u%2,u/=2;
return c;
}
void add(LL &x,LL y){x=(x+y)%MOD;return;}
int main(){
cin>>str;
n=str.size(),str=" "+str;
dp[0][0][0][1][1]=1;
for(int i=1;i<=n;i++)
for(int m=0;m<3;m++)
for(int s=0;s<(1<<10);s++)
for(int x=0;x<2;x++)
for(int y=0;y<2;y++){
if(!dp[i-1][m][s][x][y])continue;
for(int ad=0;ad<=(x?str[i]-'0':9);ad++){
int m2=(m+ad)%3;
int x2=(x&&(ad==str[i]-'0'));
int y2=(y&&ad==0);
int s2=s;if(!y2)s2|=(1<<ad);
add(dp[i][m2][s2][x2][y2],dp[i-1][m][s][x][y]);
}
}
for(int m=0;m<3;m++)
for(int s=1;s<(1<<10);s++)
for(int x=0;x<2;x++)
for(int y=0;y<2;y++){
int cnt=0;
if(!m)cnt++;
if(pop_count__(s)==3)cnt++;
if((s>>3)&1)cnt++;
if(cnt==1)add(Ans,dp[n][m][s][x][y]);
}
cout<<Ans<<"\n";
return 0;
}
::::
如果本篇题解对你有帮助的话,麻烦你点一个小小的赞,真是太感谢啦!