FFT && NTT板子

贴板子啦……


FFT板子:luogu P3803 【模板】多项式乘法(FFT)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 4e6 + 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, lim = 0, rev[maxn];
struct Comp
{
  db x, y;
  In Comp operator + (const Comp& oth)const
  {
    return (Comp){x + oth.x, y + oth.y};
  }
  In Comp operator - (const Comp& oth)const
  {
    return (Comp){x - oth.x, y - oth.y};
  }
  In Comp operator * (const Comp& oth)const
  {
    return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
  }
  friend In void swap(Comp& a, Comp& b)
  {
    swap(a.x, b.x); swap(a.y, b.y);
  }
}a[maxn], b[maxn];

In void fft(Comp* a, int flg)
{
  for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
  for(int i = 1; i < len; i <<= 1)
    {
      Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
      for(int j = 0; j < len; j += (i << 1))
	{
	  Comp o = (Comp){1, 0};
	  for(int k = 0; k < i; ++k, o = o * omg)
	    {
	      Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
	      a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
	    }
	}
    }
}

int main()
{
  n = read(); m = read();
  for(int i = 0; i <= n; ++i) a[i].x = read();
  for(int i = 0; i <= m; ++i) b[i].x = read();
  while(len <= n + m) len <<= 1, ++lim;
  for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
  fft(a, 1); fft(b, 1);
  for(int i = 0; i < len; ++i) a[i] = a[i] * b[i];
  fft(a, -1);
  for(int i = 0; i <= n + m; ++i) write((int)(a[i].x / len + 0.5)), space; enter;
  return 0;
}

NTT板子:[luogu P3803 【模板】多项式乘法(FFT)](https://www.luogu.org/problemnew/show/P3803) ```c++ #include #include #include #include #include #include #include #include #include #include using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 4e6 + 5; const ll mod = 998244353; const ll G = 3; 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, lim = 0, rev[maxn];
ll a[maxn], b[maxn];

In ll quickpow(ll a, ll b)
{
ll ret = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ret = ret * a % mod;
return ret;
}

In void ntt(ll* a, int len, int flg)
{
for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
for(int i = 1; i < len; i <<= 1)
{
ll gn = quickpow(G, (mod - 1) / (i << 1));
for(int j = 0; j < len; j += (i << 1))
{
ll g = 1;
for(int k = 0; k < i; ++k, g = g * gn % mod)
{
ll tp1 = a[k + j], tp2 = g * a[k + j + i] % mod;
a[k + j] = (tp1 + tp2) % mod, a[k + j + i] = (tp1 - tp2 + mod) % mod;
}
}
}
if(flg == 1) return;
ll inv = quickpow(len, mod - 2); reverse(a + 1, a + len);
for(int i = 0; i < len; ++i) a[i] = a[i] * inv % mod;
}

int main()
{
n = read(), m = read();
for(int i = 0; i <= n; ++i) a[i] = read();
for(int i = 0; i <= m; ++i) b[i] = read();
while(len <= n + m) len <<= 1, ++lim;
for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
ntt(a, len, 1); ntt(b, len, 1);
for(int i = 0; i < len; ++i) a[i] *= b[i];
ntt(a, len, -1);
for(int i = 0; i <= n + m; ++i) write(a[i]), space; enter;
return 0;
}

</br>
高精fft这里走:[[CQOI2018]九连环 题解](https://www.cnblogs.com/mrclr/p/10376699.html)
原文地址:https://www.cnblogs.com/mrclr/p/10087471.html