跳到正文

个人记录

常熟大没优化的最劣解 DFT

OneLastNOIP约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年3月9日
收录
2026年3月9日
更新
2026年11月11日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

一种神秘 DFT 写法,没有任何用处。

考虑做 DFT 时,我们可以分单位根的次数的奇偶性来讨论:

设 F(x)=∑i=02k−1fixiF(x)=\sum_{i=0}^{2^k-1}f_ix^i,则

  • 对于 ω2k2i\omega_{2^k}^{2i},其必然同时也是 2k−12^{k-1} 次单位根,有 ω2k2i(j+2k−1)=ω2k2ij\omega_{2^k}^{2i(j + 2^{k-1})}=\omega_{2^k}^{2ij},也就是 F(ω2k2i)=G(ω2k2i)F(\omega_{2^k}^{2i}) = G(\omega_{2^k}^{2i}),其中 G(x)=∑i=02k−1−1(fi+fi+2k−1)xiG(x) = \sum_{i=0}^{2^{k-1}-1}(f_i + f_{i + 2^{k-1}})x^i,对 G(x)G(x) 做 DFT 即可。
  • 对于 ω2k2i+1\omega_{2^k}^{2i+1},我们有 ω2k2i(j+2k−1)=−ω2k2ij\omega_{2^k}^{2i(j + 2^{k-1})}=-\omega_{2^k}^{2ij},且对于任意的 ω2k(2i+1)j\omega_{2^k}^{(2i+1)j} 有 ω2k(2i+1)j=ω2k2ij×ω2kj\omega_{2^k}^{(2i+1)j} = \omega_{2^k}^{2ij} \times \omega_{2^k}^{j},所以 F(ω2k(2i+1))=H(ω2k2i)F(\omega_{2^k}^{(2i+1)}) = H(\omega_{2^k}^{2i}),H(x)=∑i=02k−1−1(fi−fi+2k−1)ω2k−1ixiH(x) = \sum_{i=0}^{2^{k-1}-1}(f_i - f_{i + 2^{k-1}}) \omega_{2^{k-1}}^i x^i,对 H(x)H(x) 做 DFT 即可。

至于 IFFT,考虑到已知 fif_i 与 fi+2k−1f_{i+2^{k-1}} 间的两个关系,直接解二元一次方程组即可。

于是我们就得到了常熟大没优化的最劣解 DFT,要卡常才能过板子。

注:这是乐子作者在晚自习发现自己不会 FFT 的时候自己推的,所以非常抽象。

