답안 #382242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
382242 2021-03-26T19:32:07 Z ritul_kr_singh 아름다운 순열 (IZhO12_beauty) C++17
100 / 100
604 ms 164588 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    int n, x, y; cin >> n;
    int b2[n], b3[n];
    for(int i=0; i<n; ++i){
    	b2[i] = b3[i] = 0;
    	cin >> x;
    	y = x;
    	while(y) b2[i] += (y % 2)==1, y /= 2;
    	while(x) b3[i] += (x % 3)==1, x /= 3;
    }

    bool d[n][n];
    for(int i=0; i<n; ++i)
    	for(int j=0; j<n; ++j)
    		d[i][j] = b2[i]==b2[j] or b3[i]==b3[j];

    int ans[1<<n][n];
    for(int i=0; i<(1<<n); ++i){
    	for(int j=0; j<n; ++j){
    		ans[i][j] = 0;
    		if(i==(1<<j)) ans[i][j] = 1;
    	}
    	for(int j=0; j<n; ++j){
    		for(int k=j+1; k<n; ++k){
    			if(!(i&(1<<j)) or !(i&(1<<k)) or j==k) continue;
    			if(d[j][k]){
    				ans[i][j] += ans[i^(1<<j)][k];
    				ans[i][k] += ans[i^(1<<k)][j];
    			}
    		}
    	}
    }
    cout << accumulate(ans[(1<<n)-1], ans[(1<<n)-1]+n, 0LL);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 6 ms 2156 KB Output is correct
12 Correct 6 ms 2156 KB Output is correct
13 Correct 28 ms 8556 KB Output is correct
14 Correct 129 ms 37356 KB Output is correct
15 Correct 123 ms 37228 KB Output is correct
16 Correct 137 ms 37356 KB Output is correct
17 Correct 137 ms 37356 KB Output is correct
18 Correct 133 ms 37356 KB Output is correct
19 Correct 604 ms 164548 KB Output is correct
20 Correct 592 ms 164588 KB Output is correct