Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/unit_test/string/dynamic_rolling_hash.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../src/template/template.hpp"
#include "../../../src/random/random_number_generator.hpp"
#include "../../../src/template/modint_2_61m1.hpp"
#include "../../../src/string/dynamic_rolling_hash.hpp"
using mint = Modint_2_61m1;
void test() {
    int n = rng(1, 2000), q = 2000;
    string s = "";
    rep(i, 0, n) {
        s += 'a' + rng(0, 25);
    }
    ll base = rng(1ll << 10, 1ll << 60);
    DynamicRollingHash drh(s, base);
    while(q--) {
        int type = rng(0, 1);
        if(type == 0) {
            int len = rng(0, n);
            int l = rng(0, n - len), r = l + len;
            mint ans = 0, b = 1;
            rep(i, l, r) {
                ans += b * s[i];
                b *= base;
            }
            assert(drh.get(l, r) == ans.val());
        } else {
            int idx = rng(0, n - 1);
            char c = 'a' + rng(0, 25);
            s[idx] = c;
            drh.set(idx, c);
        }
    }
}
int main(void) {
    constexpr int test_num = 100;
    rep(_, 0, test_num) {
        test();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
#line 1 "verify/unit_test/string/dynamic_rolling_hash.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/random/random_number_generator.hpp"
struct RandomNumberGenerator {
    RandomNumberGenerator()
        : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
    template <typename T>
    inline T operator()(const T lower, const T upper) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(lower <= upper);
        if constexpr(is_integral_v<T>) {
            uniform_int_distribution<T> dist(lower, upper);
            return dist(mt);
        } else if constexpr(is_floating_point_v<T>) {
            uniform_real_distribution<T> dist(lower, upper);
            return dist(mt);
        }
    }
    template <typename T>
    inline vector<T> vec(const int n, const T lower, const T upper, const bool dup = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(lower <= upper);
        if(n == 0) return {};
        vector<T> res(n);
        if(dup or is_floating_point_v<T>) {
            for(int i = 0; i < n; ++i) res[i] = this->operator()(lower, upper);
        } else {
            assert(upper - lower + 1 >= n);
            if(upper - lower + 1 >= 2 * n) {
                set<T> used;
                while((int)used.size() < n) {
                    const T a = this->operator()(lower, upper);
                    used.insert(a);
                }
                int i = 0;
                for(const T a : used) {
                    res[i] = a;
                    ++i;
                }
            } else {
                const vector<int> p = perm(upper - lower + 1, false);
                for(int i = 0; i < n; ++i) {
                    res[i] = p[i] + lower;
                }
            }
        }
        return res;
    }
    inline vector<int> perm(const int n, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        vector<int> res(n);
        for(int i = 0; i < n; ++i) res[i] = i + one;
        for(int i = n - 1; i > 0; --i) {
            swap(res[i], res[this->operator()(0, i)]);
        }
        return res;
    }
    inline pair<vector<int>, vector<int>> tree(const int n, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        if(n <= 1) return {{}, {}};
        if(n == 2) return {{0 + one}, {1 + one}};
        vector<int> u(n - 1), v(n - 1);
        const vector<int> pruefer = vec(n - 2, 0, n - 1);
        set<int> st;
        vector<int> cnt(n);
        for(int i = 0; i < n; ++i) st.insert(i);
        auto add = [&](const int x) -> void {
            if(x > n) return;
            if(cnt[x] == 0) st.erase(x);
            ++cnt[x];
        };
        auto del = [&](const int x) -> void {
            if(x > n) return;
            --cnt[x];
            if(cnt[x] == 0) st.insert(x);
        };
        for(int i = 0; i < n - 2; ++i) add(pruefer[i]);
        for(int i = 0; i < n - 2; ++i) {
            const int a = *st.begin();
            const int b = pruefer[i];
            u[i] = a + one;
            v[i] = b + one;
            del(b);
            add(a);
        }
        const int a = *st.begin();
        add(a);
        const int b = *st.begin();
        u[n - 2] = a + one;
        v[n - 2] = b + one;
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_tree(const int n, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(lower <= upper);
        if(n == 0) return {{}, {}, {}};
        const auto [u, v] = tree(n, one);
        const vector<T> w = vec(n - 1, lower, upper, true);
        return {u, v, w};
    }
    inline pair<vector<int>, vector<int>> graph(const int n, const int m, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        assert(0 <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        vector<int> u, v;
        u.reserve(m);
        v.reserve(m);
        if(1ll * n * (n - 1) / 2 >= 2e6) {
            set<pair<int, int>> edge;
            while((int)edge.size() < m) {
                const int a = this->operator()(0, n - 1);
                const int b = this->operator()(0, n - 1);
                if(a >= b) continue;
                edge.insert({a, b});
            }
            for(const auto& [a, b] : edge) {
                u.push_back(a + one);
                v.push_back(b + one);
            }
        } else {
            vector<pair<int, int>> edge;
            edge.reserve(n * (n - 1) / 2);
            for(int i = 0; i < n; ++i) {
                for(int j = i + 1; j < n; ++j) {
                    edge.push_back({i, j});
                }
            }
            const vector<int> p = perm(n * (n - 1) / 2, false);
            for(int i = 0; i < m; ++i) {
                u.push_back(edge[p[i]].first + one);
                v.push_back(edge[p[i]].second + one);
            }
        }
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_graph(const int n, const int m, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(0 <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        assert(lower <= upper);
        const auto [u, v] = graph(n, m, one);
        const vector<T> w = vec(m, lower, upper, true);
        return {u, v, w};
    }
    inline pair<vector<int>, vector<int>> connected_graph(const int n, const int m, const bool one = true) {
        assert(0 <= n and n <= (int)1e8);
        assert(max(n - 1, 0) <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        if(n <= 1) return {{}, {}};
        vector<int> u, v;
        u.reserve(m);
        v.reserve(m);
        auto [ut, vt] = tree(n, false);
        if(1ll * n * (n - 1) / 2 >= 2e6) {
            set<pair<int, int>> edge;
            for(int i = 0; i < n - 1; ++i) {
                edge.insert({min(ut[i], vt[i]), max(ut[i], vt[i])});
            }
            while((int)edge.size() < m) {
                const int a = this->operator()(0, n - 1);
                const int b = this->operator()(0, n - 1);
                if(a >= b) continue;
                edge.insert({a, b});
            }
            for(const auto& [a, b] : edge) {
                u.push_back(a + one);
                v.push_back(b + one);
            }
        } else {
            set<pair<int, int>> used;
            for(int i = 0; i < n - 1; ++i) {
                u.push_back(ut[i] + one);
                v.push_back(vt[i] + one);
                used.insert({min(ut[i], vt[i]), max(ut[i], vt[i])});
            }
            vector<pair<int, int>> edge;
            edge.reserve(n * (n - 1) / 2 - (n - 1));
            for(int i = 0; i < n; ++i) {
                for(int j = i + 1; j < n; ++j) {
                    if(used.find({i, j}) == used.end()) edge.push_back({i, j});
                }
            }
            const vector<int> p = perm(n * (n - 1) / 2 - (n - 1), false);
            for(int i = 0; i < m - (n - 1); ++i) {
                u.push_back(edge[p[i]].first + one);
                v.push_back(edge[p[i]].second + one);
            }
        }
        return {u, v};
    }
    template <typename T>
    inline tuple<vector<int>, vector<int>, vector<T>> weighted_connected_graph(const int n, const int m, const T lower, const T upper, const bool one = true) {
        static_assert(is_integral_v<T> or is_floating_point_v<T>);
        assert(0 <= n and n <= (int)1e8);
        assert(max(n - 1, 0) <= m and m <= (int)min((ll)1e8, 1ll * n * (n - 1) / 2));
        assert(lower <= upper);
        const auto [u, v] = connected_graph(n, m, one);
        const vector<T> w = vec(m, lower, upper, true);
        return {u, v, w};
    }
    inline string parenthesis(const int n) {
        assert(0 <= n and n <= 1e8);
        string res = "";
        int N = n, M = n;
        for(int i = 0; i < 2 * n; ++i) {
            if(this->operator()(0.0l, 1.0l) > 1.0l * (N - M) * (N + 1) / ((N - M + 1) * (N + M))) {
                res += "(";
                --M;
            } else {
                res += ")";
                --N;
            }
        }
        return res;
    }

   private:
    mt19937_64 mt;
} rng;
#line 3 "src/template/modint_2_61m1.hpp"
struct Modint_2_61m1 {
    using mint = Modint_2_61m1;
    using u64 = uint64_t;
    using u128 = __uint128_t;
    static constexpr u64 mod() {
        return m;
    }
    static constexpr mint raw(const u64 v) {
        mint a;
        a._v = v;
        return a;
    }
    constexpr Modint_2_61m1()
        : _v(0) {}
    template <class T>
    constexpr Modint_2_61m1(const T& v) {
        static_assert(is_integral_v<T>);
        if constexpr(is_signed_v<T>) {
            int64_t x = int64_t(v % int64_t(m));
            if(x < 0) x += m;
            _v = u64(x);
        } else _v = u64(v % m);
    }
    constexpr u64 val() const {
        return _v;
    }
    constexpr mint& operator++() {
        return *this += 1;
    }
    constexpr mint& operator--() {
        return *this -= 1;
    }
    constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr mint& operator+=(mint rhs) {
        if(_v >= m - rhs._v) _v -= m;
        _v += rhs._v;
        return *this;
    }
    constexpr mint& operator-=(mint rhs) {
        if(_v < rhs._v) _v += m;
        _v -= rhs._v;
        return *this;
    }
    constexpr mint& operator*=(mint rhs) {
        return *this = *this * rhs;
    }
    constexpr mint& operator/=(mint rhs) {
        return *this *= rhs.inv();
    }
    constexpr mint operator+() const {
        return *this;
    }
    constexpr mint operator-() const {
        return mint{} - *this;
    }
    constexpr mint pow(u64 n) const {
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    constexpr mint inv() const {
        assert(_v);
        return pow(m - 2);
    }
    friend constexpr mint operator+(mint lhs, mint rhs) {
        return lhs += rhs;
    }
    friend constexpr mint operator-(mint lhs, mint rhs) {
        return lhs -= rhs;
    }
    friend constexpr mint operator*(mint lhs, mint rhs) {
        return raw(modulo(u128(lhs._v) * rhs._v));
    }
    friend constexpr mint operator/(mint lhs, mint rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(mint lhs, mint rhs) {
        return lhs._v == rhs._v;
    }
    friend constexpr bool operator!=(mint lhs, mint rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    static constexpr u64 m = (1ull << 61) - 1;
    u64 _v = 0;
    inline static constexpr u64 modulo(const u128& x) {
        const u64 val = (x >> 61) + (x & m);
        return val >= m ? val - m : val;
    }
};
#line 3 "src/data_structure/fenwick_tree.hpp"
template <typename T>
struct FenwickTree {
    FenwickTree(const int N)
        : n(N), data(N) {}
    void add(int p, const T& x) {
        assert(0 <= p and p < n);
        ++p;
        while(p <= n) {
            data[p - 1] += x;
            p += p & -p;
        }
    }
    T sum(const int l, const int r) const {
        assert(0 <= l and l <= r and r <= n);
        return sum(r) - sum(l);
    }
    T get(const int x) const {
        assert(0 <= x and x < n);
        return sum(x + 1) - sum(x);
    }

   private:
    int n;
    vector<T> data;
    inline T sum(int r) const {
        T s = 0;
        while(r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
#line 5 "src/string/dynamic_rolling_hash.hpp"
struct DynamicRollingHash {
    using mint = Modint_2_61m1;
    DynamicRollingHash(const string& s, unsigned long long BASE = 0)
        : len((int)s.size()), pow(len + 1), inv_pow(len + 1), hash(len) {
        if(BASE == 0) {
            mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
            uniform_int_distribution<unsigned long long> dist(1ull << 10, 1ull << 60);
            BASE = dist(mt);
        }
        base = BASE;
        pow[0] = 1;
        for(int i = 0; i < len; ++i) {
            pow[i + 1] = pow[i] * base;
        }
        inv = base.inv();
        inv_pow[0] = 1;
        for(int i = 0; i < len; ++i) {
            inv_pow[i + 1] = inv_pow[i] * inv;
        }
        for(int i = 0; i < len; ++i) {
            hash.add(i, pow[i] * s[i]);
        }
    }
    unsigned long long get(const int lower, const int upper) const {
        assert(0 <= lower and lower <= upper and upper <= len);
        return (hash.sum(lower, upper) * inv_pow[lower]).val();
    }
    unsigned long long get_hash(const string& t) const {
        mint res = 0;
        for(int i = 0; i < (int)t.size(); ++i) {
            res += pow[i] * t[i];
        }
        return res.val();
    }
    void set(const int idx, const char c) {
        assert(0 <= idx and idx < len);
        hash.add(idx, pow[idx] * c - hash.get(idx));
    }

   private:
    int len;
    mint base, inv;
    vector<mint> pow, inv_pow;
    FenwickTree<mint> hash;
};
#line 6 "verify/unit_test/string/dynamic_rolling_hash.test.cpp"
using mint = Modint_2_61m1;
void test() {
    int n = rng(1, 2000), q = 2000;
    string s = "";
    rep(i, 0, n) {
        s += 'a' + rng(0, 25);
    }
    ll base = rng(1ll << 10, 1ll << 60);
    DynamicRollingHash drh(s, base);
    while(q--) {
        int type = rng(0, 1);
        if(type == 0) {
            int len = rng(0, n);
            int l = rng(0, n - len), r = l + len;
            mint ans = 0, b = 1;
            rep(i, l, r) {
                ans += b * s[i];
                b *= base;
            }
            assert(drh.get(l, r) == ans.val());
        } else {
            int idx = rng(0, n - 1);
            char c = 'a' + rng(0, 25);
            s[idx] = c;
            drh.set(idx, c);
        }
    }
}
int main(void) {
    constexpr int test_num = 100;
    rep(_, 0, test_num) {
        test();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
Back to top page