Library

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

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: Hadamard Transform
(set_function/hadamard_transform.hpp)

説明

アダマール変換を行う。 $O(N\log N)$

Required by

Verified with

Code

#pragma once

#include <vector>

namespace ebi {

template <class T>
std::vector<T> hadamard_transform(std::vector<T> f, bool inverse = false) {
    int n = f.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; j++) {
            if ((i & j) == 0) {
                T x = f[j], y = f[j | i];
                f[j] = x + y;
                f[j | i] = x - y;
            }
        }
    }
    if (inverse) {
        for (auto& x : f) {
            x /= T(n);
        }
    }
    return f;
}

}  // namespace ebi
#line 2 "set_function/hadamard_transform.hpp"

#include <vector>

namespace ebi {

template <class T>
std::vector<T> hadamard_transform(std::vector<T> f, bool inverse = false) {
    int n = f.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; j++) {
            if ((i & j) == 0) {
                T x = f[j], y = f[j | i];
                f[j] = x + y;
                f[j | i] = x - y;
            }
        }
    }
    if (inverse) {
        for (auto& x : f) {
            x /= T(n);
        }
    }
    return f;
}

}  // namespace ebi
Back to top page