HDU 2017 多校联合训练赛7 1010 6129 Just do it 杨辉三角 组合数的奇偶判断

Just do it

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)


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
 

Source
2017 Multi-University Training Contest - Team 7


题目大意
给出一个序列A,序列B中Bi是由序列A中序号小于等于i的所有数的异或和,即b[i]=a[1]^a[2]^…..^a[i],然后把B序列赋给A重复此操作m次,要求最后一次得到的B序列。


题目分析
首先,我们容易得到,每组序列B中第i项b[i] = a[i] ^ b[i-1],即B序列中Bi的前一项与当前A序列中第i项Ai的异或。
现在列出m=5,n=5的表格

然后,把所有a1的系数提取出来(a2...a5的系数都满足这一规律)

我们会发现系数满足杨辉三角的形式,所以根据杨辉三角性质,我们可以直接通过组合数求得a[i]在第m次运算中,于b[j]的系数。
并且偶数个a[i]的异或和为0,奇数个a[i]的异或和为它本身,所以我们只需要关注系数为为奇数的项。

至于如何判断组合数的奇偶,我们用到了一条性质:对于C(n,m),如果n&m==m则C(n,m)为奇数,否则为偶数。
推荐两篇博客
http://blog.csdn.net/millky/article/details/3206730
http://blog.csdn.net/sdnuzsj/article/details/77278274



还有一件事,第m行a[1]的系数与后面a[1+i]的系数是相同的。
详情请参考代码


代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[2050000];
int b[2050000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(b,0,sizeof(b));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        for(int i=1; i<=n; i++)//数表的第m行第i项,杨辉三角第m-1+i行,第i项 //杨辉三角公式C((m-1+i)-1,i-1)
        {
            int y=i-1;
            int x=i+m-2;//(m-1+i)-1
            if((x&y)==y)///判断为奇数
            {
                for(int j=i; j<=n; j++)//a的贡献系数为C(x,y)
                    b[j]^=a[j-i+1];
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(i>1)
                printf(" ");
            printf("%d",b[i]);
        }
        printf("\n");
    }
}


参考
http://blog.csdn.net/qq_37412229/article/details/77231474


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值