QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293918#7775. 【模板】矩阵快速幂georgeyucjr100 ✓1103ms29272kbC++2327.0kb2023-12-29 22:58:082023-12-29 22:58:09

Judging History

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

  • [2023-12-29 22:58:09]
  • 评测
  • 测评结果:100
  • 用时:1103ms
  • 内存:29272kb
  • [2023-12-29 22:58:08]
  • 提交

answer

#pragma GCC optimize(2, 3, "Ofast", "unroll-loops", "inline")
#include <bits/stdc++.h>
using namespace std;

#if __cplusplus >= 201700LL
#define INLINE_V inline
#else
#define INLINV_V
#endif

#ifndef ATCODER_MODINT_HPP
#define ATCODER_MODINT_HPP 1

#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 < 2^31`
  barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

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

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

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

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

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

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

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

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

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

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

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

} // namespace internal

} // namespace atcoder

#endif // ATCODER_INTERNAL_MATH_HPP

#ifndef ATCODER_INTERNAL_TYPE_TRAITS_HPP
#define ATCODER_INTERNAL_TYPE_TRAITS_HPP 1

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

# ifndef INLINE_V
# if __cplusplus >= 201700LL
# define INLINE_V inline
# else
# define INLINE_V
# endif
# endif

#ifndef LUOGU
#define auto_att __attribute__((target("avx2"), optimize(3, "Ofast", "unroll-loops", "inline")))
#else
#define auto_att
#endif

# ifndef __GEORGEYUCJR_READ__
# define __GEORGEYUCJR_READ__
namespace FastIO {
#define endl '\n'
INLINE_V static constexpr size_t buf_size = 1 << 18;
INLINE_V static constexpr size_t integer_size = 20;
INLINE_V static constexpr size_t block_size = 10000;

static char inbuf[buf_size + 1] = {};
static char outbuf[buf_size + 1] = {};
static char block_str[block_size * 4 + 1] = {};

static constexpr uint64_t power10[] = {1,
                                       10,
                                       100,
                                       1000,
                                       10000,
                                       100000,
                                       1000000,
                                       10000000,
                                       100000000,
                                       1000000000,
                                       10000000000,
                                       100000000000,
                                       1000000000000,
                                       10000000000000,
                                       100000000000000,
                                       1000000000000000,
                                       10000000000000000,
                                       100000000000000000,
                                       1000000000000000000,
                                       10000000000000000000u};

struct Scanner {
#ifndef LOCAL
private:
  size_t pos, end;

  inline void load() {
    end = fread(inbuf, 1, buf_size, stdin);
    inbuf[end] = '\0';
  }
  inline void reload() {
    size_t len = end - pos;
    memmove(inbuf, inbuf + pos, len);
    end = len + fread(inbuf + len, 1, buf_size - len, stdin);
    inbuf[end] = '\0';
    pos = 0;
  }
  inline void skip_space() {
    while (inbuf[pos] <= ' ') {
      if (__builtin_expect(++pos == end, 0))
        reload();
    }
  }
  inline char get_next() { return inbuf[pos++]; }
  inline char get_next_nonspace() {
    skip_space();
    return inbuf[pos++];
  }

public:
  Scanner() { load(); }

  auto_att inline void read(char &c) { c = get_next_nonspace(); }
  auto_att inline void read(string &s) {
    skip_space();
    s = "";
    do {
      size_t start = pos;
      while (inbuf[pos] > ' ')
        ++pos;
      s += string(inbuf + start, inbuf + pos);
      if (inbuf[pos] != '\0')
        break;
      reload();
    } while (true);
  }

  template <class T> typename enable_if<is_integral<T>::value, void>::type read(T &x) {
    char c = get_next_nonspace();
    if (__builtin_expect(pos + integer_size >= end, 0))
      reload();
    bool neg = false;
    if (c == '-')
      neg = true, x = 0;
    else
      x = c & 15;
    while ((c = get_next()) >= '0')
      x = (x << 3) + (x << 1) + (c & 15);
    if (neg)
      x = -x;
  }
#else
  template <typename _Tp> inline void read(_Tp &x) {
    char ch = getchar();
    int f = 1;
    x = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
      f = ch == '-' ? -1 : 1;
    for (; ch >= '0' && ch <= '9'; ch = getchar())
      x = (x << 3) + (x << 1) + (ch ^ 48);
    x *= f;
  }
  inline void read(char &c) { c = getchar(); }
  inline void read(char *x) {
    int i = 0;
    char c = ' ';
    for (; c <= ' ';)
      c = getchar();
    for (; c > ' ';)
      x[i++] = c, c = getchar();
    x[i] = 0;
  }
  inline void read(string &x) {
    x.clear();
    char c = ' ';
    for (; c <= ' ';)
      c = getchar();
    for (; c > ' ';)
      x += c, c = getchar();
  }
#endif
  template <class Head, class... Others> void read(Head &head, Others &...others) {
    read(head);
    read(others...);
  }
  template <class T1, class T2> inline void read(pair<T1, T2> &x) { read(x.first), read(x.second); }
}is;

struct Printer {
#ifndef LOCAL
private:
  size_t pos = 0;

  auto_att inline void flush() {
    fwrite(outbuf, 1, pos, stdout);
    pos = 0;
  }

  inline void pre_calc() {
    for (size_t i = 0; i < block_size; ++i) {
      size_t j = 4, k = i;
      while (j--) {
        block_str[i * 4 + j] = k % 10 | 48;
        k /= 10;
      }
    }
  }

  static constexpr size_t get_integer_size(uint64_t n) {
    if (n >= power10[10]) {
      if (n >= power10[19])
        return 20;
      if (n >= power10[18])
        return 19;
      if (n >= power10[17])
        return 18;
      if (n >= power10[16])
        return 17;
      if (n >= power10[15])
        return 16;
      if (n >= power10[14])
        return 15;
      if (n >= power10[13])
        return 14;
      if (n >= power10[12])
        return 13;
      if (n >= power10[11])
        return 12;
      return 11;
    } else {
      if (n >= power10[9])
        return 10;
      if (n >= power10[8])
        return 9;
      if (n >= power10[7])
        return 8;
      if (n >= power10[6])
        return 7;
      if (n >= power10[5])
        return 6;
      if (n >= power10[4])
        return 5;
      if (n >= power10[3])
        return 4;
      if (n >= power10[2])
        return 3;
      if (n >= power10[1])
 return 2;
      return 1;
    }
  }

public:
  Printer() { pre_calc(); }
  ~Printer() { flush(); }
  inline void write(char c) {
    outbuf[pos++] = c;
    if (__builtin_expect(pos == buf_size, 0))
      flush();
  }
  inline void write(const char *s) {
    while (*s != 0) {
      outbuf[pos++] = *s++;
      // if (pos == buf_size) flush();
      if (__builtin_expect(pos == buf_size, 0))
        flush();
    }
  }
  inline void write(const string &s) {
    for (auto c : s) {
      outbuf[pos++] = c;
      // if (pos == buf_size) flush();
      if (__builtin_expect(pos == buf_size, 0))
        flush();
    }
  }

  template <class T> typename enable_if<is_integral<T>::value, void>::type write(T x) {
    if (__builtin_expect(pos + integer_size >= buf_size, 0))
      flush();
    if (x < 0)
      write('-'), x = -x;
    size_t digit = get_integer_size(x);
    size_t len = digit;
    while (len >= 4) {
      len -= 4;
      memcpy(outbuf + pos + len, block_str + ((x % block_size) << 2), 4);
      x /= block_size;
    }
    memcpy(outbuf + pos, block_str + (x << 2) + (4 - len), len);
    pos += digit;
  }
#else
  inline void write(char c) { putchar(c); }
  inline void write(const char *x) {
    for (int i = 0; x[i]; ++i)
      write(x[i]);
  }
  inline void write(string s) {
    for (auto c : s)
      write(c);
  }
  template <typename _Tp> inline void write(_Tp x) {
    if (x < 0)
      putchar('-'), x = -x;
    if (x > 9)
      write(x / 10);
    putchar(x % 10 ^ 48);
  }
#endif
  template <class Head, class... Others> void write(const Head &head, const Others &...others) {
    write(head);
    write(' ');
    write(others...);
  }

  template <class... Args> void writeln(const Args &...args) {
    write(args...);
    write('\n');
  }
  template <class T1, class T2> inline void write(const pair<T1, T2> &x) { write(x.first, x.second); }
}os;
}; // namespace FastIO
using namespace FastIO;
# endif

#define ll long long
#define ull unsigned long long
#define rep(i, f, t, ...) for (int i = f, ##__VA_ARGS__; i <= t; ++i)
#define red(i, f, t, ...) for (int i = f, ##__VA_ARGS__; i >= t; --i)
#define emb emplace_back
#define pb push_back
#define pii pair<int, int>
#define mkp make_pair
#define arr3 array<int, 3>
#define arr4 array<int, 4>
#define FILEIO(filename) freopen(filename ".in", "r", stdin), freopen(filename ".out", "w", stdout)
#define ALrep(vc) vc.begin(), vc.end()
#define N 605
template <class T> constexpr static T inf = numeric_limits<T>::max() / 10;

# ifndef __GEORGEYUCJR_READ__
	# ifdef MACOS
		# include "/Users/yzw/GeorgeYuOI/codes/cpp/georgeyucjr/debug/debug.hpp"
		using namespace georgeyucjr;
	# else
		# define write(...) void(36)
		# define bug(...) void(36)
	# endif
# else
	# warning Do not use debug.hpp.
# endif

bool Mst;

using mint = atcoder::modint998244353;
using i128 = __int128_t;
using db = long double;
INLINE_V constexpr static i128 VAL1 = 2e18;
INLINE_V constexpr static i128 VAL2 = 1e31;
INLINE_V constexpr static i128 VAL3 = 1e27;

int n, m, Eu[N], Ev[N];
ll k, Ew[N];

namespace Solve1 { // k <= 2 * n * n

i128 dis[N], updis[N];
inline void wrk() {
  fill(updis + 1, updis + n + 1, 2 * inf<i128>);
  rep(i, 1, m) updis[Ev[i]] = min(updis[Ev[i]], dis[Eu[i]] + Ew[i]);
  copy_n(updis + 1, n, dis + 1);
}
i128 cyc[N][N], ray[N][N];

inline void SLVR() {
  fill(dis + 1, dis + n + 1, 2 * inf<i128>);
  dis[1] = 0;
  ll LEN = n * (n + 1) * 3;
  if (k <= 2 * LEN)
    rep(t, 1, k) wrk();
  else {
    ll mid = k - LEN * 2;
    rep(i, 1, n) {
      fill(dis + 1, dis + n + 1, 2 * inf<i128>);
      dis[i] = 0;
      rep(j, 1, n) wrk(), cyc[i][j] = ((dis[i] < inf<i128>) ? (dis[i]) : (-1));
    }
    fill(dis + 1, dis + n + 1, 2 * inf<i128>);
    dis[1] = 0;
    rep(t, 1, LEN) wrk();
    rep(i, 1, n) rep(j, 1, n) ray[i][j] = 2 * inf<i128>;
    rep(i, 1, n) if (dis[i] < inf<i128>) rep(j, 1, n) if (~cyc[i][j]) {
      ll t = mid / j + 1;
      ll sm = t * j - mid;
      ray[sm][i] = min(ray[sm][i], dis[i] + (i128)t * cyc[i][j]);
    }
    fill(dis + 1, dis + n + 1, 2 * inf<i128>);
    rep(t, 1, LEN) {
      wrk();
      if (t <= n)
        rep(i, 1, n) dis[i] = min(dis[i], ray[t][i]);
    }
  }
  rep(i, 1, n) os.write((dis[i] > inf<i128> ? -1 : (int)mint(dis[i]).val()), (i == n ? "\n" : " "));
}
} // namespace Solve1
namespace Solve2 { // k > 2 * n * n
i128 dis[N], updis[N];
struct Frac {
  int L;
  i128 S;
  Frac(i128 sum = 0, int len = 0) { L = len, S = sum; }
};
inline bool operator<(const Frac &lhs, const Frac &rhs) { return lhs.S * rhs.L < rhs.S * lhs.L; }
inline bool operator==(const Frac &lhs, const Frac &rhs) { return lhs.S * rhs.L == rhs.S * lhs.L; }

struct Node {
  Frac a, b;
  Node(Frac A = Frac(1, 1), Frac B = Frac(1, 1)) { a = A, b = B; }
};

bool flag;
i128 K, LIM;

inline bool operator<(const Node &x, const Node &y) {
  i128 vl1 = x.a.S * y.a.L;
  i128 vl2 = x.a.L * y.a.S;
  if (vl1 == vl2)
    return x.b < y.b;
  if (flag)
    return vl1 < vl2;
  i128 dt = vl1 - vl2;
  return (-LIM <= dt && dt <= LIM) ? dt * K + x.b.S * y.b.L < x.b.L * y.b.S : vl1 < vl2;
}

INLINE_V const static Node Nd_inf = Node(Frac(VAL2, 1), Frac(VAL2, 1));

inline void wrk() {
  fill(updis + 1, updis + n + 1, 2 * inf<i128>);
  rep(i, 1, m) updis[Ev[i]] = min(updis[Ev[i]], dis[Eu[i]] + Ew[i]);
  copy_n(updis + 1, n, dis + 1);
}

Node tempdis[N], tempud[N];
inline void SAP() {
  fill(tempud + 1, tempud + n + 1, Nd_inf);
  rep(i, 1, m) {
    auto cur = tempdis[Eu[i]];
    cur.b.S += (__int128)Ew[i] * cur.b.L;
    tempud[Ev[i]] = min(tempud[Ev[i]], cur);
  }
  copy_n(tempud + 1, n, tempdis + 1);
}
i128 cyc[N][N];
Node ray[N][N];

inline void SLVR(string S) {
  fill(dis + 1, dis + n + 1, 2 * inf<i128>);
  dis[1] = 0;
  ll LEN = n * (n + 1) * 2;

  db tmp = 0;
  for (auto ch : S)
    tmp = tmp * 10 + (ch ^ 48);
  if (tmp > VAL3) {
    flag = true;
  } else {
    flag = false;
    K = 0;
    for (auto ch : S)
      K = K * 10 + (ch ^ 48);
    K -= LEN * 2;
    if (K)
      LIM = 2e36 / K;
  }

  rep(i, 1, n) {
    fill(dis + 1, dis + n + 1, 2 * inf<i128>);
    dis[i] = 0;
    rep(j, 1, n) wrk(), cyc[i][j] = ((dis[i] < inf<i128>) ? dis[i] : -1);
  }
  fill(dis + 1, dis + n + 1, 2 * inf<i128>);
  dis[1] = 0;
  rep(t, 1, LEN) wrk();

  vector<int> pos(n + 1);
  rep(i, 1, n, w) {
    w = 0;
    for (auto &ch : S)
      w = w * 10 + (ch ^ 48), w %= i;
    pos[i] = ((w - LEN * 2) % i + i) % i;
  }

  rep(i, 1, n) rep(j, 1, n) ray[i][j] = Nd_inf;
  rep(i, 1, n) if (dis[i] < inf<i128>) rep(j, 1, n, sm) if (~cyc[i][j])
      sm = j - pos[j],
      ray[sm][i] = min(ray[sm][i], Node(Frac(cyc[i][j], j), Frac(dis[i] * j + sm * cyc[i][j], j)));
  rep(i, 1, n) tempdis[i] = Nd_inf;
  rep(t, 1, LEN) {
    SAP();
    if (t <= n)
      rep(i, 1, n) tempdis[i] = min(tempdis[i], ray[t][i]);
  }
  mint num = 0;
  for (auto ch : S)
    (num *= 10) += (ch ^ 48);
  num -= LEN * 2;

  rep(i, 1, n) {
    auto r = tempdis[i];
    if ((db)r.a.S / r.a.L > VAL1)
      os.write ("-1 ");
    else
      os.write (((num * r.a.S + r.b.S) * mint(r.b.L).inv()).val(), ' ');
  }
  os.write('\n');
}
} // namespace Solve2

bool Med;

signed main() {
  auto slv = [&]() {
    string S;
    is.read(n, m, S);
    db tmp = 0;
    for (auto ch : S)
      tmp = tmp * 10 + (ch ^ 48);
    rep(i, 1, m) is.read(Eu[i], Ev[i], Ew[i]);
    if (tmp <= n * n * 10) {
      k = 0;
      for (auto ch : S)
        k = k * 10 + (ch ^ 48);
      Solve1::SLVR();
      return;
    }
    Solve2::SLVR(S);
  };
  int T;
# define _ ,
  is.read(T _ T);
# undef _
  while (T--)
    slv();

#ifdef MACOS
  cerr << "Memory & Time Information : " << endl;
  cerr << "Memory : " << ((&Med) - (&Mst)) * 1. / 1024. / 1024. << "MB" << endl;
  cerr << "Time : " << clock() * 1. / CLOCKS_PER_SEC * 1000. << "ms" << endl;
#endif
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 22ms
memory: 27144kb

input:

1
1
100 101 899539897889989959
74 35 910832669819965536
35 85 910832669819965536
85 88 910832669819965536
88 30 910832669819965536
30 58 910832669819965536
58 60 910832669819965536
60 34 910832669819965536
34 8 910832669819965536
8 67 910832669819965536
67 89 910832669819965536
89 32 910832669819965...

output:

395495792  395495781  395495783  395495839  395495793  395495789  395495754  395495832  395495845  395495755  395495823  395495773  395495753  395495800  395495782  395495763  395495847  395495761  395495794  395495791  395495786  395495821  395495798  395495765  395495772  395495770  395495757  395...

result:

ok 100 numbers

Test #2:

score: 0
Accepted
time: 61ms
memory: 27176kb

input:

1
1
100 200 998858598565699977
89 61 596014036562538539
89 84 921297646113897322
61 84 946923234442637386
61 35 641628261157284465
84 35 979893473772327497
84 78 700172488379560611
35 78 963617193748189613
35 54 951598888254521423
78 54 680825215292116806
78 21 737055858973038555
54 21 7491794406112...

output:

590375247  265938345  203065828  597548045  369717762  226160283  377877020  360218254  956162456  408060901  387231165  759578975  67601808  790211315  608425007  343195480  177353482  436533546  717630459  417099733  542227025  861764246  913806375  587268602  989846681  435016550  66609901  81709...

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 50ms
memory: 27104kb

input:

1
1
100 181 348568663892999968
25 19 990622898175774733
19 94 871060999389241529
94 24 969317630558501400
24 43 908457844888427461
43 52 816088481082287266
52 62 978618931332609685
62 99 761714433396732044
99 85 741344935503895668
85 64 964684335126604843
64 69 988098065125373655
69 31 7506975506815...

output:

916998469  916998469  916998469  76035207  62461893  916998469  389136594  916998469  916998469  173423529  164423356  822964468  626456020  916998469  744111524  916998469  398953850  916998469  342238577  916998469  255074799  784015663  916998469  740933556  587088671  811719512  916998469  91699...

result:

ok 100 numbers

Test #4:

score: 0
Accepted
time: 42ms
memory: 27068kb

input:

1
1
100 189 295064635124730243
18 50 754672892083203214
50 88 962632394366987404
88 15 906700334097319336
15 26 967741400981618572
26 91 996214498763867892
91 35 882157548994344280
35 68 983621159612138407
68 51 563935036482744182
51 75 991205513962219551
75 72 974025375183814852
72 11 7979447663592...

output:

663199381  739882534  663199381  28600701  663199381  944601671  836329160  894091561  629507606  663199381  246830507  663199381  491987421  663199381  802123884  663199381  663199381  663199381  414785533  989396289  663199381  663199381  663199381  663199381  663199381  663199381  663199381  6631...

result:

ok 100 numbers

Test #5:

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

input:

1
254
40 74 997173688939799978
38 6 890721839505665075
6 10 992308491267087930
10 29 960202932780090595
29 20 952827125924298715
20 34 868314670055961466
34 31 756448635709788087
31 14 857625921909632516
14 18 917667459973696862
18 21 985939328882662624
21 1 734882468602343649
1 11 66102593854575036...

output:

177014577  177014577  177014577  885341552  472856470  177014577  363547548  177014577  499847464  653076748  177014577  177014577  177014577  177014577  487939796  177014577  213466543  586729345  244952763  177014577  177014577  177014577  177014577  890105934  177014577  177014577  890105934  177...

result:

ok 3575 numbers

Test #6:

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

input:

1
356
32 47 967844399484634837
4 30 776954643355911997
30 20 811634053140142741
20 22 747630229183579429
22 2 806282875388761050
2 26 719793351534499411
26 17 797537828929335673
17 24 890423236992687627
24 21 970792227007588899
21 8 850078803097295262
8 15 958474507028658347
15 1 972636122087215360
...

output:

890097469  525779071  636798453  776362497  776362497  687961593  158033324  776362497  345910504  380622623  239804834  440670451  137231885  985041116  222869127  137231885  705696901  637534644  347889826  696528073  291555427  146553026  776362497  624486185  137231885  642408114  520519927  137...

result:

ok 4784 numbers

Subtask #2:

score: 15
Accepted

Test #7:

score: 15
Accepted
time: 1021ms
memory: 29172kb

input:

2
1
300 598 8179377797889487867988994778539839593376697796496698959964978969
1 2 977880533270721156
2 1 977880533270721156
2 3 977880533270721156
3 2 977880533270721156
3 4 977880533270721156
4 3 977880533270721156
4 5 977880533270721156
5 4 977880533270721156
5 6 977880533270721156
6 5 977880533270...

output:

-1 313446627  -1 313436465  -1 313426303  -1 313416141  -1 313405979  -1 313395817  -1 313385655  -1 313375493  -1 313365331  -1 313355169  -1 313345007  -1 313334845  -1 313324683  -1 313314521  -1 313304359  -1 313294197  -1 313284035  -1 313273873  -1 313263711  -1 313253549  -1 313243387  -1 313...

result:

ok 300 numbers

Test #8:

score: 0
Accepted
time: 1027ms
memory: 29160kb

input:

2
1
300 598 9284745978997975899894787995823975998931999649789777849997467689
1 2 946893593823801228
2 1 946893593823801228
2 3 761384824565158999
3 2 761384824565158999
3 4 642721010434291429
4 3 642721010434291429
4 5 936762490761905983
5 4 936762490761905983
5 6 785485094128355256
6 5 785485094128...

output:

-1 613575042  -1 416269325  -1 387291578  -1 980556870  -1 491367967  -1 221793101  -1 191668085  -1 356035653  -1 428450970  -1 964149805  -1 511723806  -1 423081033  -1 947783979  -1 325795034  -1 115778037  -1 86469999  -1 111666379  -1 386592847  -1 223100328  -1 381885001  -1 23001328  -1 84087...

result:

ok 300 numbers

Test #9:

score: 0
Accepted
time: 1022ms
memory: 29128kb

input:

2
1
300 598 7877597936928589688789427798322599997378688496694695996269389696
1 2 866412995946330002
2 1 866412995946330002
2 3 866412995946330002
3 2 866412995946330002
3 4 866412995946330002
4 3 866412995946330002
4 5 866412995946330002
5 4 866412995946330002
5 6 866412995946330002
6 5 866412995946...

output:

708443714  -1 708438498  -1 708433282  -1 708428066  -1 708422850  -1 708417634  -1 708412418  -1 708407202  -1 708401986  -1 708396770  -1 708391554  -1 708386338  -1 708381122  -1 708375906  -1 708370690  -1 708365474  -1 708360258  -1 708355042  -1 708349826  -1 708344610  -1 708339394  -1 708334...

result:

ok 300 numbers

Test #10:

score: 0
Accepted
time: 1057ms
memory: 29212kb

input:

2
1
300 598 74686617152792803
1 2 920869599353968456
2 1 920869599353968456
2 3 920869599353968456
3 2 920869599353968456
3 4 920869599353968456
4 3 920869599353968456
4 5 920869599353968456
5 4 920869599353968456
5 6 920869599353968456
6 5 920869599353968456
6 7 920869599353968456
7 6 9208695993539...

output:

-1 537762223  -1 537752459  -1 537742695  -1 537732931  -1 537723167  -1 537713403  -1 537703639  -1 537693875  -1 537684111  -1 537674347  -1 537664583  -1 537654819  -1 537645055  -1 537635291  -1 537625527  -1 537615763  -1 537605999  -1 537596235  -1 537586471  -1 537576707  -1 537566943  -1 537...

result:

ok 300 numbers

Test #11:

score: 0
Accepted
time: 643ms
memory: 28528kb

input:

2
40
120 238 7647979978895986883485788838258737687493899697379499657768989994
1 2 940784508355800649
2 1 940784508355800649
2 3 940784508355800649
3 2 940784508355800649
3 4 940784508355800649
4 3 940784508355800649
4 5 940784508355800649
5 4 940784508355800649
5 6 940784508355800649
6 5 94078450835...

output:

383704267  -1 383701847  -1 383699427  -1 383697007  -1 383694587  -1 383692167  -1 383689747  -1 383687327  -1 383684907  -1 383682487  -1 383680067  -1 383677647  -1 383675227  -1 383672807  -1 383670387  -1 383667967  -1 383665547  -1 383663127  -1 383660707  -1 383658287  -1 383655867  -1 383653...

result:

ok 3146 numbers

Test #12:

score: 0
Accepted
time: 647ms
memory: 27932kb

input:

2
5697
96 190 8939398847797777979859997957885578698889795859699765658877967896
1 2 940438543633266209
2 1 940438543633266209
2 3 940438543633266209
3 2 940438543633266209
3 4 940438543633266209
4 3 940438543633266209
4 5 940438543633266209
5 4 940438543633266209
5 6 940438543633266209
6 5 9404385436...

output:

57861585  -1 57859879  -1 57858173  -1 57856467  -1 57854761  -1 57853055  -1 57851349  -1 57849643  -1 57847937  -1 57846231  -1 57844525  -1 57842819  -1 57841113  -1 57839407  -1 57837701  -1 57835995  -1 57834289  -1 57832583  -1 57830877  -1 57829171  -1 57827465  -1 57825759  -1 57824053  -1 5...

result:

ok 77560 numbers

Subtask #3:

score: 20
Accepted

Dependency #2:

100%
Accepted

Test #13:

score: 20
Accepted
time: 1023ms
memory: 29168kb

input:

3
1
300 600 9479768887366979469968967538414386738799799469768954967897479478
235 118 610005418879451235
118 235 610005418879451235
229 118 610005418879451235
118 229 610005418879451235
36 235 610005418879451235
235 36 610005418879451235
265 229 610005418879451235
229 265 610005418879451235
24 36 610...

output:

494335567  494326423  494248699  494244127  494344711  494326423  494358427  494335567  494362999  494303563  494344711  494294419  494344711  494344711  494239555  494285275  494298991  494335567  494294419  494221267  494344711  494353855  494289847  494404147  494298991  494294419  494230411  494...

result:

ok 300 numbers

Test #14:

score: 0
Accepted
time: 1062ms
memory: 29216kb

input:

3
1
300 600 5776769948887747678764766855867697879888989838869789796489887868
283 274 755089058915384251
274 283 755089058915384251
244 283 888168172221533892
283 244 888168172221533892
282 283 888128579062348874
283 282 888128579062348874
40 244 889268402435235212
244 40 889268402435235212
182 282 9...

output:

176036896  694748344  -1 -1 -1 -1 600566244  -1 -1 -1 -1 -1 718827887  436968623  37585847  -1 -1 504374914  -1 633560024  856820739  157217839  -1 306684175  563519989  184280158  797877375  730487505  574440187  141621833  108771729  627363885  6744545  -1 216801629  -1 -1 -1 -1 635875737  -1 -1 -...

result:

ok 300 numbers

Test #15:

score: 0
Accepted
time: 1040ms
memory: 29164kb

input:

3
1
300 600 7799975936983268595994769498698386999688649798971695584484797589
213 87 992365484371550852
87 213 992365484371550852
292 213 992365484371550852
213 292 992365484371550852
125 292 992365484371550852
292 125 992365484371550852
32 213 992365484371550852
213 32 992365484371550852
231 32 9923...

output:

-1 352350370  352353940  352300390  -1 352257550  352357510  -1 -1 -1 -1 -1 352303960  -1 -1 -1 352328950  -1 -1 352353940  -1 -1 352346800  352325380  352368220  -1 352368220  352325380  -1 352339660  352368220  352278970  352343230  -1 -1 -1 -1 -1 -1 -1 -1 -1 352261120  -1 352314670  352311100  -1...

result:

ok 300 numbers

Test #16:

score: 0
Accepted
time: 1060ms
memory: 29212kb

input:

3
1
300 600 108915867328921644
78 120 915329174369582501
120 78 915329174369582501
166 120 915329174369582501
120 166 915329174369582501
24 120 915329174369582501
120 24 915329174369582501
2 24 915329174369582501
24 2 915329174369582501
146 2 915329174369582501
2 146 915329174369582501
266 2 9153291...

output:

796638071  796675403  796642219  796633923  796625627  796613183  796621479  796625627  796600739  796609035  796617331  796625627  796617331  796621479  796625627  796600739  796629775  796579999  796675403  796613183  796625627  796617331  796604887  796671255  796629775  796675403  796658811  796...

result:

ok 300 numbers

Test #17:

score: 0
Accepted
time: 645ms
memory: 28328kb

input:

3
48
120 240 7737895866885999885898998578585996398987747885374658446818863997
97 35 804386118934281915
35 97 804386118934281915
59 35 804386118934281915
35 59 804386118934281915
111 35 804386118934281915
35 111 804386118934281915
62 111 804386118934281915
111 62 804386118934281915
54 59 804386118934...

output:

-1 817576206  817570296  -1 -1 -1 -1 817576206  817576206  -1 -1 -1 817577388  -1 817575024  817573842  817576206  -1 -1 -1 -1 817569114  817573842  -1 817578570  817579752  817578570  -1 -1 -1 817575024  -1 817570296  -1 -1 -1 817577388  817576206  -1 -1 817571478  817575024  -1 -1 817571478  81757...

result:

ok 3516 numbers

Test #18:

score: 0
Accepted
time: 721ms
memory: 27952kb

input:

3
6185
96 192 9829599865896867589898965976864696579885564749989527653879744756
27 20 972718145577806019
20 27 972718145577806019
11 27 972718145577806019
27 11 972718145577806019
44 11 972718145577806019
11 44 972718145577806019
70 11 972718145577806019
11 70 972718145577806019
57 44 972718145577806...

output:

432574626  -1 432575632  -1 432569596  432580662  432581668  -1 432577644  432579656  432583680  432571608  432582674  432582674  -1 432570602  -1 432576638  -1 432584686  -1 -1 -1 -1 432582674  432575632  -1 432576638  432573620  -1 432583680  432570602  -1 432580662  432577644  -1 -1 432578650  -1...

result:

ok 83979 numbers

Subtask #4:

score: 15
Accepted

Dependency #1:

100%
Accepted

Test #19:

score: 15
Accepted
time: 29ms
memory: 27176kb

input:

4
1
100 101 6888995999928874698772868926699656683388498575797893294688976887
25 90 495511874996847106
90 84 495511874996847106
84 82 495511874996847106
82 40 495511874996847106
40 97 495511874996847106
97 5 495511874996847106
5 24 495511874996847106
24 16 495511874996847106
16 19 495511874996847106
...

output:

662900138  662900131  662900188  662900147  662900176  662900221  662900152  662900202  662900130  662900140  662900169  662900199  662900128  662900145  662900192  662900178  662900163  662900150  662900179  662900151  662900139  662900180  662900216  662900177  662900170  662900205  662900210  662...

result:

ok 100 numbers

Test #20:

score: 0
Accepted
time: 53ms
memory: 27104kb

input:

4
1
100 200 7298898492397999688666927949888498969897838287679988999656889979
1 68 716477084362826727
1 70 849254955511480878
68 70 965501875328180109
68 27 922798232695217800
70 27 973650788054328171
70 69 992887836560799260
27 69 912347321604310534
27 41 707737334645887057
69 41 939222694708421463
...

output:

59219241  402083566  593666306  414807498  258758770  177911843  190858821  427609509  714942754  794670437  266523695  250908431  280340515  973300594  490891479  435411914  570632298  806776572  581872834  661756417  571008187  273666813  634277068  321782154  962526931  884883598  912195600  3891...

result:

ok 100 numbers

Test #21:

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

input:

4
1
100 170 8794888769994978795934858981288869698995675273759827929988816678
85 25 955229927087338794
25 69 715916767824027774
69 8 871119978520194455
8 64 918533454985251428
64 73 928715673496434787
73 95 777734955942460360
95 82 937506435422061091
82 21 972958971354009576
21 81 920481916656974333
...

output:

1429581  1429581  150584952  1429581  1429581  616522075  1429581  1429581  56697037  26641870  455335474  405458157  1429581  119209248  768006956  56697037  1429581  1429581  56697037  825267652  337654936  589008244  691024955  410854366  1429581  1429581  569334223  757829937  1429581  1429581  ...

result:

ok 100 numbers

Test #22:

score: 0
Accepted
time: 44ms
memory: 27152kb

input:

4
1
100 175 988927424060208873
73 39 871487671517176049
39 12 989592888976857487
12 60 753594598459115125
60 80 804510907944284786
80 87 837728552224523957
87 50 720829671677245259
50 55 955472566241448463
55 56 698073335743612454
56 24 907173313658798936
24 75 812098650937527351
75 23 9749473294852...

output:

120476850  665961545  406533341  950211937  665961545  665961545  665961545  160438797  665961545  860608848  665961545  665961545  665961545  665961545  698344451  665961545  115173142  157625083  665961545  665961545  665961545  817070036  795455857  665961545  615381143  665961545  363716660  579...

result:

ok 100 numbers

Test #23:

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

input:

4
272
40 69 8526848846996917979164958976967645898953979497989749477999798947
16 20 904311791006766339
20 8 861881272650260368
8 38 927651150818983482
38 22 575472375470752507
22 10 995789624005306013
10 17 862458045607914444
17 35 910173488885856948
35 12 897609723109586512
12 23 970232607197909774
...

output:

631006282  189710405  538148249  980737927  538148249  223767537  200756370  844883815  538148249  538148249  506622768  538148249  278823150  381947526  538148249  594558624  538148249  751430936  664913832  538148249  961366366  945739792  646619933  538148249  538148249  538148249  538148249  831...

result:

ok 3782 numbers

Test #24:

score: 0
Accepted
time: 22ms
memory: 27104kb

input:

4
367
32 64 7694675676969676989727597922979586548768678479696836977677459878
26 1 948535512091890169
1 28 927579818325689242
28 18 869862583518920980
18 24 916474447302009020
24 23 592009716422157884
23 12 932781568469333631
12 17 906790845067818370
17 16 733495190715963829
16 5 948217995091033642
5...

output:

242947009  937164220  269122924  473901869  812915310  815200847  138502251  269122924  269122924  269122924  269122924  269122924  269122924  790732730  269122924  269122924  423399215  858100602  269122924  259562134  269122924  613550676  269122924  242947009  269122924  269122924  210817778  269...

result:

ok 4886 numbers

Subtask #5:

score: 15
Accepted

Dependency #1:

100%
Accepted

Test #25:

score: 15
Accepted
time: 552ms
memory: 29124kb

input:

5
1
300 301 969767789936486493
164 284 964646444984408140
284 241 964646444984408140
241 281 964646444984408140
281 138 964646444984408140
138 242 964646444984408140
242 112 964646444984408140
112 217 964646444984408140
217 170 964646444984408140
170 31 964646444984408140
31 300 964646444984408140
3...

output:

562333388  562333371  562333450  562333457  562333181  562333366  562333433  562333276  562333204  562333354  562333361  562333374  562333436  562333405  562333369  562333286  562333360  562333318  562333396  562333251  562333480  562333220  562333333  562333460  562333359  562333295  562333293  562...

result:

ok 300 numbers

Test #26:

score: 0
Accepted
time: 1046ms
memory: 29272kb

input:

5
1
300 600 798876399989994933
7 196 978372754397099680
7 150 850366341978113658
196 150 741178931696536015
196 241 918555502737513857
150 241 755464499814711391
150 249 715712249601810459
241 249 834572033520725671
241 172 840925258261612828
249 172 765221764158211117
249 92 987381804975984305
172 ...

output:

103349950  4999241  142118823  400506111  885559364  196293932  888044807  431387396  656847997  382995767  154772964  775074870  360166602  822043040  871256466  771891985  42704853  943406678  158027440  486796258  972364206  191106105  158852164  825942858  973808447  981369554  98907807  6690497...

result:

ok 300 numbers

Test #27:

score: 0
Accepted
time: 589ms
memory: 29148kb

input:

5
1
300 319 999568963877948597
127 165 930758488326418731
165 155 912956207532166981
155 28 930375923771407137
28 174 952825751389557214
174 170 969510032281804566
170 241 896480622553779223
241 54 857133548480482773
54 22 748966877674282581
22 105 992399083086354199
105 73 833098032662288489
73 199...

output:

615687095  22340881  220606255  926757569  403722771  339583612  218798352  675170360  910785402  527927433  468935392  80089701  112798914  308829476  977528530  484462850  559184887  21739752  111487269  309000604  260902067  244633941  296132705  230226837  668779298  283618195  103042591  779688...

result:

ok 300 numbers

Test #28:

score: 0
Accepted
time: 962ms
memory: 29152kb

input:

5
1
300 548 824591615686303801
277 294 884790950503796190
294 241 928062180696957669
241 164 997854303092696029
164 296 922799499949016142
296 248 944988731600431360
248 153 831789824022472151
153 180 666918059700566083
180 87 790575536963511661
87 285 804674576894023412
285 211 822686794867787872
2...

output:

278188366  278188366  278188366  278188366  278188366  50768692  278188366  278188366  278188366  278188366  888601612  278188366  371280094  739457050  790269377  850776214  278188366  278188366  278188366  18157734  278188366  278188366  278188366  811551034  356306457  730311889  608326520  27818...

result:

ok 300 numbers

Test #29:

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

input:

5
40
120 131 679889999068592637
118 98 812545734198160781
98 91 917010970269512244
91 95 698053144863731543
95 14 628901820405095492
14 22 889645699347522207
22 51 871704747332576532
51 19 994723476638446914
19 108 935669854949015658
108 83 944628276409310798
83 6 997623504444369992
6 44 89978656209...

output:

347797689  625661551  663318864  430007740  779572483  678295713  604524795  482364258  563274534  733628768  109065455  813167359  237637495  314851932  792047890  731351621  209595139  105858678  353663190  171125513  429932280  382442950  478291233  424842463  792632068  72912215  20364781  71685...

result:

ok 3150 numbers

Test #30:

score: 0
Accepted
time: 597ms
memory: 27728kb

input:

5
5333
96 163 896598775993796678
48 22 988628974528111232
22 79 974327267551042014
79 89 963371577075408650
89 35 977281141965145271
35 83 933480640131723472
83 71 671664664777649600
71 6 618937617718672760
6 18 899457718948743597
18 34 950491723718783148
34 50 977014890463222654
50 25 6638914519516...

output:

301347522  422213537  486604400  730721865  21694591  422213537  422213537  254465456  422213537  422213537  611991526  115365870  422213537  422213537  422213537  422213537  868835950  422213537  422213537  422213537  422213537  422213537  422213537  212714492  422213537  966025026  459165854  3215...

result:

ok 72861 numbers

Subtask #6:

score: 25
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #31:

score: 25
Accepted
time: 504ms
memory: 29208kb

input:

6
1
300 301 8384595996895888828869667586497989989858356867776999918576807619
197 23 889495255569618624
23 210 889495255569618624
210 276 889495255569618624
276 14 889495255569618624
14 183 889495255569618624
183 61 889495255569618624
61 59 889495255569618624
59 24 889495255569618624
24 286 889495255...

output:

937633240  937633025  937633061  937633233  937633204  937632982  937633256  937633088  937633112  937633011  937633012  937633082  937633160  937632964  937633223  937633253  937633251  937633077  937633068  937633098  937633155  937633076  937632961  937632968  937633094  937633071  937633166  937...

result:

ok 300 numbers

Test #32:

score: 0
Accepted
time: 997ms
memory: 29156kb

input:

6
1
300 600 9789789686989494479737788456786397594678895998540925995156896948
290 21 697235092171065903
290 207 841244551209169832
21 207 682095809424351037
21 189 835361263265102194
207 189 990951191339397002
207 258 917872091657918107
189 258 653490007166878883
189 191 439332450210771915
258 191 88...

output:

854178606  695415407  54167003  780439843  167409484  801355950  701654566  953965612  178760608  942642800  906510085  897278913  153543624  360388057  978982814  951393388  115415722  217173088  195062095  655412091  654225839  653756028  402407017  403944332  806480939  445896994  589483153  5271...

result:

ok 300 numbers

Test #33:

score: 0
Accepted
time: 826ms
memory: 29136kb

input:

6
1
300 506 9666999949867957683699869697675545684768689786556597699589994656
220 42 744135079508767614
42 10 990513245972736114
10 85 783110904106210368
85 299 971385880129286460
299 292 777878078200498834
292 238 894707413844405732
238 160 970711825594420657
160 51 782949855870351500
51 83 96767794...

output:

321127168  62309620  290277594  321127168  139743687  321127168  321127168  592852487  583526029  894209300  321127168  530725406  191226049  180527493  321127168  321127168  848232687  495352308  177474851  321127168  321127168  80230562  247981636  763967956  321127168  62254722  278262184  348934...

result:

ok 300 numbers

Test #34:

score: 0
Accepted
time: 562ms
memory: 29264kb

input:

6
1
300 303 321914321331672332
292 96 706286861625334331
96 117 766985538566920593
117 190 866646123374410837
190 253 699904343899228106
253 274 992627860161309213
274 18 905731094179818785
18 189 936497015368432321
189 137 976195340030617717
137 24 888795711938949442
24 49 982842776594365647
49 298...

output:

579952075  262240357  319760503  770449166  784812960  602361411  94385966  71313371  588153432  210252776  135235084  778204952  130671749  518745482  269546563  915634918  627388138  875512737  65541105  714541208  925736567  836225988  38599937  188266404  6317887  846971737  523206407  555477062...

result:

ok 300 numbers

Test #35:

score: 0
Accepted
time: 618ms
memory: 27560kb

input:

6
61
120 225 9548496987496998985385876587688868976448978675485996996879794889
58 25 903529707365827911
25 92 844382629854825031
92 10 832776769580442826
10 37 970368514129094395
37 20 965763570701341070
20 77 898036357660958759
77 9 803593362000073568
9 16 494519821553145331
16 95 932842016259501403...

output:

647103036  324846200  607228494  324846200  324846200  934596794  437788878  699128109  475640505  927297014  324846200  324846200  324846200  49887853  324846200  324846200  666092644  324846200  324846200  324846200  324846200  636903904  531508761  324846200  324846200  821400082  507832579  3248...

result:

ok 4097 numbers

Test #36:

score: 0
Accepted
time: 575ms
memory: 27860kb

input:

6
5561
96 191 7974399788765585898998893849898987863898887686596898797965499795
76 36 824263098975872778
36 24 875204474682156354
24 35 862797797919844291
35 80 661226475513698744
80 38 469297954639618896
38 81 988425818788900574
81 87 922669511685429253
87 92 799827243498461418
92 61 876865110853407...

output:

566520553  566520553  566520553  32058077  566520553  211411574  566520553  566520553  566520553  566520553  566520553  566520553  867896593  566520553  566520553  517163042  562156730  566520553  566520553  566520553  120612550  62704948  566520553  566520553  566520553  685077001  212239556  56652...

result:

ok 75626 numbers

Test #37:

score: 0
Accepted
time: 915ms
memory: 29172kb

input:

6
1
300 548 9679977478879555597319728789877676727567884898517715868999638989
205 41 850110051084625530
29 100 936317680713456588
54 284 963539539209721222
41 52 901006596119373598
150 293 973817901828446370
62 227 782816382634533790
127 57 971739442919026686
9 210 871208993137784378
211 3 6910145231...

output:

461210275  488242894  316964426  348316999  -1 267124772  54634685  265235277  861070507  466087407  591405321  501931249  357414322  789604494  -1 898730421  443360496  18560897  -1 -1 404783347  747587403  -1 -1 370183538  765390194  -1 823347917  -1 -1 159434588  808147309  755405187  -1 38011447...

result:

ok 300 numbers

Test #38:

score: 0
Accepted
time: 1059ms
memory: 29232kb

input:

6
1
300 600 763643382019598931
274 79 949217951110743172
274 5 779474018712198966
79 5 959096592468295083
79 146 714223003795487710
5 146 938785292341509666
5 99 929947073447837955
146 99 948011823985503078
146 77 851282596949005601
99 77 760436393773883512
99 68 949840178776552000
77 68 44462849860...

output:

741578120  655568938  56075495  424558733  872656925  880996014  324117445  827257609  650789492  608217925  708283882  542521503  864829514  544257202  125759952  993217387  614723067  142787337  803871163  805640142  168267943  571364026  93947806  544385221  203957855  513273101  667918143  41446...

result:

ok 300 numbers

Test #39:

score: 0
Accepted
time: 545ms
memory: 29200kb

input:

6
1
300 301 20642476
241 177 860109639868240788
177 152 860109639868240788
152 277 860109639868240788
277 264 860109639868240788
264 31 860109639868240788
31 289 860109639868240788
289 42 860109639868240788
42 283 860109639868240788
283 272 860109639868240788
272 260 860109639868240788
260 215 86010...

output:

300966458  300966445  300966500  300966332  300966318  300966351  300966317  300966522  300966350  300966457  300966288  300966532  300966478  300966337  300966243  300966525  300966451  300966497  300966474  300966487  300966366  300966347  300966442  300966480  300966374  300966268  300966306  300...

result:

ok 300 numbers

Test #40:

score: 0
Accepted
time: 1103ms
memory: 29164kb

input:

6
1
300 600 24412926
229 185 914988700934613331
229 287 648991611945690703
185 287 966874111123588071
185 93 945027510794068291
287 93 880579766669330430
287 37 966946946357350230
93 37 807198657375709747
93 231 962495196997902158
37 231 942286251881219359
37 259 823860374114865985
231 259 834905433...

output:

382395843  761052443  720332041  295788940  246851461  964157175  720395540  764958722  745703251  239116698  591354600  601393815  227000383  338474698  443327197  436975347  148864272  300340002  825027477  296539785  426748174  239529013  553467590  412339111  813314755  362871747  348018862  952...

result:

ok 300 numbers