Just do it HDU - 6129

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

题意:

给你n个数,有一个操作是将a1到an变成b1到bn,bi是a1到ai的异或,问你在m次操作之后这n个数会变成什么

题解:

我们假设第i个数对后面有影响为1,无影响为0,那么会得到这样一张表:
1000000000
11111111111
1010101010
1100110011
1000100010
1111000011

然后会发现,长度为1时的循环节长度为1,2时长度为2,4的长度为4,以此类推。那么我们就可以for一遍m的所有位,如果这一位是存在的,那么就代表前面可以异或后面,然后for一遍所有可以被异或饿数就好了

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<=30;i++)
        {
            if(((1<<i)&m))
            {
                for(int j=(1<<i);j<=n;j++)
                {
                    a[j]=a[j-(1<<i)]^a[j];
                }
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d%c",a[i],i==n?'\n':' ');
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值