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

来源:互联网 发布:超人电力工程预算软件 编辑:程序博客网 时间:2024/05/16 11:24

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
21 113 31 2 3
 

Sample Output
11 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;}



原创粉丝点击