Submission #37245793


Source Code Expand

/*
    Template Version: 1.0.0 - 20220620
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define TRAV(x, a) for (auto &x : a)

using namespace std;
using ll = long long; using ld = double; 
using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<ld, ld>;
using cd = complex<ld>; using vcd = vector<cd>;

using vi = vector<int>; using vl = vector<ll>;
using vd = vector<ld>; using vs = vector<string>;
using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; // vector<pair>

template<class T> using min_pq = priority_queue<T, vector<T>, greater<T> >;
template<class T> inline int ckmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; }
template<class T> inline int ckmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sort(ALL(v)); v.erase(unique(ALL(v)), end(v)); }

constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x)) 
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }

ll ceilDiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
ll floorDiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
void setPrec(int x) { cout << fixed << setprecision(x); }

// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string) s; }
string ts(string s) { return s; }
string ts(bool b) { return (b ? "true" : "false"); }

template<class T> using V = vector<T>;
template<class T> string ts(complex<T> c);
string ts(V<bool> v);
template<size_t sz> string ts(bitset<sz> b);
template<class T> string ts(T v);
template<class T, class U> string ts(pair<T,U> p);
template<class ...U> string ts(tuple<U...> u);

template<class T> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); }
string ts(V<bool> v) {string res = "{"; FOR(i,0,SZ(v)-1) res += char('0'+v[i]); res += "}"; return res; }
template<size_t sz> string ts(bitset<sz> b) { string res = ""; FOR(i,0,SZ(b)-1) res += char('0'+b[i]); return res; }
template<class T> string ts(T v) { // containers with begin(), end()
    bool fst = 1; string res = "";
    for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); }
    return res;
}
template<class T, class U> string ts(pair<T,U> p) { return "(" + ts(p.FI) + ", " + ts(p.SE) + ")"; }
template<size_t i, class T> string print_tuple_utils(const T& tup) { if constexpr(i == tuple_size<T>::value) return ")"; else return (i ? ", " : "(") + ts(get<i>(tup)) + print_tuple_utils<i + 1, T>(tup); }
template<class ...U> string ts(tuple<U...> u) { return print_tuple_utils<0, tuple<U...>>(u); }

// OUTPUT
template<class T> void pr(T x) { cout << ts(x); }
template<class T, class ...U> void pr(const T& t, const U&... u) { pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
template<class T, class ...U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
template<class T, class ...U> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); }

#ifdef LOCAL_DEBUG
#define CONCAT(x, y) x##y
#define with_level setw(__db_level * 2) << setfill(' ') << "" << setw(0)
#define dbg(...) cerr << with_level << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << setw(__db_level * 2) << setfill(' ') << "" << setw(0) << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#define db_block() debug_block CONCAT(dbbl, __LINE__)
int __db_level = 0;
struct debug_block {
    debug_block() { cerr << with_level << "{" << endl; ++__db_level; }
    ~debug_block() { --__db_level; cerr << with_level << "}" << endl; }
};
#else
#define dbg(...) 0
#define chk(...) 0
#define db_block() 0
#endif

const ld PI = acos(-1.0);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const ld EPS = 1e-9;
const ll MODBASE = 1000000007LL;
const int INF = 0x3f3f3f3f;

const int MAXN = 110;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

struct VectorBasis {
    vl basis;
    int d, sz;
    int numVectors;

    VectorBasis(int d) {
        this->d = d;
        this->sz = 0;

        // numVectors is the number of vectors inserted
        this->numVectors = 0;
        basis.resize(d);
        FOR(i,0,d-1) basis[i] = 0;
    }

    void insertVector(ll mask, bool incCnt = true) {
        // insert mask to the basis
        if (incCnt) numVectors++;
        FORE(i,d-1,0) {
            if (!(mask & (1LL << i))) continue;

            if (!basis[i]) {
                basis[i] = mask;
                sz++;
                return;
            }

            mask ^= basis[i];
        }
    }

    bool checkXor(ll mask) {
        // check if mask is representable by the basis
        FOR(i,0,d-1) {
            if (!(mask & (1LL << i))) continue;
            if (!basis[i]) return false;
            mask ^= basis[i];
        }
        return true;
    }

    void merge(VectorBasis &v) {
        // merge 2 basis with the same dimention
        numVectors += v.numVectors;
        FOR(i,0,d-1) {
            if (v.basis[i]) {
                insertVector(v.basis[i], false);
            }
        }
    }

    ll getMax() {
        ll res = 0;
        dbg(basis);
        FORE(i,d-1,0) {
            if (!basis[i]) continue;
            if (res & (1LL << i)) continue;
            res ^= basis[i];
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vl a(n);

    ll xorSum = 0;
    FOR(i,0,n-1) {
        cin >> a[i];
        xorSum ^= a[i];
    }

    FORE(i,59,0) {
        if (xorSum & (1LL << i)) {
            FOR(j,0,n-1) {
                if (a[j] & (1LL << i)) {
                    a[j] -= (1LL << i);
                }
            }
        }
    }

    VectorBasis vb(60);
    FOR(i,0,n-1) {
        vb.insertVector(a[i]);
        dbg(a[i]);
    }
    ll maxXor = vb.getMax();
    dbg(maxXor);
    dbg(xorSum);

    cout << maxXor + (xorSum ^ maxXor);
    return 0;
}

Submission Info

Submission Time
Task F - Xor Sum 3
User farmerboy
Language C++ (GCC 9.2.1)
Score 600
Code Size 6868 Byte
Status AC
Exec Time 52 ms
Memory 3984 KiB

Compile Error

./Main.cpp: In member function ‘ll VectorBasis::getMax()’:
./Main.cpp:91:18: warning: statement has no effect [-Wunused-value]
   91 | #define dbg(...) 0
      |                  ^
./Main.cpp:160:9: note: in expansion of macro ‘dbg’
  160 |         dbg(basis);
      |         ^~~
./Main.cpp: In function ‘int main()’:
./Main.cpp:91:18: warning: statement has no effect [-Wunused-value]
   91 | #define dbg(...) 0
      |                  ^
./Main.cpp:196:9: note: in expansion of macro ‘dbg’
  196 |         dbg(a[i]);
      |         ^~~
./Main.cpp:91:18: warning: statement has no effect [-Wunused-value]
   91 | #define dbg(...) 0
      |                  ^
./Main.cpp:199:5: note: in expansion of macro ‘dbg’
  199 |     dbg(maxXor);
      |     ^~~
./Main.cpp:91:18: warning: statement has no effect [-Wunused-value]
   91 | #define dbg(...) 0
      |                  ^
./Main.cpp:200:5: note: in expansion of macro ‘dbg’
  200 |     dbg(xorSum);
      |     ^~~

Judge Result

Set Name Sample Subtask1
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 53
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
Subtask1 sample_01.txt, sample_02.txt, sample_03.txt, sub1_01.txt, sub1_02.txt, sub1_03.txt, sub1_04.txt, sub1_05.txt, sub1_06.txt, sub1_07.txt, sub1_08.txt, sub1_09.txt, sub1_10.txt, sub1_11.txt, sub1_12.txt, sub1_13.txt, sub1_14.txt, sub1_15.txt, sub1_16.txt, sub1_17.txt, sub1_18.txt, sub1_19.txt, sub1_20.txt, sub1_21.txt, sub1_22.txt, sub1_23.txt, sub1_24.txt, sub1_25.txt, sub1_26.txt, sub1_27.txt, sub1_28.txt, sub1_29.txt, sub1_30.txt, sub1_small_01.txt, sub1_small_02.txt, sub1_small_03.txt, sub1_small_04.txt, sub1_small_05.txt, sub1_small_06.txt, sub1_small_07.txt, sub1_small_08.txt, sub1_small_09.txt, sub1_small_10.txt, sub1_small_11.txt, sub1_small_12.txt, sub1_small_13.txt, sub1_small_14.txt, sub1_small_15.txt, sub1_small_16.txt, sub1_small_17.txt, sub1_small_18.txt, sub1_small_19.txt, sub1_small_20.txt
Case Name Status Exec Time Memory
sample_01.txt AC 8 ms 3496 KiB
sample_02.txt AC 2 ms 3636 KiB
sample_03.txt AC 2 ms 3608 KiB
sub1_01.txt AC 14 ms 3596 KiB
sub1_02.txt AC 18 ms 3492 KiB
sub1_03.txt AC 8 ms 3560 KiB
sub1_04.txt AC 20 ms 3636 KiB
sub1_05.txt AC 41 ms 3868 KiB
sub1_06.txt AC 37 ms 3792 KiB
sub1_07.txt AC 38 ms 3924 KiB
sub1_08.txt AC 39 ms 3916 KiB
sub1_09.txt AC 41 ms 3924 KiB
sub1_10.txt AC 38 ms 3980 KiB
sub1_11.txt AC 35 ms 3896 KiB
sub1_12.txt AC 41 ms 3916 KiB
sub1_13.txt AC 14 ms 3560 KiB
sub1_14.txt AC 30 ms 3956 KiB
sub1_15.txt AC 43 ms 3880 KiB
sub1_16.txt AC 45 ms 3880 KiB
sub1_17.txt AC 42 ms 3952 KiB
sub1_18.txt AC 47 ms 3984 KiB
sub1_19.txt AC 43 ms 3896 KiB
sub1_20.txt AC 45 ms 3880 KiB
sub1_21.txt AC 37 ms 3860 KiB
sub1_22.txt AC 39 ms 3952 KiB
sub1_23.txt AC 42 ms 3856 KiB
sub1_24.txt AC 48 ms 3816 KiB
sub1_25.txt AC 52 ms 3876 KiB
sub1_26.txt AC 47 ms 3808 KiB
sub1_27.txt AC 45 ms 3792 KiB
sub1_28.txt AC 8 ms 3608 KiB
sub1_29.txt AC 2 ms 3548 KiB
sub1_30.txt AC 18 ms 3924 KiB
sub1_small_01.txt AC 2 ms 3608 KiB
sub1_small_02.txt AC 2 ms 3592 KiB
sub1_small_03.txt AC 1 ms 3468 KiB
sub1_small_04.txt AC 2 ms 3604 KiB
sub1_small_05.txt AC 2 ms 3500 KiB
sub1_small_06.txt AC 2 ms 3500 KiB
sub1_small_07.txt AC 2 ms 3516 KiB
sub1_small_08.txt AC 2 ms 3460 KiB
sub1_small_09.txt AC 2 ms 3604 KiB
sub1_small_10.txt AC 3 ms 3596 KiB
sub1_small_11.txt AC 2 ms 3592 KiB
sub1_small_12.txt AC 1 ms 3516 KiB
sub1_small_13.txt AC 2 ms 3648 KiB
sub1_small_14.txt AC 2 ms 3464 KiB
sub1_small_15.txt AC 2 ms 3488 KiB
sub1_small_16.txt AC 2 ms 3604 KiB
sub1_small_17.txt AC 2 ms 3524 KiB
sub1_small_18.txt AC 2 ms 3560 KiB
sub1_small_19.txt AC 2 ms 3488 KiB
sub1_small_20.txt AC 2 ms 3548 KiB