QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#738790#5442. Referee Without RedXiaohubaWA 137ms94672kbC++146.7kb2024-11-12 19:55:592024-11-12 19:56:03

Judging History

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

  • [2024-11-12 19:56:03]
  • 评测
  • 测评结果:WA
  • 用时:137ms
  • 内存:94672kb
  • [2024-11-12 19:55:59]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
#endif

#if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define ssz(x) (signed((x).size()))
#define _loop_i_t(j, k) make_signed_t<decltype((j) - (k))>
#define For(i, j, k) for (_loop_i_t(j, k) i = (j); i <= (k); ++i) // NOLINT
#define ForDown(i, j, k)                                                       \
  for (_loop_i_t(j, k) i = (j); i >= decltype(i)(k); --i) // NOLINT
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
CONSTEXPR_FUNC il ll qpow(ll x, ull y, ll mod){ ll ans = 1; x %= mod; while (y) { if (y & 1) (ans *= x) %= mod; (x *= x) %= mod; y >>= 1; } return ans; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y); }
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + (c - '0'); c = getchar(); } x *= f; }
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x), read(y...); }
// clang-format on

// File head end

namespace {
constexpr ll MAXN = 3e6 + 5, mod = 998244353, inv2 = (mod + 1) >> 1;
int T, n, m, fa[MAXN], pw[MAXN], nxt[MAXN], cnt[MAXN];
pii mf[MAXN];
ll fac[MAXN], ifac[MAXN];
bool vis[MAXN];
bitset<MAXN> notPr;
vector<int> A, p, q, pr;
vector<vector<int>> cycP, cycQ;
#define id(x, y) ((x) * m + (y))
ll C(int x, int y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
void getCyc(const vector<int> &perm, vector<vector<int>> &cyc) {
  int len = ssz(perm);
  fill(vis, vis + len, 0), cyc.clear();
  For(i, 0, len - 1) if (!vis[i]) {
    int p = i;
    cyc.eb();
    do {
      cyc.back().eb(p), vis[p] = 1, p = perm[p];
    } while (p != i);
  }
}
int period(const vector<int> &x) {
  nxt[0] = -1;
  for (int i = 1, j = -1; i < ssz(x); i++) {
    while (~j && x[j + 1] != x[i])
      j = nxt[j];
    if (x[j + 1] == x[i])
      j++;
    nxt[i] = j;
  }
  int len = ssz(x), bor = nxt[len - 1] + 1;
  return (len % (len - bor)) ? len : len - bor;
}
void upd_pw(int x) {
  while (x > 1) {
    auto [_v, _c] = mf[x];
    cmax(pw[_v], _c);
    while (_c--)
      x /= _v;
  }
}
void init() {
  n = MAXN - 5;
  fac[0] = 1;
  For(i, 1, n) fac[i] = fac[i - 1] * i % mod;
  ifac[n] = qpow(fac[n], mod - 2, mod);
  ForDown(i, n, 1) ifac[i - 1] = ifac[i] * i % mod;
  notPr[1] = 1, mf[1] = {1, 1};
  For(i, 2, n) {
    if (!notPr[i]) {
      pr.eb(i), mf[i] = {i, 1};
    }
    for (int j : pr) {
      if (1ll * i * j > n)
        break;
      notPr[i * j] = 1;
      if (i % j)
        mf[i * j] = {j, 1};
      else {
        mf[i * j] = {j, mf[i].se + 1};
        break;
      }
    }
  }
}
int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }
il void Main() {
  // FileIO("matrix");
  read(T), init();
  while (T--) {
    read(n, m), p.resize(n), q.resize(m), A.resize(n * m);
    // For(i, 0, n - 1) read(p[i]), p[i]--;
    // For(i, 0, m - 1) read(q[i]), q[i]--;
    {
      int c = 0;
      for (int i = 0; i < n; i += 2)
        p[i] = c++;
      for (int i = 1; i < n; i += 2)
        p[i] = c++;
      c = 0;
      for (int i = 0; i < m; i += 2)
        q[i] = c++;
      for (int i = 1; i < m; i += 2)
        q[i] = c++;
    }
    For(i, 0, n - 1) For(j, 0, m - 1) read(A[id(i, j)]);
    getCyc(p, cycP), getCyc(q, cycQ);
    ll ans = 1;
    for (const auto &v1 : cycP)
      if (ssz(v1) == 1) {
        int x = v1[0];
        fill(pw + 1, pw + 1 + m, 0);
        for (const auto &v2 : cycQ) {
          static vector<int> tmp;
          tmp.resize(ssz(v2));
          For(i, 0, ssz(v2) - 1) tmp[i] = A[id(x, v2[i])];
          upd_pw(period(tmp));
        }
        For(i, 1, m) if (pw[i])(ans *= qpow(i, pw[i], mod)) %= mod;
      }
    for (const auto &v1 : cycQ)
      if (ssz(v1) == 1) {
        int x = v1[0];
        fill(pw + 1, pw + 1 + n, 0);
        for (const auto &v2 : cycP) {
          static vector<int> tmp;
          tmp.resize(ssz(v2));
          For(i, 0, ssz(v2) - 1) tmp[i] = A[id(v2[i], x)];
          upd_pw(period(tmp));
        }
        For(i, 1, n) if (pw[i])(ans *= qpow(i, pw[i], mod)) %= mod;
      }
    iota(fa, fa + ssz(cycP) + ssz(cycQ) + 1, 0);
    for (int i = 0; i < ssz(cycP); i++)
      if (ssz(cycP[i]) > 1)
        for (int j = 0; j < ssz(cycQ); j++)
          if (ssz(cycQ[j]) > 1) {
            const auto &v1 = cycP[i], &v2 = cycQ[j];
            int len1 = ssz(v1), len2 = ssz(v2);
            bool flg = 1;
            for (int x : v1)
              for (int y : v2) {
                flg &= !cnt[A[id(x, y)]];
                cnt[A[id(x, y)]]++;
              }
            (ans *= fac[len1 * len2]) %= mod;
            for (int x : v1)
              for (int y : v2)
                (ans *= ifac[exchange(cnt[A[id(x, y)]], 0)]) %= mod;
            if (flg) {
              int u = find((len1 & 1) ? (ssz(cycP) + ssz(cycQ)) : i);
              int v = find((len2 & 1) ? (ssz(cycP) + ssz(cycQ)) : j);
              if (u != v)
                fa[u] = v;
              (ans *= inv2) %= mod;
            }
          }
    int tot = ssz(cycP) + ssz(cycQ) + 1, pw2 = tot;
    For(i, 0, tot - 1) if (find(i) == i) pw2--;
    (ans *= qpow(2, pw2, mod)) %= mod;
    printf("%lld\n", ans);
  }
}
} // namespace

