1 solutions

  • 0
    @ 2026-7-28 16:04:10
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
     */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define rf(v)       shuffle(all(v),sd);
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    string YN(bool x,string Y="Yes",string N="No") {
    	if(x){
    		return Y;
    	} else {
    		return N;
    	}
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n;
    int ok[10][10][10][10],nok[10][10],ok2[1005],ok3[105][105],ok4[10005],les[1005];
    void tostate(int &x,int &y,int &z){
    	if(!ok2[y]||!ok2[z]||ok2[y]!=ok2[z])x&=14;
    	if(!ok3[y/10][z/10])x&=13;
    	if(x==15)x=14;
    	if(x==8)y=z=0;
    	else if(!(x&3))y-=y%100,z-=z%100;
    	else if(!(x&1))y-=y%10,z-=z%10;
    	if(x==1){
    		y=les[y];z=les[z];
    		if(!y||!z)x=0;
    	}
    }
    int n1,n2,n3;
    void getnext(int x,int y,int z,int d1,int d2){
    	n2=y/10+d1*100,n3=z/10+d2*100;
    	bool o=0;
    	if(d1==d2&&(x&8))o=1;
    	else if(x&1&&ok4[y*10+d1]&&ok4[z*10+d2]==ok4[y*10+d1])o=1;
    	else if(!o&&(x&4)&&ok[y/100][d1][z/100][d2])o=1;
    	n1=(x>>1)|(o<<3);
    }
    struct node{
    	int x,y,z;
    };
    string s,t;
    vector<short>t1[2005],t2[2005];
    vector<short>N1[2005],N2[2005],N3[2005],N4[2005];
    long long cnt[2][2005];
    bool vis[16][1005][1005];
    int X[2005],Y[2005],Z[2005],rev[16][1005][1005];
    vector<node>c1;
    int Tot=-1;
    int dfs(int x,int y,int z){
    //	cout<<x<<' '<<y<<' '<<z<<endl;
    	c1.pb({x,y,z});
    	vis[x][y][z]=1;
    	Tot++;int c=Tot;rev[x][y][z]=c;
    	X[c]=x;Y[c]=y;Z[c]=z;
    	REP(i,1,10)REP(j,1,10)if(nok[i][j]){
    		getnext(x,y,z,i,j);
    		tostate(n1,n2,n3);
    		if(n1)t1[c].pb(i),t2[c].pb(j),N1[c].pb(n1),N2[c].pb(n2),N3[c].pb(n3);
    		if(n1&&!vis[n1][n2][n3])N4[c].pb(dfs(n1,n2,n3));
    		else if(n1)N4[c].pb(rev[n1][n2][n3]);
    	}
    	return c;
    }
    void Main() {
    	REP(i,1,10)nok[i][i]=1;
    	REP(i,1,9){
    		if(i%3==0)continue;
    		nok[i][i+1]=nok[i+1][i]=1;
    		ok[i][i+1][i][i+1]=1;
    		ok[i][i+1][i+1][i]=1;
    		ok[i+1][i][i][i+1]=1;
    		ok[i+1][i][i+1][i]=1;
    	}
    	nok[1][5]=nok[2][4]=nok[2][6]=nok[3][5]=nok[4][8]=nok[5][7]=nok[5][9]=nok[6][8]=1;
    	nok[5][1]=nok[4][2]=nok[6][2]=nok[5][3]=nok[8][4]=nok[7][5]=nok[9][5]=nok[8][6]=1;
    	REP(i,1,7){
    		nok[i][i+3]=nok[i+3][i]=1;
    		ok[i][i+3][i][i+3]=1;
    		ok[i][i+3][i+3][i]=1;
    		ok[i+3][i][i][i+3]=1;
    		ok[i+3][i][i+3][i]=1;
    	}
    	REP(i,1,6)if(i%3){
    		int b[4]={i,i+1,i+3,i+4};
    		do{
    			ok2[b[0]*100+b[1]*10+b[2]]=i;
    			les[b[0]*100+b[1]*10+b[2]]=b[3];
    			ok4[b[0]*1000+b[1]*100+b[2]*10+b[3]]=i;
    		}while(next_permutation(b,b+4));
    	}
    	REP(i,1,10){
    		REP(j,1,10)if(i!=j){
    			REP(k,1,10){
    				REP(l,1,10)if(k!=l){
    					ok3[i*10+j][k*10+l]=1;
    					vector<int>v;
    					v.pb(i);v.pb(j);v.pb(k);v.pb(l);
    					deal(v);
    					REP(p,2,v.size())if(v[p]==v[p-1]+1&&v[p-1]==v[p-2]+1)ok3[i*10+j][k*10+l]=0;
    					if(v[v.size()-1]-v[0]>4)ok3[i*10+j][k*10+l]=0;
    				}
    			}
    		}
    	}
    	dfs(8,0,0);++Tot;
    	cnt[0][0]=1;
    	cin>>n;cin>>s>>t;
    	set<int>SSS;
    	REP(i,0,n){
    		REP(r,0,Tot)if(cnt[i&1][r]){
    			if(X[r]!=1){
    				REP(j,0,t1[r].size()){
    					int d1=t1[r][j],d2=t2[r][j];
    					if(s[i]=='?'||s[i]==d1+48){
    						if(t[i]=='?'||t[i]==d2+48){
    							cnt[(i&1)^1][N4[r][j]]+=cnt[i&1][r];
    						}
    					}
    				}
    			}else{
    				if(s[i]=='?'||s[i]==Y[r]+48){
    					if(t[i]=='?'||t[i]==Z[r]+48){
    						cnt[(i&1)^1][0]+=cnt[i&1][r];
    					}
    				}
    			}
    		}
    		REP(j,0,Tot){
    			cnt[i&1][j]=0;
    			cnt[(i&1)^1][j]%=MOD;
    		}
    	}
    	int ans=0;
    	REP(i,0,Tot)if(X[i]&8)(ans+=cnt[0][i]+cnt[1][i])%=MOD;
    	cout<<ans<<endl;
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    //	freopen("ex_fairlady1.in","r",stdin);
    //	freopen("2.out","w",stdout);
    	freopen("fairlady.in","r",stdin);
    	freopen("fairlady.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    2927
    Time
    10000ms
    Memory
    1024MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By