# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
795004 | Blagoj | Beautiful row (IZhO12_beauty) | C++17 | 1 ms | 332 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>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
int n, a[22], base2[22], base3[22];
int Base3(int x) {
int ans = 0;
while (x >= 1) {
ans += (x % 3 == 1);
x /= 3;
}
return ans;
}
int dp[1 << 20][20];
int solve(int mask, int last) {
if (mask == (1 << n) - 1) return dp[mask][last] = 1;
if (dp[mask][last]) return dp[mask][last];
int ans = 0;
for (int i = 0; i < n; i++) {
if (!(mask & (1 << i)) && (base2[i] == base2[last] || base3[i] == base3[last])) {
ans += solve(mask | (1 << i), i);
}
}
return dp[mask][last] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
base2[i] = __builtin_popcount(a[i]);
base3[i] = Base3(a[i]);
}
cout << solve(0, 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |