【多校训练】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}{an},对其做mm次前缀异或和,求最终的序列。1≤n≤2×105,1≤m≤109,0≤ai≤230−11≤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
;
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");
}
}
}
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}{an},对其做mm次前缀异或和,求最终的序列。1≤n≤2×105,1≤m≤109,0≤ai≤230−11≤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
;
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");
}
}
}
相关文章推荐
- (2017多校训练第七场)HDU - 6129 Just do it 找规律
- 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it
- (2017多校训练第七场)HDU - 6129 Just do it
- HDU 6129 Just do it(多校7, 组合数 规律)
- HDU 6127 Hard challenge+HDU 6129 Just do it【2017多校联赛】
- 多校7 HDU - 6129 Just do it 组合数学
- HDU 2017 多校联合训练赛7 1010 6129 Just do it 杨辉三角 组合数的奇偶判断
- hdu 6129 Just do it(递推)
- hdu 6129 Just do it(找规律)
- 2017多校 Just do it (Hdu 6129)
- 2017多校第7场 HDU 6129 Just do it 找规律
- HDU 6129 Just do it(杨辉三角+lucas \ 记忆化dp)
- hdu 6129 Just do it(递推)
- hdu 6139 Just do it(多校联赛)
- 【HDU 6129 Just do it】& 数学推导
- hdu 6129 Just do it(递推)
- hdu 6129 Just do it (杨辉三角)
- HDU-2017 多校训练赛7-1010-Just do it
- HDU 6129 Just do it
- hdu 6129 Just do it(递推)