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) .
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.
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往后写几项就会发现每个 a[i] 被异或的次数其实是杨辉三角里的数并有一定规律(与 a[k] 的第 i 次变换的第 j 项的i,j,k有关系),这个很好找出来然后就可以得到最初的数列的每一个 a[k] 在第 m 次操作后的每一项中被异或的次数(次数是一个组合数 C(x,y),这个也是有规律的)因为是异或所以只需要判断 C(x,y)的奇偶性即可当 ( x & y ) == y 时 C(x,y) 为奇例: 第1项 第2项 第3项 ............... 第n项a1 c(x,0) c(x+1,1) c(x+2,2) ............... c(x+n-1,n-1)a2 0 c(x,0) c(x+1,1) ............... c(x+n-2,n-2)a3 0 0 c(x,0) ............... c(x+n-3,n-3)a4 0 0 0 ............... c(x+n-4,n-4)大概就是这样子以上是每一个a[k]在m次操作后的b[i]中被异或的次数,a[1]在b[2]中被异或了c(x+1,1)次,这个x可以根据规律推出来的,等于m-1代码:#include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include <cmath> using namespace std; typedef long long LL; int a[200000+3],ans[200000+3]; int n,m; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]),ans[i]=0; if(m==1) { for(int i=1;i<=n;i++) { ans[i]=ans[i-1]^a[i]; printf("%d",ans[i]); if(i<n)putchar(' '); else putchar('\n'); } } int x=m-1,y=0; for(int i=1;i<=n;i++) { if((x&y)==y) { for(int j=1;j+y<=n;j++) { ans[i+j-1]^=a[j]; } } y++; x++; } for(int i=1;i<n;i++)printf("%d ",ans[i]); printf("%d\n",ans[n]); } return 0; }
本人蒟蒻,如有错误,还望指正
![]()