跳到正文

题解

CF2249E2 String 题解

zrl123456约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年7月30日
收录
2026年9月7日
更新
2026年11月16日
篇幅
约 11 分钟
获赞
1
题目
CF2249E2

版本

  1. v1当前
Hint 1

考虑如何递归到更小的子问题,我们考虑到我们可以通过差分不为 11 的位置得到这个字符串的对齐方式,考虑对其进行分类讨论。

Hint 2

我们考虑观察性质,我们猜测不存在差分不为 11 的这种字符串的长度并不会很长,考虑证明这一点。

对应 Hint 1:

我们考虑如何递归到更小的子问题,我们考虑对原字符串进行差分,设 ci=(ai+1−ai) mod kc_i=(a_{i+1}-a_i)\bmod k,那么我们找出所有 ci≠1c_i\ne 1 的位置,统计出它们 i mod ki\bmod k 的种类数:

  • 若存在两个及以上的种类,那么必定无解。
  • 若存在恰好一个种类,那么这个字符串在原来的序列上的对应方式就确定了(即字符串开头位置对应在原来的序列上的下标对 kk 取模的结果就确定了)。
  • 若不存在这样的 i mod ki\bmod k,那么对齐方式还没有确定。

我们考虑存在恰好一个种类的情况,这时我们可以考虑将这个字符串每 kk 个字符压缩成一个字符,那么我们不妨取每 kk 个字符的最开头字符作为代表元素,那么这样的话我们可以将 r−l+1r-l+1 和 nn 同时除以 kk,这样我们就打成我们递归到更小的子问题的目的了。

当 i mod ki\bmod k 不确定时,我们可能需要枚举代表元素,但是这样的话复杂度将至少来到 Θ(nlog⁡n)\Theta(n\log n) 级别,不太好,我们考虑观察一些性质。

对应 Hint 2:

我们考虑观察性质,我们猜测不存在差分不为 11 的这种字符串的长度并不会很长,考虑证明这一点。

我们注意到我们有性质:

∀a∈N,b∈N∩[0,k),popcount(ak+b)=popcount(a)+b\forall a\in\mathbb N,b\in\mathbb N\cap[0,k),\text{popcount}(ak+b)=\text{popcount}(a)+b

以及性质:

∄i∈N,popcount(i)=popcount(i+1)=popcount(i+2)\nexists i\in\mathbb N,\text{popcount}(i)=\text{popcount}(i+1)=\text{popcount}(i+2)

那么我们可以通过上面两条性质推出:

∄i∈N,popcount(i)=popcount(i+k)=popcount(i+2k)\nexists i\in\mathbb N,\text{popcount}(i)=\text{popcount}(i+k)=\text{popcount}(i+2k)

但是我们注意到如果存在一个差分数组一直是 11 的长度超过 2k2k 的序列,我们一定可以找到上面的不合法情况,那么我们的字符串长度不超过 2k2k,那么压缩后的字符串要么长度为 11,要么长度为 22。

长度为 11 是好处理的,转为求前缀和后我们分为整的长度为 kk 的块和散块,都是比较好计算的。

长度为 22 我们考虑预处理,设 fi,j,p,qf_{i,j,p,q} 表示在 [0,j⋅ki)[0,j\cdot k^i) 这段序列中 (p,q)(p,q) 的出现次数,转移是平凡的。

这样我们就做完了这道题。
时间复杂度为 Θ(k3log⁡kV+qklog⁡kV+∑n)\Theta(k^3\log_kV+qk\log_kV+\sum n),空间复杂度为 Θ(n+k3log⁡V)\Theta(n+k^3\log V)。

code
C++
#include<bits/stdc++.h>
#define int long long
#define inl inline
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i) 
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i) 
#define endl '\n'
#define INF 1e9 
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front 
#define fi first
#define se second
#define lcm(x,y) x/__gcd(x,y)*y
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int> 
#define gt(s) getline(cin,s)
#define at(x,y) for(const auto &x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream 
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
#ifdef __unix__
#define pc putchar_unlocked
#else
#define pc _putchar_nolock
#endif
using namespace std;
const int N=65,M=2e6+5;
int k,ans,f[N][N][N][N],pw[N],lg,a[M],vis[N],c[M],newa[M];
string s;
inl int addm(int a){
	if(a>=k) return a-k;
	return a;
}
inl int popcount(int x){
	int ans=0;
	while(x){
		ans=addm(ans+x%k);
		x/=k;
	}
	return ans;
}
inl int subm(int a){ 
	if(a<0) return a+k;
	return a;
}
inl int getint(char ch){
	if(idg(ch)) return ch^'0';
	if(isupper(ch)) return ch-'A'+10;
	return ch-'a'+36;
}
inl int query1(int r,int x){
	++r;
	int ans=r/k,l=r/k*k,now=popcount(l);
	if(subm(x-now)<r-l) ++ans;
//	cout<<"  "<<r<<' '<<x<<' '<<l<<' '<<ans<<' '<<subm(x-now)<<' '<<r-l<<endl;
	return ans;
}
inl int query2(int r,int x,int y){
	++r;
	rep(i,0,lg) vis[i]=r/pw[i]%k;
//	rep(i,0,lg) cout<<vis[i]<<' ';
//	cout<<endl; 
	int ans=0,sum=0,lst=-1; 
	per(i,lg,0)
		if(vis[i]){
			ans+=f[i][vis[i]][subm(x-sum)][subm(y-sum)];
//			cout<<"    "<<i<<' '<<vis[i]<<' '<<subm(x-sum)<<' '<<subm(y-sum)<<' '<<f[i][vis[i]][subm(x-sum)][subm(y-sum)]<<endl;
			if(~lst&&subm(sum-1-lst%k)==x&&sum==y) ++ans;
			sum=addm(sum+vis[i]);
			lst=i;
		}
//	cout<<"  "<<r<<' '<<x<<' '<<y<<' '<<ans<<endl; 
	return ans;
}
inl void dfs(int l,int r,int n){
	if(l>r) return;
//	cout<<l<<' '<<r<<' '<<n<<endl;
//	rep(i,0,n-1) cout<<a[i]<<' ';
//	cout<<endl; 
	if(n==1){
//		cout<<query1(r,a[0])<<' '<<query1(l-1,a[0])<<endl; 
		ans+=query1(r,a[0])-query1(l-1,a[0]);
		return;
	}
	if(n==2){
//		cout<<query2(r-1,a[0],a[1])<<' '<<query2(l-1,a[0],a[1])<<endl; 
		ans+=query2(r,a[0],a[1])-query2(l,a[0],a[1]);
		return;
	}
	rep(i,1,n-1) c[i]=subm(a[i]-a[i-1]);
	int idx=-1;
	rep(i,1,n-1)
		if(c[i]!=1)
			if(!~idx||idx==i%k) idx=i%k;
			else return;
	if(~idx){
		int lenl=k,lenr=k,cnt=0;
		if(idx){
			lenl=idx;
			newa[cnt++]=addm(a[idx-1]+1);
		}
		rpr(i,idx,n-1,k){
			lenr=n-i;
			newa[cnt++]=a[i];
		}
		int pl=(l/k+1)*k-1,pr=r/k*k;
		if(pl-l+1<lenl) l=l/k+1;
		else l/=k;
		if(r-pr+1<lenr) r=r/k-1;
		else r/=k;
		rep(i,0,cnt-1) a[i]=newa[i];
		dfs(l,r,cnt);
		return;
	}
	int pl=(l/k+1)*k-1,pr=r/k*k;
	if(n<=k){
		newa[0]=a[0];
		rep(i,0,k-n){
			a[0]=subm(newa[0]-i);
			int newl,newr;
			if(pl-l+1<k-i) newl=l/k+1;
			else newl=l/k;
			if(r-pr+1<i+n) newr=r/k-1;
			else newr=r/k;
			dfs(newl,newr,1);
		}
	}
	rep(i,0,n-1) newa[i]=a[i];
	rep(i,max(n-1-k,0ll),min(n-1,k)-1){
		int newl,newr;
		if(pl-l+1<i+1) newl=l/k+1;
		else newl=l/k;
		if(r-pr+1<n-i-1) newr=r/k-1;
		else newr=r/k;
		a[0]=a[1]=newa[i+1];
		dfs(newl,newr,2);
	}
}
inl void solve(){
	int l,r,n;
	cin>>l>>r>>n>>s;
	ans=0;
	rep(i,0,n-1) a[i]=getint(s[i]);
	dfs(l,r,n);
	cout<<ans<<endl; 
//	cout<<endl; 
}
si main(){
	fst;
	int t;
	cin>>k>>t;
	pw[lg]=1;
	while(pw[lg]*k<=1e17){
		++lg;
		pw[lg]=pw[lg-1]*k;
	}
	rep(i,1,k)
		rep(j,1,i-1) f[0][i][j-1][j]=1;
	rep(i,1,lg){
		rep(p,0,k-1)
			rep(q,0,k-1) f[i][1][p][q]=f[i-1][k][p][q];
		rep(j,2,k){
			rep(p,0,k-1)
				rep(q,0,k-1) f[i][j][p][q]=f[i][j-1][p][q];
			rep(p,0,k-1)
				rep(q,0,k-1) f[i][j][p][q]+=f[i][1][subm(p-j+1)][subm(q-j+1)];
			++f[i][j][subm(addm(j-(i+2)%k))][j-1];
		}
	}
	while(t--) solve();
	return 0;
}
原文
存档评论1 条抓取于