传送门


先不考虑”不是连续的一段“这一个约束条件。可以知道:第$i$位与第$j$位相同,可以对第$\frac{i+j}{2}$位置上产生$1$的贡献(如果$i+j$为奇数表明它会对一条缝产生$1$的贡献),而每一个位置上或缝上的满足条件的字符串的个数就是$2^\text{贡献}-1$。把$\frac{1}{2}$忽略掉,也就是说:第$i$位与第$j$位相同时会在第$i+j$位产生$1$的贡献。这个是经典的生成函数+$FFT$求解的问题。具体来说,将$a,b$两个字母分开计算,以计算$a$的贡献为例,如果第$i$位上为$a$,则两个多项式的$x^i$项的系数为$1$,否则为$0$,然后将两个多项式做卷积得到的结果的每一项的系数就是$a$字母对每一位做的贡献,$b$同理。

然后我们考虑减掉连续的一段。连续的一段就是一段回文串,使用$Manacher$求解即可。

#include<bits/stdc++.h>
#define ld long double
//This code is written by Itst
using namespace std;

inline int read(){
    ;
    ;
    char c = getchar();
    while(c != EOF && !isdigit(c)){
        if(c == '-')
            f = ;
        c = getchar();
    }
    while(c != EOF && isdigit(c)){
        a = (a << ) + (a << ) + (c ^ ');
        c = getchar();
    }
    return f ? -a : a;
}

 , MOD = 1e9 + ;
] , news[MAXN];
struct comp{
    ld x , y;

    comp(ld _x =  , ld _y = ){
        x = _x;
        y = _y;
    }

    comp operator +(comp a){
        return comp(x + a.x , y + a.y);
    }

    comp operator -(comp a){
        return comp(x - a.x , y - a.y);
    }

    comp operator *(comp a){
        return comp(x * a.x - y * a.y , x * a.y + y * a.x);
    }
}A[MAXN];
int need , dir[MAXN] , calc[MAXN] , manacher[MAXN];
);

inline int poww(long long a , int b){
    ;
    while(b){
        )
            times = times * a % MOD;
        a = a * a % MOD;
        b >>= ;
    }
    return times;
}

inline void swap(comp& a , comp& b){
    comp t = a;
    a = b;
    b = t;
}

inline void FFT(int type){
    comp wn , w;
     ; i < need ; ++i)
        if(i < dir[i])
            swap(A[i] , A[dir[i]]);
     ; i < need ; i <<= ){
        wn = comp(cos(pi / i) , type * sin(pi / i));
         ; j < need ; j += i << ){
            w = comp( , );
             ; k < i ; ++k , w = w * wn){
                comp x = A[j + k] , y = A[i + j + k] * w;
                A[j + k] = x + y;
                A[i + j + k] = x - y;
            }
        }
    }
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("4199.in" , "r" , stdin);
    //freopen("4199.out" , "w" , stdout);
#endif
    scanf("%s" , s);
    ;
    need = ;
    )
        need <<= ;
     ; i < need ; ++i)
        dir[i] = (dir[i >> ] >> ) | (i &  ? need >>  : );

     ; i < l ; ++i)
        if(s[i] == 'a')
            A[i].x = ;
    FFT();
     ; i < need ; ++i)
        A[i] = A[i] * A[i];
    FFT(-);
     ; i < need ; ++i)
        calc[i] = A[i].x / need /  + 0.6;

    memset(&A ,  , sizeof(A));
     ; i < l ; ++i)
        if(s[i] == 'b')
            A[i].x = ;
    FFT();
     ; i < need ; ++i)
        A[i] = A[i] * A[i];
    FFT(-);
     ; i < need ; ++i){
        calc[i] += A[i].x / need /  + 0.6;
        sum = (sum + poww( , calc[i]) - ) % MOD;
    }

     ; i < l ; ++i)
        news[(i << ) + ] = s[i];
     , maxI = ;
     ; i < l <<  ; ++i){
        if(maxD > i)
            manacher[i] = min(manacher[maxI *  - i] , maxD - i - );
         && i + manacher[i] <= l <<  && news[i - manacher[i]] == news[i + manacher[i]])
            ++manacher[i];
        sum = (sum - (manacher[i] >> ) + MOD) % MOD;
        if(i + manacher[i] > maxD){
            maxD = i + manacher[i];
            maxI = i;
        }
    }
    cout << sum;
    ;
}

