QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672894#9444. Again Permutation ProblemwangjunruiWA 2ms5796kbC++146.5kb2024-10-24 19:44:112024-10-24 19:44:12

Judging History

This is the latest submission verdict.

  • [2024-10-24 19:44:12]
  • Judged
  • Verdict: WA
  • Time: 2ms
  • Memory: 5796kb
  • [2024-10-24 19:44:11]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


template <int MAX_N> struct PermBasis {
  int n;
  bool on[MAX_N][MAX_N];
  int rCnt[MAX_N];
  int r[MAX_N][MAX_N][MAX_N], invR[MAX_N][MAX_N][MAX_N];
  int sLen[MAX_N];
  int s[MAX_N][MAX_N * (MAX_N - 1) / 2][MAX_N];

  PermBasis() {}
  explicit PermBasis(int n_) : n(n_), on{}, rCnt{}, r{}, invR{}, sLen{}, s{} {
    assert(0 <= n); assert(n <= MAX_N);
    for (int u = 0; u < n; ++u) {
      on[u][u] = true;
      rCnt[u] = 1;
      for (int w = u; w < n; ++w) r[u][u][w] = invR[u][u][w] = w;
    }
  }
  bool contains(int u, const int *g) const {
    if (u == n) return true;
    const int v = g[u];
    if (!on[u][v]) return false;
    // contains(u + 1, r[u][v]^-1 g)
    int h[MAX_N] = {};
    for (int w = u; w < n; ++w) h[w] = invR[u][v][g[w]];
    return contains(u + 1, h);
  }
  bool contains(const vector<int> &g) const {
    return contains(0, g.data());
  }
  void add(int u, const int *g) {
    if (contains(u, g)) return;
    for (int w = u; w < n; ++w) s[u][sLen[u]][w] = g[w];
    ++sLen[u];
    int h[MAX_N] = {};
    for (int v = u; v < n; ++v) if (on[u][v]) {
      // dfs(u, g r[u][v])
      for (int w = u; w < n; ++w) h[w] = g[r[u][v][w]];
      dfs(u, h);
    }
  }
  void add(const vector<int> &g) {
    return add(0, g.data());
  }
  void dfs(int u, const int *g) {
    const int v = g[u];
    int h[MAX_N] = {};
    if (on[u][v]) {
      // add(u + 1, r[u][v]^-1 g)
      for (int w = u + 1; w < n; ++w) h[w] = invR[u][v][g[w]];
      add(u + 1, h);
    } else {
      on[u][v] = true;
      ++rCnt[u];
      for (int w = u; w < n; ++w) invR[u][v][r[u][v][w] = g[w]] = w;
      for (int i = 0; i < sLen[u]; ++i) {
        // dfs(u, s[u][i] g)
        for (int w = u; w < n; ++w) h[w] = g[s[u][i][w]];
        dfs(u, h);
      }
    }
  }
};

////////////////////////////////////////////////////////////////////////////////

int N, M;
int P[30][30];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    for (int i = 0; i < M; ++i) for (int u = 0; u < N; ++u) {
      scanf("%d", &P[i][u]);
      --P[i][u];
    }
    
    PermBasis<30> pb(N);
    for (int i = 0; i < M; ++i) {
      pb.add(0, P[i]);
    }
    Mint dp[31][30][30] = {};
    for (int x = 0; x < N; ++x) for (int y = x + 1; y < N; ++y) {
      dp[N][x][y] += 1;
    }
    for (int u = N; --u >= 0; ) {
      for (int v = u; v < N; ++v) if (pb.on[u][v]) {
        for (int x = 0; x < N; ++x) for (int y = 0; y < N; ++y) {
          const int rx = (x < u) ? x : pb.r[u][v][x];
          const int ry = (y < u) ? y : pb.r[u][v][y];
          dp[u][rx][ry] += dp[u + 1][x][y];
        }
      }
    }
    Mint ans = 0;
    for (int x = 0; x < N; ++x) for (int y = 0; y < x; ++y) {
      ans += dp[0][x][y];
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5652kb

input:

3 2
1 2 3
2 3 1

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

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

output:

50

result:

ok 1 number(s): "50"

Test #3:

score: -100
Wrong Answer
time: 2ms
memory: 5744kb

input:

30 12
1 2 9 4 5 6 7 8 3 10 11 12 19 14 15 25 17 18 20 26 21 22 23 24 16 29 27 28 13 30
9 2 27 4 5 10 7 8 1 25 11 12 24 14 15 16 17 18 19 20 21 22 23 28 6 26 3 13 29 30
1 5 3 29 2 6 7 8 9 10 11 12 13 16 15 18 17 14 19 20 21 22 28 27 25 26 24 23 4 30
7 2 3 25 5 6 1 28 21 15 11 12 13 14 10 17 16 18 19 ...

output:

16619692

result:

wrong answer 1st numbers differ - expected: '701414999', found: '16619692'