Submission #795016

#TimeUsernameProblemLanguageResultExecution timeMemory
795016BlagojBeautiful row (IZhO12_beauty)C++17
100 / 100
1590 ms164544 KiB
#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 timeMemoryGrader output
Fetching results...