#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;
vl adj[25];
bool used[25];
ll ans, n;
void dfs(ll v, ll depth){
used[v] = 1;
if(depth == n){
ans++;
used[v] = 0;
return ;
}
for(auto edge: adj[v]){
if(!used[edge]){
dfs(edge, depth + 1);
}
}
used[v] = 0;
}
void solve()
{
ll i, j;
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+1;j<n;j++){
if(v[i].fi == v[j].fi || v[i].se == v[j].se){
adj[i].pb(j);
adj[j].pb(i);
}
}
}
for(i=0;i<n;i++){
dfs(i, 1);
}
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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
107 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
49 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
3056 ms |
348 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |