QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#495773#9141. Array Spreaducup-team180#TL 2497ms4128kbC++1754.3kb2024-07-27 23:34:152024-07-27 23:34:15

Judging History

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

  • [2024-09-18 18:58:44]
  • hack成功,自动添加数据
  • (/hack/840)
  • [2024-09-18 18:53:02]
  • hack成功,自动添加数据
  • (/hack/839)
  • [2024-07-29 03:53:23]
  • hack成功,自动添加数据
  • (/hack/753)
  • [2024-07-29 03:51:16]
  • hack成功,自动添加数据
  • (/hack/752)
  • [2024-07-29 03:50:24]
  • hack成功,自动添加数据
  • (/hack/751)
  • [2024-07-29 03:48:52]
  • hack成功,自动添加数据
  • (/hack/750)
  • [2024-07-27 23:34:15]
  • 评测
  • 测评结果:TL
  • 用时:2497ms
  • 内存:4128kb
  • [2024-07-27 23:34:15]
  • 提交

answer

#pragma region Macros
#ifdef noimi
#pragma comment(linker, "/stack:256000000")
#include "my_template.hpp"
#else
// #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <immintrin.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>

#ifdef noimi
#define oj_local(a, b) b
#else
#define oj_local(a, b) a
#endif

#define LOCAL if(oj_local(0, 1))
#define OJ if(oj_local(1, 0))

using namespace std;
using ll = long long;
using ull = unsigned long long int;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>;
using vl = vc<ll>;
using vpi = vc<pii>;
using vpl = vc<pll>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T> int si(const T &x) { return x.size(); }
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
vi iota(int n) {
    vi a(n);
    return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(res.begin(), res.end(), 0);
    sort(res.begin(), res.end(), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}

// macros
#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define endl '\n'
#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)
#define REP1(i, n) for(ll i = 0; i < (n); ++i)
#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)
#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)
#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)
#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))
#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)
#define fore0(a) rep(a.size())
#define fore1(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)
#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)
#define setbits(j, n) for(ll iiiii = (n), j = lowbit(iiiii); iiiii; iiiii ^= 1 << j, j = lowbit(iiiii))
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(all(v)));)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define drop(s) cout << #s << endl, exit(0)
#define si(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
#define rng(v, l, r) v.begin() + (l), v.begin() + (r)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(v) sort(all(v))
#define REV(v) reverse(all(v))
#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())
template <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};
constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

namespace yesno_impl {
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
const string firstsecond[2] = {"second", "first"};
const string FirstSecond[2] = {"Second", "First"};
const string possiblestr[2] = {"impossible", "possible"};
const string Possiblestr[2] = {"Impossible", "Possible"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
void first(bool t = 1) { cout << firstsecond[t] << endl; }
void First(bool t = 1) { cout << FirstSecond[t] << endl; }
void possible(bool t = 1) { cout << possiblestr[t] << endl; }
void Possible(bool t = 1) { cout << Possiblestr[t] << endl; }
}; // namespace yesno_impl
using namespace yesno_impl;

#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define INTd(...)                                                                                                                                              \
    int __VA_ARGS__;                                                                                                                                           \
    IN2(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define LLd(...)                                                                                                                                               \
    ll __VA_ARGS__;                                                                                                                                            \
    IN2(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define VECd(type, name, size)                                                                                                                                 \
    vector<type> name(size);                                                                                                                                   \
    IN2(name)
#define VEC2(type, name1, name2, size)                                                                                                                         \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i])
#define VEC2d(type, name1, name2, size)                                                                                                                        \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i])
#define VEC3(type, name1, name2, name3, size)                                                                                                                  \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])
#define VEC3d(type, name1, name2, name3, size)                                                                                                                 \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i])
#define VEC4(type, name1, name2, name3, name4, size)                                                                                                           \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);
#define VEC4d(type, name1, name2, name3, name4, size)                                                                                                          \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i], name4[i]);
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define VVd(type, name, h, w)                                                                                                                                  \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN2(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
void IN2() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
template <class Head, class... Tail> void IN2(Head &head, Tail &...tail) {
    scan(head);
    --head;
    IN2(tail...);
}

template <int p = -1> void pat() {}
template <int p = -1, class Head, class... Tail> void pat(Head &h, Tail &...tail) {
    h += p;
    pat<p>(tail...);
}

template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}

template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <typename T, typename S, typename U> U bigmul(const T &x, const S &y, const U &lim) { // clamp(x * y, -lim, lim)
    if(x < 0 and y < 0) return bigmul(-x, -y, lim);
    if(x < 0) return -bigmul(-x, y, lim);
    if(y < 0) return -bigmul(x, -y, lim);
    return y == 0 or x <= lim / y ? x * y : lim;
}
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
template <class T, class S> T POW(T x, S n, const ll &mod) {
    T res = 1;
    x %= mod;
    for(; n; n >>= 1, x = x * x % mod)
        if(n & 1) res = res * x % mod;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    UNIQUE(y);
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
    return v;
}

template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
    vector<T> w(v.size());
    rep(i, si(id)) w[i] = v[id[i]];
    v.swap(w);
}
// 並び替える順番, 並び替える vector 達
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
    rearrange_exec(id, a);
    rearrange(id, tail...);
}

template <typename T> vector<T> RUI(const vector<T> &v) {
    vector<T> res(v.size() + 1);
    for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
    return res;
}
template <typename T> void zeta_supersetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] += f[b | i];
}

template <typename T> void zeta_subsetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] += f[b];
}
template <typename T> void mobius_subset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] -= f[b | i];
}
template <typename T> void mobius_superset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] -= f[b];
}
// 反時計周りに 90 度回転
template <typename T> void rot(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
    v.swap(res);
}

vector<int> counter(const vector<int> &v, int max_num = -1) {
    if(max_num == -1) max_num = MAX(v);
    vector<int> res(max_num + 1);
    fore(e, v) res[e]++;
    return res;
}

// x in [l, r)
template <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }
template <class T, class S> bool inc(const T &x, const pair<S, S> &p) { return p.first <= x and x < p.second; }

// 便利関数
constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
constexpr ll tri(ll n) { return n * (n + 1) / 2; }
// l + ... + r
constexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
// bit 演算系
#define bit(i) (1LL << i)       // (1 << i)
#define test(b, i) (b >> i & 1) // b の i bit 目が立っているか
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
// int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ull a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
constexpr ll mask(int n) { return (1LL << n) - 1; }
// int popcount(signed t) { return __builtin_popcount(t); }
// int popcount(ll t) { return __builtin_popcountll(t); }
int popcount(uint64_t t) { return __builtin_popcountll(t); }
static inline uint64_t popcount64(uint64_t x) {
    uint64_t m1 = 0x5555555555555555ll;
    uint64_t m2 = 0x3333333333333333ll;
    uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;
    uint64_t h01 = 0x0101010101010101ll;

    x -= (x >> 1) & m1;
    x = (x & m2) + ((x >> 2) & m2);
    x = (x + (x >> 4)) & m4;

    return (x * h01) >> 56;
}
bool ispow2(int i) { return i && (i & -i) == i; }

ll rnd(ll l, ll r) { //[l, r)
#ifdef noimi
    static mt19937_64 gen;
#else
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
    return uniform_int_distribution<ll>(l, r - 1)(gen);
}
ll rnd(ll n) { return rnd(0, n); }

template <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }

int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
// 開閉
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
    fore(e, v) e++;
    return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e++;
    return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
    fore(e, v) e--;
    return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e--;
    return res;
}
template <class T> void connect(vector<T> &l, const vector<T> &r) { fore(e, r) l.eb(e); }
template <class T> vector<T> operator+(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] += r[i];
    return res;
}
template <class T> vector<T> operator-(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] -= r[i];
    return res;
}
template <class T> vector<T> &operator+=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] += r[i];
    return l;
}
template <class T> vector<T> &operator-=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] -= r[i];
    return l;
}
template <class T> vector<T> &operator+=(vector<T> &v, const T &x) {
    fore(e, v) e += x;
    return v;
}
template <class T> vector<T> &operator-=(vector<T> &v, const T &x) {
    fore(e, v) e -= x;
    return v;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
    friend ostream operator<<(ostream &os, const edge &e) { return os << e.to; }
};
template <typename T> using Edges = vector<edge<T>>;

template <typename T = int> Edges<T> read_edges(int m, bool weighted = false) {
    Edges<T> res;
    res.reserve(m);
    for(int i = 0; i < m; i++) {
        int u, v, c = 0;
        scan(u), scan(v), u--, v--;
        if(weighted) scan(c);
        res.eb(u, v, c, i);
    }
    return res;
}

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return res;
}
Graph getTreeFromPar(int n, int margin = 1) {
    Graph res(n);
    for(int i = 1; i < n; i++) {
        int a;
        cin >> a;
        res[a - margin].emplace_back(i);
    }
    return res;
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        scan(a), scan(b), scan(c);
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return res;
}
void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }
template <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }

#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)

i128 abs(const i128 &x) { return x > 0 ? x : -x; }
istream &operator>>(istream &is, i128 &v) {
    string s;
    is >> s;
    v = 0;
    for(int i = 0; i < (int)s.size(); i++) {
        if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
    }
    if(s[0] == '-') { v *= -1; }
    return is;
}

ostream &operator<<(ostream &os, const i128 &v) {
    if(v == 0) { return (os << "0"); }
    i128 num = v;
    if(v < 0) {
        os << '-';
        num = -num;
    }
    string s;
    for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
    reverse(s.begin(), s.end());
    return (os << s);
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
    static void output(std::ostream &os, const T &v) {
        os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
        tp<T, N + 1, L>::output(os, v);
    }
};
template <typename T, unsigned N> struct tp<T, N, N> {
    static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
    if(&os == &cerr) { os << '('; }
    aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
    if(&os == &cerr) { os << ')'; }
    return os;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
    auto pq = _pq;
    vector<T> res;
    while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
    return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
    return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
    bool f = true;
    if(&os == &cerr) os << "[";
    for(auto &y : x) {
        if(&os == &cerr)
            os << (f ? "" : ", ") << y;
        else
            os << (f ? "" : " ") << y;
        f = false;
    }
    if(&os == &cerr) os << "]";
    return os;
}

#define dump(...) static_cast<void>(0)
#define dbg(...) static_cast<void>(0)

void OUT() { cout << endl; }
template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {
    cout << head;
    if(sizeof...(tail)) cout << ' ';
    OUT(tail...);
}

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};

template <class T> void OUT2(const T &t, T INF = inf<T>, T res = -1) { OUT(t != INF ? t : res); }
template <class T> void OUT2(vector<T> &v, T INF = inf<T>, T res = -1) {
    fore(e, v) if(e == INF) e = res;
    OUT(v);
    fore(e, v) if(e == res) e = INF;
}

template <class F> struct REC {
    F f;
    REC(F &&f_) : f(forward<F>(f_)) {}
    template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }
};

template <class S> vector<pair<S, int>> runLength(const vector<S> &v) {
    vector<pair<S, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}
vector<pair<char, int>> runLength(const string &v) {
    vector<pair<char, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}

struct string_converter {
    char start = 0;
    char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); }
    int convert(const char &c) {
        if(!start) start = type(c);
        return c - start;
    }
    int convert(const char &c, const string &chars) { return chars.find(c); }
    template <typename T> auto convert(const T &v) {
        vector<decltype(convert(v[0]))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e));
        return ret;
    }
    template <typename T> auto convert(const T &v, const string &chars) {
        vector<decltype(convert(v[0], chars))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e, chars));
        return ret;
    }
    int operator()(const char &v, char s = 0) {
        start = s;
        return convert(v);
    }
    int operator()(const char &v, const string &chars) { return convert(v, chars); }
    template <typename T> auto operator()(const T &v, char s = 0) {
        start = s;
        return convert(v);
    }
    template <typename T> auto operator()(const T &v, const string &chars) { return convert(v, chars); }
} toint;

template <class T, class F> T bin_search(T ok, T ng, const F &f) {
    while(abs(ok - ng) > 1) {
        T mid = ok + ng >> 1;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
    while(iter--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(11);
    }
} setup_io;

#endif

#pragma endregion

template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
    const auto INF = numeric_limits<T>::max();
    vector<T> dist(V, INF);
    dist[s] = 0;
    for(int i = 0; i < V - 1; i++) {
        for(auto &e : edges) {
            if(dist[e.from] == INF) continue;
            dist[e.to] = min(dist[e.to], dist[e.from] + e.cost);
        }
    }
    for(auto &e : edges) {
        if(dist[e.from] == INF) continue;
        if(dist[e.from] + e.cost < dist[e.to]) return vector<T>();
    }
    return dist;
}

namespace modular {
constexpr int MOD = 998244353;
const int MAXN = 11000000;
template <int Modulus> class modint;
using mint = modint<MOD>;
using vmint = vector<mint>;
vector<mint> Inv;
mint inv(int x);
template <int Modulus> class modint {

  public:
    static constexpr int mod() { return Modulus; }
    int a;

    constexpr modint(const ll x = 0) noexcept : a(((x % Modulus) + Modulus) % Modulus) {}
    constexpr int &val() noexcept { return a; }
    constexpr const int &val() const noexcept { return a; }
    constexpr modint operator-() const noexcept { return modint() - *this; }
    constexpr modint operator+() const noexcept { return *this; }
    constexpr modint &operator++() noexcept {
        if(++a == MOD) a = 0;
        return *this;
    }
    constexpr modint &operator--() noexcept {
        if(!a) a = MOD;
        a--;
        return *this;
    }
    constexpr modint operator++(int) {
        modint res = *this;
        ++*this;
        return res;
    }
    constexpr modint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr modint &operator+=(const modint rhs) noexcept {
        a += rhs.a;
        if(a >= Modulus) { a -= Modulus; }
        return *this;
    }
    constexpr modint &operator-=(const modint rhs) noexcept {
        if(a < rhs.a) { a += Modulus; }
        a -= rhs.a;
        return *this;
    }
    constexpr modint &operator*=(const modint rhs) noexcept {
        a = (long long)a * rhs.a % Modulus;
        return *this;
    }
    constexpr modint &operator/=(const modint rhs) noexcept {
        a = (long long)a * (modular::inv(rhs.a)).a % Modulus;
        return *this;
    }
    constexpr modint pow(long long n) const noexcept {
        if(n < 0) {
            n %= Modulus - 1;
            n = (Modulus - 1) + n;
        }
        modint x = *this, r = 1;
        while(n) {
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    constexpr modint inv() const noexcept { return pow(Modulus - 2); }
    constexpr friend modint operator+(const modint &lhs, const modint &rhs) { return modint(lhs) += modint(rhs); }
    constexpr friend modint operator-(const modint &lhs, const modint &rhs) { return modint(lhs) -= modint(rhs); }
    constexpr friend modint operator*(const modint &lhs, const modint &rhs) { return modint(lhs) *= modint(rhs); }
    constexpr friend modint operator/(const modint &lhs, const modint &rhs) { return modint(lhs) /= modint(rhs); }
    constexpr friend bool operator==(const modint &lhs, const modint &rhs) { return lhs.a == rhs.a; }
    constexpr friend bool operator!=(const modint &lhs, const modint &rhs) { return lhs.a != rhs.a; }
    // constexpr friend modint operator^=(const modint &lhs, const modint &rhs) { return modint(lhs) ^= modint(rhs); }
};
vmint Fact{1, 1}, Ifact{1, 1};
mint inv(int n) {
    if(n > MAXN) return (mint(n)).pow(MOD - 2);
    if(Inv.empty()) Inv.emplace_back(0), Inv.emplace_back(1);
    if(Inv.size() > n)
        return Inv[n];
    else {
        for(int i = Inv.size(); i <= n; ++i) {
            auto [y, x] = div(int(MOD), i);
            Inv.emplace_back(Inv[x] * (-y));
        }
        return Inv[n];
    }
}
mint fact(int n) {
    if(Fact.size() > n)
        return Fact[n];
    else
        for(int i = Fact.size(); i <= n; ++i) Fact.emplace_back(Fact[i - 1] * i);
    return Fact[n];
}
mint ifact(int n) {
    if(Ifact.size() > n)
        return Ifact[n];
    else
        for(int i = Ifact.size(); i <= n; ++i) Ifact.emplace_back(Ifact[i - 1] * inv(i));
    return Ifact[n];
}
mint modpow(ll a, ll n) { return mint(a).pow(n); }
mint inv(mint a) { return inv(a.a); }
mint ifact(mint a) { return ifact(a.a); }
mint fact(mint a) { return fact(a.a); }
mint modpow(mint a, ll n) { return modpow(a.a, n); }
mint C(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    if(a > MAXN) {
        mint res = 1;
        rep(i, b) res *= a - i, res /= i + 1;
        return res;
    }
    return fact(a) * ifact(b) * ifact(a - b);
}
mint P(int a, int b) {
    if(a < 0 || b < 0) return 0;
    if(a < b) return 0;
    if(a > MAXN) {
        mint res = 1;
        rep(i, b) res *= a - i;
        return res;
    }
    return fact(a) * ifact(a - b);
}
ostream &operator<<(ostream &os, mint a) {
    os << a.a;
    return os;
}
istream &operator>>(istream &is, mint &a) {
    ll x;
    is >> x;
    a = x;
    return is;
}
ostream &operator<<(ostream &os, const vmint &a) {
    if(!a.empty()) {
        os << a[0];
        for(int i = 1; i < si(a); i++) os << " " << a[i];
    }
    return os;
}
#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace convolution {

namespace internal {
int ceil_pow2(int n) {
    int x = 0;
    while((1U << x) < (unsigned int)(n)) x++;
    return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if(x < 0) x += m;
    return x;
}
struct barrett {
    unsigned int _m;
    unsigned long long im;
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
    unsigned int umod() const { return _m; }
    unsigned int mul(unsigned int a, unsigned int b) const {
        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;
    }
};
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;
}
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;
    for(long long a : {2, 7, 61}) {
        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);

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};
    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
        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    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);

void butterfly(std::vector<mint> &a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);

    static bool first = true;
    static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
    if(first) {
        first = false;
        mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for(int i = cnt2; i >= 2; i--) {
            // e^(2^i) == 1
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for(int i = 0; i < cnt2 - 2; i++) {
            sum_e[i] = es[i] * now;
            now *= ies[i];
        }
    }
    for(int ph = 1; ph <= h; ph++) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint now = 1;
        for(int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for(int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p] * now;
                a[i + offset] = l + r;
                a[i + offset + p] = l - r;
            }
            now *= sum_e[bsf(~(unsigned int)(s))];
        }
    }
}

void butterfly_inv(std::vector<mint> &a) {
    static constexpr int g = internal::primitive_root<mint::mod()>;
    int n = int(a.size());
    int h = internal::ceil_pow2(n);

    static bool first = true;
    static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
    if(first) {
        first = false;
        mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1
        int cnt2 = bsf(mint::mod() - 1);
        mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
        for(int i = cnt2; i >= 2; i--) {
            // e^(2^i) == 1
            es[i - 2] = e;
            ies[i - 2] = ie;
            e *= e;
            ie *= ie;
        }
        mint now = 1;
        for(int i = 0; i < cnt2 - 2; i++) {
            sum_ie[i] = ies[i] * now;
            now *= es[i];
        }
    }

    for(int ph = h; ph >= 1; ph--) {
        int w = 1 << (ph - 1), p = 1 << (h - ph);
        mint inow = 1;
        for(int s = 0; s < w; s++) {
            int offset = s << (h - ph + 1);
            for(int i = 0; i < p; i++) {
                auto l = a[i + offset];
                auto r = a[i + offset + p];
                a[i + offset] = l + r;
                a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val();
            }
            inow *= sum_ie[bsf(~(unsigned int)(s))];
        }
    }
    mint z = mint(n).inv();
    for(int i = 0; i < n; i++) a[i] *= z;
}

} // namespace internal

std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    if(!n || !m) return {};
    if(std::min(n, m) <= 60) {
        if(n < m) {
            std::swap(n, m);
            std::swap(a, b);
        }
        std::vector<mint> ans(n + m - 1);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; }
        }
        return ans;
    }
    int z = 1 << internal::ceil_pow2(n + m - 1);
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for(int i = 0; i < z; i++) { a[i] *= b[i]; }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    // mint iz = mint(z).inv();
    // for(int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

} // namespace convolution

using Poly = vmint;
Poly low(const Poly &f, int s) { return Poly(f.begin(), f.begin() + min<int>(max(s, 1), f.size())); }
Poly operator-(Poly f) {
    for(auto &&e : f) e = -e;
    return f;
}
Poly &operator+=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] += r[i];
    return l;
}
Poly operator+(Poly l, const Poly &r) { return l += r; }
Poly &operator-=(Poly &l, const Poly &r) {
    l.resize(max(l.size(), r.size()));
    rep(i, r.size()) l[i] -= r[i];
    return l;
}
Poly operator-(Poly l, const Poly &r) { return l -= r; }
Poly &operator<<=(Poly &f, size_t n) { return f.insert(f.begin(), n, 0), f; }
Poly operator<<(Poly f, size_t n) { return f <<= n; }
Poly &operator>>=(Poly &f, size_t n) { return f.erase(f.begin(), f.begin() + min(f.size(), n)), f; }
Poly operator>>(Poly f, size_t n) { return f >>= n; }
Poly operator*(const Poly &l, const Poly &r) { return convolution::convolution(l, r); }
Poly &operator*=(Poly &l, const Poly &r) { return l = l * r; }
Poly &operator*=(Poly &l, const mint &x) {
    for(auto &e : l) e *= x;
    return l;
}
Poly operator*(const Poly &l, const mint &x) {
    auto res = l;
    return res *= x;
}

Poly inv(const Poly &f, int s = -1) {
    if(s == -1) s = f.size();
    Poly r(s);
    r[0] = mint(1) / f[0];
    for(int n = 1; n < s; n *= 2) {
        auto F = low(f, 2 * n);
        F.resize(2 * n);
        convolution::internal::butterfly(F);
        auto g = low(r, 2 * n);
        g.resize(2 * n);
        convolution::internal::butterfly(g);
        rep(i, 2 * n) F[i] *= g[i];
        convolution::internal::butterfly_inv(F);
        rep(i, n) F[i] = 0;
        convolution::internal::butterfly(F);
        rep(i, 2 * n) F[i] *= g[i];
        convolution::internal::butterfly_inv(F);
        rep(i, n, min(2 * n, s)) r[i] -= F[i];
    }
    return r;
}
Poly integ(const Poly &f) {
    Poly res(f.size() + 1);
    for(int i = 1; i < (int)res.size(); ++i) res[i] = f[i - 1] / i;
    return res;
}
Poly deriv(const Poly &f) {
    if(f.size() == 0) return Poly();
    Poly res(f.size() - 1);
    rep(i, res.size()) res[i] = f[i + 1] * (i + 1);
    return res;
}
Poly log(const Poly &f) {
    Poly g = integ(inv(f) * deriv(f));
    return Poly{g.begin(), g.begin() + f.size()};
}
Poly exp(const Poly &f) {
    Poly g{1};
    while(g.size() < f.size()) {
        Poly x(f.begin(), f.begin() + min(f.size(), g.size() * 2));
        x[0] += 1;
        g.resize(2 * g.size());
        x -= log(g);
        x *= {g.begin(), g.begin() + g.size() / 2};
        rep(i, g.size() / 2, min<int>(x.size(), g.size())) g[i] = x[i];
    }
    return {g.begin(), g.begin() + f.size()};
}
Poly pow(const Poly &f, ll k, int need = -1) {
    const int n = (int)f.size();
    if(need == -1) need = n;
    int z = 0;
    rep(i, n) {
        if(f[i].a) break;
        z++;
    }
    if(z * k >= need) return Poly(n);
    mint rev = f[z].inv();
    auto ff = f;
    ff.resize(need);
    Poly res = exp(log((ff >> z) * rev) * k) * f[z].pow(k);
    res.resize(need - z * k);
    return res << z * k;
}

struct Prd {
    deque<Poly> deq;
    Prd() = default;
    void emplace(const Poly &f) { deq.emplace_back(f); }
    Poly calc() {
        if(deq.empty()) return {1};
        sort(all(deq), [&](const Poly &f, const Poly &g) { return si(f) < si(g); });
        while(deq.size() > 1) {
            deq.emplace_back(deq[0] * deq[1]);
            for(int i = 0; i < 2; ++i) deq.pop_front();
        }
        return deq.front();
    }
};
Poly prd(vector<Poly> &v) {
    Prd p;
    for(auto &e : v) p.emplace(e);
    return p.calc();
}

vmint power_table(mint x, int len) {
    vmint res(len + 1);
    res[0] = 1;
    rep(i, len) res[i + 1] = res[i] * x;
    return res;
}

// calc f(x + a)
Poly TaylorShift(Poly f, mint a) {
    int n = f.size();
    rep(i, n) f[i] *= fact(i);
    reverse(all(f));
    Poly g(n, 1);
    rep(i, 1, n) g[i] = g[i - 1] * a * inv(i);
    f = (f * g);
    f.resize(n);
    reverse(begin(f), end(f));

    rep(i, n) f[i] *= ifact(i);
    return f;
}

// ボールの数、一個以上必要な数、入っていなくてもいい数(区別あり)
mint choose(int num, int a, int b = 0) {
    if(num == 0) return !a;
    return C(num + b - 1, a + b - 1);
}

// +1 n 個 -1 m 個で累積和 >= 0
mint Catalan(int n, int m) { return C(n + m, m) - C(n + m, m - 1); }

// +1 n 個 -1 m 個で累積和 > -k
mint Catalan2(int n, int m, int k) {
    if(m < k) return C(n + m, m);
    if(m < n + k) return C(n + m, m) - C(n + m, m - k);
    return 0;
}

// +1 n 個 -1 m 個で累積和 < +k
mint Catalan3(int n, int m, int k) { return Catalan2(m, n, k); }
string to_fraction(mint x) {
    static const int M = sqrtl(MOD);
    rep(i, 1, M + 1) {
        if((x * i).a < M) return (i > 1 ? to_string((x * i).a) + " / " + to_string(i) : to_string((x * i).a));
        if(MOD - (x * i).a < M) return (i > 1 ? to_string(MOD - (x * i).a) + " / " + to_string(i) : to_string(MOD - (x * i).a));
    }
    return "?";
}

} // namespace modular
using namespace modular;

// \sum a_i exp(b_i x)
vector<mint> sum_a_expbx(vmint a, vmint b, int m) {
    deque<pair<vmint, vmint>> d;
    rep(i, si(a)) d.eb(vmint{a[i]}, vmint{1, -mint(b[i])});
    while(si(d) > 1) {
        auto [p1, q1] = d[0];
        auto [p2, q2] = d[1];
        rep(2) d.pop_front();
        d.emplace_back(p1 * q2 + p2 * q1, q1 * q2);
    }
    auto res = d[0].fi * inv(d[0].se, m + 1);
    res.resize(m + 1);
    rep(i, 1, m + 1) res[i] *= ifact(i);
    return res;
}

namespace suisen {

namespace library {
template <typename Int, std::enable_if_t<std::is_integral_v<Int>, std::nullptr_t> = nullptr> struct rational {
    Int num, den;

    rational(Int n = 0, Int d = 1) {
        if(n == 0) {
            assert(d != 0);
            num = 0, den = 1;
        } else {
            Int g = std::gcd(n, d);
            n /= g, d /= g;
            if(d < 0) n = -n, d = -d;
            num = n, den = d;
        }
    }
    static rational raw(Int n, Int d) {
        rational x;
        x.num = n, x.den = d;
        return x;
    }
};

template <typename Int, std::enable_if_t<std::is_integral_v<Int>, std::nullptr_t> = nullptr> struct sbt_node {
    using sbt_arc = bool;
    static constexpr sbt_arc Left = false, Right = true;
    using sbt_path = std::vector<std::pair<sbt_arc, Int>>;

    // 1/1
    sbt_node() = default;
    // a/b (a and b must be positive integer)
    sbt_node(Int a, Int b) : sbt_node() {
        assert(a > 0 and b > 0);
        // implicitly computes the continued fraction
        sbt_arc dir = a < b ? Left : Right;
        if(dir == Left) std::swap(a, b);
        for(; b; dir = not dir) {
            Int q = a / b, r = a % b;
            // If r != 0: [...,1] ----(q   step)----> [...,q+1] = [...,q,1]
            // If r == 0: [...,1] ----(q-1 step)----> [...,q] (end)
            go_down(dir, q - (r == 0));
            a = b, b = r;
        }
    }
    sbt_node(const rational<Int> &x) : sbt_node(x.num, x.den) {}
    sbt_node(const sbt_path &path) : sbt_node() {
        for(const auto &[dir, num] : path) go_down(dir, num);
    }

    operator rational<Int>() const { return rational<Int>::raw(_l.num + _r.num, _l.den + _r.den); }
    // get the rational number
    rational<Int> get() const { return rational<Int>(*this); }
    // { inf, sup } of the subtree
    std::pair<rational<Int>, rational<Int>> range() const { return {_l, _r}; }
    // path from the root node 1/1
    const sbt_path &path() const { return _path; }
    // distance from the root node 1/1
    Int depth() const { return _dep; }

    // lowest common ancestor
    static sbt_node lca(const sbt_node &a, const sbt_node &b) {
        const sbt_path &pa = a.path(), &pb = b.path();
        const int k = std::min(pa.size(), pb.size());
        sbt_node c;
        for(int i = 0; i < k; ++i) {
            if(pa[i] == pb[i]) {
                c.go_down(pa[i].first, pa[i].second);
            } else {
                if(pa[i].first == pb[i].first) {
                    // same direction but different lengths
                    c.go_down(pa[i].first, std::min(pa[i].second, pb[i].second));
                }
                break;
            }
        }
        return c;
    }
    // lowest common ancestor
    sbt_node lca(const sbt_node &other) { return lca(*this, other); }

    // go up k steps. returns true if 0<=k<=depth, false otherwise (and makes no change).
    bool go_up(Int k) {
        if(k < 0 or k > depth()) return false;
        while(k) {
            auto &[dir, num] = _path.back();
            const Int u = std::min(k, num);
            k -= u;
            _dep -= u;
            if(dir == Left) {
                _r.num -= _l.num * u, _r.den -= _l.den * u;
            } else {
                _l.num -= _r.num * u, _l.den -= _r.den * u;
            }
            num -= u;
            if(num == 0) _path.pop_back();
        }
        return true;
    }

    // go down k steps to the left
    void go_down_left(Int k) { go_down(Left, k); }
    // go down k steps to the right
    void go_down_right(Int k) { go_down(Right, k); }
    // go down k steps in the direction `dir`
    void go_down(sbt_arc dir, Int k) {
        assert(k >= 0);
        if(k == 0) return;
        if(_path.size() and _path.back().first == dir) {
            _path.back().second += k;
        } else {
            _path.emplace_back(dir, k);
        }
        _dep += k;
        if(dir == Left) {
            _r.num += _l.num * k, _r.den += _l.den * k;
        } else {
            _l.num += _r.num * k, _l.den += _r.den * k;
        }
    }

  private:
    rational<Int> _l = rational<Int>::raw(0, 1), _r = rational<Int>::raw(1, 0);
    Int _dep = 0;
    sbt_path _path{};
};
} // namespace library

using sbt_node = library::sbt_node<int>;
using rational = library::rational<int>;

} // namespace suisen

int main() {
    TEST {
        INT(n, m);
        VEC2(int, l, r, m);
        --l;
        vi xs = l;
        fore(e, r) xs.eb(e);
        UNIQUE(xs);
        fore(e, l) e = lb(xs, e);
        fore(e, r) e = lb(xs, e);

        auto ok = [&](ll a, ll b) -> bool {
            Edges<ll> E;
            rep(i, si(xs) - 1) E.emplace_back(i + 1, i, 0);
            rep(i, si(xs) - 1) E.emplace_back(i, i + 1, inf<int>);
            rep(i, m) {
                E.emplace_back(l[i], r[i], a);
                E.emplace_back(r[i], l[i], -b);
            }
            auto res = bellman_ford(E, si(xs), 0);
            return !empty(res);
        };

        if(ok(1, 1)) {
            OUT(1);
        } else {
            int ansa = inf<int>, ansb = 1;
            suisen::library::sbt_node node(1, 1);

            bool tmp = false;
            while(true) {
                int t = 0;
                bool ng = false;
                while(true) {
                    (tmp ? node.go_down_left(1 << t) : node.go_down_right(1 << t));
                    {
                        dump(tmp);
                        suisen::rational r = node;
                        dump(r.num, r.den);
                    }
                    auto check = [&](auto node) {
                        suisen::rational r = node;
                        return ok(r.num, r.den);
                    };
                    if(check(node) != tmp) {
                        node.go_up(1 << t);
                        per(i, t) {
                            (tmp ? node.go_down_left(1 << i) : node.go_down_right(1 << i));
                            if(check(node) != tmp) node.go_up(1 << i);
                        }
                        if(tmp) {
                            suisen::rational r = node;
                            ansa = r.num, ansb = r.den;
                        }
                        (tmp ? node.go_down_left(1) : node.go_down_right(1));

                        tmp = !tmp;
                        break;
                    } else {
                        t++;
                        if(t >= 20) {
                            ng = true;
                            break;
                        }
                    }
                }
                {
                    dump(tmp, tmp);
                    suisen::rational r = node;
                    dump(r.num, r.den);
                }
                if(tmp) {
                    suisen::rational r = node;
                    ansa = r.num, ansb = r.den;
                }
                if(ng) break;
            }
            OUT(mint(ansa) * inv(ansb));
        }
    }
}

詳細信息

Test #1:

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

input:

3
3 3
1 3
2 3
1 2
12 6
2 3
5 7
1 9
4 8
1 2
7 11
4 5
3 4
2 3
1 2
4 4
1 1

output:

1
2
499122178

result:

ok 3 number(s): "1 2 499122178"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3716kb

input:

2000
1000000000 1
259923446 367011266
1000000000 1
882434225 971573327
1000000000 1
41585677 470369580
1000000000 1
371902212 947250194
1000000000 1
787209148 924205796
1000000000 1
259074809 960876164
1000000000 1
148079314 188254573
1000000000 1
940091047 948318624
1000000000 1
40636497 743979446
...

output:

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
1
1
1
...

result:

ok 2000 numbers

Test #3:

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

input:

1000
1000000000 5
575330909 661595447
708422488 913945134
658050911 930246647
786571892 904549453
851755566 969150871
1000000000 2
198072104 844159589
8876188 644559580
1000000000 2
740802634 976972118
783909534 898449184
1000000000 2
871819537 941611957
465883854 640988372
1000000000 1
99458969 462...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
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
2
1
1
1
1
...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 5ms
memory: 3744kb

input:

500
1000000000 13
964546318 987364574
367845944 907446075
259314137 890312338
458318546 959971971
353677471 522446336
782931403 845199078
514387878 786979588
532634932 793056892
905393511 960628299
747423889 986373313
796099347 833069525
906969434 971335651
574582540 647534593
1000000000 6
987712893...

output:

3
1
3
1
1
1
1
1
1
3
2
1
1
1
3
1
2
1
1
2
1
3
1
1
1
2
1
2
2
1
1
1
1
1
1
1
3
1
1
1
1
2
1
1
1
1
2
1
1
1
1
1
2
1
1
1
1
1
1
1
2
2
1
1
3
1
2
1
1
1
1
2
3
1
1
1
1
1
1
1
3
2
1
3
2
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
3
1
1
1
1
1
1
1
2
1
1
2
1
1
1
2
1
4
1
2
1
4
1
3
1
1
1
1
1
2
1
1
4
1
...

result:

ok 500 numbers

Test #5:

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

input:

250
1000000000 10
844342043 888135880
127033337 726074967
581308029 893912240
414276384 752837267
565680461 863374082
230362895 477723054
210479116 423381051
325072305 427826920
178306222 756423471
376470949 993759748
1000000000 2
468173597 607783582
266359996 863641680
1000000000 7
206599093 941381...

output:

2
1
2
1
3
3
1
1
1
2
1
2
2
1
3
5
2
1
1
1
2
1
2
1
3
1
2
1
3
499122178
1
1
1
1
3
1
1
1
3
3
3
1
4
1
1
1
1
1
1
1
1
5
1
4
2
1
3
1
1
1
2
5
2
1
2
6
2
2
1
2
1
1
1
5
8
2
1
2
1
1
2
2
2
1
1
5
8
3
1
1
1
8
2
6
1
1
4
2
1
1
1
1
2
2
1
2
1
1
1
1
1
1
2
1
2
1
1
4
1
1
3
1
2
3
3
2
5
1
1
1
3
2
1
1
1
3
1
1
2
1
1
1
1
3
1
1
...

result:

ok 250 numbers

Test #6:

score: 0
Accepted
time: 14ms
memory: 3768kb

input:

250
1000000000 4
10495745 465086423
465086424 609997778
396956207 663037010
253873206 396956206
1000000000 33
596279983 655818820
226461062 338625457
407323582 423049163
711408063 778512581
220274357 226461061
702491412 711408062
686978949 688730316
369564474 434159428
778512582 787885602
675683057 ...

output:

1
2
748683266
5
6
453747435
1
10
6
1
499122183
1
4
3
1
3
1
748683266
2
499122179
10
499122178
1
499122179
4
1
7
1
665496238
2
2
2
332748119
249561090
816745381
499122178
2
499122179
5
3
4
17
1
2
2
3
249561092
1
3
924300328
499122179
2
3
332748120
2
7
3
499122187
6
374341634
1
2
332748120
2
2
2
49912...

result:

ok 250 numbers

Test #7:

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

input:

100
1000000000 17
272213590 960979163
970159974 987653658
201788340 556786243
46564706 948945765
786605927 819103747
510930374 747773556
729597186 850647589
412727504 443334406
685627406 773178988
793614323 909668193
830162056 837607472
416766039 753918198
237455713 993045890
848459092 851118478
463...

output:

8
1
1
2
3
3
1
5
1
2
8
2
1
1
3
1
3
6
3
3
2
3
7
2
1
1
3
1
2
1
5
5
2
2
4
2
7
2
1
6
1
2
5
4
5
4
1
1
1
8
6
1
4
4
5
13
1
4
9
4
8
3
8
5
4
7
1
8
1
1
1
9
2
1
6
4
4
3
1
1
1
10
4
6
11
6
6
1
1
4
1
4
2
2
13
5
1
1
5
8

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 18ms
memory: 3732kb

input:

100
1000000000 49
187775019 193881727
145323628 162242601
964365230 971504847
226437670 229819402
46971378 49331905
871327590 883354570
310535966 323031740
904117712 916571909
458902934 484636144
13320536 14923771
571938132 574937141
89751784 102733764
412667720 421251698
908036941 932886651
2663244...

output:

2
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
1
1
2
1
1
2
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
2
1
1
1
2
3
1
1
1
1
1
1
3
1
3
1
1
1
1
1
1
1
1
2
1
1
1
2
1
1
3
1
1
1
1
3
1
1
1
1
1
2
1
1
1
1
1
2
1
2
2
1
1
1

result:

ok 100 numbers

Test #9:

score: 0
Accepted
time: 36ms
memory: 3656kb

input:

100
1000000000 33
607773622 612059886
773446566 927093401
216659567 357373353
949986996 960422356
67865304 185683459
748675762 867719748
419805439 434936264
83601801 106508219
584299087 639485780
487166380 588591547
670602250 789210083
877816826 902687951
800334389 834278741
90815648 214176329
53952...

output:

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

result:

ok 100 numbers

Test #10:

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

input:

100
1000000000 27
423127198 447304856
209683651 219301129
831320345 879604518
631502329 814498734
130918283 202258454
434769186 463838309
448295746 500976275
778017547 864887407
60178254 66348236
615735891 725460273
78684718 129678593
219427409 221445385
242513397 378886240
549135209 710348598
24951...

output:

748683266
2
332748119
2
855638018
2
2
2
1
1
499122179
1
630470119
1
873463814
10
3
598946613
499122178
499122179
720954257
24956110
686292996
499122178
6
2
499122180
332748122
665496237
27
17
1
15
5
199648872
6
4
3
1
285212675
2
1
4
2
499122186
698771050
844668300
887328319
332748120
1
2
499122179
4...

result:

ok 100 numbers

Test #11:

score: 0
Accepted
time: 69ms
memory: 3816kb

input:

50
1000000000 54
393385964 584227315
530511168 878333402
240442438 693353417
66549203 383382851
432995043 781030135
902504635 941834946
40257869 409360381
186795487 285734229
500620269 578283640
769614926 881642580
651338390 854914246
220143804 506609845
486528251 695975933
659594236 951619961
26914...

output:

6
3
9
1
5
1
5
7
4
9
11
7
4
10
1
1
3
1
1
7
11
12
7
6
6
7
1
14
9
5
3
11
7
5
10
1
1
14
2
8
16
4
4
2
2
6
4
1
1
9

result:

ok 50 numbers

Test #12:

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

input:

50
10 65
7 10
3 6
5 7
7 7
3 9
2 2
3 10
10 10
7 7
2 3
5 6
7 10
3 9
2 8
2 8
8 8
4 8
9 9
9 9
7 9
1 1
3 6
9 10
9 10
2 3
7 8
9 10
2 9
9 10
10 10
5 7
6 10
6 8
4 5
10 10
5 5
5 10
8 8
1 9
6 7
3 6
1 9
2 5
1 10
2 9
8 9
8 8
1 1
2 9
4 9
10 10
7 10
2 3
8 9
10 10
2 4
2 9
4 7
1 3
1 9
10 10
1 4
8 9
7 8
7 8
10 88
6 ...

output:

7
8
7
6
4
4
6
4
6
8
7
6
6
3
499122178
3
3
7
10
4
2
3
5
2
8
2
8
1
4
7
4
4
7
6
1
4
2
5
3
6
4
2
1
6
1
6
3
9
6
4

result:

ok 50 numbers

Test #13:

score: 0
Accepted
time: 129ms
memory: 3772kb

input:

25
1000000000 126
107069149 368376053
479032115 765537110
991540256 997326292
403046092 722244014
490526523 516722534
274125538 310843747
777271932 894507975
30859549 117930127
295842439 932626190
696990395 727705976
919364307 981912430
452436750 754049053
436429356 707440965
255169020 717543449
875...

output:

13
12
14
15
3
8
13
499122178
9
17
3
3
5
6
6
22
3
3
16
6
17
5
6
9
19

result:

ok 25 numbers

Test #14:

score: 0
Accepted
time: 295ms
memory: 4012kb

input:

10
1000000000 69
870434015 950861762
463726401 635711398
333118041 890448132
290535922 477961269
413309490 468893401
200588542 259174530
820993949 902249431
919016091 952057155
32176623 226256591
307850591 328322116
544612131 956816575
794988232 980183910
896176727 934471390
445409718 674881616
3109...

output:

7
21
17
13
6
11
30
26
17
14

result:

ok 10 numbers

Test #15:

score: 0
Accepted
time: 476ms
memory: 4032kb

input:

10
1000000000 226
722573032 815472621
582575925 607010515
411370955 463267466
92061989 217643130
187859011 258319855
811376535 844552673
426496326 431292091
785538560 983675713
328209738 364768843
338697990 509158393
502285144 536085577
202590577 293138489
873383022 956559039
765186726 836986281
219...

output:

15
5
5
12
18
2
13
12
35
8

result:

ok 10 numbers

Test #16:

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

input:

10
10 31
7 8
5 9
2 4
6 10
10 10
4 5
3 6
8 8
4 10
7 8
2 8
2 7
3 4
9 9
4 7
1 8
1 10
3 9
2 5
5 8
5 8
5 8
6 6
2 10
3 7
9 10
9 10
7 7
6 6
9 10
6 7
10 165
10 10
9 9
4 9
9 9
1 1
6 8
2 9
4 6
10 10
8 9
5 9
8 8
6 10
6 6
4 6
1 6
3 7
5 9
2 8
5 6
3 5
6 9
6 8
4 7
5 8
9 9
5 7
10 10
5 8
9 10
5 5
3 8
7 10
1 1
7 8
6 ...

output:

6
9
10
10
10
7
9
9
8
9

result:

ok 10 numbers

Test #17:

score: 0
Accepted
time: 936ms
memory: 3816kb

input:

5
1000000000 63
619459262 977043459
300995683 982228427
410548612 621234006
122929033 763884440
421486730 819706101
340188689 623537684
507398179 844353491
337184385 791508531
349294635 959826734
98096933 650360479
385580668 846357810
364950155 640902318
640098682 994083922
770432519 820631492
66011...

output:

8
17
6
40
44

result:

ok 5 number(s): "8 17 6 40 44"

Test #18:

score: 0
Accepted
time: 2384ms
memory: 4008kb

input:

2
1000000000 1954
214176902 795098577
427614652 861416360
690405909 903037538
224031724 678866146
103017905 175158461
481177251 880591454
774838238 795104831
887429528 996876768
889351335 987035745
391908934 489988622
83670551 709453888
679022699 842242196
78153409 642923089
232797325 414737043
6804...

output:

66
8

result:

ok 2 number(s): "66 8"

Test #19:

score: 0
Accepted
time: 2357ms
memory: 3992kb

input:

1
1000000000 2000
804998774 935072473
539475366 898950940
227523606 852755701
309719052 650340983
356982928 655220770
783115802 937764030
570168460 665560212
583166562 906377079
947557671 947616592
774446890 997986030
113320562 897048797
39935214 749273732
63763440 415540685
961986268 990569362
9656...

output:

62

result:

ok 1 number(s): "62"

Test #20:

score: 0
Accepted
time: 2497ms
memory: 4032kb

input:

1
1000000000 2000
983082198 998118377
133255920 610572950
206872860 997430403
184715228 358714182
577917083 618946695
457376242 788935995
213001254 402552678
805136885 901023068
230805393 394264451
647877612 836521262
260384310 990902247
409818531 847221384
791110001 876700979
380113193 775384241
98...

output:

68

result:

ok 1 number(s): "68"

Test #21:

score: 0
Accepted
time: 2494ms
memory: 4068kb

input:

1
1000000000 2000
866198326 984959665
577293370 619895730
40997921 614353847
619519915 762112999
653627047 934559654
836669385 838221693
150801344 848367607
172331400 524704520
514053116 611706075
816275630 945128934
552672251 875377371
924926041 974390075
958648050 977057013
388174710 757781221
867...

output:

65

result:

ok 1 number(s): "65"

Test #22:

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

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #23:

score: 0
Accepted
time: 19ms
memory: 4108kb

input:

1
100 2000
72 77
22 100
39 72
24 62
16 60
72 79
10 83
25 73
65 80
25 52
66 69
59 62
40 64
23 49
52 52
9 29
10 77
98 99
54 69
13 17
40 61
4 21
49 91
24 71
40 96
33 97
81 99
75 99
45 62
34 56
44 96
15 21
18 63
73 81
35 98
97 100
3 8
54 71
14 67
89 91
69 78
54 63
55 82
26 99
21 97
87 89
19 86
47 80
5 3...

output:

53

result:

ok 1 number(s): "53"

Test #24:

score: 0
Accepted
time: 2351ms
memory: 4088kb

input:

1
1000000000 2000
269842809 342989075
757696397 836492119
283800102 368175835
822590805 872323042
941319254 945363554
281911546 293866204
38600498 86445775
480456857 512409031
93001458 142464233
444440343 481314857
199837475 390806080
247541526 359208697
91559247 103334865
843979563 922498813
219394...

output:

56

result:

ok 1 number(s): "56"

Test #25:

score: 0
Accepted
time: 2351ms
memory: 4028kb

input:

1
1000000000 2000
60970930 249531903
605655603 691131570
118119998 120991935
847802043 855924405
584102854 586717700
472229670 472514717
644930188 651241444
827728709 830128844
13795393 40329809
305610899 308346192
701926206 707118828
753530803 795196944
465598902 506244732
289441054 295066017
31306...

output:

48

result:

ok 1 number(s): "48"

Test #26:

score: 0
Accepted
time: 2345ms
memory: 4016kb

input:

1
1000000000 2000
536271720 567640349
500139615 505304625
983805617 983975201
94383607 147481725
660146910 669771610
383881741 388232026
270977785 281138547
732093947 763594417
916230529 918169865
840991913 842180384
148110570 190711924
234960944 320094883
471183646 473316949
589311548 599607524
843...

output:

36

result:

ok 1 number(s): "36"

Test #27:

score: 0
Accepted
time: 2213ms
memory: 3980kb

input:

1
1000000000 2000
253665547 265466414
680907838 683090293
624375234 634603777
122927162 123370400
796036172 809472081
44051418 53038658
805455233 813555754
598048351 601880671
890314580 907216922
71975295 73805827
210790640 215291615
7828762 11464474
755748 9933627
403981737 405251546
203053255 2073...

output:

29

result:

ok 1 number(s): "29"

Test #28:

score: 0
Accepted
time: 2215ms
memory: 4108kb

input:

1
1000000000 2000
405154724 415180094
217599764 236947592
443502690 445411390
704018773 736978289
411258264 417952279
74830932 83239763
549851687 550072757
78499713 79178089
386983274 389145943
904368883 908143439
573835921 579550046
461692563 462204357
737455142 749312955
201370027 208562823
800400...

output:

18

result:

ok 1 number(s): "18"

Test #29:

score: 0
Accepted
time: 2073ms
memory: 4128kb

input:

1
1000000000 2000
636241745 637184786
72054834 72845369
389843249 390664964
168145795 172118428
893106799 895704067
299524880 300801439
29663110 31018768
821696497 823269898
555248504 561118852
786551669 788495535
241984595 244010309
88896181 90154078
409626569 413026599
276562518 278971540
34098107...

output:

12

result:

ok 1 number(s): "12"

Test #30:

score: 0
Accepted
time: 2065ms
memory: 4084kb

input:

1
1000000000 2000
775300798 775887545
414455164 414765933
482698418 483451742
61950757 62192271
660326268 660527972
631032663 631204978
697002803 698108853
355102397 355611777
428369246 428537339
804557428 805328473
927694064 928207744
45269484 45777489
8814283 9209856
715864772 716035358
298335301 ...

output:

14

result:

ok 1 number(s): "14"

Test #31:

score: 0
Accepted
time: 1773ms
memory: 4012kb

input:

1
1000000000 2000
767922821 767991850
289504691 289531721
251731008 251917208
674093628 674196482
531956403 531991130
629214886 629249556
258581533 258771850
376924559 377133497
384702776 384846804
597904466 597997168
225891755 225975116
181703875 181793417
496608917 496630853
949582964 949591315
85...

output:

3

result:

ok 1 number(s): "3"

Test #32:

score: 0
Accepted
time: 1627ms
memory: 4008kb

input:

1
1000000000 2000
228893800 228908417
247092434 247118950
444005072 444005307
11611034 11617481
174532875 174543185
817918839 817922625
970187539 970190706
670081522 670119433
387831247 387855683
302583713 302586447
247247304 247256686
378883005 378894127
227362402 227363360
1961915 1971640
18341639...

output:

2

result:

ok 1 number(s): "2"

Test #33:

score: 0
Accepted
time: 72ms
memory: 4100kb

input:

1
1000000000 2000
57718020 57719049
666380062 666380395
749991324 749991702
892182872 892183353
801943437 801944028
79294169 79294302
555724391 555726783
33922986 33924967
140433140 140433755
885613046 885614480
541055072 541055603
591953292 591956152
486054735 486054958
937249219 937249446
71466373...

output:

1

result:

ok 1 number(s): "1"

Test #34:

score: 0
Accepted
time: 253ms
memory: 3880kb

input:

20
1000000000 41
942725914 956893525
130968778 136999877
528516274 534235456
144476363 150040417
758242783 765399242
43829675 51184350
508202014 513231158
918241923 924218108
662727534 806406887
392873650 493267077
56851982 60477276
290204036 310321327
431216970 440055845
636193295 649883208
2731659...

output:

142606341
3
332748145
218365954
199648872
17
124780547
399297746
86803859
20
554580202
840358768
221832083
695746068
17
516947970
449758446
949942208
332748124
3

result:

ok 20 numbers

Test #35:

score: 0
Accepted
time: 353ms
memory: 4088kb

input:

10
1000000000 417
627781142 629714760
598651777 602008259
852433806 853778002
886286857 888427504
789562767 794791071
982787290 984372848
156909491 157679027
846484388 851062802
157686024 161849304
960912238 962168439
472530654 482013887
281175472 286597312
701329984 702139905
688522549 692226383
23...

output:

87056195
698771053
570425402
862120129
199648873
142606341
564225074
13
499122215
771370646

result:

ok 10 numbers

Test #36:

score: -100
Time Limit Exceeded

input:

1
1000000000 2000
213239071 213382300
339117973 339530479
825361841 826092857
339970803 339980741
798713033 798740067
542540242 542736231
62765592 63346300
641000665 641054005
692199416 692257820
77404143 78416629
950702620 950907897
504833797 505142552
572971840 573068998
340559923 340656260
251909...

output:

390617398

result: