Just do it
Time Limit: 5000/2500 MS (Java/Others)
Memory Limit: 524288/524288 K (Java/Others)
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
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6129
分析
题意:给出T组测试数据,每组包含两行,第一行给定n,m,第二行给定一组正整数序列,n表示序列的长度,m表示序列需要进行变形的次数,变形规则为,第i个元素等于当前序列前i个元素的异或
如果我们一项一项异或,纯暴力,会使程序特别繁琐而超时,所以可以试着去找出规律,我们在写出每次变形的结果的时候会发现,变形后第i个元素是前i个元素的异或,变形后第i-1个元素是前i-1个元素的异或,那变形后第i个元素就是变形后第i-1个元素与变形前第i个元素的异或,这规律好像于某个规律很像,我们在写出每次变换后的元素时,记录时是将每个元素的个数记录下来, 每次异或相当于加上每个元素出现的次数,加?那不正是杨辉三角么,我们会发现他们每种元素的系数都满足杨辉三角的规律
因为偶数个相同的数字异或结果为0,任意数和0异或结果为本身,所以我们就考虑每个元素的系数是偶数还是奇数,偶数的话不用考虑,奇数的话将该元素异或一次即可
代码
#include <bits/stdc++.h>
using namespace std;
const int MAX=2050000;
int a[MAX];
int b[MAX];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(b,0,sizeof(b));
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=n; i++)
{
int y=i-1;
int x=i+m-2;
if((x&y)==y)///判断元素系数是否为奇数
{
for(int j=i; j<=n; j++)
b[j]^=a[j-i+1];
}
}
for(int i=1; i<=n; i++)
printf("%d%c",b[i],i==n?'\n':' ');
}
}