# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
795016 | Blagoj | Beautiful row (IZhO12_beauty) | C++17 | 1590 ms | 164544 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()
ll n, a[22], base2[22], base3[22];
ll Base3(ll x) {
ll ans = 0;
while (x > 0) {
ans += (x % 3 == 1);
x /= 3;
}
return ans;
}
ll dp[1 << 20][20];
ll solve(ll mask, ll last) {
if (dp[mask][last] != -1) return dp[mask][last];
if (mask == (1 << n) - 1) return dp[mask][last] = 1;
ll ans = 0;
for (int i = 0; i < n; i++) {
if (!(mask & (1 << i)) && (mask == 0 || 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]);
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |