# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
339835 | nandonathaniel | Beautiful row (IZhO12_beauty) | C++14 | 3063 ms | 164664 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;
const int MAXN=20;
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
int n;
long long dp[1<<MAXN][MAXN];
int a[MAXN];
int binary(int x){
return __builtin_popcount(x);
}
int ternary(int x){
int ret=0;
while(x>0){
if(x%3==1)ret++;
x/=3;
}
return ret;
}
long long DP(int mask,int last){
if(mask==((1<<n)-1))return 1;
long long &ret=dp[mask][last];
if(ret!=-1)return ret;
ret=0;
for(int i=0;i<n;i++){
if(!(mask & (1<<i))){
if(binary(a[last])==binary(a[i]) || ternary(a[last])==ternary(a[i]))ret+=DP(mask+(1<<i),i);
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i=0;i<n;i++)cin >> a[i];
memset(dp,-1,sizeof(dp));
long long ans=0;
for(int i=0;i<n;i++)ans+=DP(1<<i,i);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |