【多校训练】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(1≤T≤5) , denoting the number of test cases.
For each test case:
The first line contains two positive integersn,m(1≤n≤2×105,1≤m≤109) .
The second line containsn nonnegative integers a1...n(0≤ai≤230−1) .
For each test case:
The first line contains two positive integers
The second line contains
Output
For each test case:
A single line containsn nonnegative integers, denoting the final sequence.
A single line contains
Sample Input
21 113 31 2 3
Sample Output
11 3 1
题意:
有一个长度为n的整数序列{an},对其做m次前缀异或和,求最终的序列。1≤n≤2×105,1≤m≤109,0≤ai≤230−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[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
- 【多校训练】hdu 6129 Just do it
- HDU-2017 多校训练赛7-1010-Just do it
- HDU 6129 Just do it
- HDU 6129 Just do it
- HDU 6129 Just do it
- HDU 6129Just do it
- HDU-6129 Just do it
- hdu 6129 Just do it
- hdu 6129 Just do it
- HDU 6129 Just do it
- HDU 6129 Just do it
- hdu--6129--Just do it
- HDU 6129 Just do it
- HDU-Just do it
- hdu 6129 Just do it(递推)
- 2017多校 Just do it (Hdu 6129)
- 【HDU 6129 Just do it】& 数学推导
- Just do it(HDU 6129)
- 通过金矿模型介绍动态规划
- JZOJsenior3456.【NOIP2013模拟联考3】恭介的法则(rule)
- Python--Python Tornado 介绍
- BFS-最短路(迷宫)
- 读源码
- 【多校训练】hdu 6129 Just do it
- Linux下paste命令详解
- 记录收集的android相关的类库
- QT 插件里面的宏编译错误 解决办法
- 使用Newtonsoft.Json处理含有未知节点的JSON
- LSTM预测时间序列
- c#
- 定时任务
- 磁盘内存实时监控