From b1fdc7d76eda029cfeef490f6f737166c1c72828 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 26 Feb 2023 08:39:10 +0800 Subject: [PATCH] =?UTF-8?q?P3812=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E7=BA=BF=E6=80=A7=E5=9F=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/103041260 --- Luogu/P3812/P3812.cpp | 63 +++++++++++++++++++++++------------- Luogu/P3812/data/P3812_2.in | 3 ++ Luogu/P3812/data/P3812_2.out | 3 ++ 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 Luogu/P3812/data/P3812_2.in create mode 100644 Luogu/P3812/data/P3812_2.out diff --git a/Luogu/P3812/P3812.cpp b/Luogu/P3812/P3812.cpp index 17e2d2f7..be123ff5 100644 --- a/Luogu/P3812/P3812.cpp +++ b/Luogu/P3812/P3812.cpp @@ -1,50 +1,67 @@ #include +#include +#include +#include +#include using std::cin; using std::cout; const char endl = '\n'; -const int N = 55; +const int N = 1e5 + 5; -int n; -long long x, a[N], ans; +template +struct LinearBasis + : std::enable_if::value, T> { + T a[std::numeric_limits::digits]; -void insert(long long x) { - for (int i = 50; ~i; i--) { - if (!(x & (1ll << i))) continue; + LinearBasis() { + std::fill(std::begin(a), std::end(a), 0); + } - if (a[i]) { - x ^= a[i]; - } else { - for (int k = 0; k < i; k++) { - if (x & (1ll << k)) x ^= a[k]; + void insert(unsigned long long x) { + for (int i = std::numeric_limits::digits - 1; ~i; i--) { + if ((x >> i) & 1) { + if (a[i]) { + x ^= a[i]; + } else { + a[i] = x; + + return; + } } - - for (int k = i + 1; k <= 50; k++) { - if (a[k] & (1ll << i)) a[k] ^= x; - } - - a[i] = x; - return; } } -} + + T query() { + T res(0); + + for (int i = std::numeric_limits::digits - 1; ~i; i--) { + if ((res ^ a[i]) > res) res ^= a[i]; + } + + return res; + } +}; int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); + int n; + LinearBasis lb; + cin >> n; for (int i = 1; i <= n; i++) { + unsigned long long x; + cin >> x; - insert(x); + lb.insert(x); } - for (int i = 0; i <= 50; i++) ans ^= a[i]; - - cout << ans << endl; + cout << lb.query() << endl; return 0; } diff --git a/Luogu/P3812/data/P3812_2.in b/Luogu/P3812/data/P3812_2.in new file mode 100644 index 00000000..9438f5eb --- /dev/null +++ b/Luogu/P3812/data/P3812_2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3eb2393f3e4c271017cc102d424a434e64ddd32e2867fc1412bb6ce72206d7a7 +size 164 diff --git a/Luogu/P3812/data/P3812_2.out b/Luogu/P3812/data/P3812_2.out new file mode 100644 index 00000000..6e230e2d --- /dev/null +++ b/Luogu/P3812/data/P3812_2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:481f710584ea6eff9ff793f84e9ca21ae379271f01ce714c667a2562f70ce690 +size 17