답안 #429740

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
429740 2021-06-16T09:01:34 Z SuckTinHock 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
1120 ms 164364 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;
int n;
int cnt2[25], cnt3[25];
int dp[1 << 20][20];
vector<int> G[25];

int ter(int x)
{
    int res = 0;
    while (x > 0)
    {
        res += (x % 3 == 1);
        x /= 3;
    }
    return res;
}

void build()
{
    for (int i = 1; i < n; ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            if (cnt2[i] == cnt2[j] || cnt3[i] == cnt3[j])
            {
                G[i].push_back(j);
                G[j].push_back(i);
            }
        }
    }
}

void xuly()
{
    build();
    int last = (1 << n) - 1;
    for (int i = 0; i < n; ++i) dp[1 << i][i] = 1;
    for (int mask = 0; mask <= last; ++mask)
    {
        for (int i = 0; i < n; ++i)
        {
            if (((mask >> i) & 1) == 0) continue;
            for (int j : G[i])
            {
                if (((mask >> j) & 1) == 1) continue;
                int p = mask + (1 << j);
                dp[p][j] += dp[mask][i];
            }
        }
    }

    int res = 0;
    for (int i = 0; i < n; ++i) res += dp[last][i];
    cout << res;
}

void nhap()
{
    int x;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> x, cnt2[i] = __builtin_popcount(x), cnt3[i] = ter(x);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    nhap();
    xuly();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 10 ms 2892 KB Output is correct
12 Correct 12 ms 2880 KB Output is correct
13 Correct 48 ms 10556 KB Output is correct
14 Correct 219 ms 41420 KB Output is correct
15 Correct 234 ms 41448 KB Output is correct
16 Correct 200 ms 41416 KB Output is correct
17 Correct 220 ms 41268 KB Output is correct
18 Correct 169 ms 41348 KB Output is correct
19 Correct 1038 ms 164364 KB Output is correct
20 Correct 1120 ms 164344 KB Output is correct