# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163509 | tselmegkh | Beautiful row (IZhO12_beauty) | C++14 | 2839 ms | 164652 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;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
vector<int> binary[33], trinary[33];
vector<int> a;
int n;
inline int count(int x){
int res = 0;
while(x > 0){
if(x % 3 == 1){
res++;
}
x /= 3;
}
return res;
}
int cnttri[20];
long long dp[(1 << 20)][20];
inline long long solve(int mask, int last){
if(mask == ((1 << n) - 1)){
return 1;
}
if(dp[mask][last] != -1)return dp[mask][last];
long long ans = 0;
if(mask == 0){
for(int j = 0; j < n; j++){
int curmask = mask | (1 << j);
ans += solve(curmask, j);
}
}
else{
int binaryones = __builtin_popcount(a[last]), trinaryones = cnttri[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(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
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);
cnttri[i] = cnt3;
}
memset(dp, -1, sizeof dp);
cout << solve(0, 0) << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |