#include <bits/stdc++.h>
using namespace std;
long long dp[(1 << 20)][20];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,a;
cin >> n;
vector<int> haha(n);
for(int i = 0; i < n; i++) {
cin >> haha[i];
}
vector<int> br2(n);
vector<int> br3(n);
for(int i = 0; i < n; i++) {
a = haha[i];
while(a > 0) {
br2[i]+=a%2;
a/=2;
}
a = haha[i];
while(a > 0) {
if(a%3 == 1) {
br3[i]+=1;
}
a/=3;
}
}
long long ans = 0;
for(int i = 1; i < (1 << n); i++) {
for(int j = 0; j < n; j++) {
if((1 << j)&i) {
int c = i-(1 << j);
dp[i][j] = 0;
if(c == 0) {
dp[i][j] = 1;
}
else {
for(int k = 0; k < n; k++) {
if(((1 << k)&c) && (br2[k] == br2[j] || br3[k] == br3[j])) {
if(dp[c][k] != -1) {
dp[i][j]+=dp[c][k];
}
}
}
}
}
else {
dp[i][j] = 0;
}
}
}
for(int i = 0; i < n; i++) {
ans+=dp[(1 << n)-1][i];
}
cout << ans;
return 0;
}
# |
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 |
408 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 |
604 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 |
600 KB |
Output is correct |
11 |
Correct |
7 ms |
4696 KB |
Output is correct |
12 |
Correct |
10 ms |
4652 KB |
Output is correct |
13 |
Correct |
33 ms |
10840 KB |
Output is correct |
14 |
Correct |
168 ms |
41556 KB |
Output is correct |
15 |
Correct |
154 ms |
41612 KB |
Output is correct |
16 |
Correct |
171 ms |
41656 KB |
Output is correct |
17 |
Correct |
167 ms |
41624 KB |
Output is correct |
18 |
Correct |
186 ms |
41636 KB |
Output is correct |
19 |
Correct |
767 ms |
164436 KB |
Output is correct |
20 |
Correct |
679 ms |
164540 KB |
Output is correct |