# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
682099 | Hydrolyzed | Beautiful row (IZhO12_beauty) | C++14 | 79 ms | 164472 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>
using namespace std;
using ll = long long;
const int MxN = 20;
ll dp[1 << MxN][MxN], a[MxN];
int count_three(ll x){
int response = 0;
while(x > 0){
response += (x % 3 == 1);
x /= 3;
}
return response;
}
bool ok(ll a, ll b){
return (__builtin_popcountll(a) == __builtin_popcountll(b) || count_three(a) == count_three(b));
}
void solve(){
memset(dp, 0, sizeof dp);
int n;
scanf("%d", &n);
for(int i=0; i<n; ++i){
scanf("%lld", &a[i]);
dp[1 << i][i] = 1;
}
for(int state=1; state<(1 << n); ++state){
for(int i=0; i<n; ++i){
if(!(state & (1 << i))){
continue;
}
for(int j=0; j<n; ++j){
if(state & (1 << j)){
continue;
}
if(ok(a[i], a[j])){
dp[state ^ (1 << j)][j] += dp[state][i];
}
}
}
}
ll answer = 0ll;
for(int i=0; i<n; ++i){
answer = answer + dp[(1 << n) - 1][i];
}
printf("%lld", answer);
}
int main(){
int q;
scanf("%d", &q);
while(q--){
solve();
printf("\n");
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |