답안 #715891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
715891 2023-03-28T10:50:01 Z mychecksedad 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
831 ms 205512 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 25, M = 1e5+10, K = 20;


ll n, a[N], b[N][N], bin[N], ter[N], dp[1<<K][N];
void solve(){
  cin >> n;
  for(int i = 0; i < n; ++i) cin >> a[i];
  for(int i = 0; i < n; ++i) bin[i] = __builtin_popcount(a[i]);
  for(int i = 0; i < n; ++i){
    ter[i] = 0;
    int v = a[i];
    while(v > 0){
      if(v % 3 == 1) ter[i]++;
      v /= 3;
    }
  }

  for(int i = 0; i < n; ++i){
    for(int j = 0; j < n; ++j){
      b[i][j] = (bin[i] == bin[j] || ter[i] == ter[j]);
    }
  }

  for(int i = 0; i < (1<<n); ++i){
    for(int j = 0; j < n; ++j) dp[i][j] = 0;
    if(__builtin_popcount(i) == 1){
      for(int j = 0; j < n; ++j){
        if((1<<j)&i)
          dp[i][j] = 1;
      }
    }
  }

  for(int mask = 1; mask < (1<<n); ++mask){
    for(int j = 0; j < n; ++j){
      if(!((1<<j)&mask)){
        for(int k = 0; k < n; ++k){
          if((1<<k)&mask){
            if(b[j][k] == 1)
              dp[mask ^ (1<<j)][j] += dp[mask][k];
          }
        }
      }
    }
  }
  // for(int i =0 ; i < (1<<n); ++i){
  //   for(int j = 0; j < n; ++j){
  //     cout << dp[i][j] << ' ';
  //   }
  //   cout << '\n';
  // }
  ll ans = 0;
  for(int i = 0; i < n; ++i) ans += dp[(1<<n) - 1][i];
  cout << ans;  
}


int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int T = 1, aa;
  // cin >> T;aa=T;
  while(T--){
    // cout << "Case #" << aa-T << ": ";
    solve();
    cout << '\n';
  }
  return 0;
 
}

Compilation message

beauty.cpp: In function 'int main()':
beauty.cpp:69:14: warning: unused variable 'aa' [-Wunused-variable]
   69 |   int T = 1, aa;
      |              ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 8 ms 3540 KB Output is correct
12 Correct 8 ms 3540 KB Output is correct
13 Correct 38 ms 13144 KB Output is correct
14 Correct 169 ms 51616 KB Output is correct
15 Correct 146 ms 51556 KB Output is correct
16 Correct 207 ms 51616 KB Output is correct
17 Correct 174 ms 51592 KB Output is correct
18 Correct 182 ms 51612 KB Output is correct
19 Correct 831 ms 205512 KB Output is correct
20 Correct 667 ms 205512 KB Output is correct