1 solutions

  • 0
    @ 2026-7-28 16:14:28
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~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 int         long long
    #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,B,q;
    int f[20][70000];
    int a[1100000];
    int dp[20][70000],dp2[20][70000];
    int res[20][70000];
    int cnT[20];
    int g[70000];
    int S[20][70000];
    void Main() {
    	cin>>n>>q>>B;
    	string s;
    	cin>>s;
    	REP(i,0,n){
    		if(s[i]>='a')a[i]=s[i]-'a'+10;
    		else a[i]=s[i]^48;
    	}
    	vector<int>c1,c2;
    	int p1=n,p2=n,cnt=0,cnt2=0,cnt3=0,lst=-1,c4=0;
    	for(int i=n-1;i>=0;--i){
    		if(a[i]==0){
    			cnt+=p2-i;cnt2+=p1-i;
    			cnt3+=p1-i;
    			if(lst!=-1)cnT[lst]+=p2-p1;
    		}else{
    			cnT[a[i]]+=p1-i-1;
    			if(a[i]!=B-1)++cnt3,++c4;
    			cnt+=p1-i,p2=p1,p1=i,lst=a[i];
    		}
    	}
    	--B;
    	for(int i=n-1;i>=0;--i){
    		for(auto j:c2)c1.pb(j|(1<<a[i]));
    		c1.pb(1<<a[i]);
    		deal(c1);
    		dp[a[i]%B][(1<<a[i])]=1;
    		REP(k,0,B){
    			for(auto j:c2)dp[(k+a[i])%B][j|(1<<a[i])]+=dp2[k][j];
    		}
    		REP(k,0,B)for(auto j:c1)f[k][j]+=dp[k][j];
    		REP(k,0,B){
    			for(auto j:c2)dp2[k][j]=0;
    			for(auto j:c1)dp2[k][j]=dp[k][j],dp[k][j]=0;
    		}
    		c2=c1;c1.clear();
    	}
    	++B;
    	REP(i,0,B-1){
    		REP(j,0,(1<<B)){
    			int c=0;
    			REP(l,0,B)if((j>>l)&1){
    				int re=(i+l)%(B-1);
    				if(re==0)c|=1|(1<<(B-1));
    				else c|=(1<<re);
    			}
    			S[i][j]=c;
    		}
    	}
    	REP(i,0,B-1){
    		REP(j,0,(1<<B))g[j]=0;
    		REP(j,0,B-1){
    			REP(k,0,(1<<B))if(f[j][k]){
    				if(i==j){
    					g[0]+=f[j][k];
    					continue;
    				}
    				g[S[(i+B-1-j)%(B-1)][k]^((1<<B)-1)]+=f[j][k];
    			}
    		}
    		REP(j,0,B){
    			for(int k=(1<<B)-1;k>=0;--k)if(!((k>>j)&1)){
    				g[k]+=g[k|(1<<j)];
    			}
    		}
    		REP(j,0,(1<<B))res[i][j]=g[j];
    	}
    	while(q--){
    		char d;cin>>d>>s;
    		int c=d<='9'? d-48:d-87,p=0;
    		for(auto i:s){
    			if(i<='9')p|=(1<<(i^48));
    			else p|=(1<<(i-87));
    		}
    		if(!c){
    			if(p&1)cout<<cnt<<endl;
    			else cout<<cnt2<<endl;
    			continue;
    		}else c%=(B-1);
    //		cout<<c<<' '<<p<<endl;
    		if(c!=0||((p>>(B-1))&1))cout<<n*(n+1)/2-res[c][p]<<endl;
    		else if(!(p&1))cout<<n*(n+1)/2-res[c][p]-cnt2<<endl;
    		else{
    			int t=n*(n+1)/2-res[c][p]-cnt3;
    			REP(i,1,B){
    				if((p>>((B-1-i)%(B-1)))&1);else t-=cnT[i];
    			}
    			cout<<t<<endl;
    		}
    	}
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("a.in","r",stdin);
    	freopen("a.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
    2936
    Time
    8000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    2
    Uploaded By