답안 #315661

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
315661 2020-10-23T15:06:19 Z jovan_b 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
773 ms 205688 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

bool check(int a, int b){
    int x = a, y = b;
    int b1 = 0, b2 = 0;
    while(x){
        b1 += (x%2 == 1);
        x /= 2;
    }
    while(y){
        b2 += (y%2 == 1);
        y /= 2;
    }
    if(b1 == b2) return 1;
    x = a, y = b, b1 = 0, b2 = 0;
    while(x){
        b1 += (x%3 == 1);
        x /= 3;
    }
    while(y){
        b2 += (y%3 == 1);
        y /= 3;
    }
    return b1 == b2;
}

bool moze[25][25];
ll dp[1500005][25];
ll a[25];

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    for(int i=0; i<n; i++){
        dp[1<<i][i] = 1;
        for(int j=0; j<n; j++){
            moze[i][j] = check(a[i], a[j]);
        }
    }
    for(int mask=0; mask<(1<<n); mask++){
        for(int i=0; i<n; i++){
            if(!(mask&(1<<i))) continue;
            for(int j=0; j<n; j++){
                if(mask&(1<<j)) continue;
                dp[mask|(1<<j)][j] += dp[mask][i]*moze[i][j];
            }
        }
    }
    ll res = 0;
    for(int i=0; i<n; i++) res += dp[(1<<n)-1][i];
    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 8 ms 3584 KB Output is correct
12 Correct 9 ms 3584 KB Output is correct
13 Correct 37 ms 13184 KB Output is correct
14 Correct 170 ms 51636 KB Output is correct
15 Correct 174 ms 51576 KB Output is correct
16 Correct 175 ms 51580 KB Output is correct
17 Correct 173 ms 51576 KB Output is correct
18 Correct 172 ms 51580 KB Output is correct
19 Correct 773 ms 205688 KB Output is correct
20 Correct 771 ms 205604 KB Output is correct