[CF1270F]Awesome Substrings
题目
点这里看题目。
分析
设前缀和\(s_r=\sum_{i=1}^r [S_i='1']\)
考虑满足要求的子串\((l,r]\)的要求:
\]
单独计算并不好算,考虑一个分块的优化。设置阈值\(T\)。
对于\(1\le k\le T\)的\(k\),对要求进行变形:
\]
设\(f(i,k)=ks_i-i\),那么当\(k\)固定的时候,设\(c(x,k)\)为\(f(i,k)=x\)的数量,答案就是:
\]
后面的那个循环,由于有值的\(c(x,k)\)不会超过\(n\)个,因此这一部分可以\(O(nT)\)计算。
对于\(T< k\le n\)的\(k\),继续变形:
\]
由于\(k>T\),所以有\(s_r-s_l< \frac nT\)。也就是说,这种情况下子串内 1 的数量比较少。那么我们就可以直接枚举子串的起点(即式子中的\(l\))和子串内 1 的数量来计算。
当我们确定了 1 的数量的时候,我们也就可以确定此时右端点的范围\((x,y]\),那么也就知道了区间长度的范围\((x-i,y-i]\)。此时的询问就变成求区间内的\(k\)的倍数的数量,可以\(O(1)\)。
需要注意的是,由于这一部分不能前面的记重,因此应该满足:
k>T&\Rightarrow \frac{r-l}{s_r-s_l}>T\\
&\Rightarrow r-l>T(s_r-s_l)
\end{aligned}\]
即区间长度必须大于\(T(s_r-s_l)\),特判一下即可。
时间复杂度是\(O(nT+\frac{n^2}T)\),取\(T=\sqrt n\)最优。
代码
#include <cmath>
#include <cstdio>
#include <cstring>
typedef long long LL;
const int MAXN = 2e5 + 5, MAXT = 505;
template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();}
x *= f;
}
template<typename _T>
void write( _T x )
{
if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
}
template<typename _T>
_T MAX( const _T a, const _T b )
{
return a > b ? a : b;
}
int buc[MAXN * MAXT];
int s[MAXN], nxt[MAXN];
int N, T;
char S[MAXN];
int main()
{
LL ans = 0;
scanf( "%s", S + 1 ), N = strlen( S + 1 ), T = ceil( sqrt( N ) );
for( int i = 1 ; i <= N ; i ++ ) s[i] = s[i - 1] + S[i] - '0';
if( s[N] == 0 ) { puts( "0" ); return 0; }
for( int i = N ; ~ i ; i -- )
{
if( s[i] ^ s[i + 1] ) nxt[i] = i + 1;
else nxt[i] = nxt[i + 1];
}
for( int k = 1, t ; k <= T ; k ++ )
{
for( int i = 0 ; i <= N ; i ++ ) buc[s[i] * k - i + N] ++;
for( int i = 0 ; i <= N ; i ++ ) t = buc[s[i] * k - i + N], ans += 1ll * t * ( t - 1 ) / 2, buc[s[i] * k - i + N] = 0;
}
int l, r, tl, tr;
for( int i = 0 ; i < N ; i ++ )
{
l = i, r = nxt[i];
for( int k = 1 ; k <= N / T && s[i] + k <= s[N] ; k ++ )
{
l = nxt[l], r = nxt[r];
tr = r - i - 1, tl = l - i;
tl = MAX( tl, k * T + 1 );
if( tl <= tr ) ans += tr / k - ( tl - 1 ) / k;
}
}
write( ans ), putchar( '\n' );
return 0;
}
[CF1270F]Awesome Substrings的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- Codeforces Round #258 D Count Good Substrings --计数
题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- Codeforces Round #306 (Div. 2) A. Two Substrings 水题
A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...
随机推荐
- python—day02_基本数据类型
1,字符串 字符串常用功能: 移除空白 分割 长度 索引 切片 1)移除空白 """S.strip([chars]) -> str Return a copy of ...
- BZOJ 1050并查集+贪心
1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3333 Solved: 1851[Submit][St ...
- Oracle 操作权限
alter user scott account unlock; 解锁 -- 新建用户create user yym identified by 123456; -- 放开用户权限grant conn ...
- [JavaWeb基础] 020.Velocity 模板引擎简单示例
1.什么是Velocity 一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的.和JSP不同的是velocity只能显示Action中的数据,不能处理数据.不能写 ...
- html5学习之路_003
html布局 使用<div>元素布局 使用<table>元素布局 <div>元素布局 <!DOCTYPE html> <html> < ...
- Linux、Ubuntu、CentOS安装和配置zsh
目录 01 zsh的安装 02 配置zsh 2.1 安装oh-my-zsh 2.2 查看oh-my-zsh目录 2.3 oh-my-zsh 插件的管理 2.3.1 添加插件 2.3.2 zsh-aut ...
- Java IO(九)FilterInputStream 和 FilterOutputStream
Java IO(九)FilterInputStream 和 FilterOutputStream 一.介绍 FilterInputStream 和 FilterOutputStream 是过滤字节输入 ...
- Linux下db2V10.5命令行安装超详细图文教程(附下载地址)
下载地址:https://pan.baidu.com/s/1GtF03x1FMF3IsGdSiBJu-g 提取码:8vfj 失效了发邮件:wells974@163.com 一.db2prereqche ...
- idea 开发 webpack项目时,只要已加入SVN 版本控制 一直 updating 问题解决
场景描述,这是一个困扰我很久的一个问题,一直百度,都解决不了,今天自己通过设置终于解决了,慢慢的都是辛酸泪,赶快写个笔记记录一下. 对于idea 开发 vue-cli+webpack 项目,idea ...
- 轻便的一句话反弹shell语句
反弹shell往往是在攻击者无法直接连接受害者的情况下进行的操作,原因有很多,例如目标是局域网,或者开启防火墙的某些策略等情况,而这时,我们就可以让受害者主动向攻击者发起连接,被控端发起请求到控制端某 ...