Submission #795016

# Submission time Handle Problem Language Result Execution time Memory
795016 2023-07-27T05:14:36 Z Blagoj Beautiful row (IZhO12_beauty) C++17
100 / 100
1590 ms 164544 KB
#include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()

ll n, a[22], base2[22], base3[22];

ll Base3(ll x) {
    ll ans = 0;
    while (x > 0) {
        ans += (x % 3 == 1);
        x /= 3;
    }
    return ans;
}

ll dp[1 << 20][20];

ll solve(ll mask, ll last) {
    if (dp[mask][last] != -1) return dp[mask][last];
    if (mask == (1 << n) - 1) return dp[mask][last] = 1;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (!(mask & (1 << i)) && (mask == 0 || base2[i] == base2[last] || base3[i] == base3[last])) {
            ans += solve(mask | (1 << i), i);
        }
    }
    return dp[mask][last] = ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        base2[i] = __builtin_popcount(a[i]);
        base3[i] = Base3(a[i]);
    }
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0);
}
# Verdict Execution time Memory Grader output
1 Correct 53 ms 164428 KB Output is correct
2 Correct 53 ms 164444 KB Output is correct
3 Correct 51 ms 164388 KB Output is correct
4 Correct 53 ms 164360 KB Output is correct
5 Correct 52 ms 164428 KB Output is correct
6 Correct 52 ms 164348 KB Output is correct
7 Correct 52 ms 164444 KB Output is correct
8 Correct 54 ms 164360 KB Output is correct
9 Correct 53 ms 164364 KB Output is correct
10 Correct 54 ms 164440 KB Output is correct
11 Correct 65 ms 164436 KB Output is correct
12 Correct 65 ms 164348 KB Output is correct
13 Correct 88 ms 164432 KB Output is correct
14 Correct 266 ms 164428 KB Output is correct
15 Correct 291 ms 164544 KB Output is correct
16 Correct 276 ms 164408 KB Output is correct
17 Correct 275 ms 164428 KB Output is correct
18 Correct 229 ms 164460 KB Output is correct
19 Correct 1573 ms 164460 KB Output is correct
20 Correct 1590 ms 164460 KB Output is correct