CF2268B What a SauSaGe! It's All Meat题解
Boeing_738
·
·
题解
CF2268B What a SauSaGe! It's All Meat 题解
题目传送门
Luogu
Codeforces
思路
每次操作选择下标 i 和整数 k\in[1,5],将 a_i 和 a_{i+1} 同时异或 3k。
设 $x_i$ 表示在第 $i$ 和 $i+1$ 个位置之间进行的所有操作的异或总和。
则最终数组满足:
$$a_i' = a_i \oplus x_{i-1} \oplus x_i$$
其中 $x_0=x_n=0$。
每个 $x_i$ 可以取由 $\{3,6,9,12,15\}$ 异或张成的线性空间 $V$ 中的任意值。
计算 $V$:
$$3=0011,\ 6=0110,\ 9=1001,\ 12=1100,\ 15=1111$$
它们的异或组合生成所有二进制中 $1$ 的个数为偶数的数,即:
$$V=\{0,3,5,6,9,10,12,15\}$$
共 $8$ 个元素。
能被 $3$ 整除的数在 $0\sim15$ 中为:
$$\{0,3,6,9,12,15\}$$
恰好是 $V$ 中排除 $5,10$ 的子集,即 $3\mathbb{Z}\subset V$。
若 $a_i\notin V$,则 $a_i\oplus V$ 与 $V$ 不相交,更不可能与 $3\mathbb{Z}$ 相交,因此该位置永远无法变成 $3$ 的倍数。
所以必要条件:$a_i\in V$。
若所有 $a_i\in V$,则总能构造出合适的 $x_i\in V$ 使得每个 $a_i'$ 都是 $3$ 的倍数。
因为对每个 $i$,约束为:
$$x_{i-1}\oplus x_i\notin \{a_i\oplus 5,\ a_i\oplus 10\}$$
即每条边禁止两个值。而 $V$ 有 $8$ 个元素,每次选择至少有 $6$ 种可能,链式构造总能成功。
因此答案就是数组中属于集合 $V$ 的元素个数。
维护一个计数 $cnt$,每次更新时若旧值在 $V$ 中则减一,若新值在 $V$ 中则加一,输出 $cnt$ 即可。
### 代码
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
bool inV[16]={1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1};
int T;
cin>>T;
while(T--){
int n,q;
cin>>n>>q;
vector<int>a(n);
int cnt=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(inV[a[i]])cnt++;
}
cout<<cnt;
for(int i=0;i<q;i++){
int p,x;
cin>>p>>x;
--p;
if(inV[a[p]])cnt--;
if(inV[x])cnt++;
a[p]=x;
cout<<' '<<cnt;
}
cout<<'\n';
}
return 0;
}
```