QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#766861#9254. Random VariablesguosounAC ✓876ms11156kbC++1716.1kb2024-11-20 18:59:302024-11-20 18:59:33

Judging History

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

  • [2024-11-20 18:59:33]
  • 评测
  • 测评结果:AC
  • 用时:876ms
  • 内存:11156kb
  • [2024-11-20 18:59:30]
  • 提交

answer

#include <bits/stdc++.h>
#ifndef ATCODER_MODINT_HPP
#define ATCODER_MODINT_HPP 1

#include <cassert>
#include <numeric>
#include <type_traits>

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

#ifndef ATCODER_INTERNAL_MATH_HPP
#define ATCODER_INTERNAL_MATH_HPP 1

#include <utility>

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

namespace atcoder {

namespace internal {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_INTERNAL_MATH_HPP

#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP
#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1

#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

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

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

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

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

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

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

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

#else

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

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

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

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

#endif

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

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

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

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_INTERNAL_TYPE_TRAITS_HPP

namespace atcoder {

namespace internal {

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

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

}  // namespace internal

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

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

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

  unsigned int val() const { return _v; }

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

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

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

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

  friend mint operator+(const mint& lhs, const mint& rhs) {
    return mint(lhs) += rhs;
  }
  friend mint operator-(const mint& lhs, const mint& rhs) {
    return mint(lhs) -= rhs;
  }
  friend mint operator*(const mint& lhs, const mint& rhs) {
    return mint(lhs) *= rhs;
  }
  friend mint operator/(const mint& lhs, const mint& rhs) {
    return mint(lhs) /= rhs;
  }
  friend bool operator==(const mint& lhs, const mint& rhs) {
    return lhs._v == rhs._v;
  }
  friend bool operator!=(const mint& lhs, const mint& rhs) {
    return lhs._v != rhs._v;
  }

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

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

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

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

  unsigned int val() const { return _v; }

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

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

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

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

  friend mint operator+(const mint& lhs, const mint& rhs) {
    return mint(lhs) += rhs;
  }
  friend mint operator-(const mint& lhs, const mint& rhs) {
    return mint(lhs) -= rhs;
  }
  friend mint operator*(const mint& lhs, const mint& rhs) {
    return mint(lhs) *= rhs;
  }
  friend mint operator/(const mint& lhs, const mint& rhs) {
    return mint(lhs) /= rhs;
  }
  friend bool operator==(const mint& lhs, const mint& rhs) {
    return lhs._v == rhs._v;
  }
  friend bool operator!=(const mint& lhs, const mint& rhs) {
    return lhs._v != rhs._v;
  }

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

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

namespace internal {

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

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

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

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

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_MODINT_HPP

using ll = long long;
using mint = atcoder::modint;
void mian() {
  int n, m;
  std::cin >> n >> m;
  std::vector bin(n + 1, std::vector<mint>(n + 1));
  for (int i = 0; i <= n; i++) {
    bin[i][0] = 1;
    for (int j = 1; j <= i; j++) bin[i][j] = bin[i - 1][j] + bin[i - 1][j - 1];
  }
  std::vector<mint> binm(n + 1);
  binm[0] = 1;
  for (int i = 1; i <= n; i++) {
    binm[i] = binm[i - 1] * (m - i + 1);
  }
  mint ans = 0;
  for (int k = 1; k <= n; k++) {
    std::vector dp(n / k + 1, std::vector<mint>(n + 1));
    dp[0][0] = 1;
    mint cur = 0;
    for (int i = 1; i * k <= n && i <= m; i++) {
      std::vector<mint> pow(n + 1, 1);
      for (int j = 1; j <= n; j++) pow[j] = pow[j - 1] * (m - i);
      for (int j = k; j <= n; j++) {
        dp[i][j] = dp[i][j - 1] * i + dp[i - 1][j - k] * bin[j - 1][k - 1];
        auto t = dp[i][j] * pow[n - j] * bin[n][j] * binm[i];
        if (i & 1)
          cur += t;
        else
          cur -= t;
      }
    }
    ans += cur;
  }
  std::cout << ans.val() << '\n';
}
int main() {
  std::cin.tie(0)->sync_with_stdio(0);
  int t, mod;
  std::cin >> t >> mod;
  mint::set_mod(mod);
  while (t--) mian();
}

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

詳細信息

Test #1:

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

input:

3 123456789
3 2
5 5
7 7

output:

18
7145
2066323

result:

ok 3 lines

Test #2:

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

input:

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

output:

1
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0

result:

ok 100 lines

Test #3:

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

input:

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

output:

1
2
0
1
2
0
1
2
0
1
2
0
0
2
0
0
2
0
0
2
0
0
0
0
0
0
0
0
0
0
1
2
0
1
2
0
1
2
0
1
2
2
0
2
2
0
2
2
0
2
0
0
0
0
0
0
0
0
0
0
1
0
0
1
0
0
1
0
0
1
2
2
0
2
2
0
2
2
0
2
0
0
0
0
0
0
0
0
0
0
1
2
0
1
2
0
1
2
0
1

result:

ok 100 lines

Test #4:

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

input:

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

output:

1
2
3
0
1
2
3
0
1
2
2
2
0
0
2
2
0
0
2
2
3
2
3
0
3
2
3
0
3
2
0
0
0
0
0
0
0
0
0
0
1
2
3
0
1
2
3
0
1
2
2
0
2
0
2
0
2
0
2
0
3
0
3
0
3
0
3
0
3
0
0
0
0
0
0
0
0
0
0
0
1
2
3
0
1
2
3
0
1
2
2
0
2
0
2
0
2
0
2
0

result:

ok 100 lines

Test #5:

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

input:

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

output:

1
2
3
4
0
1
2
3
4
0
2
1
2
0
0
2
1
2
0
0
3
3
1
3
0
3
3
1
3
0
4
4
2
4
0
4
4
2
4
0
0
0
0
0
0
0
0
0
0
0
1
2
3
4
0
1
2
3
4
0
2
3
3
2
0
2
3
3
2
0
3
4
1
2
0
3
4
1
2
0
4
4
4
4
0
4
4
4
4
0
0
0
0
0
0
0
0
0
0
0

result:

ok 100 lines

Test #6:

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

input:

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

output:

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

result:

ok 100 lines

Test #7:

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

input:

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

output:

1
2
3
4
5
6
0
1
2
3
2
6
5
6
2
0
0
2
6
5
3
4
2
3
6
3
0
3
4
2
4
2
3
5
2
5
0
4
2
3
5
5
3
6
5
4
0
5
5
3
6
0
6
1
1
6
0
6
0
6
0
0
0
0
0
0
0
0
0
0
1
2
3
4
5
6
0
1
2
3
2
1
4
4
1
2
0
2
1
4
3
3
4
3
4
4
0
3
3
4

result:

ok 100 lines

Test #8:

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

input:

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

output:

1
2
3
4
5
6
7
0
1
2
2
6
4
4
6
2
0
0
2
6
3
2
3
4
3
6
3
0
3
2
4
4
0
0
4
4
0
0
4
4
5
6
3
4
1
2
7
0
5
6
6
4
6
0
6
4
6
0
6
4
7
4
3
0
7
4
3
0
7
4
0
0
0
0
0
0
0
0
0
0
1
6
3
4
5
2
7
0
1
6
2
4
6
0
2
4
6
0
2
4

result:

ok 100 lines

Test #9:

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

input:

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

output:

1
2
3
4
5
6
7
8
0
1
2
6
3
2
3
6
2
0
0
2
3
0
6
0
6
3
6
3
0
3
4
8
3
4
5
6
4
2
0
4
5
2
0
2
8
0
8
5
0
5
6
0
0
6
0
0
6
0
0
6
7
3
6
1
3
3
4
3
0
7
8
8
6
5
8
3
2
8
0
8
0
0
0
0
0
0
0
0
0
0
1
8
3
4
2
6
7
5
0
1

result:

ok 100 lines

Test #10:

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

input:

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

output:

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

result:

ok 100 lines

Test #11:

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

input:

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

output:

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

result:

ok 100 lines

Test #12:

score: 0
Accepted
time: 314ms
memory: 9764kb

input:

10 972033073
576 523187654
758 588616188
30 532959085
476 481773028
573 76725430
520 142462406
865 820120297
687 526533288
913 38106557
67 924529654

output:

259748390
909910217
708973357
300073565
463921261
889897372
587262932
255642402
868975954
14589849

result:

ok 10 lines

Test #13:

score: 0
Accepted
time: 405ms
memory: 11080kb

input:

10 922366485
846 278501607
683 609355362
44 978777279
545 730718412
926 323835432
883 761846029
623 408215612
989 588832935
449 743830620
259 183431187

output:

461786112
672633342
164805246
547995105
9661617
154501063
370848893
402005970
886523490
435107511

result:

ok 10 lines

Test #14:

score: 0
Accepted
time: 407ms
memory: 10864kb

input:

10 13890975
949 837425969
667 981449995
991 564074312
501 604745038
593 640307170
128 408163542
80 976891742
930 710947599
852 333118419
250 333252788

output:

3898759
9290500
7087084
4913904
196250
1746549
9627740
8673120
10274253
10549775

result:

ok 10 lines

Test #15:

score: 0
Accepted
time: 214ms
memory: 8904kb

input:

10 105576445
649 937885257
141 713063090
253 716966251
845 330657011
347 664392407
810 50478969
389 530582574
228 199722046
85 256258366
605 3721959

output:

22721419
27962190
85541228
53950260
35288938
100176945
86409840
102331663
55591445
14790745

result:

ok 10 lines

Test #16:

score: 0
Accepted
time: 272ms
memory: 10012kb

input:

10 445185474
268 687201814
929 296077349
690 202741564
372 661889855
442 989604795
367 456833096
702 862601129
795 37538865
556 131444040
108 645857776

output:

39577672
390323147
423333756
49417686
12978114
278291170
60346062
410583855
68429394
296833176

result:

ok 10 lines

Test #17:

score: 0
Accepted
time: 393ms
memory: 10664kb

input:

10 265384486
870 503808438
959 733458117
126 226376632
979 205878607
747 270352323
339 384431347
373 659485098
597 832514575
832 906898547
12 869891031

output:

54820154
83262107
48675762
32938269
169458409
153632065
105152812
48645927
29870948
83831862

result:

ok 10 lines

Test #18:

score: 0
Accepted
time: 261ms
memory: 9176kb

input:

10 869896294
256 326197921
496 115501273
861 238744067
581 600444623
619 536213251
89 898877607
136 353575223
860 349472278
491 770026371
668 622723560

output:

678111040
344947200
90686837
157367547
295943299
25262829
81930384
532341712
23048077
475131428

result:

ok 10 lines

Test #19:

score: 0
Accepted
time: 492ms
memory: 10900kb

input:

10 692092859
831 647975618
792 737778459
392 768554014
854 612888229
31 148093584
793 559010229
970 237393805
339 914914862
831 979073722
988 738224088

output:

324659472
16793498
421391172
416475848
59704753
347151224
415078841
680610884
397373492
296521551

result:

ok 10 lines

Test #20:

score: 0
Accepted
time: 201ms
memory: 10372kb

input:

10 827165684
577 720722656
383 778750361
951 59165685
502 993162103
589 166261195
500 816688874
40 625075150
331 160531509
394 578798823
181 710984062

output:

736529364
199088527
528654835
586634074
442300715
383600380
707706396
763397655
534310310
338272096

result:

ok 10 lines

Test #21:

score: 0
Accepted
time: 211ms
memory: 10556kb

input:

10 691312083
185 445519030
93 44970277
951 662144708
252 766000017
83 911805458
424 816227326
770 136026896
354 763387805
247 458147285
747 14566368

output:

411209183
132362175
110569626
664410537
241484162
480388660
264805387
294178848
147876955
371900799

result:

ok 10 lines

Test #22:

score: 0
Accepted
time: 876ms
memory: 11156kb

input:

10 691312083
1000 445519030
1000 44970277
1000 662144708
1000 766000017
1000 911805458
1000 816227326
1000 136026896
1000 763387805
1000 458147285
747 14566368

output:

365043118
14826361
571573673
63977538
484010015
499398766
433242788
43269113
412491407
371900799

result:

ok 10 lines

Extra Test:

score: 0
Extra Test Passed