#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define mpr make_pair
#define lg(x) __lg(x)
#define bpc(x) __builtin_popcount(x)
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int mod = 998244353;
ll ans, n;
ll dp[(1<<21)][21];
bool ok[21][21];
void solve()
{
ll i, j, mask;
cin>>n;
vl a(n);
vector<pll> v(n);
for(i=0;i<n;i++){
cin>>a[i];
v[i].fi = bpc(a[i]);
ll lg = 20, cnt = 0, d = a[i];
while(d){
ll dv = pow(3, lg);
lg--;
if(dv > d){continue;}
ll df = d / dv;
cnt += (df == 1);
d %= dv;
}
v[i].se = cnt;
}
for(i=0;i<n;i++){
for(j=i;j<n;j++){
if(v[i].fi == v[j].fi || v[i].se == v[j].se){
ok[i][j] = ok[j][i] = 1;
}
}
}
for(i=0;i<n;i++){
dp[0][i] = 1;
}
for(mask=1;mask<(1<<n);mask++){
for(i=0;i<n;i++){
if(mask & (1<<i)){
for(j=0;j<n;j++){
if(mask & (1<<j) && ok[i][j]){
dp[mask][i] += dp[mask ^ (1<<i)][j];
if(mask+1 == (1<<n)){
ans += dp[mask ^ (1<<i)][j];
}
}
}
}
}
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//precomp();
ll tst=1;
//cin>>tst;
while(tst--){
solve();
}
}
/*
ok.
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
628 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
4700 KB |
Output is correct |
12 |
Correct |
8 ms |
4596 KB |
Output is correct |
13 |
Correct |
30 ms |
12672 KB |
Output is correct |
14 |
Correct |
161 ms |
43684 KB |
Output is correct |
15 |
Correct |
130 ms |
43652 KB |
Output is correct |
16 |
Correct |
163 ms |
43632 KB |
Output is correct |
17 |
Correct |
181 ms |
43844 KB |
Output is correct |
18 |
Correct |
162 ms |
43684 KB |
Output is correct |
19 |
Correct |
752 ms |
173136 KB |
Output is correct |
20 |
Correct |
589 ms |
172880 KB |
Output is correct |