题目1 COGS 很强的乘法问题

高精度乘法用FFT加速

 #include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define Pi acos(-1) using namespace std;
const int N = + ;
const int rad = ; struct Complex {
double r, i;
Complex(double x = , double y = ) { r = x; i = y; }
Complex operator + (Complex x) { return Complex(r + x.r, i + x.i); }
Complex operator - (Complex x) { return Complex(r - x.r, i - x.i); }
Complex operator * (Complex x) { return Complex(r * x. r - i * x.i, r * x.i + i * x.r); }
}a[N], b[N];; int n, m, l, r[N], c[N];
char ss[N]; void dft(Complex *s, int f) {
for(int i = ; i < n; ++ i)
if(i < r[i]) swap(s[i], s[r[i]]);
for(int i = ; i < n; i <<= ) {
Complex wn(cos(Pi / i), f * sin(Pi / i));
for(int j = ; j < n; j += (i << )) {
Complex w(, );
for(int k = ; k < i; ++ k) {
Complex x = s[j + k], y = w * s[j + k + i];
s[j + k] = x + y;
s[j + k + i] = x - y;
w = w * wn;
}
}
}
if(f == -)
for(int i = ; i < n; ++ i)
s[i].r /= n;
} int main() {
#ifndef stone
freopen("bettermul.in", "r", stdin);
freopen("bettermul.out", "w", stdout);
#endif int len1, len2;
scanf("%s", ss); len1 = strlen(ss);
for(int i = ; i < len1; ++ i) a[i] = ss[len1 - i - ] - '';
scanf("%s", ss); len2 = strlen(ss);
for(int i = ; i < len2; ++ i) b[i] = ss[len2 - i - ] - '';
n = max(len1, len2); m = (n << ) - ;
for(n = ; n <= m; n <<= ) l ++;
for(int i = ; i < n; ++ i)
r[i] = (r[i >> ] >> ) | ((i & ) << (l - ));
dft(a, ); dft(b, );
for(int i = ; i < n; ++ i) a[i] = a[i] * b[i];
dft(a, -);
for(int i = ; i <= m; ++ i) c[i] = (int) (a[i].r + 0.5);
for(int i = ; i <= m; ++ i) {
if(c[i] >= rad) {
c[i + ] += c[i] / rad;
c[i] %= rad;
if(i == m) m ++;
}
}
int pos = m;
while(c[pos] == ) -- pos;
for(int i = pos; i >= ; -- i) printf("%d", c[i]); #ifndef stone
fclose(stdin); fclose(stdout);
#endif
return ;
}

COGS

题目2 BZOJ4259 残缺的字符串

算法讨论:(摘自Claris的博客,写得很清楚,我就是看这个才会做的)

注意:如果开第二个字符串的倍长的话,FFT会超时。还要注意统计答案的区间是什么。不要错了。

 #include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath> using namespace std;
typedef long long ll;
const int N = + ;
#define Pi acos(-1) struct Cp {
double r, i;
Cp(double _r = , double _i = ):
r(_r), i(_i) {}
Cp operator + (Cp x) {
return Cp(r + x.r, i + x.i);
}
Cp operator - (Cp x) {
return Cp(r - x.r, i - x.i);
}
Cp operator * (Cp x) {
return Cp(r * x.r - i * x.i, r * x.i + i * x.r);
}
}; char str1[N], str2[N];
int len1, len2, n, m, l, cnt;
int t[N], ans[N], r[N];
Cp a[N], b[N], A[N], B[N], C[N], tp; void DFT(Cp *s, int f) {
for(int i = ; i < n; ++ i)
if(i < r[i]) swap(s[i], s[r[i]]);
for(int i = ; i < n; i <<= ) {
Cp wn(cos(Pi / i), f * sin(Pi / i));
for(int j = ; j < n; j += (i << )) {
Cp w(, );
for(int k = ; k < i; ++ k) {
Cp x = s[j + k], y = w * s[j + k + i];
s[j + k] = x + y;
s[j + k + i] = x - y;
w = w * wn;
}
}
}
if(f == -)
for(int i = ; i < n; ++ i)
s[i].r /= n;
} #define stone int main() {
#ifndef stone freopen("bitch.in", "r", stdin);
freopen("bitch.out", "w", stdout); #endif scanf("%d%d", &len1, &len2);
scanf("%s%s", str1, str2);
reverse(str1, str1 + len1);
for(int i = ; i < len1; ++ i) {
if(str1[i] == '*') a[i].r = ;
else a[i].r = (str1[i] - 'a' + );
}
for(int i = ; i < len2; ++ i) {
if(str2[i] == '*') b[i].r = ;
else b[i].r = (str2[i] - 'a' + );
}
m = len1 + len2;
for(n = ; n <= m; n <<= ) ++ l;
for(int i = ; i < n; ++ i) r[i] = (r[i >> ] >> ) | ((i & ) << (l - )); for(int i = ; i < n; ++ i) {
A[i].r = a[i].r * a[i].r * a[i].r;
B[i].r = b[i].r;
}
DFT(A, ); DFT(B, );
for(int i = ; i < n; ++ i) { C[i] = C[i] + A[i] * B[i]; }
memset(A, , sizeof A); memset(B, , sizeof B);
for(int i = ; i < n; ++ i) {
A[i].r = a[i].r * a[i].r;
B[i].r = b[i].r * b[i].r;
}
DFT(A, ); DFT(B, ); tp = (Cp) {, };
for(int i = ; i < n; ++ i) { C[i] = C[i] - A[i] * B[i] * tp; }
memset(A, , sizeof A); memset(B, , sizeof B);
for(int i = ; i < n; ++ i) {
A[i].r = a[i].r;
B[i].r = b[i].r * b[i].r * b[i].r;
}
DFT(A, ); DFT(B, );
for(int i = ; i < n; ++ i) { C[i] = C[i] + A[i] * B[i]; }
DFT(C, -);
for(int i = len1 - ; i < len2; ++ i)
if(C[i].r < 0.5) {
ans[++ cnt] = i - len1 + ;
}
printf("%d\n", cnt);
for(int i = ; i <= cnt; ++ i) {
printf("%d ", ans[i]);
} #ifndef stone fclose(stdin); fclose(stdout); #endif return ;
}

4259

题目3 BZOJ 万径人踪灭

算法讨论:

用FFT算出所有回文串(相邻的和不相邻的),然后用Manacher计算出所有相邻的回文串,然后做差统计答案即可。

用FFT算出所有回文串的时候,是有len + len - 1个位置要计算的,分别是在字符的位置上和在两个字符之间的位置。

 #include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath> using namespace std;
const int N = + ;
typedef long long ll;
const int mod = 1e9 + ;
#define Pi acos(-1) struct Cp {
double r, i;
Cp(double _r = , double _i = ) :
r(_r), i(_i){}
Cp operator + (Cp x) {
return Cp(r + x.r, i + x.i);
}
Cp operator - (Cp x) {
return Cp(r - x.r, i - x.i);
}
Cp operator * (Cp x) {
return Cp(r * x.r - i * x.i, r * x.i + i * x.r);
}
}; char ss[N], Ma[N << ];
int n, m, l, r[N], lens;
Cp a[N], b[N];
ll Mp[N << ], t1[N], t2[N], p[N]; void DFT(Cp *s, int f) {
for(int i = ; i < n; ++ i)
if(i < r[i]) swap(s[i], s[r[i]]);
for(int i = ; i < n; i <<= ) {
Cp wn(cos(Pi / i), f * sin(Pi / i));
for(int j = ; j < n; j += (i << )) {
Cp w(, );
for(int k = ; k < i; ++ k) {
Cp x = s[j + k], y = w * s[j + k + i];
s[j + k] = x + y;
s[j + k + i] = x - y;
w = w * wn;
}
}
}
if(f == -)
for(int i = ; i < n; ++ i)
s[i].r /= n;
} ll Manacher(char *s, int len) {
int l = ; ll res = ;
Ma[l ++] = '$';
Ma[l ++] = '#';
for(int i = ; i < len; ++ i) {
Ma[l ++] = s[i];
Ma[l ++] = '#';
}
Ma[l] = '!';
int Mx = , id = ;
for(int i = ; i < l; ++ i) {
if(Mx > i) {
Mp[i] = min(Mp[ * id - i], (ll)Mx - i);
}
else {
Mp[i] = ;
}
while(Ma[i + Mp[i]] == Ma[i - Mp[i]]) Mp[i] ++;
if(i + Mp[i] > Mx) {
Mx = i + Mp[i];
id = i;
}
res += (Mp[i] >> ); res %= mod;
}
return res;
} int main() {
ll ans = , tmp;
scanf("%s", ss);
n = strlen(ss); lens = strlen(ss);
for(int i = ; i < n; ++ i) {
if(ss[i] == 'a')
a[i].r = , b[i].r = ;
else a[i].r = , b[i].r = ;
}
m = (n << ) - ;
for(n = ; n <= m; n <<= ) l ++;
for(int i = ; i < n; ++ i) r[i] = (r[i >> ] >> ) |((i & ) << (l - ));
DFT(a, ); DFT(b, );
for(int i = ; i < n; ++ i) {
a[i] = a[i] * a[i];
b[i] = b[i] * b[i];
}
DFT(a, -); DFT(b, -);
for(int i = ; i <= n; ++ i) {
t1[i] = (ll) (a[i].r + 0.5);
t2[i] = (ll) (b[i].r + 0.5);
}
for(int i = ; i < n; ++ i) {
t1[i] += t2[i];
}
tmp = Manacher(ss, strlen(ss));
p[] = ;
for(int i = ; i <= lens; ++ i)
p[i] = p[i - ] * % mod;
lens = lens + lens - ;
for(int i = ; i < lens; ++ i) {
if(i & ) {
ans = (ans + p[t1[i] >> ] - ) % mod;
}
else {
ans = (ans + (p[(t1[i] - ) >> ] << ) - ) % mod;
}
}
ans = (ans - tmp) % mod;
while(ans < ) {
ans += mod;
ans %= mod;
}
printf("%lld\n", ans);
return ;
}

万径人踪灭

题目4 BZOJ3028

首先很高兴的表示这里的汉堡是承德的。然后表示如果会生成函数的话这就是个输入输出题。

系数是种类,指数是数量性质。

如下图:

然后就没有然后了。知道答案就考虑输入和输出了。表示0ms过没有压力。这题如果你要是写个搜索。。。。。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <algorithm>
#include <cctype> using namespace std;
const int N = + ;
const int mod = ;
typedef long long ll; ll n; inline ll read() {
ll x = ;
char c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)) {
x = x * + c - '';
x %= mod;
c = getchar();
}
return x;
} int main() {
n = read();
n = n * (n + ) * (n + );
n /= ;
n %= mod;
printf("%lld\n", n);
return ;
}

3028

FFT与多项式、生成函数题目泛做的更多相关文章

  1. 基尔霍夫矩阵题目泛做(AD第二轮)

    题目1: SPOJ 2832 题目大意: 求一个矩阵行列式模一个数P后的值.p不一定是质数. 算法讨论: 因为有除法而且p不一定是质数,不一定有逆元,所以我们用辗转相除法. #include < ...

  2. 后缀自动机/回文自动机/AC自动机/序列自动机----各种自动机(自冻鸡) 题目泛做

    题目1 BZOJ 3676 APIO2014 回文串 算法讨论: cnt表示回文自动机上每个结点回文串出现的次数.这是回文自动机的定义考查题. #include <cstdlib> #in ...

  3. 二维计算几何基础题目泛做(SYX第一轮)

    题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...

  4. 生成树题目泛做(AD第二轮)

    题目1: NOI2014 魔法森林 LCT维护MST.解题报告见LOFTER #include <cstdio> #include <iostream> #include &l ...

  5. K-D Tree题目泛做(CXJ第二轮)

    题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...

  6. 插头DP题目泛做(为了对应WYD的课件)

    题目1:BZOJ 1814 URAL 1519 Formula 1 题目大意:给定一个N*M的棋盘,上面有障碍格子.求一个经过所有非障碍格子形成的回路的数量. 插头DP入门题.记录连通分量. #inc ...

  7. 数学期望和概率DP题目泛做(为了对应AD的课件)

    题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...

  8. Link-Cut-Tree题目泛做(为了对应自己的课件)

    题目1:BZOJ 2049 洞穴勘测 #include <bits/stdc++.h> #define L(x) c[x][0] #define R(x) c[x][1] using na ...

  9. 莫比乌斯反演题目泛做(为了对应smz的课件)

    题1:BZOJ2190 SDOI 2010 仪仗队 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2190 算法讨论: 我们先来考虑一个点被 ...

随机推荐

  1. 单源最短路径 hdu 2066

    #include<string.h> //和prim类似#include<stdio.h> //prim所考虑的是节点与当前所建成的树的距离最小using namespace ...

  2. jQuery自学笔记(五):关于jQuery的遍历

    向上遍历 DOM 树 parent()  //返回被选元素的直接父元素,该方法只会向上一级对 DOM 树进行遍历. parents()    //返回被选元素的所有祖先元素,它一路向上直到文档的根元素 ...

  3. 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。

    使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...

  4. 转载:spring ,struct2 在 web.xml中的配置

    转载网址:http://blog.sina.com.cn/s/blog_4c6e822d0102dv63.html <!-- Struts2 need begin-->  <filt ...

  5. PHP内置Web Server探究(二)自定义PHP控制台输出console函数

    我们在开发APP的服务器端,当和APP进行联调时通常需要实时跟踪URL请求和参数的接收情况. 但PHP并没有像Python或Java专有的控制台输出函数,Python的print()和Java的Sys ...

  6. Markdown简短教程

    前言 很早以前就已经接触到Markdown语言,由于各种原因到今天才认真的学习.其实Markdown语言还是比较简单的,在用中学就可以了. 正文 本文只是介绍而没有说明其它可选语法,详细可以参考[Ma ...

  7. 用tee和script来记录终端输出

    如何在输出信息的同时把数据保存到文件当中? 一,如何把命令运行的结果保存到文件当中?这个问题太简单了,大家都知道,用 > 把输出转向就可以了 例子:[lhd@hongdi ~]$ ls > ...

  8. jquery.mmenu

    http://mmenu.frebsite.nl/ 左右滑动效果 http://blog.sina.com.cn/s/blog_6a0a183f0100zsfk.html js的左右滑动触屏事件,主要 ...

  9. netstat命令, netstat指令在windows和linux有什么不同

    查看当前tcp监听端口[op@TIM html]$ netstat -nltp(Not all processes could be identified, non-owned process inf ...

  10. Linux企业级项目实践之网络爬虫(17)——存储页面

    在爬虫系统中数据的流量相当大,要处理的数据内容不仅包括爬虫系统的各种数据结构空间,而且包括从外部节点中得到的各种数据,比如HTTP请求,HTML页面,ROBOT.TXT等等.如果对这些内容处理不当,那 ...