3160: 万径人踪灭

题意:求一个序列有多少不连续的回文子序列


一开始zz了直接用\(2^{r_i}-1\)

总-回文子串

后者用manacher处理

前者,考虑回文有两种对称形式(以元素/缝隙作为对称轴)

f[i],i为奇数表示以缝隙对称,偶数表示以元素i>>1对称,对答案的贡献就是\(2^{f[i]}-1\)

\[f[i] = \sum_{j=1}^{i-1} [s_j = s_{i-j}]
\]

就是裸卷积

因为只有a,b两个字符,可以先后令a或b=1分别求

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define fir first
#define sec second
typedef long long ll;
const int N=(1<<19)+5, mo=1e9+7;
const double PI = acos(-1);
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
return x*f;
} struct meow{
double x, y;
meow(double a=0, double b=0):x(a), y(b){}
};
meow operator +(meow a, meow b) {return meow(a.x+b.x, a.y+b.y);}
meow operator -(meow a, meow b) {return meow(a.x-b.x, a.y-b.y);}
meow operator *(meow a, meow b) {return meow(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);}
meow conj(meow a) {return meow(a.x, -a.y);}
typedef meow cd; namespace fft {
int n, rev[N];
void ini(int lim) {
n=1; int k=0; while(n<lim) n<<=1, k++;
for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
}
void dft(cd *a, int flag) {
for(int i=0; i<n; i++) if(i<rev[i]) swap(a[i], a[rev[i]]);
for(int l=2; l<=n; l<<=1) {
int m = l>>1;
cd wn = cd(cos(2*PI/l), flag * sin(2*PI/l));
for(cd *p=a; p != a+n; p+=l) {
cd w(1, 0);
for(int k=0; k<m; k++) {
cd t = w * p[k+m];
p[k+m] = p[k] - t;
p[k] = p[k] + t;
w = w*wn;
}
}
}
if(flag == -1) for(int i=0; i<n; i++) a[i].x /= n;
}
void mul(cd *a) {
dft(a, 1);
for(int i=0; i<n; i++) a[i] = a[i] * a[i];
dft(a, -1);
}
} cd a[N];
int n, f[N]; ll ans;
char s[N];
ll Pow(ll a, int b) {
ll ans=1;
for(; b; b>>=1, a=a*a%mo)
if(b&1) ans=ans*a%mo;
return ans;
}
inline void mod(ll &x) {if(x<0) x+=mo; else if(x>=mo) x-=mo;} namespace ma {
int r[N]; char a[N];
ll manacher(char *s, int n) {
int p=0, a; ll ans=0;
for(int i=1; i<=n; i++) {
r[i] = i<p ? min(p-i+1, r[2*a-i]) : 1;
while(s[ i-r[i] ] == s[ i+r[i] ]) r[i]++;
if(i+r[i]-1 > p) p = i+r[i]-1, a=i;
mod(ans += r[i]>>1);
}
return ans;
}
ll cal(char *s) {
for(int i=1; i<=n; i++) a[(i<<1)-1] = '#', a[i<<1] = s[i];
a[(n<<1)+1] = '#';
a[0] = '@'; a[(n<<1)+2] = '$';
return manacher(a, (n<<1)+1);
}
} int main() {
freopen("in","r",stdin);
scanf("%s", s+1); n = strlen(s+1);
fft::ini(n+n+1);
for(int i=1; i<=n; i++) a[i].x = s[i]=='a';
fft::mul(a);
for(int i=1; i<=n+n; i++) f[i] = (int)floor(a[i].x + 0.5); for(int i=0; i<fft::n; i++) a[i] = cd(0, 0);
for(int i=1; i<=n; i++) a[i].x = s[i]=='b';
fft::mul(a);
for(int i=1; i<=n+n; i++) f[i] += (int)floor(a[i].x + 0.5);
for(int i=1; i<=n+n; i++) f[i] = (f[i]+1)>>1;
//for(int i=1; i<=n+n; i++) printf("f %d %d\n", i, f[i]); for(int i=1; i<=n+n; i++) mod(ans += Pow(2, f[i]) - 1); //printf("ans1 %lld\n", ans);
mod(ans -= ma::cal(s));
printf("%lld", ans);
}

