답안 #867653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
867653 2023-10-29T04:26:54 Z sleepntsheep 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
521 ms 164492 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using ll = long long;
#define N 20

int n, a[N], b3[N], d[N][N];
ll ans, dp[1<<N][N];

inline int beautiful(int i, int j) { return (__builtin_popcount(a[i]) == __builtin_popcount(a[j])) || b3[i] == b3[j]; }

int main()
{
    ShinLena;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        for (int x = a[i]; x; b3[i] += (x % 3) == 1, x /= 3);
        dp[0][i] = 1;
    }
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = beautiful(i, j);
    for (int k = 0; k < (1 << n); ++k)
    {
        for (int j = 0; j < n; ++j)
        {
            if (!(k&(1<<j))) continue;
            for (int l = 0; l < n; ++l)
            {
                if (k & (1 << l))
                    dp[k][j] += d[j][l] * dp[k&~(1<<j)][l];
            }
        }
    }

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

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 5 ms 4700 KB Output is correct
12 Correct 6 ms 4700 KB Output is correct
13 Correct 25 ms 10892 KB Output is correct
14 Correct 115 ms 41624 KB Output is correct
15 Correct 117 ms 41620 KB Output is correct
16 Correct 126 ms 42144 KB Output is correct
17 Correct 113 ms 41628 KB Output is correct
18 Correct 116 ms 41552 KB Output is correct
19 Correct 521 ms 164492 KB Output is correct
20 Correct 518 ms 164432 KB Output is correct