「CF1101G」(Zero XOR Subset)-less - 线性基

给出一个长度为nn的序列{ai}\{a_i\},试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为00

n105,ai109n\le10^5, a_i\le 10^9

CF1101G

Solution

一看到“异或”、“子段”的字眼就不难想到与前缀异或和有关

再仔细想想就会发现题目就是要求最多选出多少个前缀异或和,使得它们线性无关,且sumnsum_n必选,即sumn0sum_n\ne 0

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 2e5 + 100;

int N, A[Maxn], Sum[Maxn];

namespace Basis
{
const int Maxlen = 30;

int A[Maxlen + 10];

inline int insert (int x)
{
for (int i = Maxlen; ~i; --i)
{
if (!(x & (1 << i))) continue;
if (!A[i])
{
for (int j = 0; j < i; ++j) if (x & (1 << j)) x ^= A[j];
for (int j = i + 1; j <= Maxlen; ++j) if (A[j] & (1 << i)) A[j] ^= x;
A[i] = x;
return 1;
}
x ^= A[i];
}
return 0;
}
}

inline void Solve ()
{
for (int i = 1; i <= N; ++i) Sum[i] = Sum[i - 1] ^ A[i];
if (!Sum[N]) { puts("-1"); return ; }

Basis :: insert(Sum[N]);
int ans = 1;
for (int i = 1; i < N; ++i) ans += Basis :: insert (Sum[i]);
cout<<ans<<endl;
}

inline void Input ()
{
N = read<int>();
for (int i = 1; i <= N; ++i) A[i] = read<int>();
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("G.in", "r", stdin);
freopen("G.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}
感谢你的赞赏!