#include <bits/stdc++.h>
using namespace std;
long long d[(1<<20)+5][25],ans;
int a2[25],a3[25],x,i,j,bm,n;
int main(){
cin>>n;
for (i=0; i<n; i++){
cin>>x; d[1<<i][i]=1;
a2[i]=__builtin_popcount(x);
while (x>0){
if (x%3==1) a3[i]++;
x/=3;
}
}
//for (i=0; i<n; i++) cout<<a2[i]<<' '<<a3[i]<<'\n';
for (bm=0; bm<(1<<n); bm++)
for (i=0; i<n; i++)
for (j=0; j<n; j++)
if ((1<<i)&bm && (1<<j)&bm && i!=j)
if (a2[i]==a2[j] || a3[i]==a3[j])
d[bm][i]+=d[bm-(1<<i)][j];
for (i=0; i<n; i++)
ans+=d[(1<<n)-1][i];
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 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 |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
10 ms |
4700 KB |
Output is correct |
12 |
Correct |
9 ms |
4732 KB |
Output is correct |
13 |
Correct |
48 ms |
15220 KB |
Output is correct |
14 |
Correct |
220 ms |
51844 KB |
Output is correct |
15 |
Correct |
195 ms |
51832 KB |
Output is correct |
16 |
Correct |
217 ms |
51892 KB |
Output is correct |
17 |
Correct |
219 ms |
51896 KB |
Output is correct |
18 |
Correct |
220 ms |
51840 KB |
Output is correct |
19 |
Correct |
1006 ms |
205388 KB |
Output is correct |
20 |
Correct |
938 ms |
205532 KB |
Output is correct |