# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
261556 | sckmd | 아름다운 순열 (IZhO12_beauty) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}