답안 #716476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
716476 2023-03-30T06:57:48 Z vjudge1 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
765 ms 164388 KB
#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 = 2e6 + 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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 7 ms 2900 KB Output is correct
12 Correct 9 ms 2848 KB Output is correct
13 Correct 34 ms 10556 KB Output is correct
14 Correct 154 ms 41300 KB Output is correct
15 Correct 173 ms 41260 KB Output is correct
16 Correct 166 ms 41264 KB Output is correct
17 Correct 171 ms 41324 KB Output is correct
18 Correct 165 ms 41416 KB Output is correct
19 Correct 765 ms 164388 KB Output is correct
20 Correct 721 ms 164368 KB Output is correct