FFT与NTT的模板
网上相关博客不少,这里给自己留个带点注释的模板,以后要是忘了作提醒用。
以洛谷3803多项式乘法裸题为例。
FFT:
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#define ri readint()
#define gc getchar() int readint() {
int x = , s = , c = gc;
while (c <= ) c = gc;
if (c == '-') s = -, c = gc;
for (; isdigit(c); c = gc) x = x * + c - ;
return x * s;
} const int maxn = * 1e6 + ;
const double PI = acos(-1.0); struct Complex {
double x, y;
Complex(double a = , double b = ):x(a), y(b){}
};
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); } Complex a[maxn], b[maxn];
int n, m;
int r[maxn], l, limit = ; void fft(Complex *A, int type) {
for (int i = ; i < limit; i++)
if (i < r[i])
std::swap(A[i], A[r[i]]);
//迭代方式模拟递归写法,需要理解递归是怎么做的才能看懂这个
for (int mid = ; mid < limit; mid <<= ) {
//本来单位根是2*PI/len,这里len替换成2*mid,2就约掉了
Complex Wn(cos(PI / mid), type * sin(PI / mid));
for (int R = mid << , j = ; j < limit; j += R) {
Complex w(, );//单位根的k次幂
for (int k = ; k < mid; k++, w = w * Wn) {
//蝴蝶变换
Complex x = A[j+k], y = w * A[j+k+mid];
A[j+k] = x + y;
A[j+k+mid] = x - y;
}
}
}
} int main() {
n = ri, m = ri;
for (int i = ; i <= n; i++)
a[i].x = ri;
for (int i = ; i <= m; i++)
b[i].x = ri; while (limit <= n + m) {//长度变为2^l
limit <<= ;
l++;
}
for (int i = ; i < limit; i++)//二进制镜像
r[i] = (r[i>>] >> ) | ((i&) << (l-));
fft(a, );
fft(b, );
for (int i = ; i < limit; i++)
a[i] = a[i] * b[i];
fft(a, -);
for (int i = ; i <= n + m; i++)
printf("%d ", (int)(a[i].x / limit + 0.5));
return ;
}
NTT是用模域取代了复数域,性质相同只是换了单位根,所以板子基本相同。我这两个相比NTT确实比FFT快一点的:
#include <bits/stdc++.h>
#define ll long long
#define ri readll()
#define gc getchar()
#define rep(i, a, b) for (int i = a; i <= b; i++)
using namespace std; const int P = , G = , Gi = , maxn = * 1e6 + ;
//P的原根为3,3%P的逆元为332748118
//原根意味着:3^(P-1) % P = 1,其中P-1是3%P的阶,本应是φ(P),这里恰好为大素数
ll n, m;
ll a[maxn], b[maxn];
int limit = , l, r[maxn]; ll readll() {
ll x = 0ll, s = 1ll;
char c = gc;
while (c <= ) c = gc;
if (c == '-') s = -1ll, c = gc;
for (; isdigit(c); c = gc) x = x * + c - ;
return x * s;
} ll ksm(ll a, ll b, int mod) {
ll res = 1ll;
for (; b; b >>= ) {
if (b & ) res = res * a % mod;
a = a * a % mod;
}
return res;
} void NTT(ll *A, int flag) {
rep(i, , limit)
if (i < r[i])
swap(A[i], A[r[i]]); for (int mid = ; mid < limit; mid <<= ) {
//如果是变换则单位根为3^[(P-1)/(len)] % P,逆变换则用逆元
ll Wn = ksm(flag ? G : Gi, (P-) / (mid*), P);
for (int R = mid << , j = ; j < limit; j += R) {
ll w = 1ll;
for (int k = ; k < mid; k++, w = w * Wn % P) {
ll x = A[j+k], y = A[j+k+mid] * w % P;
A[j+k] = (x + y) % P;
A[j+k+mid] = (x - y + P) % P;
}
}
}
} int main() {
n = ri, m = ri;
rep(i, , n) a[i] = (ri + P) % P;
rep(i, , m) b[i] = (ri + P) % P; while (limit < n + m + ) {
limit <<= ;
l++;
}
rep(i, , limit) r[i] = (r[i>>] >> ) | ((i & ) << (l - ));
NTT(a, ); NTT(b, );
rep(i, , limit) a[i] = a[i] * b[i] % P;
NTT(a, ); ll inv = ksm(limit, P - , P);//最后变换回来要乘长度的逆元
rep(i, , n + m) printf("%lld ", a[i] * inv % P); return ;
}
FFT与NTT的模板的更多相关文章
- 多项式乘法,FFT与NTT
多项式: 多项式?不会 多项式加法: 同类项系数相加: 多项式乘法: A*B=C $A=a_0x^0+a_1x^1+a_2x^2+...+a_ix^i+...+a_{n-1}x^{n-1}$ $B=b ...
- FFT,NTT 专题
学习傅里叶的基本性质及其代码,可以参考大神理解 还有 ACdream 的博客 贴一下NTT的模板: using namespace std; typedef long long ll; int n; ...
- FFT和NTT学习笔记_基础
FFT和NTT学习笔记 算法导论 参考(贺) http://picks.logdown.com/posts/177631-fast-fourier-transform https://blog.csd ...
- fft,ntt总结
一个套路:把式子推成卷积形式,然后用fft或ntt优化求解过程. fft的扩展性不强,不可以在fft函数里多加骚操作--DeepinC T1:多项式乘法 板子题 T2:快速傅立叶之二 另一个板子,小技 ...
- 多项式fft、ntt、fwt 总结
做了四五天的专题,但是并没有刷下多少题.可能一开始就对多项式这块十分困扰,很多细节理解不深. 最简单的形式就是直接两个多项式相乘,也就是多项式卷积,式子是$N^2$的.多项式算法的过程就是把卷积做一种 ...
- FFT与NTT专题
先不管旋转操作,考虑化简这个差异值 $$begin{aligned}sum_{i=1}^n(x_i-y_i-c)^2&=sum_{i=1}^n(x_i-y_i)^2+nc^2-2csum_{i ...
- 洛谷 - P3803 -【模板】多项式乘法(FFT) - NTT
https://www.luogu.org/problemnew/show/P3803 看别人偏偏就是要用NTT去过.实验证明大概是这样用.求0~n的多项式和0~m的多项式的乘积.注意MAXN取值.A ...
- luoguP4721 【模板】分治 FFT (分治NTT)
给定 $g[1....n-1]$,求 $f[0],f[1],...,f[n-1]$,其中 $f[i]=\sum_{j=1}^{i}f[i-j]g[j]$ 变界为 $f[0]=1$ 答案模 9 ...
- 卷积FFT、NTT、FWT
先简短几句话说说FFT.... 多项式可用系数和点值表示,n个点可确定一个次数小于n的多项式. 多项式乘积为 f(x)*g(x),显然若已知f(x), g(x)的点值,O(n)可求得多项式乘积的点值. ...
随机推荐
- HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂
题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...
- POJ1006 Biorhythms —— 中国剩余定理
题目链接:https://vjudge.net/problem/POJ-1006 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total ...
- hdu1198 Farm Irrigation —— dfs or 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: #include<cstdio>//hdu1198 dfs #includ ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...
- Linux档案属性
输入命令:ls -al 档案类型权限: 第一個字元代表这个档案是『目录.档案或链接档等等』: 当为[ d ]则是目录: 当为[ - ]则是目录: 若是[ l ]则表示为链接档(link file): ...
- 万亿级日志与行为数据存储查询技术剖析(续)——Tindex是改造的lucene和druid
五.Tindex 数果智能根据开源的方案自研了一套数据存储的解决方案,该方案的索引层通过改造Lucene实现,数据查询和索引写入框架通过扩展Druid实现.既保证了数据的实时性和指标自由定义的问题,又 ...
- Python: PS 滤镜--水波特效
本文用 Python 实现 PS 滤镜中的 水波特效 import numpy as np from skimage import img_as_float import matplotlib.pyp ...
- Add GNOME to a CentOS Minimal Install
by Jeff Hunter, Sr. Database Administrator Contents Introduction CentOS 6 About the Author Introduct ...
- POJ1236 Network of Schools (强连通分量,注意边界)
A number of schools are connected to a computer network. Agreements have been developed among those ...
- 登录加密 md5
实现账户和密码登录的加密 https://github.com/AndreasPizsa/md5-jkmyers