답안 #1077632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077632 2024-08-27T08:26:53 Z coolboy19521 아름다운 순열 (IZhO12_beauty) C++17
0 / 100
132 ms 262144 KB
#include "bits/stdc++.h"
#define ll long long
#define int ll

using namespace std;

const int sz = 22;

int vi[1 << sz][sz];
ll dp[1 << sz][sz];
int tw[sz], tr[sz];
int a[sz], n;

bool matc(int i, int j) {
    if (tw[i] == tw[j] || tr[i] == tr[j])
        return true;
    return false;
}

ll go(int i, int bt) {
    if (bt == (1 << n) - 1) return 1ll;
    int& v = vi[bt][i];
    ll& r = dp[bt][i];
    if (v) return r;
    for (int j = 0; j < n; j ++) {
        int f = bt & (1 << j);
        if (!f && matc(i, j))
            r += go(j, bt | (1 << j));
    }
    return r;
}

void zero() {
    memset(dp, 0, sizeof(dp));
    memset(vi, 0, sizeof(vi));
}

signed main() {
    cin >> n;

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

    for (int i = 0; i < n; i ++) {
        for (int cn = a[i]; 0 < cn; cn /= 2)
            tw[i] += cn % 2;
        for (int cn = a[i]; 0 < cn; cn /= 3)
            tr[i] += 1 == cn % 3;
        //cout << a[i] << ' ' << tw[i] << ' ' << tr[i] << '\n';
    }

    ll cn = 0;

    for (int i = 0; i < n; i ++) {
        zero();
        cn += go(i, 1 << i);
    }

    cout << cn << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 132 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -