\(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板子的更多相关文章

  1. FFT板子

    woc......FFT这玩意儿真坑...... 一上午除了打了几遍板子什么也没干......真是废了...... 你要加油啊...... #include<cstdio> #includ ...

  2. 高精乘(fft板子

    哇..fft的原理真的是不太好懂,看了好久许多细节还是不太清楚,但感觉本质就是用了单位根的性质. https://www.luogu.org/problem/P1919 #include<cst ...

  3. FFT && NTT板子

    贴板子啦-- FFT板子:luogu P3803 [模板]多项式乘法(FFT) #include<cstdio> #include<iostream> #include< ...

  4. 卷积FFT、NTT、FWT

    先简短几句话说说FFT.... 多项式可用系数和点值表示,n个点可确定一个次数小于n的多项式. 多项式乘积为 f(x)*g(x),显然若已知f(x), g(x)的点值,O(n)可求得多项式乘积的点值. ...

  5. maomao的每日动向

    \(2019.02.04\) \(Nothing\) \(to\) \(do\). \(2019.02.05\) - 早上睡到\(12\)点 - 中午下午:吃饭串门拜年 - 晚上:吹爆<流浪地球 ...

  6. bzoj 4332 FFT型的快速幂(需要强有力的推导公式能力)

     有n个小朋友,m颗糖,你要把所有糖果分给这些小朋友. 规则第 i 个小朋友没有糖果,那么他之后的小朋友都没有糖果..如果一个小朋友分到了 xx 个糖果,那么的他的权值是 f(x) = ox^2 +  ...

  7. 【FFT】hdu1402 A * B Problem Plus

    FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cst ...

  8. noip前打板子 qwq

    在某咕上打了一晚上的模板 感觉还好... #include<bits/stdc++.h> #define LL long long using namespace std; inline ...

  9. UVa12298(生成函数的简单应用+FFT)

    I have a set of super poker cards, consisting of an infinite number of cards. For each positive compo ...

随机推荐

  1. C# WebSocket模拟发送接收

    WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...

  2. How to vi

    h:left,j:down,k:up,l:right.wq #write and quitx #cut one letterdd#cut one line/ #searchs/a/b/ #replac ...

  3. NaN与Null与undefiined的关系

    在js中,定义一个变量需要通过关键字var来定义,定义的变量可以是字符串.数字等等都行.但是如果你只是定义了一个变量,没有给他赋值,那么它就默认为'undefined'.例如 var name; co ...

  4. Vue命令行工具vue-cli

    前面的话 Vue.js 提供一个官方命令行工具,可用于快速搭建大型单页应用.该工具提供开箱即用的构建工具配置,带来现代化的前端开发流程.只需几分钟即可创建并启动一个带热重载.保存时静态检查以及可用于生 ...

  5. Nginx 用最快方式让缓存失效

    陶辉103 一般让及时缓存失效针对nginx官方是收费的 我们可以用第三方模块 https://github.com/FRiCKLE/ngx_cache_purge proxy_cache_path ...

  6. 炎黄流程中改流程节点颜色的js

  7. hashlib 模块用来进行hash

    hashlib的基本概述: python中的 hashlib 模块用来进行hash 或者md5加密,而且这种加密是不可逆的,所以这种算法又被称为摘要算法, 其支持Opennssl库提供的所有算法,包括 ...

  8. [NOIp2009] $Hankson$ 的趣味题

    类型:数论 传送门:>Here< 题意:给出四个数$a_0,a_1,b_0,b_1$,求满足$gcd(x,a_0)=a_1,lcm(x,b_0)=b_1$的$x$的个数 解题思路 显然$a ...

  9. [CEOI2007] 树的匹配Treasury

    类型:树形 DP 传送门:>Here< 题意:给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配是多少,并且计算出有多少种最大匹配. 解题思路 首先树形Dp是很明显的,$f[i][ ...

  10. MT【300】余弦的三倍角公式

    2017清华大学THUSSAT附加学科测试数学(二测)$\cos^5\dfrac{\pi}{9}+\cos^5\dfrac{5\pi}{9}+\cos^5\dfrac{7\pi}{9}$ 的值为___ ...