# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
41134 | wzy | Beautiful row (IZhO12_beauty) | C++11 | 3056 ms | 145132 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 F first
#define pb push_back
#define S second
#define pii pair<int,int>
#define int long long
int n;
vector<int> v;
vector<int> adj[30];
int dp[21][1LL<<20];
bool vis[21][1LL<<20];
pair<int,int> f(int x){
int a1 = 0 , a2 = 0;
int z =x;
while(z > 0){
if(z%2 == 1) a1++;
z/= 2;
}
z = x;
while(z > 0){
if(z%3 == 1) a2++;
z/=3;
}
return pair<int,int>(a1, a2);
}
int solve(int i , int mask){
if(vis[i][mask]) return dp[i][mask];
vis[i][mask] = true;
int maxxi = 1<<n;
maxxi--;
if(mask == maxxi) return dp[i][mask] = 1;
for(int j = 0 ; j < adj[i].size() ; j++){
int v = adj[i][j];
if(mask & 1<<v) continue;
dp[i][mask] += solve(v , 1<<v | mask);
}
return dp[i][mask];
}
int32_t main(){
cin>>n;
v.resize(n);
for(int i = 0 ; i < n ;i++) cin>>v[i];
for(int i = 0 ; i <n; i++){
for(int j = i + 1 ; j < n;j++){
if(f(v[i]).F == f(v[j]).F || f(v[i]).S ==f(v[j]).S){
adj[i].pb(j);
adj[j].pb(i);
}
}
}
int ans = 0;
for(int i = 0 ; i < n ; i++){
ans+= solve(i , 1<<i);
}
cout<<ans<<endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |