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
题意:
给你n个数,有一个操作是将a1到an变成b1到bn,bi是a1到ai的异或,问你在m次操作之后这n个数会变成什么
题解:
我们假设第i个数对后面有影响为1,无影响为0,那么会得到这样一张表:
1000000000
11111111111
1010101010
1100110011
1000100010
1111000011
…
然后会发现,长度为1时的循环节长度为1,2时长度为2,4的长度为4,以此类推。那么我们就可以for一遍m的所有位,如果这一位是存在的,那么就代表前面可以异或后面,然后for一遍所有可以被异或饿数就好了
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=0;i<=30;i++)
{
if(((1<<i)&m))
{
for(int j=(1<<i);j<=n;j++)
{
a[j]=a[j-(1<<i)]^a[j];
}
}
}
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
}
return 0;
}