답안 #261559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261559 2020-08-11T21:13:36 Z sckmd 아름다운 순열 (IZhO12_beauty) C++14
100 / 100
830 ms 164600 KB
#include <bits/stdc++.h>

using namespace std;
int a[25];
bool moze[25][25];

bool check(int x,int y)
{
  int binx = __builtin_popcount(x);
  int biny = __builtin_popcount(y);
  if(binx==biny)return true;
  int cntx = 0;
  int cnty = 0;
  while(x > 0)
  {
    cntx += (x%3==1);
    x /= 3;
  }
  while(y > 0)
  {
    cnty += (y%3==1);
    y /= 3;
  }
  return cntx==cnty;
}
typedef long long ll;
ll dp[(1<<20)][20];

int main()
{
  int n;
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  for(int i = 0; i < n; i++)
  {
    for(int j = 0; j < n; j++)
    {
      moze[i][j]=check(a[i],a[j]);
    }
  }
  for(int i = 0; i < n; i++)dp[0][i]=1LL;
  for(int mask = 0; mask < (1<<n); mask++)
  {
    for(int i = 0; i < n; i++)
    {
      if(!(mask&(1<<i)))continue;
      for(int j = 0; j < n; j++)
      {
        if(!(mask&(1<<j)))continue;
        if(!moze[i][j])continue;
        dp[mask][i] += dp[mask^(1<<i)][j];
      }
    }
  }
  ll ans = 0LL;
  for(int i = 0; i < n; i++)ans += dp[(1<<n)-1][i];
  cout << ans;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 8 ms 2944 KB Output is correct
12 Correct 10 ms 2920 KB Output is correct
13 Correct 39 ms 10592 KB Output is correct
14 Correct 195 ms 41336 KB Output is correct
15 Correct 143 ms 41464 KB Output is correct
16 Correct 179 ms 41336 KB Output is correct
17 Correct 184 ms 41336 KB Output is correct
18 Correct 189 ms 41336 KB Output is correct
19 Correct 830 ms 164472 KB Output is correct
20 Correct 639 ms 164600 KB Output is correct