signed main() { return Main(), 0; }

詳細信息

Test #1:

score: 100
Accepted
time: 44ms
memory: 84404kb

input:

2
4 4
1 2 3 4
3 4 1 2
1 2 4 1
4 3 3 2
3 9
1 8 1 1 8 1 1 8 1
1 8 8 8 8 8 8 8 1
1 1 1 8 8 8 1 1 1

output:

96
6336

result:

ok 2 number(s): "96 6336"

Test #2:

score: 0
Accepted
time: 48ms
memory: 84348kb

input:

1
18 16
8 8 1 1 8 8 8 1 8 8 8 1 8 8 8 1
8 1 8 1 8 1 1 1 8 1 1 1 8 1 1 1
8 8 8 1 8 8 8 1 8 8 8 1 8 8 8 1
8 8 1 1 8 1 1 1 8 1 1 1 8 1 1 1
8 1 8 1 8 8 8 1 8 1 1 1 8 8 8 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
8 8 1 1 8 8 8 1 8 8 8 1 7 7 7 1
8 1 8 1 8 1 1 1 8 1 1 1 1 1 7 1
8 8 8 1 8 8 8 1 8 8 8 1 1 7 7 1
8 8 ...

output:

690561281

result:

ok 1 number(s): "690561281"

Test #3:

score: -100
Wrong Answer
time: 137ms
memory: 94672kb

input:

71117
7 8
2868391 1228870 2892937 349733 664891 1675356 1981526 762573
2892937 2892937 664891 1228870 959280 762573 664891 959280
349733 250147 1675356 349733 349733 762573 1675356 250147
1675356 959280 664891 250147 250147 250147 2868391 959280
1675356 664891 250147 1228870 1981526 250147 2868391 2...

output:

462363428
38853786
97370043
215040
40320
322560
32456681
1166400
887214222
542386470
375765881
9
4
361590980
913481201
527607149
85428015
311271219
16
645120
557106771
388800
173057174
232068778
460990604
1
618786068
9
571768698
519171522
97370043
799392782
155415144
1
36
3991680
322560
771952940
27...

result:

wrong answer 3rd numbers differ - expected: '194740086', found: '97370043'