Submission #673277

# Submission time Handle Problem Language Result Execution time Memory
673277 2022-12-20T05:20:12 Z Alihan_8 Beautiful row (IZhO12_beauty) C++17
100 / 100
868 ms 205552 KB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector <int> p(n);
    for ( auto &I: p ) cin >> I;
    vector <array<int,2>> bits(n);
    for ( int i = 0; i < n; i++ ){
        int cnt = 0, val = p[i];
        while ( val ){
            cnt += val & 1;
            val >>= 1;
        }
        val = p[i];
        bits[i][0] = cnt;
        cnt = 0;
        while ( val ){
            cnt += val%3 == 1;
            val /= 3;
        }
        bits[i][1] = cnt;
    }
    const int S = 1 << n;
    vector <vector<int>> dp(S, vector <int> (n));
    auto same = [&](int i, int j){
        for ( int k = 0; k < 2; k++ ){
            if ( bits[i][k] == bits[j][k] ) return true;
        }
        return false;
    };
    for ( int i = 0; i < n; i++ ) dp[1 << i][i] = 1;
    for ( int mask = 1; mask < S; mask++ ){
        for ( int i = 0; i < n; i++ ){
            if ( mask >> i & 1 ){
                for ( int j = 0; j < n; j++ ){
                    if ( mask >> j & 1 ) continue;
                    int v = mask | (1 << j);
                    if ( !same(i, j) ) continue;
                    dp[v][j] += dp[mask][i];
                }
            }
        }
    }
    int cnt = 0;
    for ( int i = 0; i < n; i++ ) cnt += dp[S-1][i];
    cout << cnt;

    cout << '\n';
}

Compilation message

beauty.cpp: In function 'void IO(std::string)':
beauty.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 11 ms 2772 KB Output is correct
12 Correct 8 ms 2752 KB Output is correct
13 Correct 37 ms 11092 KB Output is correct
14 Correct 169 ms 47444 KB Output is correct
15 Correct 159 ms 47520 KB Output is correct
16 Correct 189 ms 47512 KB Output is correct
17 Correct 204 ms 47512 KB Output is correct
18 Correct 177 ms 47508 KB Output is correct
19 Correct 868 ms 205516 KB Output is correct
20 Correct 759 ms 205552 KB Output is correct