답안 #336131

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
336131 2020-12-14T19:13:11 Z tevdore 아름다운 순열 (IZhO12_beauty) C++14
100 / 100
911 ms 205804 KB
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int const N = 1 << 23;
int n;
ll a[25], dp[N][25], F1[25], F2[25];
ll ans;
int bin(ll x) {
	int r = 0;
	while(x) {
		r += x % 2;
		x /= 2;
	}
	return r;
}
int ter(ll x) {
	int r = 0;
	while(x) {
		r += (x % 3 == 1);
		x /= 3;
	}
	return r;
}
main() {
	ios::sync_with_stdio(0);
	cin.tie();
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	int mask = 1 << n;
	mask--;
	for(int i = 1; i <= n; i++) {
		F1[i] = bin(a[i]);
		F2[i] = ter(a[i]);
	}
	for(int i = 1; i <= mask; i++) {
		for(int j = 0; j < n; j++) {
			if(i >> j & 1) {
				int x = i ^ (1 << j);
				if(!x) {
					dp[i][j + 1] = 1;
					continue;
				}
				for(int z = 0; z < n; z++) {
					if((x >> z) & 1) {
						bool f1 = false, f2 = false;
						f1 = (F1[j + 1] == F1[z + 1]);
						f2 = (F2[j + 1] == F2[z + 1]);
						if(f1 || f2) dp[i][j + 1] += dp[x][z + 1];
					}
				}
			}
		}
	}
	for(int j = 0; j < n; j++) ans += dp[mask][j + 1];
	cout << ans << "\n";
}

Compilation message

beauty.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   24 | main() {
      |      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 0 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 10 ms 3564 KB Output is correct
12 Correct 9 ms 3564 KB Output is correct
13 Correct 40 ms 13164 KB Output is correct
14 Correct 195 ms 51692 KB Output is correct
15 Correct 178 ms 51820 KB Output is correct
16 Correct 202 ms 51708 KB Output is correct
17 Correct 204 ms 51676 KB Output is correct
18 Correct 216 ms 51628 KB Output is correct
19 Correct 911 ms 205804 KB Output is correct
20 Correct 813 ms 205548 KB Output is correct