Submission #1240778

#TimeUsernameProblemLanguageResultExecution timeMemory
1240778eirinayukariBeautiful row (IZhO12_beauty)C++20
0 / 100
3097 ms27684 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];

void solve(int id) {
    // cout << "Case " << id << ": ";
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        dp[i][1 << i] = 1;
    }

    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 (i != j && (cbin(a[i]) == cbin(a[j]) || cter(a[i]) == cter(a[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...