Submission #895030

#TimeUsernameProblemLanguageResultExecution timeMemory
895030raul2008487Beautiful row (IZhO12_beauty)C++17
100 / 100
752 ms173136 KiB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define mpr make_pair
#define lg(x) __lg(x)
#define bpc(x) __builtin_popcount(x)
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int mod = 998244353;
ll ans, n;
ll dp[(1<<21)][21];
bool ok[21][21];
void solve()
{
    ll i, j, mask;
    cin>>n;
    vl a(n);
    vector<pll> v(n);
    for(i=0;i<n;i++){
        cin>>a[i];
        v[i].fi = bpc(a[i]);
        ll lg = 20, cnt = 0, d = a[i];
        while(d){
            ll dv = pow(3, lg);
            lg--;
            if(dv > d){continue;}
            ll df = d / dv;
            cnt += (df == 1);
            d %= dv;
        }
        v[i].se = cnt;
    }
    for(i=0;i<n;i++){
        for(j=i;j<n;j++){
            if(v[i].fi == v[j].fi || v[i].se == v[j].se){
                ok[i][j] = ok[j][i] = 1;
            }
        }
    }
    for(i=0;i<n;i++){
        dp[0][i] = 1;
    }
    for(mask=1;mask<(1<<n);mask++){
        for(i=0;i<n;i++){
            if(mask & (1<<i)){
                for(j=0;j<n;j++){
                    if(mask & (1<<j) && ok[i][j]){
                        dp[mask][i] += dp[mask ^ (1<<i)][j];
                        if(mask+1 == (1<<n)){
                            ans += dp[mask ^ (1<<i)][j];
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //precomp();
    ll tst=1;
    //cin>>tst;
    while(tst--){
            solve();
    }
}
/*
ok.
*/
#Verdict Execution timeMemoryGrader output
Fetching results...