QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#316052#8177. Sum is Integerucup-team1191#AC ✓252ms19072kbC++2026.9kb2024-01-27 17:09:282024-01-27 17:09:29

Judging History

你现在查看的是最新测评结果

  • [2024-01-27 17:09:29]
  • 评测
  • 测评结果:AC
  • 用时:252ms
  • 内存:19072kb
  • [2024-01-27 17:09:28]
  • 提交

answer

/*
    author:  Maksim1744
    created: 27.01.2024 11:49:52
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#define OSTREAM(...)    ;
#define OSTREAM0(...)   ;
#endif

struct Sieve {
    struct PrimeIterator {
        int prime;
        int count;
        int num;
        const Sieve& sieve;

        PrimeIterator(int num, const Sieve& sieve) : num(num), sieve(sieve) {
            step();
        }

        void step() {
            prime = sieve.mn[num];
            count = 0;
            while (num != 1 && sieve.mn[num] == prime) {
                num /= sieve.mn[num];
                ++count;
            }
        }

        bool operator == (const PrimeIterator& other) const {
            return tie(prime, count, num) == tie(other.prime, other.count, other.num);
        }
        bool operator != (const PrimeIterator& other) const {
            return !(*this == other);
        }

        PrimeIterator& operator ++ () {
            step();
            return *this;
        }

        pair<int, int> operator * () const {
            return {prime, count};
        }
    };

    struct PrimeIteratorContainer {
        int num;
        const Sieve& sieve;
        using iterator = PrimeIterator;

        PrimeIteratorContainer(int num, const Sieve& sieve) : num(num), sieve(sieve) {}

        PrimeIterator begin() const {
            return PrimeIterator(num, sieve);
        }

        PrimeIterator end() const {
            return PrimeIterator(1, sieve);
        }

        vector<pair<int, int>> collect() const {
            vector<pair<int, int>> result;
            for (auto p : *this)
                result.push_back(p);
            return result;
        }
    };

    template<typename U, typename V>
    struct DoubleIterator {
        U u;
        V v;
        U::iterator uit;
        V::iterator vit;
        int prime;
        int count;

        DoubleIterator(const U& u, const V& v, U::iterator uit, V::iterator vit) : u(u), v(v), uit(uit), vit(vit) {
            tie(prime, count) = this->operator*();
        }

        DoubleIterator& operator ++ () {
            if (uit == u.end()) {
                ++vit;
            } else if (vit == v.end()) {
                ++uit;
            } else if ((*uit).first < (*vit).first) {
                ++uit;
            } else if ((*uit).first > (*vit).first) {
                ++vit;
            } else {
                ++uit;
                ++vit;
            }
            return *this;
        }

        bool operator == (const DoubleIterator& other) const {
            return tie(uit, vit) == tie(other.uit, other.vit);
        }
        bool operator != (const DoubleIterator& other) const {
            return !(*this == other);
        }

        pair<int, int> operator * () const {
            if (uit == u.end()) return *vit;
            if (vit == v.end()) return *uit;
            if ((*uit).first < (*vit).first) return *uit;
            if ((*uit).first > (*vit).first) return *vit;
            return {(*uit).first, (*uit).second + (*vit).second};
        }
    };

    template<typename U, typename V>
    struct DoubleIteratorContainer {
        using iterator = DoubleIterator<U, V>;
        U u;
        V v;

        DoubleIteratorContainer(const U& u, const V& v) : u(u), v(v) {}

        iterator begin() const {
            return iterator(u, v, u.begin(), v.begin());
        }

        iterator end() const {
            return iterator(u, v, u.end(), v.end());
        }

        vector<pair<int, int>> collect() const {
            vector<pair<int, int>> result;
            for (auto p : *this)
                result.push_back(p);
            return result;
        }
    };

    vector<bool> isp;
    vector<int> primes;
    vector<int> mn;

    Sieve(int n, bool gen_primes = false, bool gen_mn = false) {
        isp.assign(n + 1, true); isp[0] = isp[1] = false;
        for (int i = 2; i * i <= n; ++i)
            if (isp[i])
                for (int j = i * i; j <= n; j += i)
                    isp[j] = false;

        if (gen_primes)
            for (int i = 2; i <= n; ++i)
                if (isp[i])
                    primes.push_back(i);

        if (gen_mn) {
            mn.assign(n + 1, -1);
            for (int i = 2; i <= n; ++i) {
                if (isp[i]) {
                    mn[i] = i;
                    if ((ll)i * i <= n)
                        for (int j = i * i; j <= n; j += i)
                            if (mn[j] == -1)
                                mn[j] = i;
                }
            }
        }
    }

    bool is_prime(int k) const {
        return isp[k];
    }

    bool operator[](int k) const {
        return isp[k];
    }

    // can be used as vector<pair<int, int>>, as in function phi() below
    // except doesn't create a vector to avoid heap allocations
    // can be transformed into vector<pair<int, int>> with .collect()
    auto get_prime_divs(int p) const {
        return PrimeIteratorContainer(p, *this);
    }

    // if you want to get prime divs for n = a * b * c where max(a, b, c) < size(sieve), then call get_prime_divs(a, b, c)
    template<typename... Args>
    auto get_prime_divs(int p, Args... args) const {
        return DoubleIteratorContainer(PrimeIteratorContainer(p, *this), get_prime_divs(args...));
    }

    int phi(int n) const {
        auto v = get_prime_divs(n);
        for (auto [p, c] : v)
            n = n / p * (p - 1);
        return n;
    }
};

namespace mint_ns {
template<auto P>
struct Modular {
    using value_type = decltype(P);
    value_type value;

    Modular(long long k = 0) : value(norm(k)) {}

    friend Modular<P>& operator += (      Modular<P>& n, const Modular<P>& m) { n.value += m.value; if (n.value >= P) n.value -= P; return n; }
    friend Modular<P>  operator +  (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r += m; }

    friend Modular<P>& operator -= (      Modular<P>& n, const Modular<P>& m) { n.value -= m.value; if (n.value < 0)  n.value += P; return n; }
    friend Modular<P>  operator -  (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r -= m; }
    friend Modular<P>  operator -  (const Modular<P>& n)                      { return Modular<P>(-n.value); }

    friend Modular<P>& operator *= (      Modular<P>& n, const Modular<P>& m) { n.value = n.value * 1ll * m.value % P; return n; }
    friend Modular<P>  operator *  (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r *= m; }

    friend Modular<P>& operator /= (      Modular<P>& n, const Modular<P>& m) { return n *= m.inv(); }
    friend Modular<P>  operator /  (const Modular<P>& n, const Modular<P>& m) { Modular<P> r = n; return r /= m; }

    Modular<P>& operator ++ (   ) { return *this += 1; }
    Modular<P>& operator -- (   ) { return *this -= 1; }
    Modular<P>  operator ++ (int) { Modular<P> r = *this; *this += 1; return r; }
    Modular<P>  operator -- (int) { Modular<P> r = *this; *this -= 1; return r; }

    friend bool operator == (const Modular<P>& n, const Modular<P>& m) { return n.value == m.value; }
    friend bool operator != (const Modular<P>& n, const Modular<P>& m) { return n.value != m.value; }

    explicit    operator       int() const { return value; }
    explicit    operator      bool() const { return value; }
    explicit    operator long long() const { return value; }

    constexpr static value_type mod()      { return     P; }

    value_type norm(long long k) {
        if (!(-P <= k && k < P)) k %= P;
        if (k < 0) k += P;
        return k;
    }

    Modular<P> inv() const {
        value_type a = value, b = P, x = 0, y = 1;
        while (a != 0) { value_type k = b / a; b -= k * a; x -= k * y; swap(a, b); swap(x, y); }
        return Modular<P>(x);
    }
};
template<auto P> Modular<P> pow(Modular<P> m, long long p) {
    Modular<P> r(1);
    while (p) {
        if (p & 1) r *= m;
        m *= m;
        p >>= 1;
    }
    return r;
}

template<auto P> ostream& operator << (ostream& o, const Modular<P>& m) { return o << m.value; }
template<auto P> istream& operator >> (istream& i,       Modular<P>& m) { long long k; i >> k; m.value = m.norm(k); return i; }
template<auto P> string   to_string(const Modular<P>& m) { return to_string(m.value); }

// using Mint = long double;
}
using namespace mint_ns;

using Mint1 = Modular<1000000007>;
using Mint2 = Modular<998244353>;

const Mint1 P1 = 1238796;
const Mint2 P2 = 9123606;

vector<Mint1> degp1;
vector<Mint2> degp2;

namespace segtree {

// This implementation is disgusting, but it seems to work and do it faster than previous version.

template<typename Item>
Item tree_merge(const Item& a, const Item& b) {
    Item i;
    i.update(a, b);
    return i;
}

template<typename Item, bool lazy>
struct Pusher {};

template<typename Item>
struct Pusher<Item, false> {
    void push(const vector<Item>&, int, int, int) {}
    Item ask_on_segment(const vector<Item>& tree, int n, int l, int r) {
        l |= n;
        r |= n;
        Item resl, resr;
        while (l <= r) {
            if (l & 1) {
                resl = tree_merge(resl, tree[l]);
                ++l;
            }
            if (!(r & 1)) {
                resr = tree_merge(tree[r], resr);
                --r;
            }
            l >>= 1;
            r >>= 1;
        }
        return tree_merge(resl, resr);
    }
    void push_point(const vector<Item>&, int, int) {}

    template<typename P>
    int lower_bound(const vector<Item>& tree, int n, int l, P p) {
        Item cur;
        if (p(cur)) return l - 1;
        l |= n;
        int r = n | (n - 1);
        // carefully go up
        while (true) {
            if (p(tree_merge(cur, tree[l]))) {
                break;
            }
            if (l == r) return n;
            if (l & 1) {
                cur = tree_merge(cur, tree[l]);
                ++l;
            }
            l >>= 1;
            r >>= 1;
        }

        // usual descent from l
        while (l < n) {
            if (p(tree_merge(cur, tree[l * 2]))) {
                l = l * 2;
            } else {
                cur = tree_merge(cur, tree[l * 2]);
                l = l * 2 + 1;
            }
        }
        return (l ^ n);
    }

    template<typename P>
    int lower_bound_rev(const vector<Item>& tree, int n, int r, P p) {
        Item cur;
        if (p(cur)) return r + 1;
        r |= n;
        int l = n;
        // carefully go up
        while (true) {
            if (p(tree_merge(tree[r], cur))) {
                break;
            }
            if (l == r) return -1;
            if (!(r & 1)) {
                cur = tree_merge(tree[r], cur);
                --r;
            }
            l >>= 1;
            r >>= 1;
        }

        // usual descent from r
        while (r < n) {
            if (p(tree_merge(tree[r * 2 + 1], cur))) {
                r = r * 2 + 1;
            } else {
                cur = tree_merge(tree[r * 2 + 1], cur);
                r = r * 2;
            }
        }
        return (r ^ n);
    }
};

template<typename Item>
struct Pusher<Item, true> {
    void push(vector<Item>& tree, int ind, int l, int r) {
        tree[ind].push(tree[ind * 2], tree[ind * 2 + 1], l, r);
    }

    Item ask_on_segment(vector<Item>& tree, int n, int l, int r) {
        int vl = 0, vr = n - 1;
        int i = 1;
        Item result;
        while (vl != vr) {
            int m = (vl + vr) / 2;
            if (l > m) {
                push(tree, i, vl, vr);
                i = i * 2 + 1;
                vl = m + 1;
            } else if (r <= m) {
                push(tree, i, vl, vr);
                i = i * 2;
                vr = m;
            } else {
                break;
            }
        }
        if (l == vl && r == vr) {
            return tree[i];
        }
        push(tree, i, vl, vr);
        // left
        {
            int ind = i * 2;
            int L = vl, R = (vl + vr) / 2;
            while (l != L) {
                int m = (L + R) / 2;
                push(tree, ind, L, R);
                if (l <= m) {
                    result = tree_merge(tree[ind * 2 + 1], result);
                    ind *= 2;
                    R = m;
                } else {
                    ind = ind * 2 + 1;
                    L = m + 1;
                }
            }
            result = tree_merge(tree[ind], result);
        }
        // right
        {
            int ind = i * 2 + 1;
            int L = (vl + vr) / 2 + 1, R = vr;
            while (r != R) {
                int m = (L + R) / 2;
                push(tree, ind, L, R);
                if (r > m) {
                    result = tree_merge(result, tree[ind * 2]);
                    ind = ind * 2 + 1;
                    L = m + 1;
                } else {
                    ind = ind * 2;
                    R = m;
                }
            }
            result = tree_merge(result, tree[ind]);
        }
        return result;
    }

    void push_point(vector<Item>& tree, int n, int ind) {
        int l = 0, r = n - 1;
        int i = 1;
        while (l != r) {
            push(tree, i, l, r);
            int m = (l + r) / 2;
            if (ind <= m) {
                r = m;
                i *= 2;
            } else {
                l = m + 1;
                i = i * 2 + 1;
            }
        }
    }

    template<typename P>
    pair<int, Item> _lower_bound(vector<Item>& tree, int l, P p, Item cur, int i, int vl, int vr) {
        if (vl == vr) {
            if (p(tree_merge(cur, tree[i]))) {
                return {vl, tree[i]};
            } else {
                return {vl + 1, tree[i]};
            }
        }

        push(tree, i, vl, vr);
        int m = (vl + vr) / 2;
        if (l > m) {
            return _lower_bound(tree, l, p, cur, i * 2 + 1, m + 1, vr);
        } else if (l <= vl) {
            if (!p(tree_merge(cur, tree[i]))) {
                return {vr + 1, tree_merge(cur, tree[i])};
            }
            if (p(tree_merge(cur, tree[i * 2]))) {
                return _lower_bound(tree, l, p, cur, i * 2, vl, m);
            } else {
                return _lower_bound(tree, l, p, tree_merge(cur, tree[i * 2]), i * 2 + 1, m + 1, vr);
            }
        } else {
            auto [ind, it] = _lower_bound(tree, l, p, cur, i * 2, vl, m);
            if (ind <= m) return {ind, it};
            return _lower_bound(tree, l, p, it, i * 2 + 1, m + 1, vr);
        }
    }

    template<typename P>
    int lower_bound(vector<Item>& tree, int n, int l, P p) {
        Item cur;
        if (p(cur)) return l - 1;
        return _lower_bound(tree, l, p, cur, 1, 0, n - 1).first;
    }

    template<typename P>
    pair<int, Item> _lower_bound_rev(vector<Item>& tree, int r, P p, Item cur, int i, int vl, int vr) {
        if (vl == vr) {
            if (p(tree_merge(tree[i], cur))) {
                return {vl, tree[i]};
            } else {
                return {vl - 1, tree[i]};
            }
        }

        push(tree, i, vl, vr);
        int m = (vl + vr) / 2;
        if (r <= m) {
            return _lower_bound_rev(tree, r, p, cur, i * 2, vl, m);
        } else if (r >= vr) {
            if (!p(tree_merge(tree[i], cur))) {
                return {vl - 1, tree_merge(cur, tree[i])};
            }
            if (p(tree_merge(tree[i * 2 + 1], cur))) {
                return _lower_bound_rev(tree, r, p, cur, i * 2 + 1, m + 1, vr);
            } else {
                return _lower_bound_rev(tree, r, p, tree_merge(tree[i * 2 + 1], cur), i * 2, vl, m);
            }
        } else {
            auto [ind, it] = _lower_bound_rev(tree, r, p, cur, i * 2 + 1, m + 1, vr);
            if (ind > m) return {ind, it};
            return _lower_bound_rev(tree, r, p, it, i * 2, vl, m);
        }
    }

    template<typename P>
    int lower_bound_rev(vector<Item>& tree, int n, int r, P p) {
        Item cur;
        if (p(cur)) return r + 1;
        return _lower_bound_rev(tree, r, p, cur, 1, 0, n - 1).first;
    }
};

template<typename Item, bool lazy = false>
struct Segtree {
    vector<Item> tree;
    Pusher<Item, lazy> pusher;
    int n;
    int n0;

    Segtree(int n = 0) {
        build(n);
    }

    template<typename U>
    Segtree(const vector<U>& v) {
        build(v);
    }

    void build(int n) {
        this->n0 = n;
        while (n & (n - 1)) ++n;
        this->n = n;
        tree.assign(n * 2, {});
    }

    template<typename U>
    void build(const vector<U>& v) {
        build(v.size());
        for (int i = 0; i < v.size(); ++i) {
            tree[n | i].init(v[i], i);
        }
        build();
    }

    void build() {
        for (int i = n - 1; i >= 1; --i) {
            tree[i].update(tree[i * 2], tree[i * 2 + 1]);
        }
    }

    void push(int ind, int l, int r) {
        pusher.push(tree, ind, l, r);
    }

    template<typename T>
    void set(int ind, const T& t) {
        pusher.push_point(tree, n, ind);
        ind |= n;
        tree[ind].init(t, ind ^ n);
        ind >>= 1;
        while (ind) {
            tree[ind].update(tree[ind * 2], tree[ind * 2 + 1]);
            ind >>= 1;
        }
    }

    template<typename T>
    void update(int ind, const T& t) {
        pusher.push_point(tree, n, ind);
        ind |= n;
        tree[ind].update(t, ind ^ n);
        ind >>= 1;
        while (ind) {
            tree[ind].update(tree[ind * 2], tree[ind * 2 + 1]);
            ind >>= 1;
        }
    }

    Item& ith(int ind) {
        static_assert(!lazy, "don't use this method with lazy propagation, unless you're sure you need it");
        return tree[ind | n];
    }

    const Item& root() const {
        return tree[1];
    }

    Item ask(int l, int r) {
        l = max(l, 0);
        r = min(r, n - 1);
        if (l > r) return {};
        return pusher.ask_on_segment(tree, n, l, r);
    }

    template<typename T>
    void modify(int l, int r, const T& t) {
        static_assert(lazy, "lazy must be set to true to use this function");
        l = max(l, 0);
        r = min(r, n - 1);
        if (l > r) return;
        int vl = 0, vr = n - 1;
        int i = 1;
        while (vl != vr) {
            int m = (vl + vr) / 2;
            if (l > m) {
                push(i, vl, vr);
                i = i * 2 + 1;
                vl = m + 1;
            } else if (r <= m) {
                push(i, vl, vr);
                i = i * 2;
                vr = m;
            } else {
                break;
            }
        }
        if (l == vl && r == vr) {
            tree[i].modify(t, l, r);
        } else {
            push(i, vl, vr);
            // left
            {
                int ind = i * 2;
                int L = vl, R = (vl + vr) / 2;
                while (l != L) {
                    int m = (L + R) / 2;
                    push(ind, L, R);
                    if (l <= m) {
                        tree[ind * 2 + 1].modify(t, m + 1, R);
                        ind *= 2;
                        R = m;
                    } else {
                        ind = ind * 2 + 1;
                        L = m + 1;
                    }
                }
                tree[ind].modify(t, L, R);
                ind >>= 1;
                while (ind != i) {
                    tree[ind].update(tree[ind * 2], tree[ind * 2 + 1]);
                    ind >>= 1;
                }
            }
            // right
            {
                int ind = i * 2 + 1;
                int L = (vl + vr) / 2 + 1, R = vr;
                while (r != R) {
                    int m = (L + R) / 2;
                    push(ind, L, R);
                    if (r > m) {
                        tree[ind * 2].modify(t, L, m);
                        ind = ind * 2 + 1;
                        L = m + 1;
                    } else {
                        ind = ind * 2;
                        R = m;
                    }
                }
                tree[ind].modify(t, L, R);
                ind >>= 1;
                while (ind != i) {
                    tree[ind].update(tree[ind * 2], tree[ind * 2 + 1]);
                    ind >>= 1;
                }
            }
            tree[i].update(tree[i * 2], tree[i * 2 + 1]);
        }
        i >>= 1;
        while (i) {
            tree[i].update(tree[i * 2], tree[i * 2 + 1]);
            i >>= 1;
        }
    }

    // first index r such that p(tree.ask(l, r)) == true
    // if p() is true for empty item, return l-1
    // if p() is never true, returns n
    template<typename P>
    int lower_bound(int l, P p) {
        l = max(l, 0);
        if (l >= n0) return n0;
        return min(n0, pusher.lower_bound(tree, n, l, p));
    }

    // similarly to lower_bound, returns first (largest) l such that p(tree.ask(l, r)) == true
    template<typename P>
    int lower_bound_rev(int r, P p) {
        r = min(r, n0 - 1);
        if (r < 0) return -1;
        return pusher.lower_bound_rev(tree, n, r, p);
    }
};

}
using segtree::Segtree;

struct Item {
    Mint1 h1 = 0;
    Mint2 h2 = 0;
    int len = 0;

    template<typename T>
    void init(const T& t, int ind) {
        h1 = t;
        h2 = t;
        len = 1;
    }

    void update(const Item& a, const Item& b) {
        h1 = a.h1 + b.h1 * degp1[a.len];
        h2 = a.h2 + b.h2 * degp2[a.len];
        len = a.len + b.len;
    }

    // similar to init, but more convenient for doing a[i] += x, implement only if needed
    template<typename T>
    void update(const T& t, int ind) {
        h1 += t;
        h2 += t;
    }

    //// apply here, save for children
    // template<typename T>
    // void modify(const T& m, int l, int r) {}

    // void push(Item& a, Item& b, int l, int r) {
    //     int m = (l + r) / 2;
    //     a.modify(mod, l, m);
    //     b.modify(mod, m + 1, r);
    //     // reset mod
    // }
};

// finds x and y such that a * x + b * y = c
template<typename T>
pair<T, T> egcd(T a, T b, T c) {
    if (a == 0) {
        // b * y = c
        assert(c % b == 0);
        return {0, c / b};
    }
    // a * x + b * y = c
    // a * x + (b % a + (b/a) * a) * y = c
    // a * (x + (b/a) * y) + (b % a) * y = c
    auto [y0, x0] = egcd(b % a, a, c);
    T y = y0;
    T x = x0 - (b / a) * y;
    return {x, y};
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    degp1.pb(1);
    degp2.pb(1);

    int n;
    cin >> n;
    vector<pair<int, int>> v(n);
    cin >> v;

    const int N = 1e5 + 20;

    Sieve sieve(N, true, true);
    for (auto& [p, q] : v) {
        int g = gcd(p, q);
        p /= g;
        q /= g;
    }

    vector<int> maxd(N, 0);

    for (int i = 0; i < n; ++i) {
        for (auto [p, c] : sieve.get_prime_divs(v[i].second)) {
            maxd[p] = max(maxd[p], c);
        }
    }

    vector<int> ind(N);
    int last = 0;

    for (int i = 0; i < N; ++i) {
        if (maxd[i] > 0) {
            ind[i] = last;
            ++last;
        }
    }
    if (!last) ++last;

    vector<int> cur(last, 0);

    for (int i = 0; i < cur.size() + 10; ++i) {
        degp1.pb(degp1.back() * P1);
        degp2.pb(degp2.back() * P2);
    }

    Segtree<Item, false> tree(cur);

    map<pair<int, int>, ll> cnts;
    auto mem = [&]() {
        cnts[{(int)tree.root().h1, (int)tree.root().h2}]++;
    };

    mem();

    auto mpow = [&](ll a, ll p) {
        ll r = 1;
        while (p) {
            if (p & 1) r *= a;
            a *= a;
            p >>= 1;
        }
        return r;
    };

    auto inv = [&](ll a, ll mod) {
        a %= mod;
        // a * x = 1 + mod * y
        auto [x, y] = egcd<ll>(a, -mod, 1);
        x = (x % mod + mod) % mod;
        assert(x * a % mod == 1);
        return x;
    };

    for (int i = 0; i < n; ++i) {
        shows;
        show(i);
        auto [p, q] = v[i];
        for (auto [p1, c1] : sieve.get_prime_divs(q)) {
            ll who = mpow(p1, maxd[p1]);
            ll p0 = p * mpow(p1, maxd[p1] - c1) % who;
            ll q0 = q / mpow(p1, c1);

            int val = p0 * inv(q0, who);
            cur[ind[p1]] = (cur[ind[p1]] + val) % who;
            tree.set(ind[p1], cur[ind[p1]]);
        }

        mem();
    }

    ll ans = 0;
    for (const auto& [p, c] : cnts)
        ans += c * (c - 1) / 2;
    cout << ans << '\n';

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 4480kb

input:

4
1 6
1 3
1 2
1 2

output:

2

result:

ok "2"

Test #2:

score: 0
Accepted
time: 2ms
memory: 4464kb

input:

5
1 1
2 2
3 3
4 4
5 5

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 0ms
memory: 4532kb

input:

2
1 99999
99999 100000

output:

0

result:

ok "0"

Test #4:

score: 0
Accepted
time: 23ms
memory: 6008kb

input:

200000
82781 82781
86223 86223
16528 16528
84056 84056
94249 94249
54553 54553
25943 25943
10415 10415
52417 52417
46641 46641
70298 70298
84228 84228
55441 55441
49326 49326
11753 11753
89499 89499
58220 58220
71482 71482
32373 32373
7251 7251
78573 78573
74268 74268
46682 46682
20314 20314
85519 8...

output:

10603308211

result:

ok "10603308211"

Test #5:

score: 0
Accepted
time: 24ms
memory: 6256kb

input:

200000
50741 50741
86798 95775
51104 51104
29372 29372
43295 43295
55065 55065
68947 68947
35282 35282
62467 62467
68481 68481
82613 82613
95921 95921
46302 46302
53806 53806
61244 61244
16078 16078
33476 33476
9084 9084
99273 99273
11678 11678
36816 36816
30311 30311
51479 51479
2667 2667
57043 570...

output:

20066919

result:

ok "20066919"

Test #6:

score: 0
Accepted
time: 33ms
memory: 7564kb

input:

200000
98235 98235
67434 96040
49102 49102
16569 16569
1095 1095
23901 23901
6143 6143
78285 78285
9853 9853
46454 46454
52131 52131
72378 72378
53983 53983
91453 91453
38655 83910
6455 6455
80993 80993
66871 66871
45005 45005
72124 72124
17949 17949
34378 34378
81399 81399
89147 89147
72892 72892
8...

output:

1808373

result:

ok "1808373"

Test #7:

score: 0
Accepted
time: 71ms
memory: 9460kb

input:

200000
64364 74993
65425 91573
10305 10305
31901 31901
90499 95090
13337 47707
32342 38531
75909 93251
95924 95924
12789 12789
77190 77190
82753 99616
33824 79787
48159 48159
32648 32648
90698 98365
89028 89028
36982 36982
11377 11377
79190 88165
23457 23457
24114 24114
55183 71128
65165 65165
4196 ...

output:

593601

result:

ok "593601"

Test #8:

score: 0
Accepted
time: 122ms
memory: 12660kb

input:

200000
42985 42985
30472 30472
4697 50160
91745 95118
77209 77209
32676 32676
96375 96550
18636 18636
93176 93202
27039 27039
2001 85497
74148 94045
82232 92935
71481 80579
99738 99977
90865 90865
93800 99894
11923 64394
29930 29930
40659 40659
12932 24625
47502 47502
34808 52414
37132 37132
78333 8...

output:

200046

result:

ok "200046"

Test #9:

score: 0
Accepted
time: 173ms
memory: 15748kb

input:

200000
38189 83791
82487 82487
42636 69054
46661 46661
55193 83194
40205 87111
29683 29683
79038 79038
98893 99029
1297 1297
29036 29036
14288 14288
92671 94553
93133 96764
20394 27614
51001 51001
95715 99182
57011 64387
40743 84877
53871 53871
9572 36322
52854 52854
48164 48164
54890 90222
13149 23...

output:

66793

result:

ok "66793"

Test #10:

score: 0
Accepted
time: 241ms
memory: 19052kb

input:

200000
88510 92529
59515 86234
77518 89158
30499 67484
21592 66929
22068 85765
56040 80464
47128 86562
72993 74449
47648 55593
41645 65306
93686 98745
93577 97957
63897 92381
99261 99663
14073 53816
84719 96510
35202 78173
8823 28953
6740 40358
34790 66738
22323 83799
8982 93169
41275 70673
93933 99...

output:

2006

result:

ok "2006"

Test #11:

score: 0
Accepted
time: 178ms
memory: 18904kb

input:

200000
37542 94781
52292 94781
41475 94781
64295 94781
19942 94781
20632 94781
23926 32922
15988 32922
70400 94781
33866 79612
73489 94781
11346 32922
29597 94781
18851 32922
75353 79612
10468 18421
68719 94781
30040 80989
27637 94781
9313 21313
26684 47093
10860 79612
2343 63521
2159 32922
4106 470...

output:

2

result:

ok "2"

Test #12:

score: 0
Accepted
time: 200ms
memory: 18868kb

input:

200000
14155 82459
4550 92282
9935 59120
35119 43577
69681 92282
6573 33835
193 59120
2297 88354
33149 59120
21365 59120
14906 59120
4898 71617
802 25637
46411 59120
65015 92282
20057 71617
25342 71617
7440 82459
8525 71617
22625 25637
19203 82459
85531 92282
37402 71617
2660 82459
17454 33835
7833 ...

output:

5

result:

ok "5"

Test #13:

score: 0
Accepted
time: 182ms
memory: 16784kb

input:

200000
1 2
2 3
4 5
3 7
7 11
6 13
9 17
15 19
17 23
12 29
18 31
10 37
23 41
41 43
20 47
24 53
12 59
14 61
63 67
59 71
16 73
42 79
15 83
18 89
12 97
4 101
91 103
13 107
86 109
3 113
53 127
84 131
112 137
22 139
37 149
58 151
153 157
160 163
156 167
41 173
91 179
66 181
29 191
64 193
107 197
91 199
158 ...

output:

41227

result:

ok "41227"

Test #14:

score: 0
Accepted
time: 187ms
memory: 17100kb

input:

200000
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

39056

result:

ok "39056"

Test #15:

score: 0
Accepted
time: 170ms
memory: 16164kb

input:

200000
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

59805

result:

ok "59805"

Test #16:

score: 0
Accepted
time: 185ms
memory: 15832kb

input:

200000
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

66145

result:

ok "66145"

Test #17:

score: 0
Accepted
time: 99ms
memory: 14428kb

input:

137337
1 2
2 3
4 5
3 7
7 11
6 13
9 17
15 19
17 23
12 29
18 31
10 37
23 41
41 43
20 47
24 53
12 59
14 61
63 67
59 71
16 73
42 79
15 83
18 89
12 97
4 101
91 103
13 107
86 109
3 113
53 127
84 131
112 137
22 139
37 149
58 151
153 157
160 163
156 167
41 173
91 179
66 181
29 191
64 193
107 197
91 199
158 ...

output:

0

result:

ok "0"

Test #18:

score: 0
Accepted
time: 74ms
memory: 12980kb

input:

112633
1 2
2 3
4 5
3 7
7 11
6 13
9 17
15 19
17 23
12 29
18 31
10 37
23 41
41 43
20 47
24 53
12 59
14 61
63 67
59 71
16 73
42 79
15 83
18 89
12 97
4 101
91 103
13 107
86 109
3 113
53 127
84 131
112 137
22 139
37 149
58 151
153 157
160 163
156 167
41 173
91 179
66 181
29 191
64 193
107 197
91 199
158 ...

output:

0

result:

ok "0"

Test #19:

score: 0
Accepted
time: 30ms
memory: 8448kb

input:

48424
1 2
2 3
4 5
3 7
7 11
6 13
9 17
15 19
17 23
12 29
18 31
10 37
23 41
41 43
20 47
24 53
12 59
14 61
63 67
59 71
16 73
42 79
15 83
18 89
12 97
4 101
91 103
13 107
86 109
3 113
53 127
84 131
112 137
22 139
37 149
58 151
153 157
160 163
156 167
41 173
91 179
66 181
29 191
64 193
107 197
91 199
158 2...

output:

0

result:

ok "0"

Test #20:

score: 0
Accepted
time: 153ms
memory: 18256kb

input:

187753
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

0

result:

ok "0"

Test #21:

score: 0
Accepted
time: 153ms
memory: 17080kb

input:

173667
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

0

result:

ok "0"

Test #22:

score: 0
Accepted
time: 172ms
memory: 19072kb

input:

200000
1 2
1 3
2 5
5 7
5 11
7 13
16 17
6 19
16 23
20 29
23 31
16 37
29 41
2 43
13 47
28 53
3 59
10 61
47 67
1 71
39 73
13 79
56 83
26 89
81 97
11 101
16 103
84 107
17 109
77 113
122 127
4 131
79 137
41 139
146 149
29 151
104 157
64 163
84 167
5 173
25 179
124 181
140 191
45 193
112 197
71 199
166 21...

output:

0

result:

ok "0"

Test #23:

score: 0
Accepted
time: 3ms
memory: 4692kb

input:

11032
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 ...

output:

32611835

result:

ok "32611835"

Test #24:

score: 0
Accepted
time: 31ms
memory: 6052kb

input:

197164
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1...

output:

7012456789

result:

ok "7012456789"

Test #25:

score: 0
Accepted
time: 25ms
memory: 5760kb

input:

152617
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1 3
1...

output:

3698154517

result:

ok "3698154517"

Test #26:

score: 0
Accepted
time: 9ms
memory: 5016kb

input:

51168
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 5
1 ...

output:

140488539

result:

ok "140488539"

Test #27:

score: 0
Accepted
time: 134ms
memory: 17580kb

input:

193934
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100000
1 100...

output:

8327

result:

ok "8327"

Test #28:

score: 0
Accepted
time: 94ms
memory: 12064kb

input:

143685
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 99999
1 999...

output:

41222

result:

ok "41222"

Test #29:

score: 0
Accepted
time: 73ms
memory: 14440kb

input:

140833
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 99998
1 999...

output:

0

result:

ok "0"

Test #30:

score: 0
Accepted
time: 24ms
memory: 6008kb

input:

200000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000...

output:

10687007638

result:

ok "10687007638"

Test #31:

score: 0
Accepted
time: 2ms
memory: 4392kb

input:

1000
155 1129
42123 42937
53055 56269
84026 89671
36 59
48552 64783
15754 29599
24054 35407
15451 21617
2093 58441
65844 86959
17502 77641
40827 66569
26523 95089
8906 38069
36696 41687
12551 16981
18568 49103
10523 19507
19599 45751
970 7951
6346 61441
5486 12239
80215 87133
11572 43457
42895 48821...

output:

1

result:

ok "1"

Test #32:

score: 0
Accepted
time: 2ms
memory: 4384kb

input:

1072
23184 65839
4268 65843
11075 12487
33343 49757
62837 74717
91526 94693
7014 10459
5497 9371
15993 29741
85921 98669
64910 74197
15087 22229
25783 61381
110 241
32062 36473
16770 28627
17493 23473
15377 15467
509 1619
6541 31957
4299 21377
22010 49411
7335 17231
6477 38201
4617 5869
10432 13147
...

output:

1

result:

ok "1"

Test #33:

score: 0
Accepted
time: 0ms
memory: 4556kb

input:

970
2066 13291
2775 25939
8058 9029
2218 8893
28676 55487
28912 36073
3193 15881
19577 49993
1925 25849
37099 53629
33112 47609
4410 13789
7396 9767
10521 13577
25025 31907
7917 55291
54695 62969
11204 86959
13022 16829
61908 75083
37351 37423
8731 40231
12005 26189
8559 40853
35811 78283
10828 3235...

output:

1

result:

ok "1"

Test #34:

score: 0
Accepted
time: 10ms
memory: 4968kb

input:

30185
2 6
9 9
9 10
7 7
7 10
10 10
1 2
8 8
4 7
2 8
8 8
9 10
4 4
8 9
1 2
4 9
5 9
8 8
5 10
6 9
6 10
6 7
7 9
6 9
5 10
2 10
9 10
9 9
5 7
9 10
5 10
4 7
10 10
7 8
7 10
8 10
4 10
2 3
9 10
10 10
3 10
5 6
9 9
10 10
1 9
4 7
6 9
2 3
9 9
4 7
2 2
5 10
10 10
6 10
2 8
9 9
4 5
3 7
2 2
3 6
10 10
7 9
8 9
4 5
1 6
4 4
4...

output:

115097

result:

ok "115097"

Test #35:

score: 0
Accepted
time: 23ms
memory: 5484kb

input:

82975
6 10
4 7
5 10
1 1
5 5
5 8
3 10
7 8
10 10
10 10
9 10
5 7
10 10
6 9
10 10
5 10
6 10
1 7
5 7
9 10
1 3
5 7
9 9
10 10
10 10
9 10
8 10
9 9
2 4
2 9
8 9
5 10
6 7
2 6
9 10
7 8
9 10
4 9
7 10
9 9
6 10
3 3
9 9
7 8
7 8
7 9
8 8
8 9
4 4
4 4
7 10
9 10
10 10
7 8
9 10
9 10
9 9
5 9
10 10
3 8
1 2
4 10
9 10
1 2
7 ...

output:

1078095

result:

ok "1078095"

Test #36:

score: 0
Accepted
time: 39ms
memory: 5992kb

input:

154244
6 9
8 10
4 4
5 5
9 9
3 9
10 10
2 7
3 5
2 3
2 8
7 10
6 10
8 8
2 5
10 10
5 6
4 4
6 6
4 4
5 7
4 8
5 5
2 8
10 10
2 2
8 8
6 8
1 9
4 8
2 5
4 9
7 10
3 9
9 9
3 3
4 4
9 9
9 9
2 4
9 10
5 5
8 8
2 3
5 8
6 10
1 9
3 7
10 10
5 8
3 5
8 9
8 8
2 7
10 10
2 9
3 8
8 10
3 9
10 10
5 8
2 9
6 9
5 10
8 10
10 10
1 7
6 ...

output:

2570774

result:

ok "2570774"

Test #37:

score: 0
Accepted
time: 10ms
memory: 4976kb

input:

31108
4 7
10 10
5 10
3 7
2 6
10 10
2 2
8 9
8 10
7 7
5 8
4 5
3 4
5 7
5 5
10 10
5 8
7 10
10 10
1 6
8 8
5 7
6 9
7 7
6 7
6 6
8 10
7 8
3 9
10 10
6 10
10 10
10 10
5 5
3 7
2 3
10 10
9 10
8 9
8 10
7 8
9 9
9 9
8 10
9 9
3 9
9 9
10 10
4 5
5 8
8 10
9 9
6 9
5 10
9 9
7 10
9 9
10 10
5 6
2 2
2 10
2 3
8 8
4 10
1 9
9...

output:

107255

result:

ok "107255"

Test #38:

score: 0
Accepted
time: 6ms
memory: 5016kb

input:

15206
6 6
6 10
8 10
3 5
3 8
10 10
5 10
4 4
6 7
5 6
1 8
4 6
4 7
4 4
1 3
9 10
10 10
9 9
2 8
8 8
1 3
4 10
9 9
2 7
3 8
5 9
3 9
1 2
4 4
7 7
8 8
8 8
5 7
7 10
6 6
4 4
4 7
3 8
10 10
10 10
10 10
6 10
1 8
8 9
8 8
1 5
10 10
9 9
10 10
4 8
7 9
6 10
3 7
7 9
1 7
6 7
2 5
3 10
4 5
8 8
4 9
6 8
7 10
4 5
3 5
4 5
8 9
7 ...

output:

27575

result:

ok "27575"

Test #39:

score: 0
Accepted
time: 7ms
memory: 5324kb

input:

33942
1 1
9 9
2 10
4 6
8 8
1 9
3 3
5 10
3 4
4 8
2 2
5 7
8 9
10 10
1 3
6 8
2 6
5 8
7 9
5 5
1 10
5 10
1 2
4 7
10 10
8 10
8 8
1 1
5 10
10 10
1 6
2 9
4 5
6 9
6 9
6 6
4 7
7 8
9 9
4 9
1 6
9 9
1 5
1 9
7 9
4 10
1 6
3 4
1 10
5 5
6 7
7 8
7 10
5 9
7 9
9 10
9 9
9 10
4 9
1 6
6 8
8 10
10 10
3 5
3 9
9 9
3 7
3 7
2 ...

output:

129646

result:

ok "129646"

Test #40:

score: 0
Accepted
time: 15ms
memory: 5204kb

input:

52270
5 6
4 4
9 9
9 9
6 6
7 8
3 4
3 6
2 8
9 10
10 10
3 7
6 8
7 9
2 9
3 7
1 4
2 9
2 5
2 10
3 4
8 8
10 10
3 6
8 8
8 10
7 8
4 5
8 9
9 9
3 5
6 9
5 9
10 10
8 9
10 10
8 9
4 6
1 2
2 5
2 9
7 8
7 8
9 10
5 9
8 9
7 7
8 10
1 6
6 8
7 9
2 5
10 10
3 7
8 10
3 10
5 6
2 10
2 10
4 7
4 8
9 10
6 7
4 8
3 7
9 9
1 2
5 10
9...

output:

355057

result:

ok "355057"

Test #41:

score: 0
Accepted
time: 20ms
memory: 5452kb

input:

73881
5 10
7 10
3 8
2 7
7 10
7 8
10 10
1 3
3 9
6 10
8 8
2 4
1 1
7 10
10 10
6 10
3 4
2 4
2 3
9 9
8 8
2 2
5 10
7 8
2 2
7 8
7 8
6 10
10 10
2 3
2 9
10 10
3 10
7 8
6 9
1 4
10 10
10 10
6 10
4 6
4 10
5 5
1 6
6 7
9 10
10 10
10 10
10 10
8 8
3 7
8 10
3 10
7 10
1 7
7 7
4 7
6 8
10 10
9 10
9 9
2 2
1 6
4 9
5 7
5 ...

output:

570346

result:

ok "570346"

Test #42:

score: 0
Accepted
time: 6ms
memory: 5060kb

input:

31299
8 10
3 3
5 8
9 10
6 10
4 9
9 9
5 7
8 10
3 9
5 8
10 10
7 9
2 3
2 3
1 9
4 8
8 9
6 8
9 9
10 10
1 9
5 9
4 5
4 5
9 10
10 10
2 10
8 9
8 10
10 10
6 9
6 7
5 9
4 4
5 10
6 8
9 9
8 9
10 10
9 9
7 10
3 5
6 7
7 9
8 9
3 8
7 7
10 10
3 4
2 9
5 10
4 5
8 10
1 3
1 2
6 6
10 10
2 9
10 10
8 8
7 10
9 10
9 9
8 10
10 1...

output:

187751

result:

ok "187751"

Test #43:

score: 0
Accepted
time: 29ms
memory: 5808kb

input:

110539
6 6
2 10
8 9
1 6
2 5
7 8
2 5
9 10
7 10
1 5
10 10
5 7
1 8
7 10
7 10
1 1
6 7
3 3
8 8
5 7
1 8
2 3
7 9
4 6
7 8
5 8
5 7
4 8
2 2
1 7
5 7
4 9
7 8
6 10
10 10
8 8
7 9
4 4
8 9
8 8
3 10
4 8
10 10
7 10
7 10
3 6
8 10
6 7
6 6
7 10
5 5
8 9
8 9
5 6
10 10
10 10
3 10
3 9
6 10
2 10
5 7
10 10
6 9
8 8
7 8
5 8
4 9...

output:

1387215

result:

ok "1387215"

Test #44:

score: 0
Accepted
time: 73ms
memory: 12036kb

input:

110772
914 957
2 801
14 171
312 902
749 793
392 514
431 770
832 948
434 570
941 968
28 590
637 908
226 280
758 809
607 918
986 987
199 471
737 910
780 791
573 733
404 813
764 969
111 867
532 820
16 800
718 749
999 999
420 929
736 863
906 954
682 711
22 488
939 981
709 904
125 133
129 343
37 934
349 ...

output:

804

result:

ok "804"

Test #45:

score: 0
Accepted
time: 151ms
memory: 17320kb

input:

185368
406 988
30 287
902 921
21 907
155 276
258 785
965 976
520 809
39 783
479 741
648 923
578 694
71 911
180 291
520 871
38 457
279 918
88 971
974 974
973 987
353 509
504 734
399 638
997 998
364 846
153 588
424 941
465 896
70 84
295 700
661 719
806 928
859 897
499 511
376 638
282 588
155 550
979 9...

output:

1404

result:

ok "1404"

Test #46:

score: 0
Accepted
time: 59ms
memory: 10360kb

input:

80624
561 929
218 448
718 902
982 987
845 883
752 956
955 974
206 613
699 875
187 770
911 949
519 661
861 891
844 923
982 1000
196 855
822 927
823 823
405 902
251 891
125 325
963 990
590 707
109 375
218 453
782 785
550 732
471 724
581 697
9 853
885 928
338 405
579 882
975 980
214 879
131 383
111 193...

output:

621

result:

ok "621"

Test #47:

score: 0
Accepted
time: 110ms
memory: 14440kb

input:

143557
402 538
947 965
576 612
462 532
514 908
750 972
217 241
864 989
22 376
575 802
353 366
493 821
425 514
494 519
373 458
571 799
853 883
78 383
253 990
835 909
480 659
574 807
692 722
432 697
752 859
36 993
521 609
793 946
662 893
92 245
856 922
513 659
684 907
938 982
800 954
650 975
997 998
3...

output:

1127

result:

ok "1127"

Test #48:

score: 0
Accepted
time: 62ms
memory: 11328kb

input:

97315
921 978
44 442
730 995
55 424
211 642
549 943
713 996
603 855
404 815
111 336
135 421
106 146
499 633
812 962
235 398
702 801
840 979
935 979
476 800
792 915
858 996
807 875
444 701
704 942
266 963
319 872
250 526
993 993
885 954
388 876
242 872
918 983
524 607
23 890
914 969
493 997
408 552
5...

output:

769

result:

ok "769"

Test #49:

score: 0
Accepted
time: 200ms
memory: 17232kb

input:

174383
76731 80769
85582 95535
19527 43540
94601 99568
38575 56730
35290 87476
10792 96791
80394 97170
51523 64441
32819 90767
63286 64139
65364 74552
294 71141
33859 62528
93404 99486
84072 93907
73792 97534
68529 91753
79929 87975
80139 94432
38293 83292
55685 70159
39853 82939
26848 51659
97417 9...

output:

25

result:

ok "25"

Test #50:

score: 0
Accepted
time: 95ms
memory: 11536kb

input:

93777
87944 91152
62892 65645
48426 87227
60393 82604
79777 85414
73837 97347
24107 96990
42891 57126
21472 58814
6801 57465
35241 62362
11812 55806
75917 91116
85731 87423
964 57294
59594 78952
78592 91255
8220 92844
60447 60847
86613 98669
62965 84627
35882 52545
64305 65671
92560 96711
4709 43293...

output:

10

result:

ok "10"

Test #51:

score: 0
Accepted
time: 137ms
memory: 13368kb

input:

121407
75963 92805
24546 54809
68230 69763
10782 42279
57172 63316
19130 87426
11545 62298
47498 91468
57257 92032
83542 88387
88861 94437
90291 95231
17733 44223
60789 68488
97001 97378
90442 92111
11659 60597
43262 44779
38870 98005
66176 75009
90483 92721
52768 82985
58408 77859
93391 95806
90461...

output:

10

result:

ok "10"

Test #52:

score: 0
Accepted
time: 228ms
memory: 18400kb

input:

191232
60186 71460
31957 58347
21651 93700
68170 83397
47616 88359
24312 88455
44040 77256
81853 92907
11023 42472
69440 70079
60063 68260
45812 88833
52768 71301
92138 99441
8668 43189
75977 92087
94537 97781
59848 98664
744 64125
67663 83344
18730 32164
88050 93152
48528 83722
82036 99773
75264 92...

output:

21

result:

ok "21"

Test #53:

score: 0
Accepted
time: 37ms
memory: 7852kb

input:

43906
87915 96847
81273 99550
48700 50135
1358 28441
12646 92275
98632 98920
37813 68168
21427 36637
83036 89887
21771 60113
54940 86829
16113 89894
57706 60467
835 54265
20099 95356
34477 70331
12254 82997
28495 87764
77718 84667
29179 81009
15423 86791
93104 95631
94917 96000
46814 59063
38217 995...

output:

6

result:

ok "6"

Test #54:

score: 0
Accepted
time: 3ms
memory: 4672kb

input:

1774
37567 70592
43201 91437
23487 39287
3364 46553
27713 64954
60608 62209
44801 75576
5029 87679
93976 97206
91724 91753
60477 91040
69270 82652
71390 82966
44855 52372
47412 86699
90944 94082
42092 70069
77369 82664
66584 71354
12964 89901
54835 85054
22952 82050
31955 68706
85504 99610
52874 945...

output:

0

result:

ok "0"

Test #55:

score: 0
Accepted
time: 3ms
memory: 4664kb

input:

1800
32974 81533
16669 29888
93281 96507
94460 95605
55920 65922
42778 76669
69836 93949
64093 95925
57774 68096
8034 38375
48032 57942
57468 84556
58308 71521
63078 73104
96078 96254
2246 43247
70510 85361
91132 94357
62916 68027
51146 58479
42935 79272
69657 74810
43282 46997
22472 87407
55699 784...

output:

0

result:

ok "0"

Test #56:

score: 0
Accepted
time: 3ms
memory: 4644kb

input:

1518
59191 78247
755 28517
92246 96620
3272 10839
49333 96489
21708 71903
15460 17938
44600 53021
69185 74246
42516 94516
9079 88330
93456 97904
63143 88073
29205 67369
60109 92814
91463 93217
11679 24174
19369 57984
11816 15011
60627 84552
93747 94801
1228 32824
53999 95848
54128 64230
60091 98650
...

output:

0

result:

ok "0"

Test #57:

score: 0
Accepted
time: 3ms
memory: 4708kb

input:

1528
33112 61031
28427 39003
11377 49891
46672 53067
33232 49212
4633 72869
24045 54455
22277 29616
31520 71751
25296 43842
91557 96629
73788 86708
84635 89189
93834 99738
69097 96992
56405 69231
78604 98017
3838 86064
85161 95463
41424 52399
90273 99063
40180 97529
39432 65486
74584 79301
16940 197...

output:

0

result:

ok "0"

Test #58:

score: 0
Accepted
time: 0ms
memory: 4692kb

input:

1763
12939 97368
33041 98236
11715 45482
1719 57632
87676 90558
27668 29623
79058 97865
91402 97378
39917 67148
12302 84641
27194 35415
96153 97371
79754 88183
78688 96934
17757 21309
8823 69987
27660 62073
88455 92507
55464 80507
64762 64949
27411 66886
63560 76499
7028 59352
93173 97096
42572 7827...

output:

1

result:

ok "1"

Test #59:

score: 0
Accepted
time: 237ms
memory: 19072kb

input:

200000
74315 93217
25846 34468
2523 34907
30463 61813
51848 70658
97774 99836
55765 75465
63957 93101
88114 92292
96717 97357
12011 83987
93051 97530
80066 96531
7433 78113
60967 96641
99273 99301
88802 96129
10778 31592
52534 97659
1438 63315
69405 86265
77394 78241
37692 50742
6403 74250
19398 278...

output:

26

result:

ok "26"

Test #60:

score: 0
Accepted
time: 232ms
memory: 19000kb

input:

200000
61852 70790
64911 87515
56632 77757
32925 42571
66821 94204
21143 64398
50884 62267
55345 61701
35666 69162
57329 82265
80655 93118
26343 92565
24759 55374
79185 95023
75569 88041
17199 72837
49396 71904
44586 75753
20404 47643
64854 87074
88607 91720
9240 56600
73700 87357
15118 57088
36800 ...

output:

20

result:

ok "20"

Test #61:

score: 0
Accepted
time: 233ms
memory: 18996kb

input:

200000
25522 56519
44534 99842
70676 83315
49728 75079
92311 96965
13472 56857
34531 91033
46225 74142
20696 45316
37103 61615
93786 94183
42078 73170
9490 63324
59823 99830
45412 76251
30087 69067
48911 92255
69749 86481
36893 71063
65084 99270
97205 97286
73308 99759
39604 71707
36115 82097
99214 ...

output:

18

result:

ok "18"

Test #62:

score: 0
Accepted
time: 252ms
memory: 18964kb

input:

200000
49183 97119
2893 60208
26461 76165
98973 99426
96301 99550
63543 74867
88462 99444
2221 36607
79573 80362
28488 52296
89862 90975
11367 32542
49223 78548
84252 99751
87847 94592
47991 80180
90087 97401
42105 91428
66048 67588
66214 78112
3242 64538
6583 13010
49720 72487
85173 89483
33454 862...

output:

24

result:

ok "24"

Test #63:

score: 0
Accepted
time: 244ms
memory: 18996kb

input:

200000
95248 98622
27567 31521
50592 94404
81684 95553
81753 83838
76373 79050
31604 73027
68794 91040
96693 98125
96812 99768
3675 78308
38714 84314
57755 71057
81170 96504
81349 87696
35313 57443
95904 96455
91273 95488
62429 94946
44334 98062
57782 58133
94380 99635
82896 86147
28826 40501
88698 ...

output:

22

result:

ok "22"

Test #64:

score: 0
Accepted
time: 17ms
memory: 6032kb

input:

200000
10 10
17 17
1 1
30 30
20 20
11 11
14 14
8 8
1 1
27 27
4 4
18 18
4 4
15 15
17 17
13 13
24 24
4 4
23 23
9 9
13 13
2 2
23 23
9 9
18 18
7 7
4 4
19 19
28 28
26 26
9 9
24 24
11 11
23 23
2 2
12 12
6 6
24 24
13 13
5 5
26 26
12 12
14 14
3 3
2 2
11 11
17 17
21 21
13 13
6 6
5 5
28 28
2 2
9 9
6 6
8 8
27 ...

output:

20000100000

result:

ok "20000100000"

Test #65:

score: 0
Accepted
time: 23ms
memory: 7128kb

input:

200000
1 1
27 27
23 23
26 26
22 26
9 9
19 19
13 13
26 26
23 23
24 24
9 9
13 13
10 10
16 16
5 5
6 6
10 10
1 1
25 25
29 29
11 11
1 1
25 25
18 18
5 5
4 29
22 22
16 16
4 4
11 11
3 3
23 26
27 27
5 5
3 9
3 3
12 12
22 22
1 1
27 27
14 14
4 4
12 12
27 27
14 14
23 30
22 22
23 23
14 14
10 10
19 19
29 29
14 24
...

output:

2113979

result:

ok "2113979"

Test #66:

score: 0
Accepted
time: 59ms
memory: 11324kb

input:

200000
17 24
2 2
24 24
4 4
7 7
20 30
13 13
16 16
20 20
25 29
7 7
8 13
5 5
6 15
12 12
30 30
23 25
11 12
23 23
20 20
24 24
26 28
7 8
25 28
9 9
7 28
20 20
7 7
30 30
30 30
3 9
23 25
12 12
6 6
22 22
1 1
21 21
18 18
19 24
29 29
5 5
11 11
23 23
1 1
16 26
19 23
17 23
8 8
15 20
25 25
20 20
24 24
10 27
20 20
...

output:

263743

result:

ok "263743"

Test #67:

score: 0
Accepted
time: 140ms
memory: 18424kb

input:

199990
1 7
7 11
5 13
5 17
13 16
1 19
19 23
6 25
10 27
7 29
1 7
5 11
1 13
7 17
3 16
14 19
2 23
19 25
2 27
2 29
6 7
9 11
3 13
13 17
5 16
3 19
6 23
11 25
8 27
25 29
1 7
1 11
4 13
10 17
11 16
4 19
9 23
7 25
10 27
27 29
1 7
3 11
10 13
12 17
9 16
3 19
10 23
19 25
22 27
11 29
3 7
5 11
5 13
5 17
9 16
11 19
...

output:

0

result:

ok "0"

Test #68:

score: 0
Accepted
time: 40ms
memory: 5932kb

input:

200000
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 30
1 3...

output:

666573336

result:

ok "666573336"

Test #69:

score: 0
Accepted
time: 41ms
memory: 9232kb

input:

69918
21 27
11 29
11 27
18 19
15 17
1 25
10 12
9 29
1 21
10 17
9 19
4 4
12 22
7 28
4 27
16 21
4 27
7 9
4 30
8 24
3 15
16 23
17 23
3 5
15 15
21 25
1 7
5 12
17 27
17 26
16 17
5 7
3 11
9 27
8 11
21 22
2 2
19 26
16 21
7 9
18 21
12 21
1 4
2 23
21 27
16 23
5 17
27 28
5 7
18 27
8 28
4 22
18 21
4 17
17 29
1...

output:

3035

result:

ok "3035"

Test #70:

score: 0
Accepted
time: 3ms
memory: 4644kb

input:

4206
9 12
24 25
3 9
10 29
20 28
4 24
11 26
7 23
13 18
7 30
11 18
12 22
8 11
18 25
3 27
7 30
6 7
25 27
3 19
5 14
15 19
13 19
7 16
8 27
2 6
18 26
1 29
17 22
1 22
1 28
12 14
6 30
1 18
18 23
10 22
16 25
19 24
8 28
11 30
26 27
9 18
13 29
20 29
10 23
16 30
2 13
22 30
11 12
13 14
14 29
15 19
22 27
1 14
5 1...

output:

174

result:

ok "174"

Test #71:

score: 0
Accepted
time: 148ms
memory: 17860kb

input:

199951
11 26
13 23
2 13
25 25
20 28
12 24
13 30
12 13
14 21
15 21
5 18
11 30
16 18
10 18
5 26
14 21
10 12
16 28
2 23
13 25
10 28
4 22
18 27
15 17
7 16
15 26
1 27
14 29
1 16
14 30
9 11
8 10
8 20
16 22
14 15
3 8
19 25
12 18
1 12
2 28
14 16
2 12
8 26
13 24
8 9
3 3
6 8
10 14
7 11
2 29
2 29
3 18
7 13
3 9...

output:

8672

result:

ok "8672"

Test #72:

score: 0
Accepted
time: 75ms
memory: 12832kb

input:

126098
25 26
8 16
6 23
10 26
3 30
26 26
11 14
3 16
3 19
9 23
10 28
12 12
3 19
9 23
7 17
5 15
8 9
4 14
9 29
18 20
5 6
3 13
1 21
20 20
22 23
21 23
7 14
14 24
18 18
13 27
6 9
15 25
9 11
20 28
24 28
25 27
14 30
2 8
5 9
1 27
16 25
14 16
4 30
1 24
10 24
9 26
16 16
5 12
5 14
4 5
28 29
13 25
13 21
2 4
8 25
...

output:

5644

result:

ok "5644"

Test #73:

score: 0
Accepted
time: 24ms
memory: 8096kb

input:

53265
23 26
2 16
9 10
24 29
5 21
5 17
5 9
9 13
1 29
8 29
2 3
18 26
1 3
7 21
17 27
24 27
9 11
10 17
5 7
17 20
6 22
30 30
4 20
27 30
5 18
3 30
7 20
11 17
2 4
1 30
8 8
12 22
8 16
15 21
14 18
5 19
7 29
7 28
22 28
11 27
6 8
19 28
26 29
14 25
18 25
2 13
13 25
1 19
5 11
8 13
20 27
2 22
9 17
29 29
4 20
19 2...

output:

2418

result:

ok "2418"

Test #74:

score: 0
Accepted
time: 43ms
memory: 9932kb

input:

81813
17 23
14 28
6 28
8 22
18 28
2 4
9 30
13 25
6 21
1 2
4 6
1 9
10 21
2 18
16 24
17 30
20 24
25 29
22 24
23 28
6 27
2 14
9 24
6 13
22 28
10 24
9 9
19 22
20 30
18 24
17 26
6 10
16 24
18 27
9 23
19 23
12 30
24 30
16 23
5 22
7 15
4 28
18 26
14 15
28 29
8 29
19 20
13 28
2 8
5 21
9 30
18 29
12 30
4 7
8...

output:

3639

result:

ok "3639"

Test #75:

score: 0
Accepted
time: 112ms
memory: 15716kb

input:

168554
22 23
22 23
22 25
11 20
6 16
8 29
10 10
13 13
7 24
8 9
2 21
15 23
17 24
8 13
12 16
1 12
1 4
2 29
6 13
5 14
6 26
11 19
5 12
10 27
15 25
8 18
26 29
5 28
6 22
5 22
17 22
23 30
9 10
3 25
10 21
14 24
17 21
11 26
1 28
13 17
4 13
1 5
4 26
7 9
7 14
9 14
1 15
12 22
1 16
5 9
5 10
18 27
2 6
19 20
1 30
7...

output:

7201

result:

ok "7201"

Test #76:

score: 0
Accepted
time: 35ms
memory: 8608kb

input:

62596
3 9
18 27
12 13
8 10
12 20
16 16
20 24
8 13
11 24
8 9
14 27
8 21
7 10
12 14
14 15
10 30
9 10
6 14
2 21
2 21
13 14
5 19
7 10
22 29
15 29
7 12
5 7
16 28
15 29
13 26
10 19
8 22
10 26
28 29
21 26
2 16
10 18
11 20
5 29
20 23
5 20
8 22
21 29
10 22
16 27
9 12
25 27
9 27
14 20
10 15
4 29
4 23
4 23
19 ...

output:

2796

result:

ok "2796"

Test #77:

score: 0
Accepted
time: 7ms
memory: 6644kb

input:

29743
8 15
22 27
17 30
8 10
4 21
5 21
22 23
4 17
14 28
25 25
15 18
22 22
15 19
3 25
18 27
18 24
2 11
4 24
2 9
28 29
3 21
7 9
26 27
4 16
3 7
18 29
19 22
17 25
25 25
4 22
20 24
4 26
14 23
14 28
22 28
10 30
2 23
5 13
10 13
7 17
11 11
17 23
21 25
5 15
9 11
26 28
3 24
18 24
1 1
1 23
3 28
6 19
11 18
15 25...

output:

1300

result:

ok "1300"

Test #78:

score: 0
Accepted
time: 118ms
memory: 16144kb

input:

170442
13 17
2 4
27 28
9 11
21 23
28 28
6 15
12 13
26 30
7 30
24 26
8 25
9 11
7 28
10 25
9 27
1 28
5 6
7 24
7 11
11 14
8 11
2 9
5 22
1 18
22 26
8 29
13 17
4 7
1 22
6 28
1 25
25 29
2 5
3 13
22 28
14 25
10 30
4 10
4 8
11 20
8 16
4 25
10 15
15 20
18 25
11 27
2 6
5 25
1 24
1 10
3 22
5 15
20 26
6 21
22 3...

output:

7453

result:

ok "7453"

Extra Test:

score: 0
Extra Test Passed