Luogu4199 万径人踪灭 FFT、Manacher的更多相关文章

  1. BZOJ 3160: 万径人踪灭 [fft manacher]

    3160: 万径人踪灭 题意:求一个序列有多少不连续的回文子序列 一开始zz了直接用\(2^{r_i}-1\) 总-回文子串 后者用manacher处理 前者,考虑回文有两种对称形式(以元素/缝隙作为 ...

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

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

  3. P4199 万径人踪灭 FFT + manacher

    \(\color{#0066ff}{ 题目描述 }\) \(\color{#0066ff}{输入格式}\) 一行,一个只包含a,b两种字符的字符串 \(\color{#0066ff}{输出格式}\) ...

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

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

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

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

  6. 万径人踪灭(FFT+manacher)

    传送门 这题--我觉得像我这样的菜鸡选手难以想出来-- 题目要求求出一些子序列,使得其关于某个位置是对称的,而且不能是连续一段,求这样的子序列的个数.这个直接求很困难,但是我们可以先求出所有关于某个位 ...

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

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

  8. 洛谷P4199 万径人踪灭(manacher+FFT)

    传送门 题目所求为所有的不连续回文子序列个数,可以转化为回文子序列数-回文子串数 回文子串manacher跑一跑就行了,考虑怎么求回文子序列数 我们考虑,如果$S_i$是回文子序列的对称中心,那么只要 ...

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

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

随机推荐

  1. 腾讯Tars环境搭建 ---- centos

    1,安装git yum install git 2,下载脚本 git clone https://github.com/tangramor/Tars_Install.git 注意:会有3个脚本,cen ...

  2. iOS开发-本地存储(偏好设置,Plist,归档)

    1.   NSUserDefaults //TODO: 1.NSUserDefaults NSUserDefaults类除了可以存储数组.字典.NSdata外,还可以直接存储OC基本类型属性.但是不能 ...

  3. persist与checkpoint

    1.当反复使用某些RDD时建议使用persist(缓存级别)(采用默认缓存级别时为cache())来对数据进行缓存. 2.如果某个步骤的RDD计算特别耗时或经历很多步骤的计算,当重新计算时代价特别大, ...

  4. python中常用函数整理

    1.map map是python内置的高阶函数,它接收一个函数和一个列表,函数依次作用在列表的每个元素上,返回一个可迭代map对象. class map(object): ""&q ...

  5. Derek解读Bytom源码-持久化存储LevelDB

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  6. IntelliJ IDEA 2017 永久注册方法

    https://blog.csdn.net/weixin_39913200/article/details/80859897 在安装的idea下面的bin目录下面有2个文件 : 一个是idea64.e ...

  7. Python scikit-learn (metrics): difference between r2_score and explained_variance_score?

    I noticed that that 'r2_score' and 'explained_variance_score' are both build-in sklearn.metrics meth ...

  8. Navicat连接Oracle的几个问题及解决方案

    1.用Navicat连接Oracle数据库时报错ORA-28547:connection to server failed,probable Oracle Net admin error 解决方案: ...

  9. Vim和Vi的常用命令

    Vim 文本编辑器 1.Vim 和 Vi: 两者都是多模式编辑器: Vim 是 Vi 升级版,再兼容 Vi 所有指令的同时增加了一些新功能支持: 特点: 语法加亮:使用不同的颜色加亮代码: 多级撤销: ...

  10. Linux运维平台工具:pstree、ps、top、htop、free、vmstat、dstat、kill、killall

    1.pstree命令 查看进程树,centos7下统一由systemd进行管理 [root@ELK-chaofeng systemd]# pstree systemd─┬─AliYunDun───*[ ...