This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ebi-fly13/Library
#include "utility/hash.hpp"
ハッシュを簡単に計算するための構造体。内部では $\mod 2^{61} - 1$ で計算している。
ハッシュを $n$ 乗する
ランダムなハッシュを生成。
ランダムなハッシュを生成。各要素は $\mod 2^{61} - 1$ で原始根となる。
#pragma once #include <array> #include "../modint/modint61.hpp" #include "../utility/random_number_generator.hpp" namespace ebi { template <int BASE_NUM = 2> struct Hash : std::array<modint61, BASE_NUM> { private: using std::array<modint61, BASE_NUM>::array; using std::array<modint61, BASE_NUM>::operator=; public: Hash() : std::array<modint61, BASE_NUM>() {} constexpr static Hash set(const modint61 &a) { Hash res; std::fill(res.begin(), res.end(), a); return res; } constexpr Hash &operator+=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs[i]; } return *this; } constexpr Hash &operator-=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs[i]; } return *this; } constexpr Hash &operator*=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs[i]; } return *this; } constexpr Hash &operator+=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs; } return *this; } constexpr Hash &operator-=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs; } return *this; } constexpr Hash &operator*=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs; } return *this; } Hash operator+(const Hash &rhs) const { return Hash(*this) += rhs; } Hash operator-(const Hash &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const Hash &rhs) const { return Hash(*this) *= rhs; } Hash operator+(const modint61 &rhs) const { return Hash(*this) += rhs; } Hash operator-(const modint61 &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const modint61 &rhs) const { return Hash(*this) *= rhs; } Hash pow(long long n) const { Hash a = *this, res = set(1); while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; } static Hash get_basis() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1; } return h; } Hash inv() const { Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = (*this)[i].inv(); } return h; } static Hash get_basis_primitive() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { while (!is_primitive( (h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1) .val())) ; } return h; } private: static bool is_primitive(long long x) { for (long long d : {2, 3, 5, 7, 11, 13, 31, 41, 61, 151, 331, 1321}) { if (modint61(x).pow((modint61::mod() - 1) / d).val() <= 1) return false; } return true; } }; } // namespace ebi
#line 2 "utility/hash.hpp" #include <array> #line 2 "modint/modint61.hpp" #include <cassert> #include <cstdint> #include <iostream> #line 2 "modint/base.hpp" #include <concepts> #line 5 "modint/base.hpp" #include <utility> namespace ebi { template <class T> concept Modint = requires(T a, T b) { a + b; a - b; a * b; a / b; a.inv(); a.val(); a.pow(std::declval<long long>()); T::mod(); }; template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) { long long x; os >> x; a = x; return os; } template <Modint mint> std::ostream &operator<<(std::ostream &os, const mint &a) { return os << a.val(); } } // namespace ebi #line 8 "modint/modint61.hpp" namespace ebi { struct modint61 { private: using mint = modint61; using u64 = std::uint64_t; constexpr static u64 m = (1ull << 61) - 1; constexpr static u64 MASK31 = (1ull << 31) - 1; constexpr static u64 MASK30 = (1ull << 30) - 1; public: constexpr static u64 mod() { return m; } constexpr modint61() : _v(0) {} constexpr modint61(long long v) { v %= (long long)umod(); if (v < 0) v += (long long)umod(); _v = u64(v); } constexpr u64 val() const { return _v; } constexpr u64 value() const { return val(); } constexpr mint &operator++() { _v++; if (_v == umod()) _v = 0; return *this; } constexpr mint &operator--() { if (_v == 0) _v = umod(); _v--; return *this; } constexpr mint &operator+=(const mint &rhs) { _v += rhs._v; _v = safe_mod(_v); return *this; } constexpr mint &operator-=(const mint &rhs) { if (_v < rhs._v) _v += umod(); assert(_v >= rhs._v); _v -= rhs._v; return *this; } constexpr mint &operator*=(const mint &rhs) { u64 au = _v >> 31, ad = _v & MASK31; u64 bu = rhs._v >> 31, bd = rhs._v & MASK31; u64 mid = ad * bu + au * bd; u64 midu = mid >> 30; u64 midd = mid & MASK30; _v = (au * bu * 2 + midu + (midd << 31) + ad * bd); _v = safe_mod(_v); return *this; } constexpr mint &operator/=(const mint &rhs) { return *this *= rhs.inv(); } constexpr mint pow(long long n) const { assert(0 <= n); mint x = *this, res = 1; while (n) { if (n & 1) res *= x; x *= x; n >>= 1; } return res; } constexpr mint inv() const { assert(_v); return pow(umod() - 2); } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs.val() == rhs.val(); } friend bool operator!=(const mint &lhs, const mint &rhs) { return !(lhs == rhs); } friend bool operator<(const mint &lhs, const mint &rhs) { return lhs._v < rhs._v; } friend bool operator>(const mint &lhs, const mint &rhs) { return rhs < lhs; } private: u64 _v = 0; constexpr static u64 umod() { return m; } constexpr u64 safe_mod(const u64 &a) { u64 au = a >> 61; u64 ad = a & umod(); u64 res = au + ad; if (res >= umod()) res -= umod(); return res; } }; } // namespace ebi #line 2 "utility/random_number_generator.hpp" #line 4 "utility/random_number_generator.hpp" #include <random> namespace ebi { struct random_number_generator { random_number_generator(int seed = -1) { if (seed < 0) seed = rnd(); mt.seed(seed); } void set_seed(int seed) { mt.seed(seed); } template <class T> T get(T a, T b) { std::uniform_int_distribution<T> dist(a, b - 1); return dist(mt); } private: std::mt19937_64 mt; std::random_device rnd; }; } // namespace ebi #line 7 "utility/hash.hpp" namespace ebi { template <int BASE_NUM = 2> struct Hash : std::array<modint61, BASE_NUM> { private: using std::array<modint61, BASE_NUM>::array; using std::array<modint61, BASE_NUM>::operator=; public: Hash() : std::array<modint61, BASE_NUM>() {} constexpr static Hash set(const modint61 &a) { Hash res; std::fill(res.begin(), res.end(), a); return res; } constexpr Hash &operator+=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs[i]; } return *this; } constexpr Hash &operator-=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs[i]; } return *this; } constexpr Hash &operator*=(const Hash &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs[i]; } return *this; } constexpr Hash &operator+=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] += rhs; } return *this; } constexpr Hash &operator-=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] -= rhs; } return *this; } constexpr Hash &operator*=(const modint61 &rhs) { for (int i = 0; i < BASE_NUM; i++) { (*this)[i] *= rhs; } return *this; } Hash operator+(const Hash &rhs) const { return Hash(*this) += rhs; } Hash operator-(const Hash &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const Hash &rhs) const { return Hash(*this) *= rhs; } Hash operator+(const modint61 &rhs) const { return Hash(*this) += rhs; } Hash operator-(const modint61 &rhs) const { return Hash(*this) -= rhs; } Hash operator*(const modint61 &rhs) const { return Hash(*this) *= rhs; } Hash pow(long long n) const { Hash a = *this, res = set(1); while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; } static Hash get_basis() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1; } return h; } Hash inv() const { Hash h; for (int i = 0; i < BASE_NUM; i++) { h[i] = (*this)[i].inv(); } return h; } static Hash get_basis_primitive() { static random_number_generator rng; Hash h; for (int i = 0; i < BASE_NUM; i++) { while (!is_primitive( (h[i] = rng.get<std::uint64_t>(0, modint61::mod() - 1) + 1) .val())) ; } return h; } private: static bool is_primitive(long long x) { for (long long d : {2, 3, 5, 7, 11, 13, 31, 41, 61, 151, 331, 1321}) { if (modint61(x).pow((modint61::mod() - 1) / d).val() <= 1) return false; } return true; } }; } // namespace ebi