BZOJ 3160: 万径人踪灭 [fft manacher]的更多相关文章

  1. BZOJ 3160: 万径人踪灭 FFT+快速幂+manacher

    BZOJ 3160: 万径人踪灭 题目传送门 [题目大意] 给定一个长度为n的01串,求有多少个回文子序列? 回文子序列是指从原串中找出任意个,使得构成一个回文串,并且位置也是沿某一对称轴对称. 假如 ...

  2. bzoj 3160 万径人踪灭 FFT

    万径人踪灭 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1936  Solved: 1076[Submit][Status][Discuss] De ...

  3. bzoj 3160 万径人踪灭——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3160 似乎理解加深了. 用卷积算相同的位置:先把 a 赋成1. b 赋成0,卷积一遍:再把 ...

  4. bzoj 3160 万径人踪灭 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3160 求出关于一个位置有多少对对称字母,如果 i 位置有 f[i] 对,对答案的贡献是 2^ ...

  5. bzoj 3160: 万径人踪灭 manachar + FFT

    3160: 万径人踪灭 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 133  Solved: 80[Submit][Status][Discuss] ...

  6. BZOJ 3160: 万径人踪灭

    Description 一个ab串,问有多少回文子序列,字母和位置都对称,并且不连续. Sol FFT+Manacher. 不连续只需要减去连续的就可以了,连续的可以直接Manacher算出来. 其他 ...

  7. bzoj 3160: 万径人踪灭【FFT+manacher】

    考虑正难则反,我们计算所有对称子序列个数,再减去连续的 这里减去连续的很简单,manacher即可 然后考虑总的,注意到关于一个中心对称的两点下标和相同(这样也能包含以空位为对称中心的方案),所以设f ...

  8. 【BZOJ】3160: 万径人踪灭 FFT+回文串

    [题意]给定只含'a'和'b'字符串S,求不全连续的回文子序列数.n<=10^5. [算法]FFT+回文串 [题解]不全连续的回文子序列数=回文子序列总数-回文子串数. 回文子串数可以用回文串算 ...

  9. BZOJ 3160 万径人踪灭 解题报告

    这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 $T_i$ 为关于直线 $i$ ...

随机推荐

  1. BC#64 4.Tree

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5589 对于u,v的xor和就是u到根的xor和 xor上 v到根的xor和.看到n<=5w,考虑莫队 ...

  2. [国嵌笔记][011][Linux密码破解]

    破解步骤 1.在系统启动时进入grub选项菜单 2.在grub选项菜单中按e进入编辑模式 3.编辑kernel行,添加 /init 1 (表示进入单用户启动模式,在单用户启动模式中不会要求输入密码) ...

  3. sql子查询

    一.子查询入门: 1.单值子查询: 单值子查询的唯一限制:子查询的返回值必须只有一行记录,而且只能有一列(又被称为标量子查询). 可以使用在select语句的列表.表达式中,以及where语句中等. ...

  4. bootstrap-multiselect 的简单使用,样式修改,动态创建option

    1.bootstrap-multiselect 顾名思义基于bootstrap,bootstrap基于jquery,所以第一步,引入文件 bootstrap.css/ juery.js /bootst ...

  5. 一篇文章帮你解决python的包管理

    写python代码的人都知道,一个项目写下下来,不可避免的都需要使用很多第三方包,通常我们都是通过pip install ,然而当我们需要上线的时候问题来了,如果中间你自己不记得自己安装了多少个包,这 ...

  6. vue中引入jQuery和bootstrap

    一.引入jQuery: 首先在当前项目的根目录下(就是与package.json同目录),运行命令npm install jquery --save-dev   这样就将jquery安装到了这个项目中 ...

  7. Linux - ubuntu读取/root/.profile时发现错误:mesg:ttyname fa

    启动ubuntu,以root用户登陆,打开命令行终端 输入命令:#vim /root/.profile 找到.profile文件中的mesg n 将其替换成tty -s && mesg ...

  8. 我看过得最易懂的一段AOP的解释

    http://blog.csdn.net/zhangliangzi/article/details/51648032 面向切面编程(AOP是Aspect Oriented Program的首字母缩写) ...

  9. ThinkServer TD340服务器安装操作系统[转]

    一:服务器概况 服务器是联想旗下ThinkServer品牌TD340型号服务器,服务器标配32G内存,1T硬盘.其中服务器使用RAID(磁盘阵列)技术,拥有一个RAID卡,服务器标配一个大小为1T的磁 ...

  10. apache日志管理【转】

    web服务器日志轮循比较好的方式有三种:第一种方法是利用Linux系统自身的日志文件轮循机制:logrotate:第二种方法是利用apache自带的日志轮循程序rotatelogs:第三种是使用在ap ...