답안 #382251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
382251 2021-03-26T20:27:04 Z knightron0 아름다운 순열 (IZhO12_beauty) C++14
0 / 100
2 ms 364 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;

int n;
int a[MAXN], b[MAXN], t[MAXN], dp[MAXN][2^21];

int check(int i, int j){
	if(b[i] == b[j] || t[i] == t[j]) return true;
	return false;
}

int f(int i, int mask){
	for(int j = 0;j<n;j++){
		if(mask == (1<<j)){
			return 1;
		}
	}
	if(dp[i][mask] != -1){
		return dp[i][mask];
	}
	int res = 0;
	for(int j= 0;j<n;j++){
		if(!(mask & (1<<j)) || (j == i)) continue;
		if(check(i, j)){
			res += f(j, mask ^ (1<<i));
		}
	}
	return dp[i][mask] = res;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    #endif
    cin>>n;
    clr(b, 0);
    clr(t, 0);
    clr(dp, -1);
    for(int i= 0;i<n;i++){
    	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 ans = 0;
    for(int i= 0;i<n;i++){
    	ans += f(i, (1<<n)-1);
    }
    cout<<ans<<endl;
    return 0;
}



# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 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 Incorrect 1 ms 364 KB Output isn't correct
7 Halted 0 ms 0 KB -