1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <cstring> #include <queue> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second #define Abs(x) ((x)>0?(x):-(x)) const ll mod=998244353; inline void Add(ll &x,ll y){x+=y; (x<mod)?0:(x-=mod);} inline ll Dec(ll x,ll y){x-=y; return (x<0)?x+mod:x;} inline ll Mul(ll x,ll y){return x*y%mod;} inline ll Pow(ll x,ll y) { y%=(mod-1);ll ans=1;for(;y;y>>=1,x=x*x%mod)if(y&1) ans=ans*x%mod; return ans; } const int N=1003; const int M=1000; ll f[N][N<<1][2][2],g[N][N<<1][2][2],ans; char s[N]; int n; int main() { scanf("%s",s+1); n=strlen(s+1); f[0][M][1][1]=1; g[0][M][1][1]=0; int nx,ny; ll sg,sf; fo(i,1,n) fo(j,-i,i) fo(x,0,1) fo(y,0,1) { nx=x?(s[i]-'0'):1; ny=y?(s[i]-'0'):1; sg=g[i-1][j+M][x][y],sf=f[i-1][j+M][x][y]; Add(g[i][j+M][x&!nx][y&!ny],(sf*Abs(j)+sg)%mod), Add(f[i][j+M][x&!nx][y&!ny],sf); if(nx) Add(g[i][j+1+M][x][y&!ny],(sf*Abs(j+1)+sg)%mod), Add(f[i][j+1+M][x][y&!ny],sf); if(ny) Add(g[i][j-1+M][x&!nx][y],(sf*Abs(j-1)+sg)%mod), Add(f[i][j-1+M][x&!nx][y],sf); if(nx&&ny) Add(g[i][j+M][x][y],(sf*Abs(j)+sg)%mod), Add(f[i][j+M][x][y],sf); } fo(i,0,1) fo(j,0,1) Add(ans,g[n][M][i][j]); printf("%lld",ans*((mod+1)/2)%mod); return 0; }
|