# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1112209 | Kirill22 | 아름다운 순열 (IZhO12_beauty) | C++17 | 796 ms | 172880 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n, vector<int> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (__builtin_popcount(a[i]) == __builtin_popcount(a[j])) {
g[i][j] = 1;
}
int x = a[i], y = a[j], s = 0;
while (x > 0) {
s += x % 3 == 1;
x /= 3;
}
while (y > 0) {
s -= y % 3 == 1;
y /= 3;
}
if (s == 0) {
g[i][j] = 1;
}
}
}
vector<vector<long long>> dp(n, vector<long long> (1 << n));
for (int i = 0; i < n; i++) {
dp[i][1 << i] = 1;
}
for (int msk = 0; msk < (1 << n); msk++) {
for (int v = 0; v < n; v++) {
for (int u = 0; u < n; u++) {
if (msk >> v & 1) {
if (!(msk >> u & 1)) {
if (g[v][u]) {
dp[u][msk + (1 << u)] += dp[v][msk];
}
}
}
}
}
}
long long ans = 0;
for (int v = 0; v < n; v++) {
ans += dp[v].back();
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |