結果

問題 No.2255 Determinant Sum
ユーザー KudeKude
提出日時 2023-03-24 22:43:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 2,381 bytes
コンパイル時間 2,568 ms
コンパイル使用メモリ 237,008 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 21:21:31
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 31 ms
4,348 KB
testcase_07 AC 27 ms
4,348 KB
testcase_08 AC 18 ms
4,348 KB
testcase_09 AC 24 ms
4,348 KB
testcase_10 AC 24 ms
4,348 KB
testcase_11 AC 8 ms
4,348 KB
testcase_12 AC 11 ms
4,348 KB
testcase_13 AC 7 ms
4,348 KB
testcase_14 AC 34 ms
4,348 KB
testcase_15 AC 14 ms
4,348 KB
testcase_16 AC 14 ms
4,348 KB
testcase_17 AC 15 ms
4,348 KB
testcase_18 AC 12 ms
4,348 KB
testcase_19 AC 11 ms
4,348 KB
testcase_20 AC 12 ms
4,348 KB
testcase_21 AC 18 ms
4,348 KB
testcase_22 AC 14 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while(tt--) {
    int n, p;
    cin >> n >> p;
    mint::set_mod(p);
    VVI a(n, VI(n));
    rep(i, n) rep(j, n) cin >> a[i][j];
    if (p == 2) {
      bool ok = true;
      rep(i, n) {
        int cnt = 0;
        rep(j, n) cnt += a[i][j] == -1;
        if (cnt >= 2) {
          ok = false;
          break;
        }
        if (cnt == 1) {
          rep(j, n) {
            if (a[i][j] == -1) a[i][j] = 1;
            else a[i][j] = 0;
          }
        }
      }
      if (!ok) {
        cout << 0 << '\n';
        continue;
      }
    } else {
      int cnt = 0;
      rep(i, n) rep(j, n) cnt += a[i][j] == -1;
      if (cnt) {
        cout << 0 << '\n';
        continue;
      }
    }
    vector d(n, vector<mint>(n));
    rep(i, n) rep(j, n) {
      assert(a[i][j] >= 0);
      d[i][j] = a[i][j];
    }
    auto rec = [&](auto self, vector<vector<mint>> d) -> mint {
      if (d.empty()) return 1;
      int n = d.size();
      mint coeff = 1;
      bool found = false;
      rep(i, n) if (d[i][0].val()) {
        found = true;
        swap(d[i], d[0]);
        if (i & 1) coeff *= -1;
      }
      if (!found) return 0;
      assert(d[0][0].val());
      mint c = d[0][0];
      mint ic = c.inv();
      for(int i = 1; i < n; i++) {
        mint x = -d[i][0] * ic;
        rep(j, n) d[i][j] += d[0][j] * x;
      }
      coeff *= c;
      d.erase(d.begin());
      rep(i, n - 1) d[i].erase(d[i].begin());
      return self(self, move(d)) * coeff;
    };
    mint ans = rec(rec, move(d));
    cout << ans.val() << '\n';
  }
}
0