QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738729 | #5442. Referee Without Red | Xiaohuba | TL | 0ms | 0kb | C++14 | 6.4kb | 2024-11-12 19:48:32 | 2024-11-12 19:48:35 |
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 = 2e6 + 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]--;
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: 0
Time Limit Exceeded
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