maomao的fft板子
\(QwQ\)
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 4000010
using namespace std;
const double Pi = acos(-1.0);
struct complex {
double x, y;
complex (double xx = 0, double yy = 0) {
x = xx, y = yy;
}
}a[MAXN], b[MAXN], c[MAXN];
complex operator + (complex a, complex b) {
return complex(a.x + b.x , a.y + b.y);
}
complex operator - (complex a, complex b) {
return complex(a.x - b.x , a.y - b.y);
}
complex operator * (complex a, complex b) {
return complex(a.x * b.x - a.y * b.y , a.x * b.y + a.y * b.x);
}
int N, M, l, limit = 1, r[MAXN];
void fast_fast_tle (complex *A, int type) {
for (int i = 0; i < limit; i++) {
if (i < r[i]) {
swap(A[i], A[r[i]]);
}
//effect as A[i] = A_original[r[i]];
}
for (int mid = 1; mid < limit; mid <<= 1) {
complex Wn (cos(Pi / mid) ,type * sin(Pi / mid)); //w (1, mid);
for (int R = mid << 1, j = 0; j < limit; j += R) {
//R -> len of sequence
//j -> last position
complex w(1, 0); //w (0, mid);
for (int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + mid + k] = x - y;
}
//mid对应当前的中间值,对应下一次的n。
}
}
}
int main () {
cin >> N >> M;
for (int i = 0; i <= N; i++) cin >> a[i].x;
for (int i = 0; i <= M; i++) cin >> b[i].x;
while (limit <= N + M) limit <<= 1, l++;
for (int i = l - 1, p = 0; i >= 0; --i) {
int go_dis = 0;
while (go_dis < (1 << (l - i - 1))) {
p = p + 1;
r[p] = r[p - (1 << (l - i - 1))] + (1 << i);
++go_dis;
}
}
fast_fast_tle (a, 1);
fast_fast_tle (b, 1);
for (int i = 0; i < limit; i++) {
c[i] = a[i] * b[i];
}
fast_fast_tle(c, -1);
for (int i = 0; i <= N + M; i++) {
printf("%d ", (int)(c[i].x / limit + 0.5));
}
return 0;
}
附上\(nlogn\)高精乘法的板子
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 4000010
using namespace std;
struct complex {
double x, y;
complex (double xx = 0, double yy = 0) {
x = xx, y = yy;
}
}a[N], b[N], c[N];
complex operator + (complex lhs, complex rhs) {
return complex (lhs.x + rhs.x, lhs.y + rhs.y);
}
complex operator - (complex lhs, complex rhs) {
return complex (lhs.x - rhs.x, lhs.y - rhs.y);
}
complex operator * (complex lhs, complex rhs) {
complex t;
t.x = lhs.x * rhs.x - lhs.y * rhs.y;
t.y = lhs.x * rhs.y + rhs.x * lhs.y;
return t;
}
int read () {
int s = 0, w = 1, ch = getchar ();
while ('9' < ch || ch < '0') {
if (ch == '-') w = -1;
ch = getchar ();
}
while ('0' <= ch && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar ();
}
return s * w;
}
int r[N];
int n, m, l, lim = 1;
const double pi = acos (-1);
void fast_fast_tle (complex *A, int type) {
register int i, k, p, len, mid;
register complex Wn, w, x, y;
for (i = 0; i < lim; ++i) if (i < r[i]) swap (A[i], A[r[i]]);
for (mid = 1; mid < lim; mid *= 2) {
Wn = complex (cos (pi / mid), type * sin (pi / mid)); // w (1, mid);
for (len = mid * 2, p = 0; p < lim; p += len) {
w = complex (1, 0);
for (k = 0; k < mid; ++k, w = w * Wn) {// w (k, mid);
x = A[p + k], y = w * A[p + k + mid];
A[p + k] = x + y;
A[p + k + mid] = x - y;
}
}
}
}
int main () {
n = read (), m = read ();
register int i, p, go_dis;
for (i = 0; i <= n; ++i) a[i].x = read ();
for (i = 0; i <= m; ++i) b[i].x = read ();
while (lim <= n + m) lim <<= 1, ++l;
for (i = l - 1, p = 0; i >= 0; --i) {
go_dis = 0;
while (go_dis < (1 << (l - i - 1))) {
p = p + 1;
r[p] = r[p - (1 << (l - i - 1))] + (1 << i);
++go_dis;
}
}
fast_fast_tle (a, +1);
fast_fast_tle (b, +1);
for (i = 0; i < lim; ++i) c[i] = a[i] * b[i];
fast_fast_tle (c, -1);
for (i = 0; i <= n + m; ++i) printf ("%d ", (int) (c[i].x / lim + 0.5));
}
maomao的fft板子的更多相关文章
- FFT板子
woc......FFT这玩意儿真坑...... 一上午除了打了几遍板子什么也没干......真是废了...... 你要加油啊...... #include<cstdio> #includ ...
- 高精乘(fft板子
哇..fft的原理真的是不太好懂,看了好久许多细节还是不太清楚,但感觉本质就是用了单位根的性质. https://www.luogu.org/problem/P1919 #include<cst ...
- FFT && NTT板子
贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...
- 卷积FFT、NTT、FWT
先简短几句话说说FFT.... 多项式可用系数和点值表示,n个点可确定一个次数小于n的多项式. 多项式乘积为 f(x)*g(x),显然若已知f(x), g(x)的点值,O(n)可求得多项式乘积的点值. ...
- maomao的每日动向
\(2019.02.04\) \(Nothing\) \(to\) \(do\). \(2019.02.05\) - 早上睡到\(12\)点 - 中午下午:吃饭串门拜年 - 晚上:吹爆<流浪地球 ...
- bzoj 4332 FFT型的快速幂(需要强有力的推导公式能力)
有n个小朋友,m颗糖,你要把所有糖果分给这些小朋友. 规则第 i 个小朋友没有糖果,那么他之后的小朋友都没有糖果..如果一个小朋友分到了 xx 个糖果,那么的他的权值是 f(x) = ox^2 + ...
- 【FFT】hdu1402 A * B Problem Plus
FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cst ...
- noip前打板子 qwq
在某咕上打了一晚上的模板 感觉还好... #include<bits/stdc++.h> #define LL long long using namespace std; inline ...
- UVa12298(生成函数的简单应用+FFT)
I have a set of super poker cards, consisting of an infinite number of cards. For each positive compo ...
随机推荐
- 洛谷 p1019 单词接龙
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
- Python——glob模块
一.作用: 找到匹配上特定格式的所有文件和文件夹,跟windows的文件搜索功能差不多 二.三种匹配符 *代表0个或多个字符 ?代表一个字符 []匹配指定范围内的字符,如[0-9]匹配数 三.应用方法 ...
- codeforces362B
Petya and Staircases CodeForces - 362B 题意: 一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个 ...
- P1020 导弹拦截
思路:贪心思路 拿比飞来的导弹高并且高度和飞来的导弹最相近的拦截系统去接, 如果全部都比导弹矮,那就新开一个拦截系统 #include<cstdio> #include<string ...
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...
- Linux block(1k) block(4k) 换算 gb
输入 df 显示1k blocks 大小 再输入 df -h 显示 gb换算大小 结论 block(1k) 计算公式为: block(1k) /1024/1000 = xx gb ...
- session的基本原理及安全性
1.session原理 提到session,大家肯定会联想到登录,登录成功后记录登录状态,同时标记当前登录用户是谁.功能大体上就是这个样子,但是今天要讲的不是功能,而是实现.通过探讨session的实 ...
- 【BZOJ2829】[SHOI2012]信用卡凸包(凸包)
[BZOJ2829][SHOI2012]信用卡凸包(凸包) 题面 BZOJ 洛谷 题解 既然圆角的半径都是一样的,而凸包的内角和恰好为\(360°\),所以只需要把圆角的圆心弄下来跑一个凸包,再额外加 ...
- 739. Daily Temperatures && 单调栈 && Python collections deque
题目大意 给你接下来每一天的气温,求出对于每一天的气温,下一次出现比它高气温的日期距现在要等多少天 解题思路 利用单调栈,维护一个单调递减的栈 将每一天的下标i入栈,维护一个温度递减的下标 若下一个温 ...
- 查看Ubuntu的显卡信息
lspci -vnn|grep VGA -A 12 查看openGL信息: sudo apt install mesa-utils glxinfo|grep OpenGL -A 12