Submission #1092121

# Submission time Handle Problem Language Result Execution time Memory
1092121 2024-09-23T09:01:29 Z juicy Beautiful row (IZhO12_beauty) C++17
100 / 100
819 ms 172800 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

int C2(int x) {
  return __builtin_popcount(x);
}

int C3(int x) {
  int cnt = 0;
  while (x) {
    cnt += x % 3 == 1;
    x /= 3;
  }
  return cnt;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  int n; cin >> n;
  vector<int> a(n);
  for (int &x : a) {
    cin >> x;
  }
  vector b(n, vector<bool>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      b[i][j] = C2(a[i]) == C2(a[j]) || C3(a[i]) == C3(a[j]);
    }
  }
  vector dp(n, vector<long long>(1 << n, 0));
  for (int i = 0; i < n; ++i) {
    dp[i][1 << i] = 1;
  }
  for (int m = 0; m < 1 << n; ++m) {
    for (int i = 0; i < n; ++i) {
      if (dp[i][m]) {
        for (int j = 0; j < n; ++j) {
          if (!(m >> j & 1) && b[i][j]) {
            dp[j][m + (1 << j)] += dp[i][m];
          }
        }
      }
    }
  }
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    res += dp[i][(1 << n) - 1];
  }
  cout << res;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 7 ms 2396 KB Output is correct
12 Correct 5 ms 2396 KB Output is correct
13 Correct 30 ms 9052 KB Output is correct
14 Correct 146 ms 39516 KB Output is correct
15 Correct 149 ms 39516 KB Output is correct
16 Correct 130 ms 39516 KB Output is correct
17 Correct 164 ms 39512 KB Output is correct
18 Correct 123 ms 39516 KB Output is correct
19 Correct 819 ms 172800 KB Output is correct
20 Correct 735 ms 172792 KB Output is correct