給定n個整數(數字可能重複),求在這些數中選取任意個,使得他們的異或和最大。ios
\(1\leq n\leq50, 0\leq a_i\leq 2^{50}\)spa
顯然重複不重複沒有什麼區別。code
直接套線性基板子。ci
#include <iostream> using namespace std; typedef long long type; const int W = 51; type basis[W + 1]; void ins(type x) { for (int i = W; i >= 1; i--) { if (x >> (i - 1)) { cerr << i << endl; if (basis[i] == 0) { basis[i] = x; return; } x ^= basis[i]; } } } type maxxor() { type ans = 0; for (int i = W; i >= 1; i--) { ans = max(ans, ans ^ basis[i]); } return ans; } int n; type x; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; ins(x); cerr << i << endl; } cout << maxxor() << endl; }