結果

問題 No.1937 Various Tournament
ユーザー そすうぽよそすうぽよ
提出日時 2023-07-22 09:59:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 892 ms / 2,000 ms
コード長 2,111 bytes
コンパイル時間 2,540 ms
コンパイル使用メモリ 220,912 KB
実行使用メモリ 6,828 KB
最終ジャッジ日時 2023-10-23 15:03:05
合計ジャッジ時間 31,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 778 ms
6,364 KB
testcase_02 AC 827 ms
6,568 KB
testcase_03 AC 875 ms
6,752 KB
testcase_04 AC 816 ms
6,540 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 852 ms
6,696 KB
testcase_07 AC 874 ms
6,764 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 814 ms
6,540 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 852 ms
6,684 KB
testcase_12 AC 834 ms
6,592 KB
testcase_13 AC 877 ms
6,784 KB
testcase_14 AC 838 ms
6,596 KB
testcase_15 AC 866 ms
6,724 KB
testcase_16 AC 835 ms
6,608 KB
testcase_17 AC 818 ms
6,400 KB
testcase_18 AC 887 ms
6,828 KB
testcase_19 AC 864 ms
6,740 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 856 ms
6,684 KB
testcase_23 AC 832 ms
6,604 KB
testcase_24 AC 858 ms
6,708 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 866 ms
6,740 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 768 ms
6,332 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 892 ms
6,788 KB
testcase_31 AC 805 ms
6,500 KB
testcase_32 AC 881 ms
6,788 KB
testcase_33 AC 869 ms
6,772 KB
testcase_34 AC 863 ms
6,732 KB
testcase_35 AC 853 ms
6,684 KB
testcase_36 AC 856 ms
6,660 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 866 ms
6,696 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 877 ms
6,776 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 863 ms
6,728 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <variant>

#define rep2(i, k, n) for (i64 i = (i64)(k); i < (i64)(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(x) begin(x), end(x)
#ifdef ENV_LOCAL
#define dump \
  if (1) cerr
#else
#define dump \
  if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

namespace std {
bool operator<(const bitset<16>& lhs, const bitset<16>& rhs) {
  return lhs.to_ulong() < rhs.to_ulong();
}
}  // namespace std

i64 permanent(const vector<bitset<16>>& s) {
  i64 n = size(s);
  i64 ans = 0;
  rep(i, 1 << n) {
    i64 prod = 1;
    rep(j, n) {
      i64 sum = __builtin_popcount(i & s[j].to_ulong());
      prod *= sum;
    }
    if (__builtin_popcount(i) % 2) prod *= -1;
    ans += prod;
  }
  if (n % 2) ans *= -1;
  return ans;
}

map<vector<bitset<16>>, vi64> cache;
vi64 solve(const vector<bitset<16>>& s) {
  i64 n = size(s);
  if (n == 1) {
    return {1};
  }
  if (n == 2) {
    if (s[0][1]) {
      return {2, 0};
    } else {
      return {0, 2};
    }
  }
  if (cache.count(s)) {
    return cache.at(s);
  }
  vi64 ans(n);
  rep(i, 1 << n) {
    if (__builtin_popcount(i) != n / 2) continue;
    vi32 winner;
    vi32 loser;
    rep(j, n) {
      if ((i >> j) & 1) {
        winner.push_back(j);
      } else {
        loser.push_back(j);
      }
    }
    vector<bitset<16>> mat(n / 2);
    rep(j, n / 2) rep(k, n / 2) { mat[j][k] = s[winner[j]][loser[k]]; }
    i64 cnt = permanent(mat);
    vector<bitset<16>> ss(n / 2);
    rep(j, n / 2) rep(k, n / 2) { ss[j][k] = s[winner[j]][winner[k]]; }
    auto sans = solve(ss);
    cnt *= (1 << (n / 2));
    rep(j, n / 2) { ans[winner[j]] += cnt * sans[j]; }
  }
  cache[s] = ans;
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 n;
  cin >> n;
  vector<bitset<16>> s(n);
  rep(i, n) rep(j, n) {
    i64 b;
    cin >> b;
    s[i][j] = b;
  }
  vi64 ans = solve(s);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
0