#include<bits/stdc++.h>
using namespace std;
vector<int> binary[33], trinary[33];
vector<int> a;
int n;
bool same[25], block[25];
int count(int x){
int res = 0;
while(x > 0){
if(x % 3 == 1){
res++;
}
x /= 3;
}
return res;
}
int dp[(1 << 21)][21];
int solve(int mask, int last){
if(mask == ((1 << n) - 1)){
return 1;
}
int ans = 0;
if(mask == 0){
for(int j = 0; j < n; j++){
int curmask = mask | (1 << j);
ans += solve(curmask, j);
}
}
else{
if(dp[mask][last] != -1)return dp[mask][last];
int binaryones = __builtin_popcount(a[last]), trinaryones = count(a[last]);
for(int j = 0; j < binary[binaryones].size(); j++){
int k = binary[binaryones][j];
if(mask & (1 << k))continue;
int curmask = mask | (1 << k);
ans += solve(curmask, k);
}
for(int j = 0; j < trinary[trinaryones].size(); j++){
int k = trinary[trinaryones][j];
if(mask & (1 << k))continue;
int curmask = mask | (1 << k);
if(!(binaryones == __builtin_popcount(a[k])))ans += solve(curmask, k);
}
}
return dp[mask][last] = ans;
}
int main(){
cin >> n;
a.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i];
int cnt3 = count(a[i]), cnt2 = __builtin_popcount(a[i]);
binary[cnt2].push_back(i);
trinary[cnt3].push_back(i);
}
memset(dp, -1, sizeof dp);
cout << solve(0, -1) << '\n';
}
Compilation message
beauty.cpp: In function 'int solve(int, int)':
beauty.cpp:35:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < binary[binaryones].size(); j++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < trinary[trinaryones].size(); j++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp: In function 'int main()':
beauty.cpp:48:22: warning: array subscript is below array bounds [-Warray-bounds]
return dp[mask][last] = ans;
~~~~~~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
172648 KB |
Output is correct |
2 |
Correct |
216 ms |
172748 KB |
Output is correct |
3 |
Correct |
159 ms |
172636 KB |
Output is correct |
4 |
Correct |
340 ms |
172664 KB |
Output is correct |
5 |
Correct |
148 ms |
172636 KB |
Output is correct |
6 |
Correct |
148 ms |
172640 KB |
Output is correct |
7 |
Correct |
149 ms |
172604 KB |
Output is correct |
8 |
Correct |
150 ms |
172764 KB |
Output is correct |
9 |
Correct |
149 ms |
172752 KB |
Output is correct |
10 |
Correct |
150 ms |
172692 KB |
Output is correct |
11 |
Incorrect |
167 ms |
172664 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |