#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define s second
#define f first
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 10;
const int block = 1e3;
int c(int x, int y) {
int ret = 0;
while (x) {
ret += (x%y==1);
x /= y;
}
return ret;
}
bool ok[20][20];
int dp[1 << 20][20], a[20];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (c(a[i], 2) == c(a[j], 2) || c(a[i], 3) == c(a[j], 3))
ok[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
dp[1 << i][i] = 1;
}
for (int mask = 0; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
for (int j = 0; j < n; j++) {
if (mask & (1 << j) || ok[i][j] == 0)
continue;
dp[mask | (1 << j)][j] += dp[mask][i];
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans += dp[(1 << n) - 1][i];
}
cout << ans << '\n';
return 0;
}
/*
6 1
1 1 2
3 2 4 2
1 3 5
3 2 3 1
2 1
3 0 3 1
*/
/*
6 0
1 3 10
1 3 5
3 6 10 6
2 1
1 3 10
3 6 4 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
6 ms |
1628 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |