Submission #336125

# Submission time Handle Problem Language Result Execution time Memory
336125 2020-12-14T19:00:28 Z nikatamliani Beautiful row (IZhO12_beauty) C++14
100 / 100
1048 ms 164460 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
#define ll long long
int n, a[N], binary[N], ternary[N];
ll dp[1 << N][N];
int main() {
    cin >> n; 
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
        binary[i] = __builtin_popcount(a[i]);
        for(int p = a[i]; p > 0; p /= 3) {
            if((p % 3) == 1) {
                ++ternary[i];
            }
        }
    }
    for(int mask = 0; mask < (1 << n); ++mask) {
        for(int i = 0; i < n; ++i) {
            if(mask >> i & 1) {
                int s_mask = mask ^ (1 << i);
                if(s_mask == 0) {
                    dp[mask][i] = 1;
                    continue;
                }
                for(int j = 0; j < n; ++j) {
                    if(s_mask >> j & 1) {
                        if(ternary[i] == ternary[j] || binary[i] == binary[j]) {
                            dp[mask][i] += dp[s_mask][j];
                        }
                    }
                }
            }
        }  
    }
    ll ans = 0; 
    for(int i = 0; i < n; ++i) {
        ans += dp[(1 << n) - 1][i];
    }
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 12 ms 2924 KB Output is correct
12 Correct 10 ms 2924 KB Output is correct
13 Correct 48 ms 10604 KB Output is correct
14 Correct 223 ms 41324 KB Output is correct
15 Correct 228 ms 41324 KB Output is correct
16 Correct 235 ms 41324 KB Output is correct
17 Correct 233 ms 41452 KB Output is correct
18 Correct 233 ms 41304 KB Output is correct
19 Correct 1048 ms 164460 KB Output is correct
20 Correct 1002 ms 164460 KB Output is correct