@@ -38,51 +38,54 @@ data:
3838 \ two for use with the FFT implementation in this file.\n */\n template <typename\
3939 \ T> void fft_change(std::vector<T> &a) {\n size_t n = a.size();\n std::vector<size_t>\
4040 \ rev(n);\n for (size_t i = 0; i < n; ++i) {\n rev[i] = rev[i >> 1] >> 1;\n \
41- \ if (i & 1)\n rev[i] |= n >> 1;\n if (i < rev[i])\n swap(a[i],\
42- \ a[rev[i]]);\n }\n }\n } // namespace weilycoder\n\n\n #line 8 \" weilycoder/poly/fft.hpp\" \
43- \n\n /**\n * @file fft.hpp\n * @brief Implementation of Fast Fourier Transform\
44- \ (FFT) and its inverse.\n */\n\n namespace weilycoder {\n /**\n * @brief In-place\
45- \ iterative Cooley\u2013 Tukey FFT / inverse FFT on a complex vector.\n * \
46- \ The length of the input vector should be a power of two.\n * @tparam on Direction\
47- \ of the transform: 1 for FFT, -1 for inverse FFT.\n * @tparam float_t Floating-point\
48- \ type for computations (default: double).\n * @param y Vector of complex numbers\
49- \ to be transformed in-place.\n */\n template <int32_t on = 1, typename float_t\
50- \ = double>\n void fft(std::vector<std::complex<float_t>> &y) {\n static_assert(on\
51- \ == 1 || on == -1, \" on must be 1 or -1\" );\n fft_change(y);\n for (size_t\
52- \ h = 2; h <= y.size(); h <<= 1) {\n std::complex<float_t> wn(cos(2 * PI<float_t>\
53- \ / h), sin(on * 2 * PI<float_t> / h));\n for (size_t j = 0; j < y.size();\
54- \ j += h) {\n std::complex<float_t> w(1, 0);\n for (size_t k = j; k\
55- \ < j + (h >> 1); ++k, w *= wn) {\n std::complex<float_t> u = y[k], t =\
56- \ w * y[k + (h >> 1)];\n y[k] = u + t, y[k + (h >> 1)] = u - t;\n \
57- \ }\n }\n }\n size_t len = y.size();\n if constexpr (on == -1)\n for\
58- \ (size_t i = 0; i < len; ++i)\n y[i] /= len;\n }\n } // namespace weilycoder\n \
59- \n\n #line 8 \" weilycoder/poly/fft_convolve.hpp\"\n\n /**\n * @file fft_convolve.hpp\n \
60- \ * @brief Functions for performing convolution using Fast Fourier Transform (FFT).\
61- \ \n */\n\n namespace weilycoder {\n /**\n * @brief Perform convolution of two\
62- \ complex vectors using FFT.\n * @tparam float_t Floating-point type for computations\
41+ \ if (i & 1)\n rev[i] |= n >> 1;\n if (i < rev[i])\n std::swap(a[i],\
42+ \ a[rev[i]]);\n }\n }\n } // namespace weilycoder\n\n\n #line 7 \" weilycoder/poly/fft.hpp\" \
43+ \n #include <stdexcept>\n #line 9 \" weilycoder/poly/fft.hpp\"\n\n /**\n * @file fft.hpp\n \
44+ \ * @brief Implementation of Fast Fourier Transform (FFT) and its inverse.\n */\n \
45+ \n namespace weilycoder {\n /**\n * @brief In-place iterative Cooley\u2013 Tukey\
46+ \ FFT / inverse FFT on a complex vector.\n * The length of the input vector\
47+ \ should be a power of two.\n * @tparam on Direction of the transform: 1 for FFT,\
48+ \ -1 for inverse FFT.\n * @tparam float_t Floating-point type for computations\
49+ \ (default: double).\n * @param y Vector of complex numbers to be transformed\
50+ \ in-place.\n */\n template <int32_t on = 1, typename float_t = double>\n void fft(std::vector<std::complex<float_t>>\
51+ \ &y) {\n static_assert(on == 1 || on == -1, \" on must be 1 or -1\" );\n fft_change(y);\n \
52+ \ size_t len = y.size();\n if (len == 0 || (len & (len - 1)) != 0)\n throw\
53+ \ std::invalid_argument(\" Length of input vector must be a power of two\" );\n \
54+ \ for (size_t h = 2; h <= len; h <<= 1) {\n std::complex<float_t> wn(cos(2\
55+ \ * PI<float_t> / h), sin(on * 2 * PI<float_t> / h));\n for (size_t j = 0;\
56+ \ j < len; j += h) {\n std::complex<float_t> w(1, 0);\n for (size_t\
57+ \ k = j; k < j + (h >> 1); ++k, w *= wn) {\n std::complex<float_t> u =\
58+ \ y[k], t = w * y[k + (h >> 1)];\n y[k] = u + t, y[k + (h >> 1)] = u -\
59+ \ t;\n }\n }\n }\n if constexpr (on == -1)\n for (size_t i = 0; i\
60+ \ < len; ++i)\n y[i] /= len;\n }\n } // namespace weilycoder\n\n\n #line 8 \" \
61+ weilycoder/poly/fft_convolve.hpp\"\n\n /**\n * @file fft_convolve.hpp\n * @brief\
62+ \ Functions for performing convolution using Fast Fourier Transform (FFT). \n \
63+ \ */\n\n namespace weilycoder {\n /**\n * @brief Perform convolution of two complex\
64+ \ vectors using FFT.\n * @tparam float_t Floating-point type for computations\
6365 \ (default: double).\n * @param a First input vector of complex numbers.\n * @param\
6466 \ b Second input vector of complex numbers.\n * @return Vector containing the\
6567 \ convolution result.\n */\n template <typename float_t = double>\n std::vector<std::complex<float_t>>\
6668 \ fft_convolve(std::vector<std::complex<float_t>> a,\n \
6769 \ std::vector<std::complex<float_t>> b) {\n size_t n\
6870 \ = 1;\n while (n < a.size() + b.size() - 1)\n n <<= 1;\n a.resize(n), b.resize(n);\n \
69- \ fft(a), fft(b);\n for (size_t i = 0; i < n; ++i)\n a[i] *= b[i];\n return\
70- \ fft<-1>(a), a;\n }\n\n /**\n * @brief Perform convolution of two real-valued vectors\
71- \ using FFT.\n * @tparam float_t Floating-point type for computations (default:\
72- \ double).\n * @param a First input vector of real numbers.\n * @param b Second\
73- \ input vector of real numbers.\n * @return Vector containing the convolution\
74- \ result.\n * @note This function uses a technique that combines two real sequences\
75- \ into\n * a single complex sequence to perform the convolution more efficiently.\n \
76- \ */\n template <typename float_t = double>\n std::vector<float_t> fft_convolve_real(const\
77- \ std::vector<float_t> &a,\n const std::vector<float_t>\
78- \ &b) {\n size_t n = 1;\n while (n < a.size() + b.size() - 1)\n n <<= 1;\n \
79- \ std::vector<std::complex<float_t>> F(n);\n for (size_t i = 0; i < a.size();\
80- \ ++i)\n F[i].real(a[i]);\n for (size_t i = 0; i < b.size(); ++i)\n F[i].imag(b[i]);\n \
81- \ fft(F);\n for (size_t i = 0; i < n; ++i)\n F[i] *= F[i];\n fft<-1>(F);\n \
82- \ std::vector<float_t> result(a.size() + b.size() - 1);\n for (size_t i = 0;\
83- \ i < result.size(); ++i)\n result[i] = F[i].imag() / 2;\n return result;\n \
84- }\n } // namespace weilycoder\n\n\n #line 4 \" test/multiplication_of_big_integers.fft.test.cpp\" \
85- \n #include <algorithm>\n #include <iostream>\n #include <string>\n #line 8 \" test/multiplication_of_big_integers.fft.test.cpp\" \
71+ \ fft(a), fft(b);\n for (size_t i = 0; i < n; ++i)\n a[i] *= b[i];\n fft<-1>(a),\
72+ \ a.resize(a.size() + b.size() - 1);\n return a;\n }\n\n /**\n * @brief Perform\
73+ \ convolution of two real-valued vectors using FFT.\n * @tparam float_t Floating-point\
74+ \ type for computations (default: double).\n * @param a First input vector of\
75+ \ real numbers.\n * @param b Second input vector of real numbers.\n * @return\
76+ \ Vector containing the convolution result.\n * @note This function uses a technique\
77+ \ that combines two real sequences into\n * a single complex sequence to\
78+ \ perform the convolution more efficiently.\n */\n template <typename float_t =\
79+ \ double>\n std::vector<float_t> fft_convolve_real(const std::vector<float_t> &a,\n \
80+ \ const std::vector<float_t> &b) {\n size_t\
81+ \ n = 1;\n while (n < a.size() + b.size() - 1)\n n <<= 1;\n std::vector<std::complex<float_t>>\
82+ \ F(n);\n for (size_t i = 0; i < a.size(); ++i)\n F[i].real(a[i]);\n for\
83+ \ (size_t i = 0; i < b.size(); ++i)\n F[i].imag(b[i]);\n fft(F);\n for (size_t\
84+ \ i = 0; i < n; ++i)\n F[i] *= F[i];\n fft<-1>(F);\n std::vector<float_t>\
85+ \ result(a.size() + b.size() - 1);\n for (size_t i = 0; i < result.size(); ++i)\n \
86+ \ result[i] = F[i].imag() / 2;\n return result;\n }\n } // namespace weilycoder\n \
87+ \n\n #line 4 \" test/multiplication_of_big_integers.fft.test.cpp\"\n #include <algorithm>\n \
88+ #include <iostream>\n #include <string>\n #line 8 \" test/multiplication_of_big_integers.fft.test.cpp\" \
8689 \n using namespace std;\n using namespace weilycoder;\n\n static void solve() {\n \
8790 \ string a, b;\n cin >> a >> b;\n\n reverse(a.begin(), a.end()), reverse(b.begin(),\
8891 \ b.end());\n\n bool negative = false;\n if (a.back() == '-')\n negative\
@@ -124,7 +127,7 @@ data:
124127 isVerificationFile : true
125128 path : test/multiplication_of_big_integers.fft.test.cpp
126129 requiredBy : []
127- timestamp : ' 2025-11-06 23:46:32 +08:00'
130+ timestamp : ' 2025-11-07 09:14:02 +08:00'
128131 verificationStatus : TEST_ACCEPTED
129132 verifiedWith : []
130133documentation_of : test/multiplication_of_big_integers.fft.test.cpp
0 commit comments