結果

問題 No.2428 Returning Shuffle
ユーザー い
提出日時 2023-08-18 22:29:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 8,817 bytes
コンパイル時間 4,177 ms
コンパイル使用メモリ 276,484 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-11-28 08:18:30
合計ジャッジ時間 6,843 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
11,392 KB
testcase_01 AC 227 ms
11,392 KB
testcase_02 AC 209 ms
11,392 KB
testcase_03 AC 7 ms
7,296 KB
testcase_04 AC 7 ms
7,296 KB
testcase_05 AC 7 ms
7,296 KB
testcase_06 AC 7 ms
7,296 KB
testcase_07 AC 7 ms
7,296 KB
testcase_08 AC 7 ms
7,296 KB
testcase_09 AC 7 ms
7,296 KB
testcase_10 AC 7 ms
7,296 KB
testcase_11 AC 6 ms
7,296 KB
testcase_12 AC 7 ms
7,168 KB
testcase_13 AC 7 ms
7,296 KB
testcase_14 AC 7 ms
7,296 KB
testcase_15 AC 7 ms
7,168 KB
testcase_16 AC 6 ms
7,296 KB
testcase_17 AC 7 ms
7,168 KB
testcase_18 AC 7 ms
7,296 KB
testcase_19 AC 141 ms
11,520 KB
testcase_20 AC 141 ms
11,392 KB
testcase_21 AC 7 ms
7,296 KB
testcase_22 AC 7 ms
7,296 KB
testcase_23 AC 7 ms
7,168 KB
testcase_24 AC 136 ms
11,304 KB
testcase_25 AC 137 ms
11,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128_t;

#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()

template <class A>
int len(const A &a) {
  return a.size();
}

template <typename T>
using vec = vector<T>;
template <typename T>
using vec2 = vec<vec<T>>;
template <typename T>
using vec3 = vec<vec2<T>>;
template <typename T>
using vec4 = vec<vec3<T>>;
template <typename T>
using vec5 = vec<vec4<T>>;

#define VEC(T, a, ...) \
  vec<T> a(__VA_ARGS__)

#define VEC2(T, a, n, ...) \
  vector a(n, vec<T>(__VA_ARGS__));

#define VEC3(T, a, n, m, ...) \
  vector a( \
    n, \
    vector(m, vec<T>(__VA_ARGS__)) \
  );

#define VEC4(T, a, n, m, l, ...) \
  vector a( \
    n, \
    vector( \
      m, \
      vector(l, vec<T>(__VA_ARGS__)) \
    ) \
  );

#define eval_4(a, b, c, d, e, ...) e

#define loop while (1)

#define rep(n) \
  for (int __ = 0; __ < n; __++)

#define range_1(i, n) \
  for (int i = 0; i < n; i++)
#define range_2(i, a, b) \
  for (ll i = a; i < b; i++)
#define range_3(i, a, b, c) \
  for (ll i = a; i < b; i += c)

#define range(...) \
  eval_4(__VA_ARGS__, range_3, range_2, range_1, rep)( \
    __VA_ARGS__ \
  )

#define ranger_1(i, n) \
  for (int i = n; i--;)
#define ranger_2(i, a, b) \
  for (ll i = b; i-- > a;)
#define ranger_3(i, a, b, c) \
  for (ll i = b - 1; i >= a; i -= c)

#define range_rev(...) \
  eval_4(__VA_ARGS__, ranger_3, ranger_2, ranger_1)( \
    __VA_ARGS__ \
  )

#define iter(x, a) \
  for (const auto &x : a)
#define iter_mut(x, a) \
  for (auto &&x : a)

template <typename T, typename U>
istream &
operator>>(istream &in, pair<T, U> &p) {
  return in >> p.first >> p.second;
}

template <typename T, typename U>
ostream &operator<<(
  ostream &out,
  pair<T, U> &p
) {
  out << p.first << ' ' << p.second;
  return out;
}

