# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
261556 | sckmd | Beautiful row (IZhO12_beauty) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int a[25];
bool moze[25][25];
bool check(int x,int y)
{
binx = __builtin_popcount(x);
biny = __builtin_popcount(y);
if(binx==biny)return true;
int cntx = 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;
}