Problem Description
There is a nonnegative integer sequence a1…n of length n. HazelFan wants to do a type of transformation called prefix-XOR, which means a1…n changes into b1…n, where bi equals to the XOR value of a1,…,ai. He will repeat it for m times, please tell him the final sequence.
Input
The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
The first line contains two positive integers n,m(1≤n≤2×105,1≤m≤109).
The second line contains n nonnegative integers a1…n(0≤ai≤230−1).
Output
For each test case:
A single line contains n nonnegative integers, denoting the final sequence.
Sample Input
2
1 1
1
3 3
1 2 3
Sample Output
1
1 3 1
Source
2017 Multi-University Training Contest - Team 7
题意
给你数组a,求每个数m次异或和之后的数组b。
即 b[i]=a[1]^a[2]^a[3]…^a[i](这里的a[]是指上一次异或之后的a[])
想法
我们写m=5,n=5的来看一看
我们单独把某一个的异或次数拿出来看 ,比如a的
发现斜着来看就是杨辉三角!
变化第x次,第y项的系数就是
Cy−1x+y−2
C
x
+
y
−
2
y
−
1
而又因为在异或关系中,异或自身偶数次结果为0,异或自身奇数次结果还是本身。所以我们只要寻找异或次数为奇数次的就可以。
比如上图m=5,n=5.我们要求第m次一伙的第1项时,算出C(4,0)=1,为奇数都有1这个系数,b[1]~b[5](最终要求的)异或上a[1]~a[5].第二项C(5,1)=5,为奇数,b[2]~b[5]分别异或a[1]~a[4],………….一直按这个规律求下去。
接下来的重点就是怎么判断这个次数的奇偶性。
其实是有这么一个公式的,但是在网上不容易找到/(ㄒoㄒ)/~~
Cmn
C
n
m
,如果n&m==m,则
Cmn
C
n
m
为奇数
基本问题就是这些了,慢慢敲代码吧~
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn+10],b[maxn+10];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i)
scanf("%d",&a[i]);
memset(b,0,sizeof b);
for(int i=1; i<=n; ++i)
{
int x=i+m-2,y=i-1;
if((x&y)==y)
{
for(int j=i; j<=n; ++j)
b[j]^=a[j-i+1];
}
}
for(int i=1; i<=n; ++i)
{
if(i!=1)
printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
return 0;
}
ps:强迫症画个图简直要气哭TAT
(谢特当时写的不清楚 再看时重做了一遍 →图是这么来的:上边的那个和左边的异或得到当前框内的 每次异或时次数增加一次