#include <bits/stdc++.h>
using namespace std;;
#define ll long long
#define ar array
#define ld long double
#define int long long
#define all(v) v.begin(), v.end()
const int N = 5e4 + 20;
const int K = 469;
const int LOG = 26;
const int INF = 1e9;
int MOD = 998244353;
void chmin(int &x,int y){x = min(x, y);}
void chmax(int &x,int y){x = max(x, y);}
int n, k, m, q;
struct Koce{
vector<vector<int> > A;
Koce(){
A.resize(k, vector<int>(k, INF));
}
};
Koce operator+(Koce a, Koce b){
Koce ans;
for(int i = 0;i < k;i++){
for(int j = 0;j < k;j++){
for(int x = 0;x < k;x++)chmin(ans.A[i][j], a.A[i][x] + b.A[x][j]);
}
}
return ans;
}
Koce s[4 * N];
void bld(int u,int tl,int tr, Koce A[]){
if(tl == tr){
s[u] = A[tl];
return;
}
int tm = (tl + tr) / 2;
bld(u * 2, tl, tm, A);
bld(u * 2 + 1, tm + 1, tr, A);
s[u] = s[u * 2] + s[u * 2 + 1];
}
Koce qry(int u,int tl,int tr,int l,int r){
if(l > tr || tl > r){
Koce ret;
for(int i = 0;i < k;i++)ret.A[i][i] = 0;
return ret;
}
if(l <= tl && tr <= r)return s[u];
int tm = (tl + tr) / 2;
return qry(u * 2, tl, tm, l, r) + qry(u * 2 + 1,tm + 1, tr, l, r);
}
int f(int x){
int ans = 0;
while(x){
if(x % 3 == 1)ans++;
x /= 3;
}
return ans;
}
void orz(){
int n;
cin>>n;
int A[n];
for(int i = 0;i < n;i++)cin>>A[i];
vector<int> g[n];
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(__builtin_popcount(A[i]) == __builtin_popcount(A[j]) || f(A[i]) == f(A[j]))g[i].push_back(j);
}
}
int dp[(1 << n)][n];
memset(dp, 0, sizeof dp);
for(int i = 0;i < n;i++)dp[(1 << i)][i] = 1;
for(int msk = 0;msk < (1 << n);msk++){
for(int x = 0;x < n;x++){
for(auto u: g[x]){
if((1 << u) & msk)continue;
dp[msk ^ (1 << u)][u] += dp[msk][x];
}
}
}
int ans = 0;
for(int i = 0;i < n;i++)ans += dp[(1 << n) - 1][i];
cout<<ans;
}
signed main(){ios_base::sync_with_stdio(false);cin.tie(0);
int t;
//cin>>t;
t = 1;
while(t--)orz();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |