Submission #17752840


Source Code Expand

Copy
#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 998244353;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 1000;
int f[N], g[N], fc[N], gc[N];
int find(int f[], int x) {
  if (f[x] != f[f[x]]) f[x] = find(f, f[x]);
  return f[x];
}

int fact[N], ifact[N], inv[N];

struct comb_init {
  comb_init() {
    inv[1] = 1;
    for (int i = 2; i < N; ++i) {
      inv[i] = (MOD - MOD / i) * (int64)inv[MOD % i] % MOD;
    }
    fact[0] = ifact[0] = 1;
    for (int i = 1; i < N; ++i) {
      fact[i] = (int64)fact[i - 1] * i % MOD;
      ifact[i] = (int64)ifact[i - 1] * inv[i] % MOD;
    }
  }
} comb_init_;

int64 comb(int n, int m) {
  if (n < m || m < 0) return 0;
  return (int64)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    f[i] = g[i] = i;
    fc[i] = gc[i] = 1;
  }
  // row
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      bool found = false;
      for (int k = 0; k < n; ++k) {
        if (a[i][k] + a[j][k] > m) found = true;
      }
      if (!found) {
        int ri = find(f, i), rj = find(f, j);
        if (ri != rj) {
          f[rj] = ri;
          fc[ri] += fc[rj];
        }
      }
    }
  }
  // col
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      bool found = false;
      for (int k = 0; k < n; ++k) {
        if (a[k][i] + a[k][j] > m) found = true;
      }
      if (!found) {
        int ri = find(g, i), rj = find(g, j);
        if (ri != rj) {
          g[rj] = ri;
          gc[ri] += gc[rj];
        }
      }
    }
  }
  int ret = 1;
  for (int i = 0; i < n; ++i) {
    if (find(f, i) != i) continue;
    ret = 1LL * ret * fact[fc[i]] % MOD;
  }
  for (int i = 0; i < n; ++i) {
    if (find(g, i) != i) continue;
    ret = 1LL * ret * fact[gc[i]] % MOD;
  }
  cout << ret << '\n';

  return 0;
}

Submission Info

Submission Time
Task C - Shuffle Permutation
User cuiaoxiang
Language C++ (Clang 10.0.0)
Score 500
Code Size 4478 Byte
Status AC
Exec Time 8 ms
Memory 3236 KB

Compile Error

./Main.cpp:78:11: warning: unused variable 'INF' [-Wunused-const-variable]
const int INF = 1 << 29;
          ^
1 warning generated.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 25
Set Name Test Cases
Sample example_00, example_01
All example_00, example_01, handmade_00, handmade_01, max_random2_00, max_random2_01, max_random2_02, max_random2_03, max_random2_04, max_random2_05, max_random2_06, max_random_00, max_random_01, max_random_02, random_00, random_01, random_02, small2_00, small2_01, small2_02, small2_03, small2_04, small_00, small_01, small_02
Case Name Status Exec Time Memory
example_00 AC 6 ms 3184 KB
example_01 AC 2 ms 3192 KB
handmade_00 AC 2 ms 3184 KB
handmade_01 AC 2 ms 3180 KB
max_random2_00 AC 4 ms 3056 KB
max_random2_01 AC 4 ms 3140 KB
max_random2_02 AC 3 ms 3208 KB
max_random2_03 AC 5 ms 3096 KB
max_random2_04 AC 4 ms 3200 KB
max_random2_05 AC 2 ms 3204 KB
max_random2_06 AC 5 ms 3180 KB
max_random_00 AC 3 ms 3152 KB
max_random_01 AC 8 ms 3132 KB
max_random_02 AC 4 ms 3236 KB
random_00 AC 4 ms 3204 KB
random_01 AC 2 ms 3048 KB
random_02 AC 2 ms 3092 KB
small2_00 AC 2 ms 3192 KB
small2_01 AC 2 ms 3136 KB
small2_02 AC 2 ms 3132 KB
small2_03 AC 2 ms 3192 KB
small2_04 AC 2 ms 3196 KB
small_00 AC 3 ms 3176 KB
small_01 AC 2 ms 3144 KB
small_02 AC 2 ms 3048 KB