HDU 6129(找规律+杨辉三角)

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
往后写几项就会发现每个 a[i] 被异或的次数其实是杨辉三角里的数并有一定规律(与 a[k] 的第 i 次变换的第 j 项的i,j,k有关系),这个很好找出来
然后就可以得到最初的数列的每一个 a[k] 在第 m 次操作后的每一项中被异或的次数(次数是一个组合数 C(x,y),这个也是有规律的)
因为是异或所以只需要判断 C(x,y)的奇偶性即可
当 ( x & y ) == y 时 C(x,y) 为奇
例: 第1项 第2项 第3项 ............... 第n项
a1 c(x,0) c(x+1,1) c(x+2,2) ............... c(x+n-1,n-1)
a2 0 c(x,0) c(x+1,1) ............... c(x+n-2,n-2)
a3 0 0 c(x,0) ............... c(x+n-3,n-3)
a4 0 0 0 ............... c(x+n-4,n-4)
大概就是这样子
以上是每一个a[k]在m次操作后的b[i]中被异或的次数,a[1]在b[2]中被异或了c(x+1,1)次,这个x可以根据规律推出来的,等于m-1
代码:
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
int a[200000+3],ans[200000+3];
int n,m;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),ans[i]=0;
        if(m==1)
        {
            for(int i=1;i<=n;i++)
            {
                ans[i]=ans[i-1]^a[i];
                printf("%d",ans[i]);
                if(i<n)putchar(' ');
                else putchar('\n');
            }
        }
        int x=m-1,y=0;
        for(int i=1;i<=n;i++)
        {
            if((x&y)==y)
            {
                for(int j=1;j+y<=n;j++)
                {
                    ans[i+j-1]^=a[j];
                }
            }
            y++;
            x++;
        }
        for(int i=1;i<n;i++)printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}

本人蒟蒻,如有错误,还望指正


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值