Submission #429740

#TimeUsernameProblemLanguageResultExecution timeMemory
429740SuckTinHockBeautiful row (IZhO12_beauty)C++17
100 / 100
1120 ms164364 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int n; int cnt2[25], cnt3[25]; int dp[1 << 20][20]; vector<int> G[25]; int ter(int x) { int res = 0; while (x > 0) { res += (x % 3 == 1); x /= 3; } return res; } void build() { for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { if (cnt2[i] == cnt2[j] || cnt3[i] == cnt3[j]) { G[i].push_back(j); G[j].push_back(i); } } } } void xuly() { build(); int last = (1 << n) - 1; for (int i = 0; i < n; ++i) dp[1 << i][i] = 1; for (int mask = 0; mask <= last; ++mask) { for (int i = 0; i < n; ++i) { if (((mask >> i) & 1) == 0) continue; for (int j : G[i]) { if (((mask >> j) & 1) == 1) continue; int p = mask + (1 << j); dp[p][j] += dp[mask][i]; } } } int res = 0; for (int i = 0; i < n; ++i) res += dp[last][i]; cout << res; } void nhap() { int x; cin >> n; for (int i = 0; i < n; ++i) cin >> x, cnt2[i] = __builtin_popcount(x), cnt3[i] = ter(x); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); nhap(); xuly(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...