제출 #716474

#제출 시각아이디문제언어결과실행 시간메모리
716474vjudge1아름다운 순열 (IZhO12_beauty)C++17
0 / 100
728 ms164544 KiB
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
struct segment{int l, r, id;
int size(){return r-l+1;}};
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"

const int maxn = 1e6 + 1;
const ll INF = (1ll<<61);
const int MOD = 1e9 + 7;
const int inf = (1<<30);
const int maxl = 20;
const int P = 31;

int n;
int a[maxn];
int b[maxn];
ll dp[maxn][maxl];

void test(){
    cin >> n;
    for(int i = 0; i < n; i++){
        int x; cin >> x;
        a[i] = __builtin_popcount(x);
        while(x){
            b[i] += (x % 3) == 1;
            x /= 3;
        }
    }
    for(int mask = 1; mask < (1<<n); mask++){
        if(__builtin_popcount(mask) == 1){
            for(int i = 0; i < n; i++){
                if(mask & (1<<i)) dp[mask][i] = 1;
            }
        } else{
            for(int i = 0; i < n; i++){
                if(!(mask & (1<<i))) continue;
                for(int j = 0; j < n; j++){
                    if(j == i) continue;
                    if(!(mask & (1<<j))) continue;
                    if(a[i] != a[j] && b[i] != b[j]) continue;
                    dp[mask][i] += dp[mask ^ (1<<i)][j];
                }
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += dp[(1<<n) - 1][i];
    }
    cout << ans;
}

int main(){
    ios_base::sync_with_stdio(0); 
    cin.tie(0), cout.tie(0);
    int t; t = 1;
    while(t--) test();
}
#Verdict Execution timeMemoryGrader output
Fetching results...