12tqian's Competitive Programming Library

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

View the Project on GitHub 12tqian/cp-library

:heavy_check_mark: library/set-function/walsh-hadamard-transform.hpp

Verified with

Code

#pragma once

template <class T> 
void walsh_hadamard_transformation(std::vector<T>& f, bool inverse = false) {
	int n = (int)f.size();
	for (int i = 1; i < n; i <<= 1) {
		for (int j = 0; j < n; ++j) {
			if ((j & i) == 0) {
				T x = f[j], y = f[j | i];
				f[j] = x + y;
				f[j | i] = x - y;
			}
		}
	}
	if (inverse) {
		if constexpr(std::is_integral<T>::value) {
			for (auto& x : f) x /= n;
		} else {
			T divide = T(1) / T(f.size());
			for (auto& x : f) x *= divide;
		}
	}
}
template <class T> 
void walsh_hadamard_transformation(std::vector<T>& f, bool inverse = false) {
	int n = (int)f.size();
	for (int i = 1; i < n; i <<= 1) {
		for (int j = 0; j < n; ++j) {
			if ((j & i) == 0) {
				T x = f[j], y = f[j | i];
				f[j] = x + y;
				f[j | i] = x - y;
			}
		}
	}
	if (inverse) {
		if constexpr(std::is_integral<T>::value) {
			for (auto& x : f) x /= n;
		} else {
			T divide = T(1) / T(f.size());
			for (auto& x : f) x *= divide;
		}
	}
}
Back to top page