QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#678285#9530. A Game On Treeucup-team133#AC ✓696ms35348kbC++2319.7kb2024-10-26 14:33:242024-10-26 14:33:26

Judging History

This is the latest submission verdict.

  • [2024-10-26 14:33:26]
  • Judged
  • Verdict: AC
  • Time: 696ms
  • Memory: 35348kb
  • [2024-10-26 14:33:24]
  • Submitted

answer

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    for (std::string_view sep = ""; const auto& e : v) {
        os << std::exchange(sep, " ") << e;
    }
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) {
    return y < x and (x = std::forward<U>(y), true);
}

template <class T, class U = T> bool chmax(T& x, U&& y) {
    return x < y and (x = std::forward<U>(y), true);
}

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    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._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    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._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using namespace std;

using ll = long long;

template <class TreeDP> struct Rerooting {
    using T = TreeDP::T;

    struct edge {
        int to, e;
        T dp, ndp;
        edge(int to, int e, T dp, T ndp) : to(to), e(e), dp(dp), ndp(ndp) {}
    };

    std::vector<std::vector<edge>> G;
    unordered_map<ll, T> mp;

    Rerooting(int n, const TreeDP& treedp) : n(n), m(0), G(n), treedp(treedp), sub(n), dp(n) {}

    void add_edge(int u, int v) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        G[u].emplace_back(v, m, treedp.e(), treedp.e());
        G[v].emplace_back(u, m, treedp.e(), treedp.e());
        m++;
    }

    std::vector<T> solve() {
        dfs_sub(0, -1);
        dfs_all(0, -1, treedp.e());
        return dp;
    }

    T get(int u, int v) { return mp[1LL * n * u + v]; }

  private:
    int n, m;
    TreeDP treedp;
    std::vector<T> sub, dp;

    void dfs_sub(int v, int p) {
        sub[v] = treedp.e();
        for (auto& e : G[v]) {
            if (e.to == p) continue;
            dfs_sub(e.to, v);
            sub[v] = treedp.op(sub[v], treedp.add_edge(sub[e.to], e.e));
        }
        sub[v] = treedp.add_vertex(sub[v], v);
    }

    void dfs_all(int v, int p, const T& top) {
        T cur = treedp.e();
        for (int i = 0; i < int(G[v].size()); i++) {
            auto& e = G[v][i];
            e.ndp = cur;
            e.dp = treedp.add_edge(e.to == p ? top : sub[e.to], e.e);
            cur = treedp.op(cur, e.dp);
        }
        dp[v] = treedp.add_vertex(cur, v);
        cur = treedp.e();
        for (int i = int(G[v].size()) - 1; i >= 0; i--) {
            auto& e = G[v][i];
            e.ndp = treedp.add_vertex(treedp.op(e.ndp, cur), v);
            if (e.to != p) {
                mp[1LL * n * v + e.to] = e.ndp;
                mp[1LL * n * e.to + v] = sub[e.to];
                dfs_all(e.to, v, e.ndp);
            }
            cur = treedp.op(cur, e.dp);
        }
    }
};

using mint = atcoder::modint998244353;

struct TreeDP {
    struct T {
        mint n, sum;
    };

    T e() { return T(0, 0); }

    T op(const T& l, const T& r) { return T(l.n + r.n, l.sum + r.sum); }

    T add_vertex(const T& t, int v) { return T(t.n + 1, t.sum); }

    T add_edge(const T& t, int e) { return T(t.n, t.sum + t.n * t.n); }
};

void solve() {
    int n;
    cin >> n;
    TreeDP treedp;
    Rerooting<TreeDP> G(n, treedp);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        G.add_edge(--u, --v);
    }

    mint ans = 0, tot = mint(n) * (n - 1) / 2;
    G.solve();
    for (int i = 0; i < n; i++) {
        for (auto e : G.G[i]) {
            int j = e.to;
            if (j < i) continue;
            {
                // 同一辺
                auto p = G.get(i, j);
                auto s = p.n;
                ans += s * (n - s) * s * (n - s);
            }
            {
                auto p = G.get(i, j);
                auto s = p.n, sum = p.sum;
                ans += (n - s) * sum * (n - s);
            }
            {
                auto p = G.get(j, i);
                auto s = p.n, sum = p.sum;
                ans += (n - s) * sum * (n - s);
            }
        }
    }
    ans /= tot * tot;

    cout << ans.val() << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int T;
    cin >> T;
    for (; T--;) solve();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3536kb

input:

2
3
1 2
2 3
5
1 2
1 5
3 2
4 2

output:

443664158
918384806

result:

ok 2 lines

Test #2:

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

input:

1000
7
3 6
4 3
5 3
2 6
1 4
7 1
12
5 7
10 7
2 10
11 2
1 7
8 1
4 2
9 11
6 9
12 11
3 5
6
2 5
1 2
4 5
6 4
3 6
5
2 5
1 5
4 5
3 2
8
1 8
2 8
4 2
6 1
5 6
7 6
3 8
8
3 8
7 3
4 8
6 4
2 7
5 2
1 4
4
3 1
4 3
2 1
6
5 1
6 1
2 5
3 5
4 2
12
8 11
5 11
12 8
3 12
6 12
2 3
4 6
10 11
1 5
9 5
7 5
9
6 1
7 6
4 7
8 7
5 4
9 6
...

output:

948445317
468414020
550143557
918384806
711758412
487662742
776412276
869581749
240852807
765628773
211048577
887328316
890334966
940494682
760637552
908032643
592850815
584006902
908525604
221832080
433351719
56023919
867301808
183319566
698771049
366957926
449579681
599710576
310564911
286902823
3...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 45ms
memory: 3852kb

input:

1000
94
59 1
33 59
73 1
6 33
83 59
4 59
20 59
61 6
39 1
76 73
71 6
44 39
9 71
24 4
87 9
57 83
2 9
81 71
82 20
90 2
85 39
12 9
30 83
66 30
53 9
47 9
36 44
43 53
29 12
31 53
64 81
38 31
84 82
77 38
23 71
93 84
78 83
58 31
68 90
42 1
55 64
13 78
70 78
62 24
19 55
92 87
14 57
10 84
65 81
63 6
75 36
91 1...

output:

508107725
996793960
201633249
335988372
842755864
460619380
342223697
207523414
429241811
391691799
542977964
786416604
454278948
685531402
25914978
440729774
228518323
679471537
82764520
554190841
432505337
143444089
189106586
337234245
61954935
905141094
532919674
703954588
185671863
942858630
692...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 681ms
memory: 34464kb

input:

10000
8
1 4
3 1
5 1
7 3
8 4
6 8
2 7
8
2 6
4 6
5 6
8 5
7 6
3 5
1 7
8
8 5
6 5
2 5
7 2
1 6
3 1
4 8
9
8 6
9 8
3 6
1 8
5 9
2 8
4 3
7 9
8
8 6
3 6
5 8
1 6
4 3
7 6
2 6
9
9 5
7 5
2 7
8 7
4 9
3 7
6 3
1 4
8
1 4
5 1
6 5
3 4
8 4
7 8
2 5
9
1 8
6 1
2 1
3 8
5 3
9 8
7 8
4 8
9
4 9
2 9
1 2
3 4
5 2
6 9
8 3
7 2
8
1 2
8 ...

output:

49657566
56023919
387074343
97051536
701572244
211048577
711758412
308100110
761007271
711758412
178698065
285212675
80216065
43380497
267677376
818005792
53239701
765628773
970145625
387074343
436731906
422725927
479157293
977872021
436731906
925779210
487662742
705549251
267677376
711758412
526851...

result:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 685ms
memory: 35088kb

input:

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

output:

711758412
286902823
691130166
841483019
650641410
887328317
331207619
733278261
56023919
977872021
414394648
183319566
239374924
696059768
855285904
761007271
711758412
86268032
599710576
728310932
178698065
178698065
422725927
219002589
178698065
202450068
599710576
56023919
449579681
760637552
925...

result:

ok 10000 lines

Test #6:

score: 0
Accepted
time: 676ms
memory: 35260kb

input:

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

output:

211048577
354315128
178698065
705549251
285212675
138645051
449579681
286902823
925779210
294297225
519087065
368179632
422725927
603876215
539175192
867301808
977540027
669439919
211048577
701572244
977872021
138645051
267677376
855285904
977872021
286902823
925286249
705549251
219002589
331207619
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 675ms
memory: 33048kb

input:

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

output:

422725927
977872021
867301808
407446676
466833287
387074343
97051536
292325385
301691628
765628773
285212675
711758412
650641410
178698065
543242114
286902823
473241769
109930120
841975980
836553418
422725927
286902823
414394648
739440264
436731906
56023919
436731906
530918109
603876215
977872021
40...

result:

ok 10000 lines

Test #8:

score: 0
Accepted
time: 683ms
memory: 35348kb

input:

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

output:

409773147
306621231
836553418
760637552
519087065
304649390
97051536
742521264
387074343
855285904
874737082
358875008
733278261
698524570
908525604
387074343
970145625
449579681
286902823
239374924
650641410
691130166
765628773
603876215
839572800
977872021
742521264
908032643
874737082
299719788
7...

result:

ok 10000 lines

Test #9:

score: 0
Accepted
time: 685ms
memory: 34500kb

input:

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

output:

331207619
28098733
97051536
599710576
701572244
277043619
368179632
138645051
711758412
626059423
86268032
414394648
368179632
993314752
321410036
530918109
711758412
712327454
603876215
49657566
705549251
765628773
56023919
299719788
887328316
839572800
650641410
211048577
286902823
908032643
28690...

result:

ok 10000 lines

Test #10:

score: 0
Accepted
time: 696ms
memory: 33532kb

input:

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

output:

440213438
977872021
285212675
285212675
705549251
267677376
436731906
267677376
440213438
712327454
711758412
191268549
321410036
436731906
839572800
49657566
519087065
178698065
977872021
285212675
574298605
368179632
466833287
696059768
86268033
308100110
487662742
887328317
977872021
701572244
99...

result:

ok 10000 lines

Test #11:

score: 0
Accepted
time: 678ms
memory: 32696kb

input:

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

output:

202450068
449579681
742521264
56023919
705549251
599710576
765628773
887328316
599710576
97051536
286902823
603876215
321410036
221832080
294297225
479157293
650641410
765628773
908525604
285212675
125704848
414394648
599254713
286902823
707938599
13864507
599710576
304649390
691130166
56023919
7656...

result:

ok 10000 lines

Test #12:

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

input:

1
2
1 2

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed