您的位置:首页 > 其它

【多校训练】hdu 6129 Just do it

2017-08-18 16:48 573 查看
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

题意:

有一个长度为nn的整数序列{an}{a​n​​},对其做mm次前缀异或和,求最终的序列。1≤n≤2×105,1≤m≤109,0≤ai≤230−11≤n≤2×10​5​​,1≤m≤10​9​​,0≤a​i​​≤2​30​​−1。

思路:

设d[i][j]为第i次操作后的第j个数,根据前缀异或和的性质可以得到 d[i][j]=d[i-1][j]^d[i][j-1]。递归可得到d[i][j]=d[i-2][j]^d[i][j-2],那么多次递归可以得到d[i][j]=d[i-(1<<k)][j]^d[i][j-(1<<k)]。我们要求m次,可以把m考虑成2进制的形式,根据每一位上的数,进行操作得到。

例如,m=11

我们可以先求出d[1][j]=d[1][j-1]^d[0][j],其中d[0][j]已知,而d[1][j-1]在求d[1][j]之前已经求过。

之后再求d[3][j]=d[3][j-2]^d[1][j]

最后再求d[11][j]=d[11][j-8]^d[3][j]。

这样只需要求logm次就能得出答案,最终的时间复杂度为nlogm。

//
// main.cpp
// 1010
//
// Created by zc on 2017/8/18.
// Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int N=440000;
int a
;
int main(int argc, const char * argv[]) {
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]);
for(int k=0;(1<<k)<=m;k++)
{
if(m>>k&1)
{
for(int i=(1<<k)+1;i<=n;i++)
{
a[i]^=a[i-(1<<k)];
}
}
}
for(int i=1;i<=n;i++)
{
printf("%d",a[i]);
printf(i<n?" ":"\n");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: