Submission #382223

# Submission time Handle Problem Language Result Execution time Memory
382223 2021-03-26T18:48:56 Z vishesh312 Beautiful row (IZhO12_beauty) C++17
100 / 100
1451 ms 172780 KB
#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/

#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()

using ll = long long;
const int mod = 1e9+7;

void solve(int tc) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &x : v) cin >> x;
    vector<vector<ll>> dp(n, vector<ll>(1<<n));
    for (int i = 0; i < n; ++i) {
        dp[i][1<<i] = 1;
    }
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        int x = v[i];
        while (x) {
            a[i] += (x % 3 == 1);
            x /= 3;
        }
    }
    for (int mask = 0; mask < (1<<n); ++mask) {
        for (int i = 0; i < n; ++i) {
            if (mask&(1<<i)) {
                for (int j = 0; j < n; ++j) {
                    if (!(mask&(1<<j)) and (__builtin_popcount(v[i]) == __builtin_popcount(v[j]) or a[i] == a[j])) {
                        dp[j][mask|(1<<j)] += dp[i][mask];
                    }
                }
            }
        }
    }
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += dp[i][(1<<n)-1];
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int tc = 1;
    //cin >> tc;
    for (int i = 1; i <= tc; ++i) solve(i);
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 13 ms 2284 KB Output is correct
12 Correct 13 ms 2284 KB Output is correct
13 Correct 66 ms 9068 KB Output is correct
14 Correct 294 ms 39404 KB Output is correct
15 Correct 306 ms 39404 KB Output is correct
16 Correct 298 ms 39404 KB Output is correct
17 Correct 311 ms 39404 KB Output is correct
18 Correct 306 ms 39404 KB Output is correct
19 Correct 1451 ms 172780 KB Output is correct
20 Correct 1329 ms 172780 KB Output is correct