Submission #261559

#TimeUsernameProblemLanguageResultExecution timeMemory
261559sckmdBeautiful row (IZhO12_beauty)C++14
100 / 100
830 ms164600 KiB
#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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...