C++
# include <bits/stdc++.h>
# define rep(i, a, b) for(int i = (a);i <= (b);i ++)
# define lop(i, a, b) for(int i = (a);i <  (b);i ++)
# define dwn(i, a, b) for(int i = (a);i >= (b);i --)
# define int64 long long
# define ___main signed main()
# define f64 double
# define f32 float
# define f128 long double
# define int128 __int128
# define i128 ((__int128)1)
# define int32 signed
# define un unsigned
# define int16 short
# define int8 char
# define ret return
# define inl inline
# define reg register
# define fn void
# define elif else if
# define pb push_back
# define pp pop_back
using std::cout;
using std::cin;
# define CASES() int T;cin >> T;while(T--)
# define FaILurEg(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#if defined(_MSC_VER)
#  define FORCE_INLINE __forceinline
#else
#  define FORCE_INLINE __attribute__((always_inline)) inline
#endif
namespace packin {
template<typename T>struct _Arr {T *arr;int size;
FORCE_INLINE T& operator [](int i) {ret arr[i];}};
template<typename T>FORCE_INLINE fn read(T &x) {cin >> x;}
template<typename T>FORCE_INLINE fn read(_Arr<T>a) {rep(i, 1, a.size) cin >> a.arr[i];}
template<typename T>FORCE_INLINE fn read(T *a, int siz) {rep(i, 1, siz) cin >> a[i];}
template<typename T, typename... Args>FORCE_INLINE void read(T &x, Args&... args) {read(x);read(args...);}
inline void print_impl(const char* s) {while (*s) {cout << *s++;}}
template<typename T, typename... Args>inline void print_impl(const char* s, const T& x, const Args&... args) {while (*s) {if (s[0] == '{' && s[1] == '}') {cout << x;print_impl(s + 2, args...);return;}cout << *s++;}}
template<typename... Args>inline void print(const char* s, const Args&... args) {print_impl(s, args...);}
inline void error_impl(const char*s){while(*s)std::cerr<<*s++;}template<class T,class...A>inline void error_impl(const char*s,const T&x,const A&...a){while(*s){if(s[0]=='{'&&s[1]=='}'){std::cerr<<x;error_impl(s+2,a...);return;}std::cerr<<*s++;}}template<class...A>inline void error(const char*s,const A&...a){error_impl(s,a...);}
}
template<typename T>FORCE_INLINE T min(T a, T b) {ret (a < b)?a:b;}
template<typename T>FORCE_INLINE T max(T a, T b) {ret (a > b)?a:b;}
int Q;
//typedef std::complex<double> cp;
struct cp {
   double r, i;
   cp() {};
   cp(double R, double I) {r=R, i = I;}
   FORCE_INLINE cp operator*(cp b) {
      cp x;
      x.r = b.r * r - b.i * i;
      x.i = b.r * i + b.i * r;
      ret x;
   }
   FORCE_INLINE cp operator*(double b) {
      ret cp(r * b, i * b);
   }
   FORCE_INLINE cp operator+(cp b) {
      ret cp(r + b.r,  i + b.i);
   }
   FORCE_INLINE cp operator-(cp b) {
      ret cp(r - b.r,  i - b.i);
   }

};
cp I(0, 1), N1(-1, 0), Two(2, 0);
inline void FFT(std::vector<cp>&F, int n) {
   //if(n >= 18) cout << n << "\n";
   if(n == 0) ret;
   if(n == 1) {
      F[0] = F[0] + F[1];
      F[1] = F[0] - Two * F[1];
      ret;
   }
   if(n == 2) {
      cp f0 = F[0], f1 = F[1], f2 = F[2], f3 = F[3];
      F[0] = f0 + f1 + f2 + f3;
      F[1] = f0 - f2 + I * f1 - I * f3;
      F[2] = f0 - f1 + f2 - f3;
      F[3] = f0 - f2 - I * f1 + I * f3;
      ret;
   }
   std::vector<cp> G;G.resize(1 << (n - 1));
   rep(i, 0, (1 << (n - 1)) - 1)
      G[i] = (F[i] + F[i + (1 << (n - 1))]);
   FFT(G, n - 1);
   std::vector<cp> GG;GG.resize(1 << (n - 1));
   rep(i, 0, (1 << (n - 1)) - 1){GG[i] = (G[i]);
      G[i] = ((F[i] + F[i + (1 << (n - 1))] * N1) * cp(cos(acos(-1) * i * 2 / (1 << n)), sin(acos(-1) * i * 2 / (1 << n))));
   }
   dwn(i, (1 << (n - 1)) - 1, 0) F[i * 2] = GG[i];
   GG.clear();
   FFT(G, n - 1);
   rep(i, 0, (1 << (n - 1)) - 1)
      F[i * 2 + 1] = G[i];
}
inline void IFFT(cp *F, int n) {
   if(n == 0) ret ;
   //for(auto x : F) cout << x << " ";cout << "\n";
   std::vector<cp> G;
   G.resize(1 << (n - 1));
   //Ans.resize(1 << n);
   rep(i, 0, (1 << n) - 1)
      if(i & 1) G[i / 2] = (F[i]);
      else F[i / 2] = (F[i]);
   rep(i, 0, (1 << (n - 1)) - 1)
      F[i + (1 << (n - 1))] = G[i];
   G.clear();
   IFFT(F, n - 1);
   IFFT(F + (1 << (n - 1)), n - 1);
   rep(i, 0, (1 << (n - 1)) - 1) {
      cp G = F[i], H = F[i + (1 << (n - 1))] ;
      F[i + (1 << (n - 1))] =
         N1 *
         H *
         cp(cos(acos(-1) * (-i) * 2 / (1 << n)), sin(acos(-1) * (-i) * 2 / (1 << n)))
         + G,
      F[i + (1 << (n - 1))].r *= 0.5;
      F[i + (1 << (n - 1))].i *= 0.5;
      F[i] = G - F[i + (1 << (n - 1))];
   }
   //for(auto x : F) cout << x << " ";cout << "\n";
   // for(auto x : Ans) cout << x << " ";cout << "\n";
   //cout << "\n";
}
using namespace packin;
___main {
   std::ios::sync_with_stdio(0);
   cin.tie(0);
   int n,m;
   read(n, m);
   std::vector<cp> F, G, H;
   F.resize(1048576 *2), G.resize(1048576 *2);
   rep(i, 0, n)  read(F[i].r);
   rep(i, 0, m)  read(G[i].r);
   FFT(F, 21);FFT(G, 21);
   H.resize(1048576 *2);
   rep(i, 0, 1048576 * 2  - 1)
      H[i] = F[i] * G[i];
   IFFT(&H[0], 21);
   // rep(i, 0, n + m)
   //    cout << H[i] << " ";
   rep(i, 0, n + m)
      cout << (int)(H[i].r + 0.5) << " ";
   ret 0;}
原文