답안 #716436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
716436 2023-03-30T05:58:54 Z vjudge1 아름다운 순열 (IZhO12_beauty) C++17
0 / 100
3000 ms 164584 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e5 + 100;
const int mod = (int)1e9+7;
int n;
int a[maxn];
int g[22][22];
ll dp[1<<20][20];

int ter(int x) {
    int cnt = 0;
    while(x > 0) {
        if(x % 3 == 1) cnt++;
        x /= 3;
    }
    return cnt;
}
ll rec(int mask, int last) {
    if(__builtin_popcount(mask) == n) {
        return 1;
    }
    if(dp[mask][last] != -1) return dp[mask][last];
    dp[mask][last] = 0;
    for(int i = 0; i < n; i++) {
        if(g[last][i] && !(mask & (1<<i))) {
            dp[mask][last] += rec(mask|1<<i, i);
        }
    }
    return dp[mask][last];
}
int main() {
    ios_base::sync_with_stdio(false);

    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(__builtin_popcount(a[i]) == __builtin_popcount(a[j])) {
                g[i][j] = 1;
            } else {
                if(ter(a[i]) == ter(a[j])) {
                    g[i][j] = 1;
                }
            }
        }
    }
    memset(dp, -1, sizeof dp);
    ll ans = 0;
    vector<int>p(n);
    for(int i = 0; i<n; i++) p[i] = i;
    do{
        int cur = 1;
        for(int i = 1; i < n; i++) {
            if(g[p[i]][p[i-1]] == 0) {
                cur = 0;
                break;
            }
        }
        ans += cur;
    }while(next_permutation(p.begin(),  p.end()));
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 164428 KB Output is correct
2 Correct 60 ms 164412 KB Output is correct
3 Correct 70 ms 164460 KB Output is correct
4 Correct 62 ms 164340 KB Output is correct
5 Correct 61 ms 164352 KB Output is correct
6 Correct 92 ms 164584 KB Output is correct
7 Correct 79 ms 164460 KB Output is correct
8 Correct 117 ms 164464 KB Output is correct
9 Correct 84 ms 164396 KB Output is correct
10 Correct 112 ms 164396 KB Output is correct
11 Execution timed out 3066 ms 164428 KB Time limit exceeded
12 Halted 0 ms 0 KB -