답안 #968800

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968800 2024-04-24T06:11:38 Z 12345678 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
838 ms 133580 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int nx=21, kx=(1<<20)+5;

ll n, a[nx], dp[nx][kx], x[nx], y[nx], res;
vector<int> d[nx];

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=0; i<n; i++)
    {
        cin>>a[i];
        int tmp=a[i];
        while (tmp>0) x[i]+=((tmp%2)==1), tmp/=2;
        tmp=a[i];
        while (tmp>0) y[i]+=((tmp%3)==1), tmp/=3;
    }
    for (int i=0; i<n; i++) for (int j=i+1; j<n; j++) if (x[i]==x[j]||y[i]==y[j]) d[i].push_back(j), d[j].push_back(i); //cout<<"edge "<<i<<' '<<j<<'\n';
    for (int i=1; i<(1<<n); i++)
    {
        for (int u=0; u<n; u++)
        {
            if (!(i&(1<<u))) continue;
            if (__builtin_popcount(i)==1)
            {
                dp[u][i]=1;
                continue;
            }
            for (auto v:d[u])
            {
                if (i&(1<<v))
                {
                    dp[u][i]+=dp[v][i^(1<<u)];
                }
            }
            //cout<<"debug "<<i<<' '<<u<<' '<<dp[u][i]<<'\n';
        }
    }
    for (int i=0; i<n; i++) res+=dp[i][(1<<n)-1];
    cout<<res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 2 ms 18780 KB Output is correct
8 Correct 4 ms 18920 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 4 ms 18780 KB Output is correct
11 Correct 12 ms 27216 KB Output is correct
12 Correct 9 ms 27228 KB Output is correct
13 Correct 40 ms 48052 KB Output is correct
14 Correct 161 ms 60572 KB Output is correct
15 Correct 169 ms 60496 KB Output is correct
16 Correct 134 ms 60496 KB Output is correct
17 Correct 143 ms 60120 KB Output is correct
18 Correct 117 ms 60236 KB Output is correct
19 Correct 758 ms 133460 KB Output is correct
20 Correct 838 ms 133580 KB Output is correct