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

本文介绍了一种称为Prefix-XOR的序列变换算法,通过多次异或操作生成新序列,并详细解析了实现思路与代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Just do it

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 388    Accepted Submission(s): 218


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
 
题意:对一个非负整数序列a1-an进行异或操作获得新的序列,操作如下bi=a1^a2…^ai,然后ai=bi,求进行m次后的序列a
思路:我们可得到b[i]=b[i-1]^a[i]而a[i]就是上一次异或得到的b[i],那么其实对于原序列中的第x元素在异或操作中被重复异或的次数所构成的系数为杨辉三角,打表如下

斜着看系数,系数构成的是一个杨辉三角,那么对于a1在第m次对bi的贡献就是i:C(m+i-2,i-1),再根据异或的性质当系数为偶数时相互抵消异或结果为0,系数为奇数时才会有贡献,再根据组合数奇偶的判断方法对于C(n,k),若n&k == k 则c(n,k)为奇数,否则为偶数。对于a2的贡献,只是在a1的情况向后推一位,如a1在m=2,3号位的贡献是奇数那么a2就是在相同情况下的4号位贡献为奇数,a3就是在5号这样推下去
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn=200005;
int a[maxn],b[maxn];
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]);
            memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            int nn=m+i-2,k=i-1;
            if((nn&k)==k)
            for(int j=i;j<=n;j++) b[j]^=a[j-i+1];//a1有j有贡献,那么a2就在j+1有贡献
        }
        for(int i=1;i<=n;i++)
            i!=n?printf("%d ",b[i]):printf("%d\n",b[i]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值