template <int k = 0, class T>
void read_tup(istream &in, T &x) {
  if constexpr (tuple_size<T>::value > k) {
    in >> get<k>(x);
    read_tup<k + 1>(in, x);
  }
}

template <class... T>
istream &operator>>(
  istream &in,
  tuple<T...> &x
) {
  read_tup(in, x);
  return in;
}

template <int k = 0, class T>
void out_tup(ostream &out, T &x) {
  if constexpr (tuple_size<T>::value > k) {
    if constexpr (k > 0) {
      out << ' ';
    }
    out << get<k>(x);
    out_tup<k + 1>(out, x);
  }
}

template <class... T>
ostream &operator<<(
  ostream &out,
  tuple<T...> &x
) {
  out_tup(out, x);
  return out;
}

template <typename T>
auto operator<<(ostream &out, vec<T> a)
  -> ostream & {
  range(i, len(a)) {
    if (i) {
      out << ' ';
    }
    out << a[i];
  }
  return out;
}

template <typename T>
auto operator<<(ostream &out, vec2<T> a)
  -> ostream & {
  iter_mut(x, a) out << x << '\n';
  return out;
}

template <typename T>
auto operator>>(istream &in, vec<T> &a)
  -> istream & {
  iter_mut(x, a) in >> x;
  return in;
}

template <typename... T>
void in(T &...a) {
  (cin >> ... >> a);
}

template <class T, class... U>
void out(T a, const U... b) {
  cout << a;
  ((cout << ' ' << b), ...);
  cout << '\n';
}

template <typename T = int>
vec<T> iota(int n, T v = 0) {
  vec<T> a(n);
  std::iota(all(a), v);
  return a;
}

template <class T>
using max_queue = priority_queue<T>;

template <class T>
using min_queue =
  priority_queue<T, vec<T>, greater<T>>;

template <typename T>
T pop(queue<T> &q) {
  T v = q.front();
  q.pop();
  return v;
}

template <typename T>
T pop(deque<T> &q) {
  T v = q.front();
  q.pop_front();
  return v;
}

template <typename T>
T pop(vec<T> &q) {
  T v = q.back();
  q.pop_back();
  return v;
}

template <typename T>
T pop(max_queue<T> &q) {
  T v = q.top();
  q.pop();
  return v;
}

template <typename T>
T pop(min_queue<T> &q) {
  T v = q.top();
  q.pop();
  return v;
}

template <typename T>
T max(const vec<T> &a) {
  return *max_element(all(a));
}

template <typename T>
T min(const vec<T> &a) {
  return *min_element(all(a));
}

int topbit(int x) {
  return 31 - __builtin_clz(x);
}

template <class T>
bool operator==(
  const vec<T> &a,
  const vec<T> &b
) {
  int n = len(a);
  if (len(b) != n) {
    return false;
  }
  range(i, n) {
    if (a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

template <class T, class U>
bool chmin(T &a, const U &b) {
  return b < a ? a = b, 1 : 0;
}

template <class T, class U>
bool chmax(T &a, const U &b) {
  return b > a ? a = b, 1 : 0;
}

int popcnt(int x) {
  return __builtin_popcount(x);
}

template <class T, class U>
T sum(const vec<U> &a) {
  return accumulate(all(a), 0ll);
}

template <class T>
void unique(vec<T> &a) {
  sort(all(a));
  a.erase(std::unique(all(a)), a.end());
}

template <class T, class A>
int lb(const A &a, const T &x) {
  auto p = lower_bound(all(a), x);
  return distance(a.begin(), p);
}

template <class T, class A>
int ub(const A &a, const T &x) {
  auto p = upper_bound(all(a), x);
  return distance(a.begin(), p);
}

// define yes/no
#define yesno(y, n) \
  void yes(bool f = 1) { \
    out(f ? #y : #n); \
  } \
  void no() { \
    out(#n); \
  }

yesno(yes, no);

// yesno(Yes, No);
// yesno(YES, NO);

template <typename T>
auto factorize(T n)
  -> vec<pair<T, int>> {
  vec<pair<T, int>> f;
  for (T i = 2; i * i <= n; i++) {
    if (n % i) {
      continue;
    }
    int e = 0;
    while (n % i == 0) {
      e++;
      n /= i;
    }
    f.emplace_back(i, e);
  }
  if (n != 1) {
    f.emplace_back(n, 1);
  }
  return f;
}

// if p == -1, use set_mod
template <int p = -1>
class modint {
  long v;
  static int mod;

public:
  static void set_mod(int m) {
    mod = m;
  }

  static constexpr int m() {
    return p > 0 ? p : mod;
  }

  constexpr modint(): v() {
  }

  modint(long v): v(norm(v)) {
  }

  static long norm(long x) {
    if (x < -m() || x >= m()) {
      x %= m();
    }
    if (x < 0) {
      x += m();
    }
    return x;
  }

  int operator()() const {
    return v;
  }

  modint operator-() const {
    return modint(m() - v);
  }

  modint &operator+=(const modint &a) {
    if ((v += a.v) >= m()) {
      v -= m();
    }
    return *this;
  }

  modint &operator-=(const modint &a) {
    return *this += -a;
  }

  modint &operator*=(const modint &a) {
    v = norm(v * a.v);
    return *this;
  }

  modint pow(long t) const {
    if (t < 0) {
      return pow(p - 2) * pow(-t);
    }
    if (t == 0) {
      return 1;
    }
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1) {
      a *= *this;
    }
    return a;
  }

  modint inv() const {
    return pow(p - 2);
  }

  modint &operator/=(const modint &a) {
    return *this *= a.inv();
  }

  auto operator++() -> modint & {
    return *this += 1;
  }

  auto operator--() -> modint & {
    return *this -= 1;
  }

  auto operator++(int) -> modint {
    modint a(*this);
    *this += 1;
    return a;
  }

  auto operator--(int) -> modint {
    modint a(*this);
    *this -= 1;
    return a;
  }

  friend modint operator+(
    const modint &a,
    const modint &b
  ) {
    return modint(a) += b;
  }

  friend modint operator-(
    const modint &a,
    const modint &b
  ) {
    return modint(a) -= b;
  }

  friend modint operator*(
    const modint &a,
    const modint &b
  ) {
    return modint(a) *= b;
  }

  friend modint operator/(
    const modint &a,
    const modint &b
  ) {
    return modint(a) /= b;
  }

  friend bool operator==(
    const modint &a,
    const modint &b
  ) {
    return a.v == b.v;
  }

  friend istream &
  operator>>(istream &in, modint &x) {
    in >> x.v;
    x.v = norm(x.v);
    return in;
  }

  friend ostream &operator<<(
    ostream &out,
    const modint &x
  ) {
    return out << x.v;
  }
};

using mint1_000_000_007 =
  modint<1'000'000'007>;
using mint998_244_353 =
  modint<998'244'353>;

template <>
int modint<-1>::mod = 1;
using mint_runtime = modint<>;

void solve() {
  int n, m;
  in(n, m);
  auto f = iota(n);
  rep(m) {
    int t;
    in(t);
    vec<int> s(t);
    in(s);
    iter_mut(x, s) x--;
    int x = f[s[t - 1]];
    range_rev(i, t - 1) {
      f[s[i + 1]] = f[s[i]];
    }
    f[s[0]] = x;
    // out(s);
    // out(f);
  }

  int k = 1 << 20;
  vec<int> c(k);
  vec<bool> visited(n);
  range(i, n) {
    if (visited[i]) {
      continue;
    }
    int s = 0;
    while (!visited[i]) {
      visited[i] = true;
      i = f[i];
      s++;
    }
    assert(s > 0);
    for (auto [p, e] : factorize(s)) {
      chmax(c[p], e);
    }
  }
  using mint = mint998_244_353;
  mint ans = 1;
  range(i, k) {
    ans *= mint(i).pow(c[i]);
  }
  // out(f);
  out(ans);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // cout << setprecision(16);
  int t = 1;
  // in(t);
  while (t--) {
    solve();
  }
}
0