# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
21875 | ulna | Beautiful row (IZhO12_beauty) | C++11 | 1033 ms | 165860 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
// why am I so weak
int n;
int a[25];
long long dp[1 << 20][20];
bool adj[25][25];
inline bool gao(int x, int y, int t) {
int a = 0;
while (x > 0) {
if (x % t == 1) a++;
x /= t;
}
while (y > 0) {
if (y % t == 1) a--;
y /= t;
}
return a == 0;
}
bool trial(int x, int y) {
return gao(x, y, 2) || gao(x, y, 3);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
dp[1 << i][i] = 1;
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
adj[i][j] = trial(a[i], a[j]);
}
for (int mask = 0; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) if (dp[mask][i] > 0) {
for (int j = 0; j < n; j++) if (!(mask & (1 << j)) && adj[i][j]) {
dp[mask | (1 << j)][j] += dp[mask][i];
}
}
}
long long res = 0ll;
for (int i = 0; i < n; i++) res += dp[(1 << n) - 1][i];
printf("%lld\n", res);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |