#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn=20;
const int B = (1<<20);
int n;
ll dp[B][maxn];
int a[maxn],jog[maxn][maxn];
int ok(int x,int y){
int cp = 0,sg = 0;
int nwx = x,nwy = y;
while(nwx){
cp += (nwx%2);
nwx /= 2;
}
while(nwy){
sg += (nwy%2);
nwy /= 2;
}
// if(x == 5 and y == 6) cout<<cp<<' '<<sg<<'\n';
if(cp == sg) return 1;
cp = sg = 0;
while(x){
cp += ((x%3) == 1);
x /= 3;
}
while(y){
sg += ((y%3) == 1);
y /= 3;
}
return (cp == sg);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 0;i < n;i++){
cin >> a[i];
dp[(1<<i)][i] = 1;
}
for(int i = 0;i < n;i++){
for(int j = i+1;j < n;j++){
int nw = ok(a[i],a[j]);
jog[i][j] = jog[j][i] = nw;
}
}
// cout<<jog[0][2]<<'\n';
int go = (1<<n);
for(int i = 3;i < go;i++){
vector<int>nw;
for(int j = 0;j < n;j++) if((1<<j) & i) nw.push_back(j);
for(int j = 0;j < (int)nw.size();j++){
int now = i ^ (1<<nw[j]);
for(int k = 0;k < (int)nw.size();k++){
if(j == k or !jog[nw[j]][nw[k]]) continue;
dp[i][nw[j]] += dp[now][nw[k]];
}
}
}
ll ans = 0;
for(int i = 0;i < n;i++) ans += dp[go - 1][i];
// for(int i = 1;i < go;i++){
// cout<<i<<" ---> ";
// for(int j = 0;j < n;j++) cout<<dp[i][j]<<' ';
// cout<<'\n';
// }
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
464 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
6 ms |
2888 KB |
Output is correct |
12 |
Correct |
6 ms |
2852 KB |
Output is correct |
13 |
Correct |
25 ms |
10576 KB |
Output is correct |
14 |
Correct |
127 ms |
41280 KB |
Output is correct |
15 |
Correct |
98 ms |
41320 KB |
Output is correct |
16 |
Correct |
141 ms |
41248 KB |
Output is correct |
17 |
Correct |
148 ms |
41404 KB |
Output is correct |
18 |
Correct |
159 ms |
41272 KB |
Output is correct |
19 |
Correct |
547 ms |
164360 KB |
Output is correct |
20 |
Correct |
472 ms |
164372 KB |
Output is correct |