Library

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

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: Rolling Hash Monoid
(monoid/rolling_hash_monoid.hpp)

説明

ローリングハッシュのモノイド構造体。

Depends on

Required by

Verified with

Code

#pragma once

#include <utility>

#include "../modint/modint61.hpp"
#include "../utility/random_number_generator.hpp"

namespace ebi {

struct rolling_hash_monoid : std::pair<modint61, modint61> {
  private:
    using Self = rolling_hash_monoid;
    using std::pair<modint61, modint61>::pair;

  public:
    template <class T> rolling_hash_monoid(T x) {
        this->first = get_base();
        this->second = x;
    }

    modint61 get_hash() {
        return this->second;
    }

    Self &operator+=(const Self &rhs) {
        this->first = this->first * rhs.first;
        this->second = this->second * rhs.first + rhs.second;
        return *this;
    }

    friend Self operator+(const Self &lhs, const Self &rhs) {
        return Self(lhs) += rhs;
    }

    static Self e() {
        return {1, 0};
    }

    static modint61 get_base() {
        static modint61 base = 0;
        static random_number_generator rng;
        while (base == 0) {
            base = rng.get<std::uint64_t>(2, modint61::mod());
        }
        return base;
    }
};

}  // namespace ebi
#line 2 "monoid/rolling_hash_monoid.hpp"

#include <utility>

#line 2 "modint/modint61.hpp"

#include <cassert>
#include <cstdint>
#include <iostream>

#line 2 "modint/base.hpp"

#include <concepts>
#line 6 "modint/base.hpp"

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"

#include <algorithm>

#line 5 "utility/random_number_generator.hpp"
#include <numeric>

#include <random>

#include <vector>


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);
    }

    std::vector<int> get_permutation(int n) {
        std::vector<int> p(n);
        std::iota(p.begin(), p.end(), 0);
        std::shuffle(p.begin(), p.end(), mt);
        return p;
    }

  private:
    std::mt19937_64 mt;
    std::random_device rnd;
};

}  // namespace ebi
#line 7 "monoid/rolling_hash_monoid.hpp"

namespace ebi {

struct rolling_hash_monoid : std::pair<modint61, modint61> {
  private:
    using Self = rolling_hash_monoid;
    using std::pair<modint61, modint61>::pair;

  public:
    template <class T> rolling_hash_monoid(T x) {
        this->first = get_base();
        this->second = x;
    }

    modint61 get_hash() {
        return this->second;
    }

    Self &operator+=(const Self &rhs) {
        this->first = this->first * rhs.first;
        this->second = this->second * rhs.first + rhs.second;
        return *this;
    }

    friend Self operator+(const Self &lhs, const Self &rhs) {
        return Self(lhs) += rhs;
    }

    static Self e() {
        return {1, 0};
    }

    static modint61 get_base() {
        static modint61 base = 0;
        static random_number_generator rng;
        while (base == 0) {
            base = rng.get<std::uint64_t>(2, modint61::mod());
        }
        return base;
    }
};

}  // namespace ebi
Back to top page