Submission #443795

# Submission time Handle Problem Language Result Execution time Memory
443795 2021-07-12T06:24:57 Z JovanB Beautiful row (IZhO12_beauty) C++17
100 / 100
768 ms 205528 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
bool check(int a, int b){
    int x = a, y = b;
    int b1 = 0, b2 = 0;
    while(x){
        b1 += (x%2 == 1);
        x /= 2;
    }
    while(y){
        b2 += (y%2 == 1);
        y /= 2;
    }
    if(b1 == b2) return 1;
    x = a, y = b, b1 = 0, b2 = 0;
    while(x){
        b1 += (x%3 == 1);
        x /= 3;
    }
    while(y){
        b2 += (y%3 == 1);
        y /= 3;
    }
    return b1 == b2;
}
 
bool moze[25][25];
ll dp[1500005][25];
ll a[25];
 
int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;
 
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    for(int i=0; i<n; i++){
        dp[1<<i][i] = 1;
        for(int j=0; j<n; j++){
            moze[i][j] = check(a[i], a[j]);
        }
    }
    for(int mask=0; mask<(1<<n); mask++){
        for(int i=0; i<n; i++){
            if(!(mask&(1<<i))) continue;
            for(int j=0; j<n; j++){
                if(mask&(1<<j)) continue;
                dp[mask|(1<<j)][j] += dp[mask][i]*moze[i][j];
            }
        }
    }
    ll res = 0;
    for(int i=0; i<n; i++) res += dp[(1<<n)-1][i];
    cout << res;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 8 ms 3528 KB Output is correct
12 Correct 8 ms 3516 KB Output is correct
13 Correct 35 ms 13040 KB Output is correct
14 Correct 174 ms 51552 KB Output is correct
15 Correct 163 ms 51584 KB Output is correct
16 Correct 170 ms 51604 KB Output is correct
17 Correct 167 ms 51480 KB Output is correct
18 Correct 170 ms 51524 KB Output is correct
19 Correct 767 ms 205508 KB Output is correct
20 Correct 768 ms 205528 KB Output is correct