#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 20;
bool graph[N][N];
ll dp[MASK(N)][N];
void solve(){
int n; cin >> n;
vector<int> a(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){
if (pop_cnt(a[i]) == pop_cnt(a[j])) graph[i][j] = 1;
else{
int x = a[i], y = a[j];
int cnt = 0;
while(x > 0 || y > 0){
cnt += (x % 3 == 1) - (y % 3 == 1);
x /= 3; y /= 3;
}
if (cnt == 0) graph[i][j] = 1;
}
}
for(int i = 0; i< n; ++i) dp[MASK(i)][i] = 1;
for(int mask = 1; mask < MASK(n) - 1; ++mask){
for(int dcm = mask; dcm > 0; dcm -= LASTBIT(dcm)){
int i = ctz(dcm);
if (dp[mask][i] == 0) continue;
for(int vcl = MASK(n) - 1 - mask; vcl > 0; vcl -= LASTBIT(vcl)){
int j = ctz(vcl);
if (graph[i][j]) dp[mask + MASK(j)][j] += dp[mask][i];
}
}
}
ll ans = 0;
for(int i = 0; i < n; ++i) ans += dp[MASK(n) - 1][i];
cout << ans << "\n";
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |