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) .
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
Source
2017 Multi-University Training Contest - Team 7
题目大意
给出一个序列A,序列B中Bi是由序列A中序号小于等于i的所有数的异或和,即b[i]=a[1]^a[2]^…..^a[i],然后把B序列赋给A重复此操作m次,要求最后一次得到的B序列。
题目分析
首先,我们容易得到,每组序列B中第i项b[i] = a[i] ^ b[i-1],即B序列中Bi的前一项与当前A序列中第i项Ai的异或。
现在列出m=5,n=5的表格
然后,把所有a1的系数提取出来(a2...a5的系数都满足这一规律)
我们会发现系数满足杨辉三角的形式,所以根据杨辉三角性质,我们可以直接通过组合数求得a[i]在第m次运算中,于b[j]的系数。
并且偶数个a[i]的异或和为0,奇数个a[i]的异或和为它本身,所以我们只需要关注系数为为奇数的项。
至于如何判断组合数的奇偶,我们用到了一条性质:对于C(n,m),如果n&m==m则C(n,m)为奇数,否则为偶数。
推荐两篇博客
http://blog.csdn.net/millky/article/details/3206730
http://blog.csdn.net/sdnuzsj/article/details/77278274
还有一件事,第m行a[1]的系数与后面a[1+i]的系数是相同的。
详情请参考代码
代码
参考
http://blog.csdn.net/qq_37412229/article/details/77231474
题目大意
给出一个序列A,序列B中Bi是由序列A中序号小于等于i的所有数的异或和,即b[i]=a[1]^a[2]^…..^a[i],然后把B序列赋给A重复此操作m次,要求最后一次得到的B序列。
题目分析
首先,我们容易得到,每组序列B中第i项b[i] = a[i] ^ b[i-1],即B序列中Bi的前一项与当前A序列中第i项Ai的异或。
现在列出m=5,n=5的表格
然后,把所有a1的系数提取出来(a2...a5的系数都满足这一规律)
我们会发现系数满足杨辉三角的形式,所以根据杨辉三角性质,我们可以直接通过组合数求得a[i]在第m次运算中,于b[j]的系数。
并且偶数个a[i]的异或和为0,奇数个a[i]的异或和为它本身,所以我们只需要关注系数为为奇数的项。
至于如何判断组合数的奇偶,我们用到了一条性质:对于C(n,m),如果n&m==m则C(n,m)为奇数,否则为偶数。
推荐两篇博客
http://blog.csdn.net/millky/article/details/3206730
http://blog.csdn.net/sdnuzsj/article/details/77278274
还有一件事,第m行a[1]的系数与后面a[1+i]的系数是相同的。
详情请参考代码
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[2050000];
int b[2050000];
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++)//数表的第m行第i项,杨辉三角第m-1+i行,第i项 //杨辉三角公式C((m-1+i)-1,i-1)
{
int y=i-1;
int x=i+m-2;//(m-1+i)-1
if((x&y)==y)///判断为奇数
{
for(int j=i; j<=n; j++)//a的贡献系数为C(x,y)
b[j]^=a[j-i+1];
}
}
for(int i=1; i<=n; i++)
{
if(i>1)
printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
}
参考
http://blog.csdn.net/qq_37412229/article/details/77231474