답안 #382261

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
382261 2021-03-26T20:58:06 Z knightron0 아름다운 순열 (IZhO12_beauty) C++14
100 / 100
1142 ms 172780 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define int long long int
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double

const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 25;



signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif
    int n;
    cin>>n;
    int a[n], b[n], t[n];
    for(int i= 0;i<n;i++){
    	b[i] = 0;
    	t[i] = 0;
    	cin>>a[i];
    	int x = a[i];
    	while(x){
			t[i] += ((x%3)==1);
			x /= 3;
		}
		x = a[i];
		while(x){
			b[i] += ((x%2)==1);
			x /= 2;
		}
    }
    int dp[n+2][(1<<n)+2];
	for(int mask = 0;mask<(1<<n);mask++){
		for(int i= 0;i<=n;i++){
			dp[i][mask] = 0;
		}
	}
    for(int i = 0;i<n;i++) {
    	dp[i][(1<<i)] = 1;
    }
    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)) || (i == j)) continue;
    			if(b[i] == b[j] || t[i] == t[j]){
    				dp[i][mask] += dp[j][mask^(1<<i)];
    			}
    		}
    	}
    }
    int ans = 0;
    for(int i= 0;i<n;i++){
    	ans += dp[i][(1<<n)-1];
    }
    cout<<ans<<endl;
    return 0;
}



# 결과 실행 시간 메모리 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 2 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 11 ms 2284 KB Output is correct
12 Correct 11 ms 2284 KB Output is correct
13 Correct 46 ms 9068 KB Output is correct
14 Correct 226 ms 39404 KB Output is correct
15 Correct 210 ms 39404 KB Output is correct
16 Correct 227 ms 39384 KB Output is correct
17 Correct 235 ms 39276 KB Output is correct
18 Correct 233 ms 39276 KB Output is correct
19 Correct 1142 ms 172780 KB Output is correct
20 Correct 1075 ms 172764 KB Output is correct