題解「2017 山東一輪集訓 Day1 / SDWC2018 Day1」Set
阿新 • • 發佈:2020-09-18
題目大意
給出一個長度為 \(n\) 的陣列,選出一些數異或之和為 \(s1\),其餘數異或之和為 \(s2\),求 \(s1+s2\) 最大時 \(s1\) 的最小值。
思路
你發現如果你設 \(s\) 為所有數的異或和,那麼如果 \(s\) 某一位為 \(0\) 就可以拆成\(1\oplus 1\),不同就只能拆成 \(0\oplus 1\),所以我們應該多拆 \(0\) ,這個用線性基實現即可。
\(\texttt{Code}\)
#include <bits/stdc++.h> using namespace std; #define Int register int #define ll long long #define MAXN 100005 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;} template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);} template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} int n,tot,b[63]; ll s,s2,a[MAXN],p[63]; void ins (ll x){ for (Int i = 1;i <= tot;++ i) if (x & (1ll << b[i])){ if (!p[i]){p[i] = x;break;} else x ^= p[i]; } } signed main(){ read (n); for (Int i = 1;i <= n;++ i) read (a[i]),s ^= a[i]; for (Int i = 62;~i;-- i) if (!(s >> i & 1)) b[++ tot] = i; for (Int i = 62;~i;-- i) if (s >> i & 1) b[++ tot] = i; for (Int i = 1;i <= n;++ i) ins (a[i]); for (Int i = 1;i <= tot;++ i) if (!(s2 & (1ll << b[i]))) s2 ^= p[i]; write (s ^ s2),putchar ('\n'); return 0; }