/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 25, M = 1e5+10, K = 20;
int n, a[N], b[N][N], bin[N], ter[N], dp[1<<K][N];
void solve(){
cin >> n;
for(int i = 0; i < n; ++i) cin >> a[i];
for(int i = 0; i < n; ++i) bin[i] = __builtin_popcount(a[i]);
for(int i = 0; i < n; ++i){
ter[i] = 0;
int v = a[i];
while(v > 0){
if(v % 3 == 1) ter[i]++;
v /= 3;
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
b[i][j] = (bin[i] == bin[j] || ter[i] == ter[j]);
}
}
for(int i = 0; i < (1<<n); ++i){
for(int j = 0; j < n; ++j) dp[i][j] = 0;
if(__builtin_popcount(i) == 1){
for(int j = 0; j < n; ++j){
if((1<<j)&i)
dp[i][j] = 1;
}
}
}
for(int mask = 1; mask < (1<<n); ++mask){
for(int j = 0; j < n; ++j){
if(!((1<<j)&mask)){
for(int k = 0; k < n; ++k){
if((1<<k)&mask){
if(b[j][k] == 1)
dp[mask ^ (1<<j)][j] += dp[mask][k];
}
}
}
}
}
// for(int i =0 ; i < (1<<n); ++i){
// for(int j = 0; j < n; ++j){
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
ll ans = 0;
for(int i = 0; i < n; ++i) ans += dp[(1<<n) - 1][i];
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
beauty.cpp: In function 'int main()':
beauty.cpp:69:14: warning: unused variable 'aa' [-Wunused-variable]
69 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
216 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
9 ms |
1876 KB |
Output is correct |
12 |
Correct |
7 ms |
1876 KB |
Output is correct |
13 |
Incorrect |
32 ms |
6724 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |