# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
715891 | mychecksedad | Beautiful row (IZhO12_beauty) | C++17 | 831 ms | 205512 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.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 25, M = 1e5+10, K = 20;
ll n, a[N], b[N][N], bin[N], ter[N], dp[1<<K][N];
void solve(){
cin >> n;
for(int i = 0; i < n; ++i) cin >> a[i];
for(int i = 0; i < n; ++i) bin[i] = __builtin_popcount(a[i]);
for(int i = 0; i < n; ++i){
ter[i] = 0;
int v = a[i];
while(v > 0){
if(v % 3 == 1) ter[i]++;
v /= 3;
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
b[i][j] = (bin[i] == bin[j] || ter[i] == ter[j]);
}
}
for(int i = 0; i < (1<<n); ++i){
for(int j = 0; j < n; ++j) dp[i][j] = 0;
if(__builtin_popcount(i) == 1){
for(int j = 0; j < n; ++j){
if((1<<j)&i)
dp[i][j] = 1;
}
}
}
for(int mask = 1; mask < (1<<n); ++mask){
for(int j = 0; j < n; ++j){
if(!((1<<j)&mask)){
for(int k = 0; k < n; ++k){
if((1<<k)&mask){
if(b[j][k] == 1)
dp[mask ^ (1<<j)][j] += dp[mask][k];
}
}
}
}
}
// for(int i =0 ; i < (1<<n); ++i){
// for(int j = 0; j < n; ++j){
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
ll ans = 0;
for(int i = 0; i < n; ++i) ans += dp[(1<<n) - 1][i];
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |