QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#113477#6640. Talk That TalkmaspyAC ✓711ms67756kbC++2338.1kb2023-06-18 07:00:312023-06-18 07:00:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-18 07:00:35]
  • 评测
  • 测评结果:AC
  • 用时:711ms
  • 内存:67756kb
  • [2023-06-18 07:00:31]
  • 提交

answer

#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#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__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
  for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sum = 0;
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  assert(!que.empty());
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  assert(!que.empty());
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return (ok + ng) / 2;
}

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

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>

namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.write(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};

struct has_read_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.read(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <typename T,
            typename enable_if<has_read<T>::value>::type * = nullptr>
  inline bool read_single(T &x) {
    x.read();
    return true;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <size_t N = 0, typename T>
  void read_single_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
      auto &x = std::get<N>(t);
      read_single(x);
      read_single_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool read_single(tuple<T...> &tpl) {
    read_single_tuple(tpl);
    return true;
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <typename T,
            typename enable_if<has_write<T>::value>::type * = nullptr>
  inline void write(T x) {
    x.write();
  }
  template <class T>
  void write(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <size_t N = 0, typename T>
  void write_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
      if constexpr (N > 0) { write(' '); }
      const auto x = std::get<N>(t);
      write(x);
      write_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool write(tuple<T...> tpl) {
    write_tuple(tpl);
    return true;
  }
  template <class T, size_t S>
  void write(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if (val < 0) {
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if (negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)   \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "library/mod/barrett.hpp"

// https://github.com/atcoder/ac-library/blob/master/atcoder/internal_math.hpp
struct Barrett {
  u32 m;
  u64 im;
  explicit Barrett(u32 m = 1) : m(m), im(u64(-1) / m + 1) {}
  u32 umod() const { return m; }
  u32 modulo(u64 z) {
    if (m == 1) return 0;
    u64 x = (u64)(((unsigned __int128)(z)*im) >> 64);
    u64 y = x * m;
    return (z - y + (z < y ? m : 0));
  }
  u64 floor(u64 z) {
    if (m == 1) return z;
    u64 x = (u64)(((unsigned __int128)(z)*im) >> 64);
    u64 y = x * m;
    return (z < y ? x - 1 : x);
  }
  pair<u64, u32> divmod(u64 z) {
    if (m == 1) return {z, 0};
    u64 x = (u64)(((unsigned __int128)(z)*im) >> 64);
    u64 y = x * m;
    if (z < y) return {x - 1, z - y + m};
    return {x, z - y};
  }
  u32 mul(u32 a, u32 b) { return modulo(u64(a) * b); }
};
#line 3 "library/mod/mod_pow.hpp"

// int
ll mod_pow(ll a, ll n, int mod) {
  assert(n >= 0);
  a %= mod;
  if (a < 0) a += mod;
  Barrett bt(mod);
  ll p = a, v = bt.modulo(1);
  while (n) {
    if (n & 1) v = bt.mul(v, p);
    p = bt.mul(p, p);
    n >>= 1;
  }
  return v;
}

ll mod_pow_long(ll a, ll n, ll mod) {
  assert(n >= 0);
  a %= mod;
  if (a < 0) a += mod;
  ll p = a, v = 1 % mod;
  while (n) {
    if (n & 1) v = i128(v) * p % mod;
    p = i128(p) * p % mod;
    n >>= 1;
  }
  return v;
}
#line 2 "library/mod/modint_common.hpp"

struct has_mod_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.get_mod(), std::true_type{});
  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {};

template <typename mint>
mint inv(int n) {
  static const int mod = mint::get_mod();
  static vector<mint> dat = {0, 1};
  assert(0 <= n);
  if (n >= mod) n %= mod;
  while (len(dat) <= n) {
    int k = len(dat);
    int q = (mod + k - 1) / k;
    dat.eb(dat[k * q - mod] * mint(q));
  }
  return dat[n];
}

template <typename mint>
mint fact(int n) {
  static const int mod = mint::get_mod();
  assert(0 <= n);
  if (n >= mod) return 0;
  static vector<mint> dat = {1, 1};
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint(len(dat)));
  return dat[n];
}

template <typename mint>
mint fact_inv(int n) {
  static vector<mint> dat = {1, 1};
  if (n < 0) return mint(0);
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
  return dat[n];
}

template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
  return (mint(1) * ... * fact_inv<mint>(xs));
}

template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
  return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}

template <typename mint>
mint C_dense(int n, int k) {
  static vvc<mint> C;
  static int H = 0, W = 0;
  auto calc = [&](int i, int j) -> mint {
    if (i == 0) return (j == 0 ? mint(1) : mint(0));
    return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
  };
  if (W <= k) {
    FOR(i, H) {
      C[i].resize(k + 1);
      FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
    }
    W = k + 1;
  }
  if (H <= n) {
    C.resize(n + 1);
    FOR(i, H, n + 1) {
      C[i].resize(W);
      FOR(j, W) { C[i][j] = calc(i, j); }
    }
    H = n + 1;
  }
  return C[n][k];
}

template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
  assert(n >= 0);
  if (k < 0 || n < k) return 0;
  if (dense) return C_dense<mint>(n, k);
  if (!large) return multinomial<mint>(n, k, n - k);
  k = min(k, n - k);
  mint x(1);
  FOR(i, k) x *= mint(n - i);
  return x * fact_inv<mint>(k);
}

template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
  assert(n >= 0);
  assert(0 <= k && k <= n);
  if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
  return mint(1) / C<mint, 1>(n, k);
}

// [x^d] (1-x) ^ {-n} の計算
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
  assert(n >= 0);
  if (d < 0) return mint(0);
  if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
  return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "library/mod/modint.hpp"

template <int mod>
struct modint {
  static_assert(mod < (1 << 30));
  int val;
  constexpr modint(const ll val = 0) noexcept
      : val(val >= 0 ? val % mod : (mod - (-val) % mod) % mod) {}
  bool operator<(const modint &other) const {
    return val < other.val;
  } // To use std::map
  modint &operator+=(const modint &p) {
    if ((val += p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator-=(const modint &p) {
    if ((val += mod - p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator*=(const modint &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }
  modint operator-() const { return modint(-val); }
  modint operator+(const modint &p) const { return modint(*this) += p; }
  modint operator-(const modint &p) const { return modint(*this) -= p; }
  modint operator*(const modint &p) const { return modint(*this) *= p; }
  modint operator/(const modint &p) const { return modint(*this) /= p; }
  bool operator==(const modint &p) const { return val == p.val; }
  bool operator!=(const modint &p) const { return val != p.val; }
  modint inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b), swap(u -= t * v, v);
    }
    return modint(u);
  }
  modint pow(ll n) const {
    assert(n >= 0);
    modint ret(1), mul(val);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
#ifdef FASTIO
  void write() { fastio::printer.write(val); }
  void read() { fastio::scanner.read(val); }
#endif
  static constexpr int get_mod() { return mod; }
  // (n, r), r は 1 の 2^n 乗根
  static constexpr pair<int, int> ntt_info() {
    if (mod == 167772161) return {25, 17};
    if (mod == 469762049) return {26, 30};
    if (mod == 754974721) return {24, 362};
    if (mod == 880803841) return {23, 211};
    if (mod == 998244353) return {23, 31};
    if (mod == 1045430273) return {20, 363};
    if (mod == 1051721729) return {20, 330};
    if (mod == 1053818881) return {20, 2789};
    return {-1, -1};
  }
  static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};

using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "library/mod/mod_inv.hpp"
// long でも大丈夫
ll mod_inv(ll val, ll mod) {
  val %= mod;
  if (val < 0) val += mod;
  ll a = val, b = mod, u = 1, v = 0, t;
  while (b > 0) {
    t = a / b;
    swap(a -= t * b, b), swap(u -= t * v, v);
  }
  if (u < 0) u += mod;
  return u;
}
#line 1 "library/poly/convolution_naive.hpp"
template <class T>
vector<T> convolution_naive(const vector<T>& a, const vector<T>& b) {
  int n = int(a.size()), m = int(b.size());
  vector<T> ans(n + m - 1);
  if (n < m) {
    FOR(j, m) FOR(i, n) ans[i + j] += a[i] * b[j];
  } else {
    FOR(i, n) FOR(j, m) ans[i + j] += a[i] * b[j];
  }
  return ans;
}
#line 2 "library/poly/ntt.hpp"

template <class mint>
void ntt(vector<mint>& a, bool inverse) {
  assert(mint::can_ntt());
  const int rank2 = mint::ntt_info().fi;
  const int mod = mint::get_mod();
  static array<mint, 30> root, iroot;
  static array<mint, 30> rate2, irate2;
  static array<mint, 30> rate3, irate3;

  static bool prepared = 0;
  if (!prepared) {
    prepared = 1;
    root[rank2] = mint::ntt_info().se;
    iroot[rank2] = mint(1) / root[rank2];
    FOR_R(i, rank2) {
      root[i] = root[i + 1] * root[i + 1];
      iroot[i] = iroot[i + 1] * iroot[i + 1];
    }
    mint prod = 1, iprod = 1;
    for (int i = 0; i <= rank2 - 2; i++) {
      rate2[i] = root[i + 2] * prod;
      irate2[i] = iroot[i + 2] * iprod;
      prod *= iroot[i + 2];
      iprod *= root[i + 2];
    }
    prod = 1, iprod = 1;
    for (int i = 0; i <= rank2 - 3; i++) {
      rate3[i] = root[i + 3] * prod;
      irate3[i] = iroot[i + 3] * iprod;
      prod *= iroot[i + 3];
      iprod *= root[i + 3];
    }
  }

  int n = int(a.size());
  int h = topbit(n);
  assert(n == 1 << h);
  if (!inverse) {
    int len = 0;
    while (len < h) {
      if (h - len == 1) {
        int p = 1 << (h - len - 1);
        mint rot = 1;
        FOR(s, 1 << len) {
          int offset = s << (h - len);
          FOR(i, p) {
            auto l = a[i + offset];
            auto r = a[i + offset + p] * rot;
            a[i + offset] = l + r;
            a[i + offset + p] = l - r;
          }
          rot *= rate2[topbit(~s & -~s)];
        }
        len++;
      } else {
        int p = 1 << (h - len - 2);
        mint rot = 1, imag = root[2];
        for (int s = 0; s < (1 << len); s++) {
          mint rot2 = rot * rot;
          mint rot3 = rot2 * rot;
          int offset = s << (h - len);
          for (int i = 0; i < p; i++) {
            u64 mod2 = u64(mod) * mod;
            u64 a0 = a[i + offset].val;
            u64 a1 = u64(a[i + offset + p].val) * rot.val;
            u64 a2 = u64(a[i + offset + 2 * p].val) * rot2.val;
            u64 a3 = u64(a[i + offset + 3 * p].val) * rot3.val;
            u64 a1na3imag = (a1 + mod2 - a3) % mod * imag.val;
            u64 na2 = mod2 - a2;
            a[i + offset] = a0 + a2 + a1 + a3;
            a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
            a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
            a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
          }
          rot *= rate3[topbit(~s & -~s)];
        }
        len += 2;
      }
    }
  } else {
    mint coef = mint(1) / mint(len(a));
    FOR(i, len(a)) a[i] *= coef;
    int len = h;
    while (len) {
      if (len == 1) {
        int p = 1 << (h - len);
        mint irot = 1;
        FOR(s, 1 << (len - 1)) {
          int offset = s << (h - len + 1);
          FOR(i, p) {
            u64 l = a[i + offset].val;
            u64 r = a[i + offset + p].val;
            a[i + offset] = l + r;
            a[i + offset + p] = (mod + l - r) * irot.val;
          }
          irot *= irate2[topbit(~s & -~s)];
        }
        len--;
      } else {
        int p = 1 << (h - len);
        mint irot = 1, iimag = iroot[2];
        FOR(s, (1 << (len - 2))) {
          mint irot2 = irot * irot;
          mint irot3 = irot2 * irot;
          int offset = s << (h - len + 2);
          for (int i = 0; i < p; i++) {
            u64 a0 = a[i + offset + 0 * p].val;
            u64 a1 = a[i + offset + 1 * p].val;
            u64 a2 = a[i + offset + 2 * p].val;
            u64 a3 = a[i + offset + 3 * p].val;
            u64 x = (mod + a2 - a3) * iimag.val % mod;
            a[i + offset] = a0 + a1 + a2 + a3;
            a[i + offset + 1 * p] = (a0 + mod - a1 + x) * irot.val;
            a[i + offset + 2 * p] = (a0 + a1 + 2 * mod - a2 - a3) * irot2.val;
            a[i + offset + 3 * p] = (a0 + 2 * mod - a1 - x) * irot3.val;
          }
          irot *= irate3[topbit(~s & -~s)];
        }
        len -= 2;
      }
    }
  }
}
#line 1 "library/poly/fft.hpp"
namespace CFFT {
using real = double;

struct C {
  real x, y;

  C() : x(0), y(0) {}

  C(real x, real y) : x(x), y(y) {}
  inline C operator+(const C& c) const { return C(x + c.x, y + c.y); }
  inline C operator-(const C& c) const { return C(x - c.x, y - c.y); }
  inline C operator*(const C& c) const {
    return C(x * c.x - y * c.y, x * c.y + y * c.x);
  }

  inline C conj() const { return C(x, -y); }
};

const real PI = acosl(-1);
int base = 1;
vector<C> rts = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};

void ensure_base(int nbase) {
  if (nbase <= base) return;
  rev.resize(1 << nbase);
  rts.resize(1 << nbase);
  for (int i = 0; i < (1 << nbase); i++) {
    rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
  }
  while (base < nbase) {
    real angle = PI * 2.0 / (1 << (base + 1));
    for (int i = 1 << (base - 1); i < (1 << base); i++) {
      rts[i << 1] = rts[i];
      real angle_i = angle * (2 * i + 1 - (1 << base));
      rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i));
    }
    ++base;
  }
}

void fft(vector<C>& a, int n) {
  assert((n & (n - 1)) == 0);
  int zeros = __builtin_ctz(n);
  ensure_base(zeros);
  int shift = base - zeros;
  for (int i = 0; i < n; i++) {
    if (i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); }
  }
  for (int k = 1; k < n; k <<= 1) {
    for (int i = 0; i < n; i += 2 * k) {
      for (int j = 0; j < k; j++) {
        C z = a[i + j + k] * rts[j + k];
        a[i + j + k] = a[i + j] - z;
        a[i + j] = a[i + j] + z;
      }
    }
  }
}
} // namespace CFFT
#line 7 "library/poly/convolution.hpp"

template <class mint>
vector<mint> convolution_ntt(vector<mint> a, vector<mint> b) {
  if (a.empty() || b.empty()) return {};
  int n = int(a.size()), m = int(b.size());
  int sz = 1;
  while (sz < n + m - 1) sz *= 2;

  // sz = 2^k のときの高速化。分割統治的なやつで損しまくるので。
  if ((n + m - 3) <= sz / 2) {
    auto a_last = a.back(), b_last = b.back();
    a.pop_back(), b.pop_back();
    auto c = convolution(a, b);
    c.resize(n + m - 1);
    c[n + m - 2] = a_last * b_last;
    FOR(i, len(a)) c[i + len(b)] += a[i] * b_last;
    FOR(i, len(b)) c[i + len(a)] += b[i] * a_last;
    return c;
  }

  a.resize(sz), b.resize(sz);
  bool same = a == b;
  ntt(a, 0);
  if (same) {
    b = a;
  } else {
    ntt(b, 0);
  }
  FOR(i, sz) a[i] *= b[i];
  ntt(a, 1);
  a.resize(n + m - 1);
  return a;
}

template <typename mint>
vector<mint> convolution_garner(const vector<mint>& a, const vector<mint>& b) {
  int n = len(a), m = len(b);
  if (!n || !m) return {};
  static const long long nttprimes[] = {754974721, 167772161, 469762049};
  using mint0 = modint<754974721>;
  using mint1 = modint<167772161>;
  using mint2 = modint<469762049>;
  vc<mint0> a0(n), b0(m);
  vc<mint1> a1(n), b1(m);
  vc<mint2> a2(n), b2(m);
  FOR(i, n) a0[i] = a[i].val, a1[i] = a[i].val, a2[i] = a[i].val;
  FOR(i, m) b0[i] = b[i].val, b1[i] = b[i].val, b2[i] = b[i].val;
  auto c0 = convolution_ntt<mint0>(a0, b0);
  auto c1 = convolution_ntt<mint1>(a1, b1);
  auto c2 = convolution_ntt<mint2>(a2, b2);
  static const long long m01 = 1LL * nttprimes[0] * nttprimes[1];
  static const long long m0_inv_m1 = mint1(nttprimes[0]).inverse().val;
  static const long long m01_inv_m2 = mint2(m01).inverse().val;
  const int mod = mint::get_mod();
  auto garner = [&](mint0 x0, mint1 x1, mint2 x2) -> mint {
    int r0 = x0.val, r1 = x1.val, r2 = x2.val;
    int v1 = (m0_inv_m1 * (r1 + nttprimes[1] - r0)) % nttprimes[1];
    auto v2 = (mint2(r2) - r0 - mint2(nttprimes[0]) * v1) * mint2(m01_inv_m2);
    return mint(r0 + 1LL * nttprimes[0] * v1 + m01 % mod * v2.val);
  };
  vc<mint> c(len(c0));
  FOR(i, len(c)) c[i] = garner(c0[i], c1[i], c2[i]);
  return c;
}

template <typename R>
vc<double> convolution_fft(const vc<R>& a, const vc<R>& b) {
  using C = CFFT::C;
  int need = (int)a.size() + (int)b.size() - 1;
  int nbase = 1;
  while ((1 << nbase) < need) nbase++;
  CFFT::ensure_base(nbase);
  int sz = 1 << nbase;
  vector<C> fa(sz);
  for (int i = 0; i < sz; i++) {
    int x = (i < (int)a.size() ? a[i] : 0);
    int y = (i < (int)b.size() ? b[i] : 0);
    fa[i] = C(x, y);
  }
  CFFT::fft(fa, sz);
  C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0);
  for (int i = 0; i <= (sz >> 1); i++) {
    int j = (sz - i) & (sz - 1);
    C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r;
    fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r;
    fa[i] = z;
  }
  for (int i = 0; i < (sz >> 1); i++) {
    C A0 = (fa[i] + fa[i + (sz >> 1)]) * t;
    C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * CFFT::rts[(sz >> 1) + i];
    fa[i] = A0 + A1 * s;
  }
  CFFT::fft(fa, sz >> 1);
  vector<double> ret(need);
  for (int i = 0; i < need; i++) {
    ret[i] = (i & 1 ? fa[i >> 1].y : fa[i >> 1].x);
  }
  return ret;
}

vector<ll> convolution(const vector<ll>& a, const vector<ll>& b) {
  int n = len(a), m = len(b);
  if (!n || !m) return {};
  if (min(n, m) <= 60) return convolution_naive(a, b);
  ll abs_sum_a = 0, abs_sum_b = 0;
  ll LIM = 1e15;
  FOR(i, n) abs_sum_a = min(LIM, abs_sum_a + abs(a[i]));
  FOR(i, m) abs_sum_b = min(LIM, abs_sum_b + abs(b[i]));
  if (i128(abs_sum_a) * abs_sum_b < 1e15) {
    vc<double> c = convolution_fft<ll>(a, b);
    vc<ll> res(len(c));
    FOR(i, len(c)) res[i] = ll(floor(c[i] + .5));
    return res;
  }

  static constexpr unsigned long long MOD1 = 754974721; // 2^24
  static constexpr unsigned long long MOD2 = 167772161; // 2^25
  static constexpr unsigned long long MOD3 = 469762049; // 2^26
  static constexpr unsigned long long M2M3 = MOD2 * MOD3;
  static constexpr unsigned long long M1M3 = MOD1 * MOD3;
  static constexpr unsigned long long M1M2 = MOD1 * MOD2;
  static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;

  static const unsigned long long i1 = mod_inv(MOD2 * MOD3, MOD1);
  static const unsigned long long i2 = mod_inv(MOD1 * MOD3, MOD2);
  static const unsigned long long i3 = mod_inv(MOD1 * MOD2, MOD3);

  using mint1 = modint<MOD1>;
  using mint2 = modint<MOD2>;
  using mint3 = modint<MOD3>;

  vc<mint1> a1(n), b1(m);
  vc<mint2> a2(n), b2(m);
  vc<mint3> a3(n), b3(m);
  FOR(i, n) a1[i] = a[i], a2[i] = a[i], a3[i] = a[i];
  FOR(i, m) b1[i] = b[i], b2[i] = b[i], b3[i] = b[i];

  auto c1 = convolution_ntt<mint1>(a1, b1);
  auto c2 = convolution_ntt<mint2>(a2, b2);
  auto c3 = convolution_ntt<mint3>(a3, b3);

  vc<ll> c(n + m - 1);
  FOR(i, n + m - 1) {
    u64 x = 0;
    x += (c1[i].val * i1) % MOD1 * M2M3;
    x += (c2[i].val * i2) % MOD2 * M1M3;
    x += (c3[i].val * i3) % MOD3 * M1M2;
    ll diff = c1[i].val - ((long long)(x) % (long long)(MOD1));
    if (diff < 0) diff += MOD1;
    static constexpr unsigned long long offset[5]
        = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
    x -= offset[diff % 5];
    c[i] = x;
  }
  return c;
}

template <typename mint>
vc<mint> convolution(const vc<mint>& a, const vc<mint>& b) {
  int n = len(a), m = len(b);
  if (!n || !m) return {};
  if (mint::can_ntt()) {
    if (min(n, m) <= 50) return convolution_naive(a, b);
    return convolution_ntt(a, b);
  }
  if (min(n, m) <= 200) return convolution_naive(a, b);
  return convolution_garner(a, b);
}
#line 2 "library/nt/primetable.hpp"

template <typename T = long long>
vc<T> primetable(int LIM) {
  ++LIM;
  const int S = 32768;
  static int done = 2;
  static vc<T> primes = {2}, sieve(S + 1);

  if (done < LIM) {
    done = LIM;

    primes = {2}, sieve.assign(S + 1, 0);
    const int R = LIM / 2;
    primes.reserve(int(LIM / log(LIM) * 1.1));
    vc<pair<int, int>> cp;
    for (int i = 3; i <= S; i += 2) {
      if (!sieve[i]) {
        cp.eb(i, i * i / 2);
        for (int j = i * i; j <= S; j += 2 * i) sieve[j] = 1;
      }
    }
    for (int L = 1; L <= R; L += S) {
      array<bool, S> block{};
      for (auto& [p, idx]: cp)
        for (int i = idx; i < S + L; idx = (i += p)) block[i - L] = 1;
      FOR(i, min(S, R - L)) if (!block[i]) primes.eb((L + i) * 2 + 1);
    }
  }
  int k = LB(primes, LIM + 1);
  return {primes.begin(), primes.begin() + k};
}
#line 8 "main.cpp"

#line 2 "library/random/base.hpp"

u64 RNG_64() {
  static uint64_t x_
      = uint64_t(chrono::duration_cast<chrono::nanoseconds>(
                     chrono::high_resolution_clock::now().time_since_epoch())
                     .count())
        * 10150724397891781847ULL;
  x_ ^= x_ << 7;
  return x_ ^= x_ >> 9;
}

u64 RNG(u64 lim) { return RNG_64() % lim; }

ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 10 "main.cpp"

ll calc_F(ll p) {
  ll X = ((p - 1) % 4 == 0 ? p - 1 : p + 1);
  // x^2 = 0, x^2 = 1
  ll x0 = 0, x1 = 1;
  x0 = (mod_pow_long(2, (p - 1) / 2, p) == 1 ? 2 : 0);
  x1 = 2;
  // x=0,1,-1
  X -= x0 + x1 + x1;
  // y=0,1,-1
  X -= x0 + x1 + x1;
  // 両方
  X += 4;

  // 4 で割る
  assert(X % 4 == 0);
  return X / 4;
}

vc<int> leq_convolution(vc<int> F, vc<int> G) {
  ll N = len(F);

  vc<int> Fc = cumsum<int>(F);

  using mint = modint998;
  vc<mint> f, g;
  for (auto&& x: F) f.eb(mint(x));
  for (auto&& x: G) g.eb(mint(x));

  // i<=j で畳む
  vc<mint> h(N + N);

  // i=j
  FOR(i, N) h[i + i] += f[i] * g[i];

  auto dfs = [&](auto& dfs, int L, int R) -> void {
    if (R == L + 1) return;
    int M = (L + R) / 2;
    dfs(dfs, L, M);
    dfs(dfs, M, R);
    if (Fc[M] - Fc[L] == 0) return;
    if (M + R <= N) return;
    // [L,M) x [M,R)
    vc<mint> A = {f.begin() + L, f.begin() + M};
    vc<mint> B = {g.begin() + M, g.begin() + R};
    while (len(A) && A.back() == mint(0)) POP(A);
    while (len(B) && B.back() == mint(0)) POP(B);
    A = convolution(A, B);
    FOR(i, len(A)) h[L + M + i] += A[i];
  };
  dfs(dfs, 0, N);

  vc<int> H(N + N);
  FOR(i, len(H)) H[i] = h[i].val;
  return H;
}

// [<0, >0, >0]
ll calc_NG(ll p, ll t) {
  assert(2 * t < p);

  // legendre
  vc<int> dp(2 * t + 1, 1);
  dp[0] = 0;
  for (auto&& x: primetable(2 * t)) {
    if (x > 2 * t) break;
    ll pow = mod_pow_long(x, (p - 1) / 2, p);
    assert(pow == 1 || pow == p - 1);
    pow = (pow == 1 ? 1 : -1);

    ll pp = x;
    while (pp <= 2 * t) {
      int idx = pp;
      while (idx <= 2 * t) dp[idx] *= pow, idx += pp;
      pp *= x;
    }
  }

  // [0, 2t - 1]
  vc<bool> right(2 * t);
  FOR(i, 1, 2 * t) {
    right[i] = (dp[i] == 1);
    // assert(right[i] == (mod_pow_long(i, (p - 1) / 2, p) == 1));
  }

  // [-0, -(t - 1)]
  vc<bool> left(t);
  FOR(i, 1, t) {
    if (p % 4 == 1) {
      left[i] = right[i];
    } else {
      left[i] = !right[i];
    }
    // assert(left[i] == (mod_pow_long(p - i, (p - 1) / 2, p) == 1));
  }

  // 添字偶奇, true/false -> 数える累積和
  int N = 2 * t;
  vvv(int, dat, 2, 2, N);
  FOR(parity, 2) {
    FOR(ok, 2) {
      FOR(i, N) {
        if ((i - parity) % 2 == 0 && 1 * right[i] == ok) {
          dat[parity][ok][i] = 1;
        }
      }
    }
  }
  FOR(parity, 2) FOR(ok, 2) dat[parity][ok] = cumsum<int>(dat[parity][ok]);

  ll aa_ = 0, ab_ = 0;
  /*
  FOR(i, 1, t) {
    FOR(j, 1, 2 * t) {
      // -i, j, 2j+i
      if (j + i <= t) {
        if (left[i] == right[j]) aa_++;
        if (left[i] != right[j]) ab_++;
      }
    }
  }
  */
  FOR(i, 1, t) {
    ll lo = 1, hi = 2 * t - 1;
    // j + i <= t
    chmin(hi, t - i);
    if (lo > hi) continue;
    ++hi;
    FOR(pa, 2) {
      if (!left[i]) aa_ += dat[pa][0][hi] - dat[pa][0][lo];
      if (!left[i]) ab_ += dat[pa][1][hi] - dat[pa][1][lo];
      if (left[i]) aa_ += dat[pa][1][hi] - dat[pa][1][lo];
      if (left[i]) ab_ += dat[pa][0][hi] - dat[pa][0][lo];
    }
  }

  ll a_a = 0, a_b = 0;
  /*
  FOR(i, 1, t) {
    FOR(k, i + 1, 2 * t) {
      if (k + i > 2 * t) continue;
      if ((k - i) % 2 == 0) {
        if (left[i] == right[k]) a_a++;
        if (left[i] != right[k]) a_b++;
      }
    }
  }
  */
  FOR(i, 1, t) {
    int pa = i % 2;
    ll lo = i + 1, hi = 2 * t - 1;
    chmin(hi, 2 * t - i);
    if (lo > hi) continue;
    ++hi;
    if (!left[i]) a_a += dat[pa][0][hi] - dat[pa][0][lo];
    if (!left[i]) a_b += dat[pa][1][hi] - dat[pa][1][lo];
    if (left[i]) a_a += dat[pa][1][hi] - dat[pa][1][lo];
    if (left[i]) a_b += dat[pa][0][hi] - dat[pa][0][lo];
  }

  ll _aa = 0, _ab = 0;
  /*
  FOR(k, 1, 2 * t) {
    FOR(j, 1, k) {
      if (k - j > t) continue;
      if (2 * j - k < 0) {
        if (right[j] == right[k]) _aa++;
        if (right[j] != right[k]) _ab++;
      }
    }
  }
  */
  FOR(k, 1, 2 * t) {
    ll lo = 1, hi = k - 1;
    chmax(lo, k - t);
    chmin(hi, (k - 1) / 2);
    if (lo > hi) continue;
    ++hi;
    FOR(pa, 2) {
      if (!right[k]) _aa += dat[pa][0][hi] - dat[pa][0][lo];
      if (!right[k]) _ab += dat[pa][1][hi] - dat[pa][1][lo];
      if (right[k]) _aa += dat[pa][1][hi] - dat[pa][1][lo];
      if (right[k]) _ab += dat[pa][0][hi] - dat[pa][0][lo];
    }
  }

  ll ANS = aa_ * 2 + a_a * 2 + _aa * 2;
  ANS -= ab_ + a_b + _ab;
  assert(ANS % 6 == 0);
  return ANS / 6;
}

void solve() {
  LL(p, t_max);
  ll F = calc_F(p);
  chmin(t_max, p / 2);
  ll ANS = F * t_max;

  ANS -= calc_NG(p, t_max) * 2;
  print(ANS);
}

void test() {
  ll N = 1e6;
  vc<int> F(N + N), G(N + N);
  FOR(i, N, N + N) F[i] = RNG(0, 2);
  FOR(i, N + N) G[i] = RNG(0, 2);
  leq_convolution(G, F);
}

signed main() {
  // test();
  INT(T);
  FOR(T) solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 136ms
memory: 66956kb

input:

7
7 32
13 1
13 2
67 11
2003 44
1000003 1984
999999999989 987654

output:

0
2
2
146
21510
495014784
246913256130162788

result:

ok 7 numbers

Test #2:

score: 0
Accepted
time: 93ms
memory: 4268kb

input:

11696
5 1
5 2
7 1
7 2
7 3
11 1
11 2
11 3
11 4
11 5
13 1
13 2
13 3
13 4
13 5
13 6
17 1
17 2
17 3
17 4
17 5
17 6
17 7
17 8
19 1
19 2
19 3
19 4
19 5
19 6
19 7
19 8
19 9
23 1
23 2
23 3
23 4
23 5
23 6
23 7
23 8
23 9
23 10
23 11
29 1
29 2
29 3
29 4
29 5
29 6
29 7
29 8
29 9
29 10
29 11
29 12
29 13
29 14
31...

output:

0
0
0
0
0
2
4
4
6
6
2
2
4
4
4
4
2
4
4
6
6
6
8
8
4
8
10
12
16
18
18
20
20
4
8
12
16
20
22
24
24
24
24
24
6
12
18
24
24
28
32
36
40
40
40
44
44
44
6
12
18
22
26
32
34
36
42
44
44
46
46
46
46
8
14
22
26
32
38
42
44
52
54
56
60
62
62
64
64
64
64
8
16
20
28
34
38
44
52
54
56
60
64
66
68
70
74
74
74
76
76...

result:

ok 11696 numbers

Test #3:

score: 0
Accepted
time: 222ms
memory: 3900kb

input:

500000
71 1
41 1
67 1
17 1
29 1
97 1
11 1
59 1
89 1
71 1
7 1
31 1
71 1
13 1
67 1
97 1
13 1
37 1
29 1
13 1
67 1
37 1
97 1
59 1
89 1
83 1
73 1
29 1
13 1
89 1
17 1
43 1
31 1
37 1
79 1
41 1
23 1
83 1
7 1
19 1
11 1
67 1
73 1
71 1
67 1
17 1
47 1
17 1
29 1
41 1
97 1
13 1
47 1
43 1
23 1
7 1
73 1
13 1
47 1
7...

output:

16
8
16
2
6
22
2
14
20
16
0
6
16
2
16
22
2
8
6
2
16
8
22
14
20
20
16
6
2
20
2
10
6
8
18
8
4
20
0
4
2
16
16
16
16
2
10
2
6
8
22
2
10
10
4
0
16
2
10
0
2
20
6
8
14
16
16
2
14
12
2
8
16
0
16
0
18
2
2
4
16
14
16
2
2
22
14
20
20
10
16
16
22
14
16
14
18
0
20
6
2
16
10
4
14
2
22
4
20
2
18
0
6
20
2
4
14
0
12...

result:

ok 500000 numbers

Test #4:

score: 0
Accepted
time: 553ms
memory: 3964kb

input:

500000
796985057191 1
567957900049 1
827610855103 1
919580142883 1
705957627181 1
854537570797 1
653636687779 1
516224177893 1
616020013397 1
518523794483 1
904311938423 1
534402190409 1
578460069899 1
754072383349 1
556038395257 1
676067642057 1
759949674839 1
913727773951 1
792376257731 1
51332043...

output:

199246264296
141989475010
206902713774
229895035720
176489406794
213634392698
163409171944
129056044472
154005003348
129630948620
226077984604
133600547600
144615017474
188518095836
139009598812
169016910512
189987418708
228431943486
198094064432
128330108024
213838430616
148581110810
138371413484
1...

result:

ok 500000 numbers

Test #5:

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

input:

500000
4630931 1
532378711517 2
897492424691 1
933429720763 2
50396453 1
965938979207 1
757130529707 1
585648391093 2
93206693 1
582531600551 1
902156055859 1
673348731047 1
43627163 2
786341452943 1
630094950481 2
500747394781 2
55003589 2
662875627321 2
554939202737 2
729610579949 1
48444437 2
644...

output:

1157732
266189355756
224373106172
466714860380
12599112
241484744800
189282632426
292824195542
23301672
145632900136
225539013964
168337182760
21813580
196585363234
315047475234
250373697386
27501792
331437813654
277469601364
182402644986
24222216
161182653834
403691568340
409143875612
24208052
1779...

result:

ok 500000 numbers

Test #6:

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

input:

500000
999999769291 2
999999916099 2
999999337661 2
999999619897 2
999999711979 2
999999125599 2
999999617677 2
999999902791 2
999999704749 2
999999640721 2
999999284629 2
999999430669 2
999999876917 2
999999555559 2
999999545273 2
999999426997 2
999999814963 2
999999272717 2
999999621109 2
99999955...

output:

499999884644
499999958048
499999668828
499999809942
499999855988
499999562796
499999808834
499999951392
499999852370
499999820356
499999642310
499999715330
499999938456
499999777776
499999772632
499999713494
499999907480
499999636356
499999810550
499999775204
499999646592
499999695462
499999792794
4...

result:

ok 500000 numbers

Test #7:

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

input:

182082
73 3
11 5
83 3
71 8
67 1
97 3
11 8
89 4
37 4
23 1
43 7
13 5
41 5
71 9
7 6
41 6
59 4
89 7
29 3
53 8
13 9
53 4
53 7
43 1
67 10
73 10
67 4
31 2
79 4
43 5
11 10
29 2
37 1
17 6
89 2
79 3
47 1
47 1
53 3
43 10
67 1
97 6
83 6
31 2
7 8
89 7
19 6
5 9
17 10
59 2
19 5
61 6
67 7
97 5
89 5
5 3
13 3
83 1
29...

output:

44
6
56
126
16
62
6
76
26
4
58
4
34
142
0
38
54
126
18
82
4
48
74
10
138
128
58
12
68
44
6
12
8
6
40
54
10
10
36
76
16
116
110
12
0
126
18
0
8
28
16
74
100
98
94
0
4
20
40
18
10
4
10
14
32
36
38
94
54
66
20
4
66
6
96
20
62
6
62
12
20
50
58
0
122
50
6
104
20
4
20
0
82
2
26
126
38
0
128
22
116
16
122
...

result:

ok 182082 numbers

Test #8:

score: 0
Accepted
time: 487ms
memory: 3956kb

input:

181367
625226585029 1
546867924943 3
953697022237 9
546332273987 5
701504204207 5
667670881783 9
510105485471 2
766942437293 2
690753416891 9
723074805317 2
701991568721 7
507206403259 4
511745218031 2
520493347639 9
705498928573 10
647261678981 6
954299288291 1
865750160161 7
848125626919 8
6695742...

output:

156306646256
410150943702
2145818300002
682915342476
876880255250
1502259483980
255052742732
383471218644
1554195187986
361537402656
1228485245230
507206403252
255872609012
1171110032160
1763747321400
970892518454
238574822072
1515062780234
1696251253806
1506542157808
152874865660
379612385868
33052...

result:

ok 181367 numbers

Test #9:

score: 0
Accepted
time: 457ms
memory: 3876kb

input:

181864
96073657 1
802537045631 9
624461538209 9
641739372587 7
94372547 4
562283886301 10
986572793593 2
912998339921 10
78529733 6
762028834409 8
857265814973 2
852255337621 6
92213951 10
660819443131 3
552549324851 2
837226333271 2
2721421 9
623159788477 4
850163710043 6
779567606201 3
23673379 6
...

output:

24018412
1805708352654
1405038460930
1123043902010
94372540
1405709715704
493286396790
2282495849754
117794586
1524057668788
428632907484
1278383006410
230534858
495614582344
276274662424
418613166632
6123164
623159788466
1275245565050
584675704640
35510056
1878626615510
898299738454
1246361224308
1...

result:

ok 181864 numbers

Test #10:

score: 0
Accepted
time: 471ms
memory: 3968kb

input:

181572
999999412481 9
999999477089 7
999999070423 9
999999904439 3
999999474869 6
999999713329 8
999999378673 6
999999955709 5
999999203807 7
999999752107 3
999999750929 6
999999738019 4
999999033937 4
999999848293 8
999999390001 6
999999565897 7
999999067757 7
999999829553 6
999999229199 4
99999905...

output:

2249998678040
1749999084876
2249997908420
749999928324
1499999212288
1999999426588
1499999067976
1249999944622
1749998606648
749999814074
1499999626366
999999738010
999999033920
1999999696564
1499999084966
1749999240286
1749998368556
1499999744310
999999229192
1499998578510
1499999669604
19999997184...

result:

ok 181572 numbers

Test #11:

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

input:

2011
7 471
13 438
71 450
73 503
11 431
41 332
17 439
79 301
19 573
53 639
41 573
97 323
41 681
37 647
79 671
31 381
7 573
23 330
83 413
83 627
61 356
23 499
29 645
79 302
43 407
11 544
79 367
29 676
11 584
11 343
13 494
37 385
29 673
37 396
59 555
67 507
37 518
7 594
59 330
79 691
29 401
17 567
7 50...

output:

0
4
304
284
6
76
8
346
20
160
76
528
76
64
346
46
0
24
412
412
204
24
44
346
102
6
346
44
6
6
4
64
44
64
218
256
64
0
218
346
44
8
0
44
346
46
528
102
528
122
428
0
44
76
304
160
256
46
6
218
4
76
428
428
46
204
0
4
412
428
44
46
46
46
0
8
204
346
304
284
218
76
256
160
256
0
4
428
64
76
20
204
4
44...

result:

ok 2011 numbers

Test #12:

score: 0
Accepted
time: 183ms
memory: 4004kb

input:

1990
637503428677 440
549004707871 541
769745081489 695
879093250957 605
611329620529 652
864211918343 666
700791036773 349
850753783819 535
932257508641 300
985130811229 642
874333565569 392
615973720897 490
925773806137 591
719331117991 439
769555585171 649
847102210267 458
658159634039 383
900121...

output:

70125377105496
74252886666334
133743207786678
132962854114850
99646728038282
143891284293240
61144017927718
113788318513656
69919313117130
158113495098766
85684689384592
75456780748950
136783079767306
78946590150896
124860393587860
96993203022690
63018784930098
110264832212732
163656960936460
814107...

result:

ok 1990 numbers

Test #13:

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

input:

2008
37067759 665
997706855857 421
873137128477 574
622158050639 520
88965103 481
824624568587 325
746989568293 699
899174850389 657
95088403 568
588674377667 313
998841897227 489
683237363819 496
42812989 378
911280507173 580
810962042663 458
828206107847 343
66396221 455
712089656231 634
853410221...

output:

6162403114
105008646532330
125295177853110
80880546520240
10697995142
67000746170764
130536426936320
147689469067352
13502471902
46063770027660
122108421875658
84721433055518
4045789976
132135673455522
92855153832446
71018673718992
7552517926
112866210413454
65285881929922
170039581786022
1350042154...

result:

ok 2008 numbers

Test #14:

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

input:

2010
999999309887 634
999999286501 333
999999180329 463
999999721921 615
999999149251 313
999999853451 304
999999426053 531
999999318667 626
999999682133 464
999999781051 326
999999901759 632
999999138221 575
999999375383 656
999999018511 423
999999660827 412
999999679697 424
999999092647 500
999999...

output:

158499890515992
83249940573174
115749905069020
153749957145426
78249933404132
75999988838844
132749923737502
156499893272580
115999963072994
81499982128920
157999984377590
143749876036130
163999897455460
105749896162654
102999965022600
105999966001950
124999886517730
173749838494766
158499864580304
...

result:

ok 2010 numbers

Test #15:

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

input:

100
19 10256
43 9026
31 10423
73 9620
13 10093
79 10381
67 9662
43 9797
83 10140
67 9871
53 9216
53 9107
41 10325
31 10086
31 10781
41 9944
79 9163
37 10208
7 9778
43 10157
43 10185
61 10448
67 10751
37 9947
89 9049
79 9567
59 10796
71 9283
5 9306
59 9369
97 9275
5 9230
23 9726
67 9354
11 10430
17 9...

output:

20
102
46
284
4
346
256
102
412
256
160
160
76
46
46
76
346
64
0
102
102
204
256
64
428
346
218
304
0
218
528
0
24
256
6
8
64
160
4
6
304
6
44
6
8
0
46
76
160
0
6
528
6
76
528
46
4
122
64
24
412
256
160
284
428
412
46
0
346
218
412
428
76
76
304
46
76
122
160
284
20
304
428
204
6
4
428
346
122
76
8
...

result:

ok 100 numbers

Test #16:

score: 0
Accepted
time: 141ms
memory: 4608kb

input:

99
837315702089 9318
742374151357 10094
740840788559 10929
568368745229 10385
825135734509 10357
956685244061 10196
507072573797 10544
982627709341 10591
713147778787 10042
888149520173 9474
899919414971 9744
501171020969 10982
601135101293 10130
987974989147 9512
548068782361 10235
528327518347 106...

output:

1950526906289312
1873381145465826
2024162214677174
1475627327831124
2136482673729052
2438590661103730
1336643276724390
2601752489301378
1790357473423364
2103582116078956
2192203671147810
1375965007901068
1522374618358432
2349404501565580
1402370970444086
1407332398607020
1851756556673614
15148228256...

result:

ok 99 numbers

Test #17:

score: 0
Accepted
time: 135ms
memory: 4636kb

input:

100
91998043 10152
879778234633 9812
800889095533 10286
576190576349 10935
82187477 9188
985378110337 9130
666488218141 10549
886993073311 10370
12299291 9148
924809757733 10359
957294310481 9048
999186537053 10301
14306651 10234
770161755599 9915
991900668119 10216
677394473333 9851
32374597 10017
...

output:

233465255698
2158095985396262
2059486282685864
1575160958190762
188763521804
2249125515884350
1757696025460266
2299529515659056
28107554936
2395026043238452
2165399709815742
2573155103010216
36577397672
1909038427198584
2533314280268352
1668253214930594
81048985670
1891299897534418
1624606312444994
...

result:

ok 100 numbers

Test #18:

score: 0
Accepted
time: 160ms
memory: 4616kb

input:

99
999999268259 9214
999999943699 10718
999999685541 9099
999999763283 10512
999999817571 10083
999999330683 9023
999999692759 9903
999999728249 10492
999999839093 10659
999999375911 10679
999999725111 10049
999999662693 9059
999999599593 10052
999999137617 9756
999999109673 10213
999999160673 10239...

output:

2303498293221744
2679499820414282
2274749263975026
2627999350281822
2520749514734104
2255748469825378
2475749214870964
2622999259634288
2664749542805060
2669748305315522
2512249284218164
2264749215556486
2512998968500938
2438997872844218
2553247700668536
2559747825306160
2694497689619794
25057484152...

result:

ok 99 numbers

Test #19:

score: 0
Accepted
time: 121ms
memory: 67756kb

input:

1
36283549 1000000

output:

8820884420630

result:

ok 1 number(s): "8820884420630"

Test #20:

score: 0
Accepted
time: 133ms
memory: 67656kb

input:

1
999999324481 1000000

output:

249999581112252902

result:

ok 1 number(s): "249999581112252902"