Submission #1240809

#TimeUsernameProblemLanguageResultExecution timeMemory
1240809eirinayukariBeautiful row (IZhO12_beauty)C++20
100 / 100
450 ms127608 KiB
// XD XD
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define el cout << "\n";

using namespace std;
using ll = long long;

const int MAXN = 1e6 + 7;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LLINF = 1e18 + 7;

template <typename T>
bool ckmin(T& a, T b) {
    return a > b ? a = b, true : false;
}

template <typename T>
bool ckmax(T& a, T b) {
    return a < b ? a = b, true : false;
}

int cbin(int x) {
    int res = 0;
    while (x) {
        res += (x % 2 == 1);
        x /= 2;
    }
    return res;
}

int cter(int x) {
    int res = 0;
    while (x) {
        res += (x % 3 == 1);
        x /= 3;
    }
    return res;
}

int n;
ll dp[20][1 << 20];
int a[20];
bool f[20][20];
void solve(int id) {
    cin >> n;
    map<int, int> mp; 
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        mp[a[i]]++;
        dp[i][1 << i] = 1;
    }

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (cbin(a[i]) == cbin(a[j]) || cter(a[i]) == cter(a[j])) {
                f[i][j] = f[j][i] = true;
            }
        }
    }
    
    for (int mask = 0; mask < (1 << n); mask++) {
        for (int i = 0; i < n; i++) {
            if (mask >> i & 1) {
                for (int j = 0; j < n; j++) {
                    if (f[i][j]) {
                        dp[i][mask] += dp[j][mask ^ (1 << i)];
                    }
                }
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += dp[i][(1 << n) - 1];
    }
    cout << ans << "\n";
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        solve(i);
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...