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 ...
随机推荐
- js函数使用prototype和不适用prototype的区别
js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class 首先来看一个实例: function Li ...
- JarvisOJ Basic easyRSA
还记得veryeasy RSA吗?是不是不难?那继续来看看这题吧,这题也不难. 已知一段RSA加密的信息为:0xdc2eeeb2782c且已知加密所用的公钥: (N=322831561921859 e ...
- JarvisOJ Basic 熟悉的声音
两种元素,还有声音,想到了莫尔斯电码,解码得到 jbluwewnz 提交,发现不对,觉得应该是有实际意义的东西,实在想不到还能怎么解,就去看了题解. 发现这个还可以再套一个凯撒密码,就拿python写 ...
- avpicture_fill的实现
简介 avpicture_fill函数将ptr指向的数据填充到picture内,但并没有拷贝,只是将picture结构内的data指针指向了ptr的数据.其实现如下: avpiture_fill av ...
- RPM包定制
概述 问题:当领导给你 100 台已经安装好系统的服务器,然后让优化,让你提出一个快速部署方案.解答: 1.tar 打包 先编译安装 打包-->分发-->解包(比如 mysql 打包后直接 ...
- python 文件下载
为了演示urllib3的使用,我们这里将会从一个网站下载两个文件.首先,需要导入urllib3库: import urllib3 这两个文件的源url为: url1 = 'http://earthqu ...
- mpvue——引入antv-F2图表
踩坑中~ 官方文档 https://www.yuque.com/antv/f2/intro 毕竟不像echarts接触过,所以还是先看看文档较好 github https://github.com/s ...
- kibana get 查询失效
kibana版本:5.4 在使用kibana 查询数据时,如果我们根据数据id 来获得一条数据,写法 get 索引名称/类型名称/文档主键 如:get testindex/testtype/01 这样 ...
- Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
题目大意: 给一些没安排权值的边和安排了权值的边,没被安排的边全要被选入最小生成树,问你最大能把它们的权值和安排成多少.题目分析:假设建好了树,那么树边与剩下的每一条边都能构成一个环,并且非树边的权值 ...
- Scratch 简单的小游戏 --- 碰碰球
Scratch 简单的小游戏 --- 碰碰球 ================================ 积木脚本块的简要分类: 1. 角色 2. 背景 3. 角色和背景组成的场景 4. 挡板角 ...