답안 #834996

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
834996 2023-08-23T05:01:37 Z rahulverma 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
1936 ms 189128 KB
#include <iostream>
#include <vector>
#include <cstring>
#include <sstream>
#include <fstream>
using namespace std;

const int N = 100;
long long a[N];
long long b[N];
long long dp[1 << 20][23];
int n;

long long rec(long long mask, int last) {
    if (mask == ((1 << n) - 1)) return 1;

    if (last != -1 && dp[mask][last] != -1) return dp[mask][last];
    long long an = 0;
    for (int i = 0; i < n; i++) {
        if ((mask & (1 << i)) == 0) {
            if (last == -1 || a[last] == a[i] || b[last] == b[i]) {
                an += rec(mask | (1 << i), i);
            }
        }
    }
    if (last != -1) dp[mask][last] = an;
    return an;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int i;
    long long ans = 0;

    cin >> n;
    for (i = 0; i < n; i++) {
        long long p;
        cin >> p;
        a[i] = __builtin_popcountll(p);
        long long p1 = p;
        while (p1 > 0) {
            if (p1 % 3 == 1) b[i]++;
            p1 /= 3;
        }
    }

    memset(dp, -1, sizeof(dp));
    ans += rec(0, -1);

    cout << ans << "\n";

    return 0;
}

long long pow(int i) {
    long long ans = 1;
    while (i > 0) {
        ans *= 2;
        i--;
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 189000 KB Output is correct
2 Correct 58 ms 189016 KB Output is correct
3 Correct 58 ms 189040 KB Output is correct
4 Correct 59 ms 189076 KB Output is correct
5 Correct 58 ms 189088 KB Output is correct
6 Correct 58 ms 188996 KB Output is correct
7 Correct 58 ms 188972 KB Output is correct
8 Correct 69 ms 189012 KB Output is correct
9 Correct 58 ms 189084 KB Output is correct
10 Correct 60 ms 189080 KB Output is correct
11 Correct 69 ms 189088 KB Output is correct
12 Correct 65 ms 189024 KB Output is correct
13 Correct 104 ms 189036 KB Output is correct
14 Correct 345 ms 189084 KB Output is correct
15 Correct 360 ms 189092 KB Output is correct
16 Correct 288 ms 189128 KB Output is correct
17 Correct 357 ms 189040 KB Output is correct
18 Correct 257 ms 189092 KB Output is correct
19 Correct 1840 ms 189092 KB Output is correct
20 Correct 1936 ms 189084 KB Output is correct