题目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. jquery的.detach()方法

    .detach()就是从DOM中删除所有匹配的元素. 与.remove()方法不同的是, 这个方法不会把匹配的元素从jQuery对象中删除,所有绑定的事件.附加的数据等都会保留下来,因而可以在将来再使 ...

  2. .net转php laraval框架学习系列(三)项目实战---Route&Controllers

    本章来学习laravel的路由 一个简单的路由列子 Route::get('/', function() { return 'Hello World'; }); 路由的写法和Node的风格很相似.上面 ...

  3. Python中*和**的用法

    好久不用Python,今天在用Python写个小工具的时候发现好些东西都快忘记了,特别是*和**. 什么情况下使用*和**呢(不是C里面的指针哦!) 当函数的参数不确定时,可以使用*args 和**k ...

  4. iOS学习之网易新闻简易Demo

    简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.)         主要分析工程 ...

  5. S3C2440的GPIO

    S3C2440一共有A B C D E F G H J 共九组IO口,一共是130个,每组IO口的个数如下图所示, 其中A组IO口只有输出功能,没有输入功能, 关于GPXCON寄存器,这个寄存器用来配 ...

  6. “express不是内部或外部命令”解决办法

    由于安装的Express是最新版本4.13.1,4.x版本就需要安装express-generator.命令如下:npm install -g express-generator ps: 1.卸载: ...

  7. 用EF DataBase First做一个简单的MVC3报名页面

    使用EF DataBase First做一个简单的MVC3报名网站 ORM(Object Relational Mapping)是面向对象语言中的一种数据访问技术,在ASP.NET中,可以通过ADO. ...

  8. Android开发的体会

    View Functionality--------------->逻辑 Data----------------------->实体,包括从View.Net.其他对象比如Location ...

  9. 【转】Linux內核驅動之GPIO子系統(一)GPIO的使用 _蝸牛

    原文网址:http://tc.chinawin.net/it/os/article-2512b.html 一 概述 Linux內核中gpio是最簡單,最常用的資源(和interrupt ,dma,ti ...

  10. sip演示

    发现了一个很好的演示sip的网页,现粘贴在下面: http://www.in2eps.com/fo-sip/tk-fo-sip-ex3261.html