。。恩 打了四五遍 不会也背出来了。。

BZOJ3160 【听说时限紧?转C++的优势么?】

上AC代码 fft

 /*Problem: 3160
User: cyz666
Language: C++
Result: Accepted
Time:1992 ms
Memory:18492 kb
****************************************************************/ #include <bits/stdc++.h>
#define LL long long
const LL mo=;
const double pi=acos(-1.0);
using namespace std;
struct cp{
double x,y;
cp(double a=,double b=):x(a),y(b){}
//cp(double a=0,double b=0){x=a,y=b;}
}a[],b[],W[];
int h[],rev[],f[],c[],n,x,m;
char s; LL ans;
cp operator +(cp a,cp b){
cp c(a.x+b.x,a.y+b.y);
return c;
}
cp operator *(cp a,cp b){
cp c(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
return c;
}
cp operator -(cp a,cp b){
return (cp){a.x-b.x,a.y-b.y};
}
void fft(cp a[],int n,int d=){
if (d<) reverse(a+,a+n);
for (int i=;i<n;++i)
if (i<rev[i]) swap(a[i],a[rev[i]]);
for (int i=;i<n/;++i)
W[i]=cp(cos(*pi/n*i),sin(*pi/n*i));
for (int m=,k=;m<=n;k=m,m<<=)
for (int i=;i<n;i+=m)
for (int j=i;j<i+k;++j){
cp u=a[j],v=a[j+k]*W[n/m*(j-i)];
a[j]=u+v,a[j+k]=u-v;
}
if (d<) for (int i=;i<n;++i) a[i].x/=n;
}
int main(){
while (scanf("%c",&s)&&isalpha(s))
s=='a'?c[++++n]=:c[++++n]=;
c[]=-; c[n+]=-;
for (int i=;i<=n;++i){
if (x+f[x]>i) f[i]=min(f[x+x-i],x+f[x]-i);
while (c[i+f[i]]==c[i-f[i]]) ++f[i]; --f[i];
if (i+f[i]>x+f[x]) x=i;
ans+=f[i]/; if (ans>=mo) ans-=mo;
}
m=ceil((log(n)/log())); m=<<m;
for (int i=;i<m;++i)rev[i]=(rev[i>>]>>)+(m>>)*(i&);
x=; for (int i=;i<=n;++++i) c[++x]=c[i]; n=x;
for (int i=;i<=n;++i) c[i]==?a[i].x=:b[i].x=;
fft(a,m); fft(b,m);
for (int i=;i<m;++i) a[i]=a[i]*a[i],b[i]=b[i]*b[i];
fft(a,m,-); fft(b,m,-);
h[]=; for (int i=;i<=n;++i)h[i]=(h[i-]+h[i-])%mo;
int x; ans=mo-ans;
for (int i=;i<n+n;++i){
x=round(a[i].x+b[i].x);
ans+=h[x+>>]-; if (ans>=mo) ans-=mo;
}
ans=(ans-n++mo)%mo;
printf("%lld",ans);
return ;
}

KriTo

然后 顺手写了个ntt板子扔这(没用题目测过对不对)

mo的原根g的定义:g^1,g^2,...,g^(mo-1) 在%mo意义下各不相同。

 #include <bits/stdc++.h>
#define LL long long
using namespace std;
int g1,g2,N,n,k,x,W[],a[],b[],rev[],mo1,mo2;
LL po(LL x,LL y,LL mo){
LL z=; if (y<) y=y%(mo-)+mo-;
for (;y;y>>=,x=x*x%mo)
if (y&) z=z*x%mo;
return z;
}
int getg(LL mo){
LL y=mo-,x=floor(sqrt(y)); int fl;
for (int g=;;++g){
fl=;
for (int i=;i<=x;++i) if (!(y%i))
if (po(g,i,mo)==||po(g,y/i,mo)==) {fl=;break;}
if (fl) return g;
}
}
void ntt(int a[],int n,int d,int mo,int G){ //n整除(mo-1)
if (d<) reverse(a+,a+n);
for (int i=;i<n;++i)
if (i<rev[i]) swap(a[i],a[rev[i]]);
W[]=; LL X=po(G,(mo-)/n,mo);
for (int i=;i<n/;++i) W[i]=X*W[i-]%mo;
for (int m=,k=;m<=n;k=m,m<<=)
for (int i=;i<n;i+=m)
for (int j=i;j<i+k;++j){
int u=a[j],v=1ll*a[j+k]*W[n/m*(j-i)]%mo;
a[j]=u+v>=mo?u+v-mo:u+v;
a[j+k]=u-v<?u-v+mo:u-v;
}
if (d<){
X=po(n,mo-,mo);
for (int i=;i<n;++i) a[i]=X*a[i]%mo;
}
}
int main(){
mo1=; mo2=;
g1=getg(mo1); g2=getg(mo2);
scanf("%d%d",&n,&k); W[]=;
for (int i=;i<=n;++i)
scanf("%d",&x),a[x]=,b[x]=;
N=;
for (int i=;i<N;++i) rev[i]=rev[i>>]>>|(i&?N>>:);
ntt(a,N,,mo1,g1); ntt(b,N,,mo2,g2);
for (int i=;i<N;++i)
a[i]=po(a[i],k,mo1),b[i]=po(b[i],k,mo2);
ntt(a,N,-,mo1,g1); ntt(b,N,-,mo2,g2);
for (int i=;i<N;++i){
if (a[i]<) a[i]+=mo1;
if (b[i]<) b[i]+=mo2;
}
for (int i=;i<N;++i) if (a[i]||b[i]) printf("%d ",i); puts("");
return ;
}

AsuNa

若多项式相乘的最高次为n, 则FFT,NTT的数组大小要开到>n的最小的2的幂。 即 1<<(int)ceil(log(n+1)/log(2))

给几个模数:

1004535809=479*2^21+1,   原根=3

998244353=7*17*2^23+1 ,原根=3

469762049=7*2^26+1      ,原根=3

BZOJ3160【万径人踪灭】 【FFT】的更多相关文章

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

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

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

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

  3. 【bzoj3160】万径人踪灭 FFT

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3160 我是一个傻叉 微笑脸 #include<bits/stdc++.h> #de ...

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

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

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

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

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

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

  7. [bzoj3160]万径人踪灭_FFT_Manacher

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

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

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

  9. bzoj 3160 万径人踪灭 FFT

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

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

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

随机推荐

  1. CF651B-Beautiful Paintings

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. QueenAttack

    问题分析: 1.对于行和列,要求得每行或每列的棋子个数,只需要把横坐标或纵坐标相同的棋子数目相加即可,然后使用k*(k-1)/2就可求得攻击次数 2.对于对角线上的点,需要分析对角线上点的坐标关系.分 ...

  3. Educational Codeforces Round 37 (Rated for Div. 2) G

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  4. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

  5. 七天从零基础学习android(3)--实现过程

    首先这是我对自己编写程序的认识,要实现一个程序,根据之前编写C++的经验,要对所编写的软件有一个模糊的了解. 一个记账本软件,要实现的过程是,添加收支,显示本日,本月或本年的收支状态.然而基于是完全没 ...

  6. makefile的语法及写法

    什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefile还是要 ...

  7. Spring的@Qualifier注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-qualifier-an ...

  8. google --SwitchyOmega and switchysharp ***

    https://github.com/FelisCatus https://chrome.google.com/webstore/search/Proxy%20SwitchySharp%20?hl=z ...

  9. 关于MySQL的boolean和tinyint(1)

    原文:http://blog.csdn.net/woshixuye/article/details/7089508 MySQL保存boolean值时用1代表TRUE,0代表FALSE.boolean在 ...

  10. zoom to raster resolution

     don't execute the ESRI's command, just find out and write codes to zoom to the raster resolution. H ...