FFT板子
Posted mrclr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FFT板子相关的知识,希望对你有一定的参考价值。
这里只是贴一下FFT的板子,因为现在我学的还不是特别的懂,写出来可能自己都看不懂……
板子:luogu P3803 【模板】多项式乘法(FFT)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<complex>
using namespace std;
#define enter puts("")
#define space putchar(‘ ‘)
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
typedef complex<db> cp;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e6 + 5;
const db PI = acos(-1);
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ‘ ‘;
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - ‘0‘, ch = getchar();
if(last == ‘-‘) ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar(‘-‘);
if(x >= 10) write(x / 10);
putchar(x % 10 + ‘0‘);
}
int n, m, len = 1;
cp a[maxn << 1], b[maxn << 1], omg[maxn << 1], inv[maxn << 1];
void init()
{
for(int i = 0; i < len; ++i)
{
omg[i] = cp(cos(2 * PI * i / len), sin(2 * PI * i / len));
inv[i] = conj(omg[i]); //逆元
}
}
void fft(cp* a, cp* omg)
{
int lim = 0;
while((1 << lim) < len) lim++;
for(int i = 0; i < len; ++i) //得到最终序列
{
int t = 0;
//枚举二进制中的每一位,并翻转
for(int j = 0; j < lim; ++j) if((i >> j) & 1) t |= (1 << (lim - j - 1));
if(i < t) swap(a[i], a[t]); //这个if,防止翻过来又翻回去
}
for(int l = 2; l <= len; l <<= 1) //枚举每一层
{
int q = l >> 1;
for(cp* p = a; p != a + len; p += l) //枚举每一层的起始位置
for(int i = 0; i < q; ++i)
{
cp t = omg[len / l * i] * p[i + q]; //蝴 蝶 操 作……
p[i + q] = p[i] - t, p[i] += t;
}
}
}
int main()
{
n = read(); m = read(); db tp;
for(int i = 0; i <= n; ++i) scanf("%lf", &tp), a[i].real(tp);
for(int i = 0; i <= m; ++i) scanf("%lf", &tp), b[i].real(tp);
while(len <= n + m + 2) len <<= 1;
init();
fft(a, omg); fft(b, omg); //把多项式a, b转换成点值表示
for(int i = 0; i <= len; ++i) a[i] *= b[i]; //点值O(n)相乘得到结果
fft(a, inv); //转换成系数表示
for(int i = 0; i <= n + m; ++i) write((int)(a[i].real() / len + 0.5)), space; enter;
return 0;
}
luogu P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<complex>
using namespace std;
#define enter puts("")
#define space putchar(‘ ‘)
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
typedef complex<db> cp;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 6e4 + 5;
const db PI = acos(-1);
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ‘ ‘;
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - ‘0‘, ch = getchar();
if(last == ‘-‘) ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar(‘-‘);
if(x >= 10) write(x / 10);
putchar(x % 10 + ‘0‘);
}
int n, len = 1, c[maxn << 2];
char a1[maxn], b1[maxn];
cp a[maxn << 2], b[maxn << 2], omg[maxn << 2], inv[maxn << 2];
void init()
{
for(int i = 0; i < len; ++i)
{
omg[i] = cp(cos(2 * PI * i / len), sin(2 * PI * i / len));
inv[i] = conj(omg[i]);
}
}
void fft(cp* a, cp* omg)
{
int lim = 0; while((1 << lim) < len) lim++;
for(int i = 0; i < len; ++i)
{
int t = 0;
for(int j = 0; j < lim; ++j) if((i >> j) & 1) t |= (1 << (lim - j - 1));
if(i < t) swap(a[i], a[t]);
}
for(int l = 2; l <= len; l <<= 1)
{
int q = l >> 1;
for(cp* p = a; p != a + len; p += l)
for(int i = 0; i < q; ++i)
{
cp t = omg[len / l * i] * p[i + q];
p[i + q] = p[i] - t, p[i] += t;
}
}
}
int main()
{
n = read(); scanf("%s%s", a1, b1);
for(int i = 0; i < n; ++i) a[i].real(a1[n - i - 1] - ‘0‘);
for(int i = 0; i < n; ++i) b[i].real(b1[n - i - 1] - ‘0‘);
while(len < (n << 1)) len <<= 1;
init();
fft(a, omg); fft(b, omg);
for(int i = 0; i < len; ++i) a[i] *= b[i];
fft(a, inv);
for(int i = 0; i < len; ++i)
{
c[i] += (int)(a[i].real() / len + 0.5);
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
int x = len - 1;
while(x && !c[x]) x--;
for(int i = x; i >= 0; --i) write(c[i]); enter;
return 0;
}
以上是关于FFT板子的主要内容,如果未能解决你的问题,请参考以下文章