//Dedicated to my love, ivaziva
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ar array
const int mxN=20;
int n, a[mxN];
ll dp[mxN][1<<mxN];
ar<int, 2> cnt[mxN];
ar<int, 2> calc(int x) {
int cnt2=0, cnt3=0;
int p=x;
while(p) {
if(p&1) ++cnt2;
p/=2;
}
p=x;
while(p) {
if(p%3==1) ++cnt3;
p/=3;
}
return {cnt2, cnt3};
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=0; i<n; ++i) {
cin >> a[i];
cnt[i]=calc(a[i]);
}
ll ans=0;
for(int i=0; i<n; ++i) dp[i][1<<i]=1;
for(int msk=1; msk<(1<<n); ++msk) {
for(int i=0; i<n; ++i) {
if(msk&(1<<i)) {
for(int j=0; j<n; ++j) {
if(i==j) continue;
if(((msk&(1<<j))>0)&&((cnt[i][0]==cnt[j][0])||(cnt[i][1]==cnt[j][1]))) {
dp[i][msk]+=dp[j][msk^(1<<i)];
}
}
}
}
}
int sz=(1<<n)-1;
for(int i=0; i<n; ++i) ans+=dp[i][sz];
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
2028 KB |
Output is correct |
12 |
Correct |
7 ms |
2140 KB |
Output is correct |
13 |
Correct |
34 ms |
7460 KB |
Output is correct |
14 |
Correct |
167 ms |
30288 KB |
Output is correct |
15 |
Correct |
163 ms |
30128 KB |
Output is correct |
16 |
Correct |
168 ms |
30260 KB |
Output is correct |
17 |
Correct |
171 ms |
30292 KB |
Output is correct |
18 |
Correct |
172 ms |
30292 KB |
Output is correct |
19 |
Correct |
790 ms |
127572 KB |
Output is correct |
20 |
Correct |
766 ms |
127496 KB |
Output is correct |