【多校训练】hdu 6129 Just do it

来源:互联网 发布:js 从数据库导出excel 编辑:程序博客网 时间:2024/06/05 15:43

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(1T5), denoting the number of test cases.
For each test case:
The first line contains two positive integers n,m(1n2×105,1m109).
The second line contains n nonnegative integers a1...n(0ai2301).
 

Output
For each test case:
A single line contains n nonnegative integers, denoting the final sequence.
 

Sample Input
21 113 31 2 3
 

Sample Output
11 3 1

题意:

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


思路:

设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[N];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");        }    }    }

原创粉丝点击