1 solutions

  • 0
    @ 2025-11-8 15:44:03

    B 秘密任务(mission)

    1. 算法 1

    考虑增量计算答案,每一次在 xx 后面添加一个数字 cc,维护答案。

    维护 p,ans1,ans2,cnt1,cnt2p, ans1, ans2, cnt1, cnt2,分别代表:

    • pp 表示当前真实值。
    • ans1ans1 表示 [0,p)[0, p) 的答案之和。
    • ans2ans2 表示 pp 的答案。
    • cnt1cnt1 表示 [0,p)[0, p) 最后一位是 11 的总次数。
    • cnt2cnt2 表示 pp 最后一位是否为 11。

    转移式:

    $$\begin{align*} p &= 10 \times p + c \\ ans1 &\leftarrow ans1 \times 10 + ans2 \times c + cnt1 + [k \geq 3]cnt2 \\ ans2 &\leftarrow ans2 + [k \geq 3]cnt2 \\ cnt1 &\leftarrow (p - 1) + [k \geq 1] \\ cnt2 &\leftarrow [k = 1] \\ \end{align*} $$

    可以直接转移,使用矩阵可以做到 O(w3nlog⁡n)O(w^3 n \log n),其中 w=6w = 6。(这个做法还要维护一个 11 常量。)

    常数比较大但也能极限通过。

    小优化:转移矩阵时,如果 ai,ka_{i,k} 和 bk,jb_{k,j} 都有值才转移到 ci,jc_{i,j}。

    发现矩阵内只有 1010 个有用值,展开后能轻松通过。

    2. 算法 2

    By 验题人

    考虑合并两个段 x,yx, y,每个段维护信息 {len,val,cnt,res}\{len, val, cnt, res\},表示段长、段的十进制真实值  mod (109+7)\bmod (10^9 + 7)、段真实值 1313 个数、段小于真实值的 1313 个数和。

    合并 len,val,cntlen, val, cnt 是简单的,合并 resres:

    $$res = res_x \times 10^{len_y} + val_x \times f_{len_y} + cnt_x \times val_y + res_y $$

    其中 fif_i 表示 ii 位十进制数 1313 个数和,fi=10fi−1+10i−2f_i = 10f_{i-1} + 10^{i-2}。

    • xx 段的取值取到小于 valxval_x,xx 取 0∼valx−10 \sim val_x - 1,yy 任意取。 xx 内的 1313 共 resxres_x 个,yy 段任取,贡献 resx×10lenyres_x \times 10^{len_y}。

    yy 内任取的 1313 为 flenyf_{len_y} 个,xx 段方案 valxval_x 种,贡献。

    • xx 段的值取到等于 valxval_x,yy 取 0∼valy−10 \sim val_y - 1。 xx 内的 1313 共 cntxcnt_x 个,yy 段方案 valyval_y 种,贡献 cntx×valycnt_x \times val_y。 yy 内的 1313 共 resyres_y 个,贡献 resyres_y。

    发现 x,yx,y 交界处的 1313 未统计,补上,分类讨论 xx 最右侧是否为 11,yy 最左侧是否为 33:

    • xx 右侧不为 11,xx 内有 ⌊valx−110⌋\left\lfloor \frac{val_x - 1}{10} \right\rfloor 种取值,yy 任取 10leny−110^{len_y - 1} 种。
    • xx 右侧为 11:
      1. yy 左侧不为 33,xx 内有 ⌊valx10⌋\left\lfloor \frac{val_x}{10} \right\rfloor 种取值,yy 任取 10leny−110^{len_y - 1} 种。
      2. yy 左侧为 33,xx 未顶满有 $\left\lfloor \frac{val_x - 1}{10} \right\rfloor \times 10^{len_y - 1}$ 种,顶满有 valy−3×10leny−1val_y - 3 \times 10^{len_y - 1} 种。

    核心代码:

    z.l=x.l;
    z.r=y.r;
    z.len=x.len+y.len;
    z.val=x.val*pwr[y.len]+y.val;
    z.cnt=x.cnt+y.cnt+(x.r==1&&y.l==3);
    z.res=x.res*pwr[y.len]+x.val*f[y.len]+x.cnt*y.val+y.res;
    z.res+=((x.val-x.r)/10+(x.r>1))*pwr[y.len-1];
    if(x.r==1){
      if(y.l>3)z.res+=pwr[y.len-1];
      if(y.l==3)z.res+=y.val-y.l*pwr[y.len-1];
    }
    

    标程

    #include<map>
    #include<set>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<stack>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<bitset>
    #include<numeric>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int MAXN=2e5+10;
    const int N=2e5;
    const int INF=0x3f3f3f3f;
    const long long LINF=0x3f3f3f3f3f3f3f3f;
    const int mod=1e9+7;
    const int i10=7e8+5;
    inline int qpow(int x,int y){
    	int res=1;
    	while(y)
    	{
    		if(y&1){
    			res=1ll*res*x%mod;
    		}
    		y>>=1;
    		x=1ll*x*x%mod;
    	}
    	return res;
    }
    int n,m;
    int a[MAXN],f[MAXN];
    int pwr[MAXN];
    namespace segtree{
    	int L[MAXN<<2],R[MAXN<<2];
    	#define ls(x) (x<<1)
    	#define rs(x) (x<<1|1)
    	struct node{
    		char l,r;
    		int len,val,cnt,res;
    		node(){}
    		node(char c){
    			l=c;
    			r=c;
    			len=1;
    			val=c;
    			cnt=0;
    			res=0;
    		}
    	};
    	inline node merge(node x,node y){
    		node res;
    		res.l=x.l;
    		res.r=y.r;
    		res.len=x.len+y.len;
    		res.val=(1ll*x.val*pwr[y.len]+y.val)%mod;
    		res.cnt=x.cnt+y.cnt+(x.r==1&&y.l==3);
    		res.res=(1ll*x.res*pwr[y.len]+1ll*x.val*f[y.len]+1ll*x.cnt*y.val+y.res)%mod;
    		{
    			int c=1ll*(x.val-x.r+mod)*i10%mod+(x.r>1);
    			res.res=(res.res+1ll*c*pwr[y.len-1])%mod;
    		}
    		if(x.r==1){
    			if(y.l>3){
    				res.res+=pwr[y.len-1];
    				(res.res>=mod)&&(res.res-=mod);
    			}
    			if(y.l==3){
    				res.res=(res.res+y.val-1ll*y.l*pwr[y.len-1])%mod;
    				(res.res<0)&&(res.res+=mod);
    			}
    		}
    		return res;
    	}
    	node tr[MAXN<<2];
    	inline void push_up(int x){
    		tr[x]=merge(tr[ls(x)],tr[rs(x)]);
    	}
    	void bld(int l,int r,int x){
    		L[x]=l;
    		R[x]=r;
    		if(l==r){
    			tr[x]=node(a[l]);
    			return ;
    		}
    		int mid=(l+r)>>1;
    		bld(l,mid,ls(x));
    		bld(mid+1,r,rs(x));
    		push_up(x);
    	}
    	inline void bld(){
    		bld(1,n,1);
    	}
    	void mdf(int p,int v,int x){
    		int l=L[x],r=R[x];
    		if(l==r){
    			tr[x]=node(v);
    			return ;
    		}
    		int mid=(l+r)>>1;
    		mdf(p,v,p<=mid?ls(x):rs(x));
    		push_up(x);
    	}
    	inline void mdf(int p,int v){
    		mdf(p,v,1);
    	}
    	node qry(int ql,int qr,int x){
    		int l=L[x],r=R[x];
    		if(ql<=l&&r<=qr){
    			return tr[x];
    		}
    		int mid=(l+r)>>1;
    		if(ql<=mid&&mid<qr){
    			return merge(qry(ql,qr,ls(x)),qry(ql,qr,rs(x)));
    		}
    		return qry(ql,qr,ql<=mid?ls(x):rs(x));
    	}
    	inline int qry(int ql,int qr){
    		node cur=qry(ql,qr,1);
    		int res=cur.res+cur.cnt;
    		(res>=mod)&&(res-=mod);
    		return res;
    	}
    }
    inline void solve(){
    	scanf("%d%d",&n,&m);
    	static char str[MAXN];
    	scanf("%s",str+1);
    	for(int i=1;i<=n;i++)
    	{
    		a[i]=str[i]^'0';
    	}
    	pwr[0]=1;
    	for(int i=1;i<=n;i++)
    	{
    		pwr[i]=10ll*pwr[i-1]%mod;
    	}
    	for(int i=2;i<=n;i++)
    	{
    		f[i]=(10ll*f[i-1]+pwr[i-2])%mod;
    	}
    	segtree::bld();
    	printf("%d\n",segtree::qry(1,n));
    	while(m--)
    	{
    		int opt;
    		scanf("%d",&opt);
    		if(opt==1){
    			int l,r;
    			scanf("%d%d",&l,&r);
    			printf("%d\n",segtree::qry(l,r));
    		}
    		if(opt==2){
    			int p,v;
    			scanf("%d%d",&p,&v);
    			segtree::mdf(p,v);
    		}
    	}
    }
    signed main(){
    	freopen("mission.in","r",stdin);
    	freopen("mission.out","w",stdout);
    	solve();
    	return 0;
    }
    
    • 1

    Information

    ID
    128
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    1
    Uploaded By