ABC338G
Drimpossible · · 题解
借鉴 qiuzx_ 大佬的做法。
思路:
DP
设 + 的位置,* 的位置,
显然到在
因为对于
对于
可以再做一个 DP。
具体地,记
所以
答案即为
code
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+5;
const int mod=998244353;
int n,pw[N],spw[N];
int f[N],cnt[N],s[2][N];
char a[N];
signed main(){
cin>>(a+1);
n=strlen(a+1);
pw[0]=spw[0]=1;
for(int i=1;i<=n;i++)
pw[i]=pw[i-1]*10%mod,
spw[i]=(spw[i-1]+pw[i])%mod;
int tp=n+1,ap=n+1,nw=0,sr=1,ans=0;
for(int i=n;i;i--){
cnt[i]=cnt[i+1]+isdigit(a[i]);
if(a[i]=='+'){
tp=ap=i,nw=0,sr=1;
continue;
}
if(a[i]=='*'){
tp=i,(sr*=nw)%=mod,nw=0;
continue;
}
int x=a[i]-'0';
(nw+=x*pw[tp-i-1])%=mod;
s[0][i]=(s[0][i+1]+x*spw[tp-i-1])%mod;
if(ap==tp)s[1][i]=s[0][i];
else s[1][i]=(s[1][tp+1]*nw%mod+s[0][i])%mod;
f[i]=(sr*nw%mod*cnt[ap+1]%mod+f[ap+1]+s[1][i])%mod;
(ans+=f[i])%=mod;
}
cout<<ans;
return 0;
}