# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
20904 | jjwdi0 | Beautiful row (IZhO12_beauty) | C++11 | 0 ms | 1844 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>
#define pcnt __builtin_popcount
using namespace std;
typedef long long ll;
ll D[1<<21][22];
int N, A[22], B[22], C[22];
int f(int x) {
if(x <= 2) return x == 1;
return (x % 3 == 1) + f(x / 3);
}
ll dp(int state, int x) {
if(~D[state][x]) return D[state][x];
D[state][x] = 0;
if(pcnt(state) == 1) return D[state][x] = !!(state & (1 << x));
for(int i=0; i<N; i++) {
if(x == i || !((1 << i) & state)) continue;
if((state & (1 << i)) && (B[x] == B[i] || C[x] == C[i])) D[state][x] += dp(state ^ (1 << x), i);
}
return D[state][x];
}
int main() {
scanf("%d", &N);
for(int i=0; i<N; i++) scanf("%d", A+i);
for(int i=0; i<N; i++) B[i] = pcnt(A[i]);
for(int i=0; i<N; i++) C[i] = f(A[i]);
memset(D, -1, sizeof(D));
ll ans = 0LL;
for(int i=0; i<N; i++) ans += dp((1 << N) - 1, i);
printf("%lld\n", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |