#include <bits/stdc++.h>
// author : aykhn
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcount
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define int ll
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
const int MXN = 20;
int n;
int a[MXN];
pii cnt[MXN];
int dp[(1 << MXN)][MXN];
signed main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
for (int j = 0; j <= 30; j++)
{
cnt[i].fi += a[i] >> j & 1;
}
int pw = 387420489;
int y = a[i];
while (pw)
{
int x = y/pw;
cnt[i].se += (x == 1);
y -= x * pw;
pw /= 3;
}
}
for (int i = 0; i < n; i++) dp[(1 << i)][i] = 1;
for (int mask = 0; mask < (1 << n); mask++)
{
if (bpc(mask) <= 1) continue;
for (int i = 0; i < n; i++)
{
if (!(mask >> i & 1)) continue;
for (int j = 0; j < n; j++)
{
if (!(mask >> j & 1)) continue;
if (cnt[i].fi == cnt[j].fi || cnt[i].se == cnt[j].se) dp[mask][i] += dp[(mask ^ (1 << i))][j];
}
}
}
int res = 0;
for (int i = 0; i < n; i++) res += dp[(1 << n) - 1][i];
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
516 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
4724 KB |
Output is correct |
12 |
Correct |
7 ms |
4700 KB |
Output is correct |
13 |
Correct |
29 ms |
10844 KB |
Output is correct |
14 |
Correct |
148 ms |
41596 KB |
Output is correct |
15 |
Correct |
129 ms |
41556 KB |
Output is correct |
16 |
Correct |
156 ms |
41556 KB |
Output is correct |
17 |
Correct |
162 ms |
41556 KB |
Output is correct |
18 |
Correct |
161 ms |
41620 KB |
Output is correct |
19 |
Correct |
713 ms |
164688 KB |
Output is correct |
20 |
Correct |
595 ms |
164696 KB |
Output is correct |