可以发现当 i != j 时,(Ai + Aj)会与(Aj + Ai) 抵消,于是把所有的Ai * 2给xor起来就好了。
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,m,z,l;
scanf("%d%d%d%d",&n,&m,&z,&l);
ll x = 0,sum = 0;
for(int i = 2; i <= n; i++){
x = (x*m+z) % l;
sum ^= (2*x);
}
printf("%lld\n",sum);
}
return 0;
}
本文介绍了一种在特定场景下利用XOR运算进行数组元素抵消的算法优化方法,并给出了一段C++实现代码示例。该方法适用于计算一系列配对元素相加后的XOR值,通过巧妙地将每对不同索引的元素相互抵消,最终只需对数组中每个元素乘以2再进行XOR运算即可得到结果。
387

被折叠的 条评论
为什么被折叠?



