题面

Bzoj

Sol

求不连续回文子序列的个数

\(ans=\)回文子序列个数-连续回文子序列个数

即回文子序列个数-回文子串个数

后面直接\(Manacher\)就好了

考虑前面的

枚举对称轴,设\(f[i]\)表示对称轴\(i\)两边相同字符的对数

那么最终答案就是\(\sum 2^{f[i]}-1\)

考虑求\(f[i]\)

只有当原串中的两个字符相同才会有贡献

也就是\(s[i-x]=s[i+x]\)

单独考虑\(a\)和\(b\)的贡献

\(f[i]=\sum [s[i-x]==s[i+x]]\)

设当前考虑\(a\)的贡献

把是\(a\)的设为\(1\)不是的为\(0\),做个卷积就可以求出\(f\)

那么就可以\(FFT\)辣

注意两个字符之间也算对称轴

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int Zsy(1e9 + 7);
const int _(5e5 + 5);
const double PI(acos(-1)); int n, p[_];
ll f[_];
int N, M, l, r[_];
char s[_], a[_];
struct Complex{
double real, image;
IL Complex(){
real = image = 0;
} IL Complex(RG double a, RG double b){
real = a, image = b;
} IL Complex operator +(RG Complex B){
return Complex(real + B.real, image + B.image);
} IL Complex operator -(RG Complex B){
return Complex(real - B.real, image - B.image);
} IL Complex operator *(RG Complex B){
return Complex(real * B.real - image * B.image, real * B.image + image * B.real);
}
} A[_], B[_]; IL void FFT(RG Complex *P, RG int opt){
for(RG int i = 0; i < N; ++i) if(i < r[i]) swap(P[i], P[r[i]]);
for(RG int i = 1; i < N; i <<= 1){
RG Complex W(cos(PI / i), opt * sin(PI / i));
for(RG int j = 0, p = i << 1; j < N; j += p){
RG Complex w(1, 0);
for(RG int k = 0; k < i; ++k, w = w * W){
RG Complex X = P[k + j], Y = w * P[k + j + i];
P[k + j] = X + Y, P[k + j + i] = X - Y;
}
}
}
} IL void Mul(){
FFT(A, 1);
for(RG int i = 0; i < N; ++i) B[i] = A[i] * A[i];
FFT(B, -1);
for(RG int i = 0; i < N; ++i) B[i].real = B[i].real / N + 0.5;
for(RG int i = 1; i <= M; ++i) f[i] += ((ll)(B[i].real) + 1) >> 1;
} IL ll Manacher(){
RG ll ans = 0; RG int mx = 0, len = 1; a[1] = '#';
for(RG int i = 1; i <= n; ++i) a[++len] = s[i], a[++len] = '#';
for(RG int i = 1, id = 0, mx = 0; i <= len; ++i){
if(i < mx) p[i] = min(mx - i, p[(id << 1) - i]);
while(i - p[i] && i + p[i] <= len && a[i - p[i]] == a[i + p[i]]) ++p[i];
if(p[i] + i > mx) mx = p[i] + i, id = i;
(ans += p[i] >> 1) %= Zsy;
}
return ans;
} IL ll Pow(RG ll x, RG ll y){
RG ll ret = 1;
for(; y; y >>= 1, x = x * x % Zsy)
if(y & 1) ret = ret * x % Zsy;
return ret;
} int main(RG int argc, RG char* argv[]){
scanf(" %s", s + 1); n = strlen(s + 1);
for(M = n + n, N = 1; N <= M; N <<= 1) ++l;
for(RG int i = 0; i < N; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
RG ll ans = -Manacher();
for(RG int i = 0; i < N; ++i) A[i] = Complex(s[i] == 'a', 0);
Mul();
for(RG int i = 0; i < N; ++i) A[i] = Complex(s[i] == 'b', 0);
Mul();
for(RG int i = 1; i <= M; ++i) (ans += Pow(2, f[i]) - 1) % Zsy;
printf("%lld\n", (ans + Zsy) % Zsy);
return 0;
}

Bzoj3160:万径人踪灭的更多相关文章

  1. [bzoj3160]万径人踪灭_FFT_Manacher

    万径人踪灭 bzoj-3160 题目大意:给定一个ab串.求所有的子序列满足:位置和字符都关于某条对称轴对称而且不连续. 注释:$1\le n\le 10^5$. 想法: 看了大爷的题解,OrzOrz ...

  2. BZOJ3160 万径人踪灭 字符串 多项式 Manachar FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8810140.html 题目传送门 - BZOJ3160 题意 给你一个只含$a,b$的字符串,让你选择一个子序列 ...

  3. BZOJ3160 万径人踪灭(FFT+manacher)

    容易想到先统计回文串数量,这样就去掉了不连续的限制,变为统计回文序列数量. 显然以某个位置为对称轴的回文序列数量就是2其两边(包括自身)对称相等的位置数量-1.对称有啥性质?位置和相等.这不就是卷积嘛 ...

  4. BZOJ3160万径人踪灭

    Description Input & Output & Sample Input & Sample Output HINT 题解: 题意即求不连续但间隔长度对称的回文串个数. ...

  5. BZOJ3160: 万径人踪灭

    设a[i]=bool(s[i]=='a'),b[i]=bool(s[i]=='b'),考虑a和a.b和b的卷积,由于卷积是对称的,就可以统计出不连续回文子串个数了.可能说得比较简略.再用manache ...

  6. bzoj千题计划302:bzoj3160: 万径人踪灭

    https://www.lydsy.com/JudgeOnline/problem.php?id=3160 不连续的回文串数量=所有的回文序列数量-连续的回文子串 连续的回文子串: manacher ...

  7. BZOJ3160:万径人踪灭(FFT,Manacher)

    Solution $ans=$回文子序列$-$回文子串的数目. 后者可以用$manacher$直接求. 前者设$f[i]$表示以$i$为中心的对称的字母对数. 那么回文子序列的数量也就是$\sum_{ ...

  8. BZOJ3160 万径人踪灭 【fft + manacher】

    题解 此题略神QAQ orz po神牛 由题我们知道我们要求出: 回文子序列数 - 连续回文子串数 我们记为ans1和ans2 ans2可以用马拉车轻松解出,这里就不赘述了 问题是ans1 我们设\( ...

  9. BZOJ3160: 万径人踪灭(FFT,回文自动机)

    BZOJ传送门: 解题思路: FFT在处理卷积时可以将自己与自己卷,在某一种字母上标1其他标0,做字符集次就好了. (回文就是直接对称可以联系偶函数定义理解,根据这个性质就可以将字符串反向实现字符串匹 ...

  10. 多项式 之 快速傅里叶变换(FFT)/数论变换(NTT)/常用套路【入门】

    原文链接https://www.cnblogs.com/zhouzhendong/p/Fast-Fourier-Transform.html 多项式 之 快速傅里叶变换(FFT)/数论变换(NTT)/ ...

随机推荐

  1. hiveql笔记(一)

    1.创建表 create table if not exists mydb.employees{ name String COMMENT 'Employee name', salary FLOAT C ...

  2. PHPUnit-附录 B. 标注

    [http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.annotations.html] 所谓标注,是指某些编程语言中允许加在源代码中的一种特殊格式的语 ...

  3. [翻译]【目录】编写高性能 .NET 代码

    本篇是 Writing High-Performance .NET Code 的目录索引,翻译内容不定时更新,目录也会同步修改. 性能测量及工具 选择什么来衡量 平均数vs百分比 工具介绍 Visua ...

  4. Linux下 开启防火墙端口

    命令行输入: vi /etc/sysconfig/iptables 将 -A INPUT -m state --state NEW -m tcp -p tcp --dport 端口号 -j ACCEP ...

  5. js中checkbox的全选和反选的实现

    <head> <meta charset="utf-8"/> <script type="text/javascript"> ...

  6. Spring 中出现Element : property Bean definitions can have zero or more properties. Property elements correspond to JavaBean setter methods exposed by the bean classes. Spring supports primitives, refer

    在这个ApplicationContext.xml文件中出现 如下报错 Element : property Bean definitions can have zero or more proper ...

  7. LNMP Yii2 验证码不显示问题最终解决方案

    首先,本地使用OK! 然后,新配置的LNMP环境,验证码一直显示不出来,看了Yii2的验证码存在session里,怀疑是session有问题. 在测试其他页面的时候,发现:session_start( ...

  8. iOS中蓝牙的使用

    Core Bluetooth的使用 1,建立中心设备 2,扫描外设(Discover Peripheral) 3,连接外设(Connect Peripheral) 4,扫描外设中的服务和特征(Disc ...

  9. 关于HTTP,你知道哪些?

    HTTP简介 HTTP 的全称是 Hypertext Transfer Protocol,超文本传输协议 规定客户端和服务器之间的数据传输格式 让客户端和服务器能有效地进行数据沟通 HTTP 协议是网 ...

  10. DLL文件修复

    当你在Windows计算机中安装非操作系统的软件时,往往会覆盖或改写系统共享文件, 如动态链接库(.dll文件)和可执行文件(.exe文件). * 对于Windows系统来说,当用户操作不当(如非正常 ...