답안 #90169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
90169 2018-12-20T17:30:01 Z popovicirobert 아름다운 순열 (IZhO12_beauty) C++14
100 / 100
725 ms 164892 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

const int MAXN = 20;

ll dp[1 << MAXN][MAXN];
int cnt2[MAXN], cnt3[MAXN];

inline int get2(int x) {
    return __builtin_popcount(x);
}

inline int get3(int x) {
    int cnt = 0;
    while(x > 0) {
        cnt += ((x % 3) == 1);
        x /= 3;
    }
    return cnt;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(i = 0; i < n; i++) {
        int x;
        cin >> x;
        cnt2[i] = get2(x);
        cnt3[i] = get3(x);
    }
    for(int conf = 1; conf < (1 << n); conf++) {
        if((conf & (conf - 1)) == 0) {
            int bit = 0;
            while((conf & (1 << bit)) == 0) {
                bit++;
            }
            dp[conf][bit] = 1;
        }
        else {
            vector <int> cur;
            for(int bit = 0; bit < n; bit++) {
                if(conf & (1 << bit)) {
                    cur.push_back(bit);
                }
            }
            int sz = cur.size();
            for(int a = 0; a < sz; a++) {
                for(int b = 0; b < sz; b++) {
                    if(a != b && (cnt2[cur[a]] == cnt2[cur[b]] || cnt3[cur[a]] == cnt3[cur[b]])) {
                        dp[conf][cur[a]] += dp[conf ^ (1 << cur[a])][cur[b]];
                    }
                }
            }
        }
    }
    ll ans = 0;
    for(i = 0; i < n; i++) {
        ans += dp[(1 << n) - 1][i];
    }
    cout << ans;
    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 508 KB Output is correct
3 Correct 2 ms 508 KB Output is correct
4 Correct 2 ms 508 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 648 KB Output is correct
7 Correct 2 ms 652 KB Output is correct
8 Correct 2 ms 656 KB Output is correct
9 Correct 2 ms 656 KB Output is correct
10 Correct 2 ms 784 KB Output is correct
11 Correct 9 ms 3180 KB Output is correct
12 Correct 9 ms 3180 KB Output is correct
13 Correct 34 ms 10804 KB Output is correct
14 Correct 154 ms 41584 KB Output is correct
15 Correct 157 ms 41608 KB Output is correct
16 Correct 170 ms 41624 KB Output is correct
17 Correct 187 ms 41696 KB Output is correct
18 Correct 199 ms 41696 KB Output is correct
19 Correct 725 ms 164892 KB Output is correct
20 Correct 670 ms 164892 KB Output is correct