제출 #49864

#제출 시각아이디문제언어결과실행 시간메모리
49864mra2322001아름다운 순열 (IZhO12_beauty)C++14
100 / 100
2786 ms172984 KiB
#include <bits/stdc++.h>
#define f0(i, n) for(int i(0); i<(n); i++)
#define f1(i, n) for(int i(1); i<=(n); i++)
#define bit(x, y) (((x) >> (y))&1)

using namespace std;
typedef long long ll;
const int N = 21;

int n, a[N], t[N], b[N];
vector <int> y[N];
ll f[1 << 20][N];

ll calc(int s, int i){
    if(f[s][i] != -1) return f[s][i];
    if(s == (1 << n) - 1){
        f[s][i] = 1;
        return 1;
    }
    ll &ans = f[s][i];
    ans = 0;
    if(s==0){
        f0(j, n){
            ans = ans + calc(1 << j, j);
        }
    }
    if(s > 0){
        for(auto j:y[i]){
            if(bit(s, j)==0) ans = ans + calc(s | (1 << j), j);
        }
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(0);
   
    memset(f, -1, sizeof(f));
    cin >> n;
    f1(i, n){
        cin >> a[i];
        b[i] = __builtin_popcount(a[i]);
        while(a[i]){
            int r = a[i]%3;
            if(r==1) t[i]++;
            a[i] /= 3;
        }
    }
    f1(i, n){
        f1(j, n){
            if(i != j){
                if(t[i]==t[j] || b[i]==b[j]){
                    y[i - 1].push_back(j - 1);
                }
            }
        }
    }
    cout << calc(0, 0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...