Submission #1083623

# Submission time Handle Problem Language Result Execution time Memory
1083623 2024-09-03T15:31:56 Z LOLOLO Beautiful row (IZhO12_beauty) C++17
100 / 100
671 ms 164504 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           s     second
#define           f     first
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 2e5 + 10;
const int block = 1e3;

int c(int x, int y) {
    int ret = 0;
    while (x) {
        ret += (x%y==1);
        x /= y;
    }

    return ret;
}

bool ok[20][20];
ll dp[1 << 20][20];
int a[20];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) 
        cin >> a[i];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (c(a[i], 2) == c(a[j], 2) || c(a[i], 3) == c(a[j], 3))
                ok[i][j] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        dp[1 << i][i] = 1;
    }

    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)) || (ok[i][j] == 0))
                        continue;

                    dp[mask | (1 << j)][j] += dp[mask][i];
                }
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += dp[(1 << n) - 1][i];
    }

    cout << ans << '\n';
    return 0;
}

/*
6 1
1 1 2
3 2 4  2
1 3 5
3 2 3 1
2 1
3 0 3 1
*/

/*
6 0
1 3 10
1 3 5
3 6 10 6
2 1
1 3 10
3 6 4 2
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 8 ms 2908 KB Output is correct
12 Correct 6 ms 2908 KB Output is correct
13 Correct 30 ms 10512 KB Output is correct
14 Correct 138 ms 41328 KB Output is correct
15 Correct 127 ms 41328 KB Output is correct
16 Correct 139 ms 41380 KB Output is correct
17 Correct 146 ms 41392 KB Output is correct
18 Correct 142 ms 41304 KB Output is correct
19 Correct 671 ms 164432 KB Output is correct
20 Correct 607 ms 164504 KB Output is correct