【多校训练】hdu 6129 Just do it

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
  
  
2 1 1 1 3 3 1 2 3
 

Sample Output
  
  
1 1 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");
        }
    }
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值