#include <bits/stdc++.h>
//#define int long long
using namespace std;
int const maxn = 2005,MOD=1e9 + 7;
int d[maxn];
struct road{
int a,b,cost;
};
int pow(int a,int b){
int result = 1;
while (b>0){
if (b&1) {
result *= a;
}
a = a*a;
b = b>>1;
}
return result;
}
int gcd(int a,int b){
if (b==0) return a;
if (a<b) swap(a,b);
return gcd(b,a%b);
}
int count2(int x){
return __builtin_popcount(x);
}
int count3(int x){
int cnt=0;
while (x){
if (x%3==1) cnt++;
x/=3;
}
return cnt;
}
int dp[1<<20][21];
void solve(){
int n;cin>>n;
int arr[n+1];
for (int i=1;i<=n;++i) cin >> arr[i];
pair<int,int> v[n+1];
for (int i=1;i<=n;++i) v[i] = make_pair(count2(arr[i]),count3(arr[i]));
for (int mask=1;mask<(1<<n);++mask){
int sz=0;
int last=0;
for (int i=0;i<n;++i) if ((1<<i)&mask) sz++, last = i;
if (sz==1) dp[mask][last] = 1;
else{
for (int i=0;i<n;++i){
if ((1<<i)&mask){
int next_mask = (1<<i)^mask;
for (int j=0;j<n;++j){
if ((1<<j)&next_mask and (v[j+1].first==v[i+1].first or v[j+1].second==v[i+1].second)){
dp[mask][i] += dp[next_mask][j];
}
}
}
}
}
}
int ans =0;
for (int i=0;i<n;++i) ans += dp[(1<<n)-1][i];
cout << ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
solve();
}
//
//4
//4
//2
//2
//2
//4
//4
//4
//4
//2
//4
//4
//2
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
8 ms |
1612 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |