compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:heavy_check_mark: test/mytest/math/transforms-api.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../math/convolution/fft.hpp"
#include "../../../math/convolution/ntt.hpp"


int main() {
    using C = fft::Complex<long double>;
    auto close = [](C actual, complex<long double> expected) { assert(abs(complex<long double>(actual.x, actual.y) - expected) < 1e-9L); };
    C zero; close(zero, {0, 0}); C a(3, -2), b(-1, 4); complex<long double> x(3, -2), y(-1, 4);
    close(-a, -x); close(a + b, x + y); close(a - b, x - y); close(a * b, x * y); close(a / b, x / y);
    C c = a; assert(&(c += b) == &c); close(c, x + y);
    c = a; assert(&(c -= b) == &c); close(c, x - y);
    c = a; assert(&(c *= b) == &c); close(c, x * y);
    c = a; assert(&(c /= b) == &c); close(c, x / y);
    using mint = ModInt<998244353>;
    ntt::ntt_info<mint> info; assert(ntt::exp_limit(998244353) == 23 && info.limit == 23);
    for(int k = 0; k <= info.limit; ++k) {
        assert(info.root[k] * info.iroot[k] == mint(1));
        assert(info.root[k].pow(1LL << k) == mint(1));
        if(k) assert(info.root[k].pow(1LL << (k - 1)) != mint(1));
    }
    for(int width = 0; width <= 8; ++width) for(int value = 0; value < (1 << width); ++value) {
        int expected = 0; for(int i = 0; i < width; ++i) expected |= (value >> i & 1) << (width - i - 1);
        assert(fft::revbit(value, width) == expected && ntt::revbit(value, width) == unsigned(expected));
        if(value == 0) assert(ntt::revbit(value) == 0);
        else { int bits = 32 - __builtin_clz(value); assert(ntt::revbit(value) == unsigned(fft::revbit(value, bits))); }
    }
    assert(ntt::revbit(1ULL, 64) == (1ULL << 63));
    for(int width = 0; width <= 5; ++width) {
        int n = 1 << width; vector<C> values(n); vector<mint> modular(n);
        for(int i = 0; i < n; ++i) { values[i] = C(i % 7 - 3, 0); modular[i] = i % 7 - 3; }
        auto original = values; auto original_mod = modular;
        fft::fft(values, width); ntt::ntt(modular);
        for(int k = 0; k < n; ++k) {
            complex<long double> expected(0, 0); mint mod_expected = 0;
            for(int i = 0; i < n; ++i) {
                auto angle = 2 * acos(-1.0L) * i * k / n;
                expected += original[i].x * polar(1.0L, angle);
                mod_expected += original_mod[i] * info.root[width].pow(i * k);
            }
            close(values[k], expected); assert(modular[k] == mod_expected);
            close(fft::omega<long double>(k, n), polar(1.0L, 2 * acos(-1.0L) * k / n));
        }
        fft::ifft(values, width); ntt::intt(modular);
        for(int i = 0; i < n; ++i) { close(values[i], {original[i].x, 0}); assert(modular[i] == original_mod[i]); }
    }
    INT(a0, b0); print(a0 + b0);
}
#line 1 "test/mytest/math/transforms-api.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
    // https://maspypy.github.io/library/other/io_old.hpp
    struct has_val_impl {
        template <class T>
        static auto check(T &&x) -> decltype(x.val(), std::true_type{});

        template <class T> static auto check(...) -> std::false_type;
    };

    template <class T>
    class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
    };

    // debug
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void dump(const T &t) {
        cerr << t.val();
    }
    void dump(__int128_t n) {
        if(n == 0) {
            cerr << '0';
            return;
        } else if(n < 0) {
            cerr << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void dump(const string &s) { cerr << s; }
    void dump(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cerr << s[i];
    }
    template <class T1, class T2> void dump(const pair<T1, T2> &p) {
        cerr << '(';
        dump(p.first);
        cerr << ',';
        dump(p.second);
        cerr << ')';
    }
    template <class T> void dump(const vector<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const set<T> &s) {
        cerr << '{';
        for(auto it = s.begin(); it != s.end(); it++) {
            dump(*it);
            if(next(it) != s.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value> void dump(const map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value>
    void dump(const unordered_map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const deque<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(queue<T> q) {
        cerr << '{';
        while(!q.empty()) {
            dump(q.front());
            if((int)q.size() > 1) cerr << ',';
            q.pop();
        }
        cerr << '}';
    }

    void debug_print() { cerr << endl; }
    template <class Head, class... Tail>
    void debug_print(const Head &h, const Tail &...t) {
        dump(h);
        if(sizeof...(Tail)) dump(' ');
        debug_print(t...);
    }
    // print
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void print_single(const T t) {
        cout << t.val();
    }
    void print_single(__int128_t n) {
        if(n == 0) {
            cout << '0';
            return;
        } else if(n < 0) {
            cout << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cout << s;
    }
    void print_single(const string &s) { cout << s; }
    void print_single(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cout << s[i];
    }
    template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
        print_single(p.first);
        cout << ' ';
        print_single(p.second);
    }
    template <class T> void print_single(const vector<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(const set<T> &s) {
        for(auto it = s.begin(); it != s.end(); it++) {
            print_single(*it);
            if(next(it) != s.end()) cout << ' ';
        }
    }
    template <class T> void print_single(const deque<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(queue<T> q) {
        while(!q.empty()) {
            print_single(q.front());
            if((int)q.size() > 1) cout << ' ';
            q.pop();
        }
    }

    void print() { cout << '\n'; }
    template <class Head, class... Tail>
    void print(const Head &h, const Tail &...t) {
        print_single(h);
        if(sizeof...(Tail)) print_single(' ');
        print(t...);
    }

    // input
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void input_single(T &t) {
        cin >> t;
    }
    void input_single(__int128_t &n) {
        string s;
        cin >> s;
        if(s == "0") {
            n = 0;
            return;
        }
        bool is_minus = false;
        if(s[0] == '-') {
            s = s.substr(1);
            is_minus = true;
        }
        n = 0;
        for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
        if(is_minus) n = -n;
    }
    void input_single(string &s) { cin >> s; }
    template <class T1, class T2> void input_single(pair<T1, T2> &p) {
        input_single(p.first);
        input_single(p.second);
    }
    template <class T> void input_single(vector<T> &v) {
        for(auto &e : v) input_single(e);
    }
    void input() {}
    template <class Head, class... Tail> void input(Head &h, Tail &...t) {
        input_single(h);
        input(t...);
    }
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    input(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    input(__VA_ARGS__)
#define DBL(...)                                                               \
    double __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define LD(...)                                                                \
    long double __VA_ARGS__;                                                   \
    input(__VA_ARGS__)
#define UINT(...)                                                              \
    unsigned int __VA_ARGS__;                                                  \
    input(__VA_ARGS__)
#define ULL(...)                                                               \
    unsigned long long __VA_ARGS__;                                            \
    input(__VA_ARGS__)
#define VEC(name, type, len)                                                   \
    vector<type> name(len);                                                    \
    input(name);
#define VEC2(name, type, len1, len2)                                           \
    vector name(len1, vector<type>(len2));                                     \
    input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v)                                                              \
    sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
    return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
    return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
    if constexpr(dim == 1) {
        return vector(sizes[0], e);
    } else {
        int n = sizes[dim - 1];
        sizes.pop_back();
        return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
    }
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
    vector<int> s(dim);
    for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
    return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), start);
    return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
    auto ord = iota_gen((int)v.size());
    sort(ALL(ord), [&](int i, int j) {
        if(greater) return v[i] > v[j];
        return v[i] < v[j];
    });
    return ord;
}
// #endregion Macros
#line 2 "math/convolution/fft.hpp"

#line 7 "math/convolution/fft.hpp"

namespace fft {
    template <typename D> struct Complex {
        D x, y;
        Complex(): x(0), y(0) {};
        Complex(D x, D y) : x(x), y(y) {}
        Complex &operator+=(const Complex &c) {
            x += c.x;
            y += c.y;
            return (*this);
        }
        Complex &operator-=(const Complex &c) {
            x -= c.x;
            y -= c.y;
            return (*this);
        }
        Complex &operator*=(const Complex &c) {
            D nx = x * c.x - y * c.y;
            D ny = x * c.y + y * c.x;
            x = nx, y = ny;
            return (*this);
        }
        Complex &operator/=(const Complex& c) {
            D nx = (x * c.x + y * c.y) / (c.x * c.x + c.y * c.y);
            D ny = (y * c.x - x * c.y) / (c.x * c.x + c.y * c.y);
            x = nx, y = ny;
            return (*this);
        }
        Complex operator-() const { return Complex(-x, -y); }
        Complex operator+(const Complex &c) const { return Complex(*this) += c; }
        Complex operator-(const Complex &c) const { return Complex(*this) -= c; }
        Complex operator*(const Complex &c) const { return Complex(*this) *= c; }
        Complex operator/(const Complex &c) const { return Complex(*this) /= c; }
    };

    template<typename D>
    constexpr const D PI = std::acos(D(-1));

    template<typename D>
    inline Complex<D> omega(int k, int n) {
        return Complex<D>(std::cos(D(k) * 2 * PI<D> / n), std::sin(D(k) * 2 * PI<D> / n));
    }

    inline int revbit(int mask, int bitlen) {
        int res = 0;
        while(bitlen--) {
            res = (res << 1) | (mask & 1);
            mask >>= 1;
        }
        return res;
    }

    template<typename D>
    void fft(std::vector<Complex<D>>& a, int bitlen) {
        int n = (int)a.size();
        int len = n;
        while(len > 1) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    auto l = a[p] + a[p + t];
                    auto r = (a[p] - a[p + t]) * omega<D>(j, len);
                    a[p] = l;
                    a[p + t] = r;
                }
            }
            len >>= 1;
        }
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
    }

    template<typename D>
    void ifft(std::vector<Complex<D>>& a, int bitlen) {
        int n = (int)a.size();
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
        int len = 2;
        while(len <= n) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    auto l = a[p] + a[p + t] * omega<D>(-j, len);
                    auto r = a[p] - a[p + t] * omega<D>(-j, len);
                    a[p] = l;
                    a[p + t] = r;
                }
            }
            len <<= 1;
        }
        for(int i = 0; i < n; i++) a[i] /= Complex<D>(n, 0);
    }

    template<typename D>
    std::vector<D> convolution(const std::vector<D>& a, const std::vector<D>& b) {
        if(a.empty() || b.empty()) return {};
        int m = (int)a.size() + (int)b.size() - 1;
        int n = 1, bitlen = 0;
        while(n < m) {
            n <<= 1;
            bitlen++;
        }
        std::vector<Complex<D>> A(n), B(n);
        for(int i = 0; i < (int)a.size(); i++) A[i] = Complex<D>(a[i], 0);
        for(int i = 0; i < (int)b.size(); i++) B[i] = Complex<D>(b[i], 0);
        fft<D>(A, bitlen);
        fft<D>(B, bitlen);
        for(int i = 0; i < n; i++) A[i] *= B[i];
        ifft<D>(A, bitlen);
        std::vector<D> res(m);
        for(int i = 0; i < m; i++) res[i] = A[i].x;
        return res;
    } 
}; // namespace fft
#line 2 "math/convolution/ntt.hpp"

#line 8 "math/convolution/ntt.hpp"

#line 2 "modint/modint.hpp"

#line 6 "modint/modint.hpp"

template <int mod> class ModInt {
  public:
    ModInt() : x(0) {}
    ModInt(long long y)
        : x(y >= 0 ? y % umod() : (umod() - (-y) % umod()) % umod()) {}
    unsigned int val() const { return x; }
    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += umod() - p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (unsigned int)(1ULL * x * p.x % umod());
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inv();
        return *this;
    }
    ModInt operator-() const { return ModInt(-(long long)x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
    ModInt inv() const {
        long long a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return ModInt(u);
    }
    ModInt pow(unsigned long long n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {
        return os << p.x;
    }
    friend std::istream &operator>>(std::istream &is, ModInt &a) {
        long long t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }
    static constexpr int get_mod() { return mod; }

  private:
    unsigned int x;
    static constexpr unsigned int umod() { return mod; }
};
#line 2 "math/primitive-root.hpp"

#line 2 "math/pow_mod.hpp"

constexpr long long pow_mod(long long x, long long k, long long m) {
    long long res = 1 % m;
    long long mul = (x >= 0 ? x % m : x % m + m);
    while(k) {
        if(k & 1) res = (__int128_t)res * mul % m;
        mul = (__int128_t)mul * mul % m;
        k >>= 1;
    }
    return res;
}
#line 4 "math/primitive-root.hpp"

constexpr int primitive_root(int p) {
    if(p == 2) return 1;
    if(p == 998244353) return 3;
    int primes[31] = {};
    int sz = 0, t = p - 1;
    for(int i = 2; i * i <= t; i++) {
        if(t % i == 0) {
            primes[sz++] = i;
            while(t % i == 0) t /= i;
        }
    }
    if(t > 1) primes[sz++] = t;
    for(int g = 2;;g++) {
        bool f = true;
        for(int i = 0; i < sz; i++) {
            if(pow_mod(g, (p - 1) / primes[i], p) == 1) {
                f = false;
                break;
            }
        }   
        if(f) return g;
    }
}
#line 11 "math/convolution/ntt.hpp"

namespace ntt {
    constexpr int exp_limit(int mod) { return __builtin_ctz(mod - 1); }

    template <class mint, int mod = mint::get_mod(),
              int g = primitive_root(mod)>
    struct ntt_info {
        static constexpr int limit = exp_limit(mod);
        std::array<mint, limit + 1> root;
        std::array<mint, limit + 1> iroot;

        ntt_info() {
            root[limit] = mint(g).pow((mod - 1) >> limit);
            iroot[limit] = root[limit].inv();
            for(int i = limit - 1; i >= 0; i--) {
                root[i] = root[i + 1] * root[i + 1];
                iroot[i] = iroot[i + 1] * iroot[i + 1];
            }
        }
    };

    inline uint64_t revbit(uint64_t mask, int bitlen) {
        assert(0 <= bitlen && bitlen <= 64);
        if(bitlen == 0) return 0;
        uint64_t x = mask;
        x = (x >> 32) | (x << 32);
        x = ((x >> 16) & 0x0000FFFF0000FFFF) | ((x << 16) & 0xFFFF0000FFFF0000);
        x = ((x >> 8) & 0x00FF00FF00FF00FF) | ((x << 8) & 0xFF00FF00FF00FF00);
        x = ((x >> 4) & 0x0F0F0F0F0F0F0F0F) | ((x << 4) & 0xF0F0F0F0F0F0F0F0);
        x = ((x >> 2) & 0x3333333333333333) | ((x << 2) & 0xCCCCCCCCCCCCCCCC);
        x = ((x >> 1) & 0x5555555555555555) | ((x << 1) & 0xAAAAAAAAAAAAAAAA);
        return (x >> (64 - bitlen));
    }

    inline uint64_t revbit(uint64_t mask) {
        return mask == 0 ? 0 : revbit(mask, 64 - __builtin_clzll(mask));
    }

    template<class mint>
    void ntt(std::vector<mint>& a) {
        int n = (int)a.size();
        assert(n > 0 && (n & (n - 1)) == 0);
        int bitlen = __builtin_ctz(n);
        static const ntt_info<mint> info;
        assert(bitlen <= info.limit);
        for(int len = n, b = bitlen; len > 1; len >>= 1, b--) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                mint wj = 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    mint l = a[p] + a[p + t];
                    mint r = (a[p] - a[p + t]) * wj;
                    a[p] = l, a[p + t] = r;
                    wj *= info.root[b];
                }
            }
        }
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
    }

    template<class mint>
    void intt(std::vector<mint>& a) {
        int n = (int)a.size();
        assert(n > 0 && (n & (n - 1)) == 0);
        int bitlen = __builtin_ctz(n);
        static const ntt_info<mint> info;
        assert(bitlen <= info.limit);
        for(int i = 0; i < n; i++) {
            int j = revbit(i, bitlen);
            if(i < j) std::swap(a[i], a[j]);
        }
        for(int len = 2, b = 1; len <= n; len <<= 1, b++) {
            for(int i = 0; i < n; i += len) {
                int t = len >> 1;
                mint wj = 1;
                for(int j = 0; j < t; j++) {
                    int p = i + j;
                    auto l = a[p] + a[p + t] * wj;
                    auto r = a[p] - a[p + t] * wj;
                    a[p] = l, a[p + t] = r;
                    wj *= info.iroot[b];
                }
            }
        }
        mint invn = mint(n).inv();
        for(int i = 0; i < n; i++) a[i] *= invn;
    }

    template<class mint>
    std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
        if(a.empty() || b.empty()) return {};
        int m = (int)a.size() + (int)b.size() - 1;
        int n = 1;
        while(n < m) n <<= 1;
        a.resize(n), b.resize(n);
        ntt<mint>(a);
        ntt<mint>(b);
        for(int i = 0; i < n; i++) a[i] *= b[i];
        intt<mint>(a);
        a.resize(m);
        return a;
    }
}; // namespace ntt
#line 5 "test/mytest/math/transforms-api.test.cpp"


int main() {
    using C = fft::Complex<long double>;
    auto close = [](C actual, complex<long double> expected) { assert(abs(complex<long double>(actual.x, actual.y) - expected) < 1e-9L); };
    C zero; close(zero, {0, 0}); C a(3, -2), b(-1, 4); complex<long double> x(3, -2), y(-1, 4);
    close(-a, -x); close(a + b, x + y); close(a - b, x - y); close(a * b, x * y); close(a / b, x / y);
    C c = a; assert(&(c += b) == &c); close(c, x + y);
    c = a; assert(&(c -= b) == &c); close(c, x - y);
    c = a; assert(&(c *= b) == &c); close(c, x * y);
    c = a; assert(&(c /= b) == &c); close(c, x / y);
    using mint = ModInt<998244353>;
    ntt::ntt_info<mint> info; assert(ntt::exp_limit(998244353) == 23 && info.limit == 23);
    for(int k = 0; k <= info.limit; ++k) {
        assert(info.root[k] * info.iroot[k] == mint(1));
        assert(info.root[k].pow(1LL << k) == mint(1));
        if(k) assert(info.root[k].pow(1LL << (k - 1)) != mint(1));
    }
    for(int width = 0; width <= 8; ++width) for(int value = 0; value < (1 << width); ++value) {
        int expected = 0; for(int i = 0; i < width; ++i) expected |= (value >> i & 1) << (width - i - 1);
        assert(fft::revbit(value, width) == expected && ntt::revbit(value, width) == unsigned(expected));
        if(value == 0) assert(ntt::revbit(value) == 0);
        else { int bits = 32 - __builtin_clz(value); assert(ntt::revbit(value) == unsigned(fft::revbit(value, bits))); }
    }
    assert(ntt::revbit(1ULL, 64) == (1ULL << 63));
    for(int width = 0; width <= 5; ++width) {
        int n = 1 << width; vector<C> values(n); vector<mint> modular(n);
        for(int i = 0; i < n; ++i) { values[i] = C(i % 7 - 3, 0); modular[i] = i % 7 - 3; }
        auto original = values; auto original_mod = modular;
        fft::fft(values, width); ntt::ntt(modular);
        for(int k = 0; k < n; ++k) {
            complex<long double> expected(0, 0); mint mod_expected = 0;
            for(int i = 0; i < n; ++i) {
                auto angle = 2 * acos(-1.0L) * i * k / n;
                expected += original[i].x * polar(1.0L, angle);
                mod_expected += original_mod[i] * info.root[width].pow(i * k);
            }
            close(values[k], expected); assert(modular[k] == mod_expected);
            close(fft::omega<long double>(k, n), polar(1.0L, 2 * acos(-1.0L) * k / n));
        }
        fft::ifft(values, width); ntt::intt(modular);
        for(int i = 0; i < n; ++i) { close(values[i], {original[i].x, 0}); assert(modular[i] == original_mod[i]); }
    }
    INT(a0, b0); print(a0 + b0);
}
Back to top page