# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
338492 | beksultan04 | Beautiful row (IZhO12_beauty) | C++14 | 0 ms | 0 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;
#define pii pair<int,int>
#define OK puts("OK");
#define NO puts("NO");
#define YES puts("YES");
#define fr first
#define sc second
#define ret return
#define ll long long
#define scan1(a) scanf("%lld",&a);
#define scan2(a,b) scanf("%lld %lld",&a, &b);
#define scan3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c);
#define all(s) s.begin(),s.end()
#define allr(s) s.rbegin(),s.rend()
#define pb push_back
#define sz(v) (int)v.size()
#define endi puts("");
const int N = 1048579,INF=1e9+7;
int q[N],tr[30],dv[30],g[50][50],n;
ll dp[N][21];
int troich(int x){
int ans=0;
while (x > 0){
if (x%3==1)ans++;
x /=3;
}
ret ans;
}
int dvoich(int x){
int ans=0;
while (x > 0){
if (x%2==1)ans++;
x /=2;
}
ret ans;
}
bool is(int a,int b){
ret (tr[a] == tr[b] || dv[a] == dv[b]);
}
ll rec(int x,int j){
if (x == (1<<n)-1)ret 1;
ll &ret=dp[x][j];
if (~ret)
ret ret;ret=0;
int i;
for (i=0;i<n;++i){
if (i == j || (x&(1<<i)) || g[i][j] == 0)continue;
ret += rec(x+(1<<i),i);
}
ret ret;
}
main(){
memset(dp,-1,sizeof dp);
int i,j,s,cnt=1;
cin>>n;
for (i=0;i<n;++i){
cin>>q[i];
tr[i]=troich(q[i]);
dv[i]=dvoich(q[i]);
}
for (i=0;i<n;++i){
for (j=i+1;j<n;++j){
if (is(i,j)){
g[i][j]=1;
g[j][i]=1;
}
}
}
ll ans=0;
for (i=0;i<n;++i){
ans+=rec((1<<i),i);
}
cout <<ans;
}