# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
834996 | rahulverma | Beautiful row (IZhO12_beauty) | C++17 | 1936 ms | 189128 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 <iostream>
#include <vector>
#include <cstring>
#include <sstream>
#include <fstream>
using namespace std;
const int N = 100;
long long a[N];
long long b[N];
long long dp[1 << 20][23];
int n;
long long rec(long long mask, int last) {
if (mask == ((1 << n) - 1)) return 1;
if (last != -1 && dp[mask][last] != -1) return dp[mask][last];
long long an = 0;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) == 0) {
if (last == -1 || a[last] == a[i] || b[last] == b[i]) {
an += rec(mask | (1 << i), i);
}
}
}
if (last != -1) dp[mask][last] = an;
return an;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int i;
long long ans = 0;
cin >> n;
for (i = 0; i < n; i++) {
long long p;
cin >> p;
a[i] = __builtin_popcountll(p);
long long p1 = p;
while (p1 > 0) {
if (p1 % 3 == 1) b[i]++;
p1 /= 3;
}
}
memset(dp, -1, sizeof(dp));
ans += rec(0, -1);
cout << ans << "\n";
return 0;
}
long long pow(int i) {
long long ans = 1;
while (i > 0) {
ans *= 2;
i--;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |