# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
913825 | cot | 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 <iostream>
#include <algorithm>
#include <map>
#include <array>
#define int long long
#define f first
#define s second
#define pb push_back
#define int long long
using namespace std;
const int N = 21;
int fix[N][N],lst,ans,lst1,a[N],n,dp[(1<<N)][N];
int ones(int x) {
int cnt = 0;
while (x) {
cnt += ((x%3)==1);
x/=3;
}
return cnt;
}
signed main() {
cin>>n;
for (int i = 0; i < n; i++) {
cin>>a[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (__builtin_popcount(a[i]) == __builtin_popcount(a[j])) {
fix[i][j] = fix[j][i] = 1;
continue;
}
if (ones(a[i]) == ones(a[j])) {
fix[i][j] = fix[j][i] = 1;
continue;
}
}
}
for (int i = 1; i < (1<<n); i++) {
vector <int> v;
for (int j = 0; j < n; j++) {
if ((1<<j)&i) v.pb(j);
}
if (__builtin_popcount(i) == 1){ dp[i][v[0]] = 1; continue; }
for (int j = 0; j < v.size(); j++) { lst = v[j];
for (int j1 = 0; j1 < v.size(); j1++) { lst1 = v[j1];
if (j == j1) continue;
if (fix[lst][lst1]) {
dp[i][lst] += dp[i ^ (1<<lst)][lst1];
}
}
if (i == (1<<n) - 1) ans += dp[i][lst];//, ans %= mod;
}
}
cout<<ans<<endl;
}