답안 #992520

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
992520 2024-06-04T14:31:30 Z tch1cherin 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
700 ms 205648 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> a(N);
  for (int &value : a) {
    cin >> value;
  }
  vector<int> b(N), t(N);
  for (int i = 0; i < N; i++) {
    int tmp = a[i];
    while (tmp > 0) {
      b[i] += tmp % 2 == 1;
      tmp /= 2;
    }
    tmp = a[i];
    while (tmp > 0) {
      t[i] += tmp % 3 == 1;
      tmp /= 3;
    }
  }
  vector<vector<long long>> dp(1 << N, vector<long long>(N));
  for (int i = 0; i < N; i++) {
    dp[1 << i][i] = 1;
  }
  for (int mask = 0; mask < (1 << N); mask++) {
    for (int i = 0; i < N; i++) {
      if ((mask >> i) & 1) {
        for (int j = 0; j < N; j++) {
          if ((mask >> j) % 2 == 0 && (b[i] == b[j] || t[i] == t[j])) {
            dp[mask | (1 << j)][j] += dp[mask][i];
          }
        }
      }
    }
  }
  long long answer = 0;
  for (int i = 0; i < N; i++) {
    answer += dp.back()[i];
  }
  cout << answer << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 7 ms 2824 KB Output is correct
12 Correct 7 ms 2652 KB Output is correct
13 Correct 31 ms 11192 KB Output is correct
14 Correct 147 ms 47596 KB Output is correct
15 Correct 134 ms 47448 KB Output is correct
16 Correct 156 ms 47452 KB Output is correct
17 Correct 160 ms 47700 KB Output is correct
18 Correct 156 ms 47452 KB Output is correct
19 Correct 700 ms 205456 KB Output is correct
20 Correct 615 ms 205648 KB Output is correct