Just do it
HDU 6129 (规律) 2017ACM暑期多校联合训练 - Team 7 1010 Just do it
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1234 Accepted Submission(s): 724
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(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×10^5,1≤m≤10^9).
The second line contains n nonnegative integers a1…n(0≤ai≤2^30−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
题意:
给定一个数组a,然后要求出数组b,数组b于数组a之间满足这样的规律:
b[i]=a[1]^a[2]^·····^a[i].
像这样的话其实是很简单的,但是我们并不是要求第一次异或得出的数组b,而是要求经过m次异或之后得出的数组b。
分析:
ans[i][j]数组进行异或
你会发现 ans【i】【j】=ans【i-1】【j】^ans【i】【j-1】;
有木有发现杨辉三角形的痕迹。
对于每一项,他的系数就是杨辉三角的值,那么如果当前位子系数为奇数的话,结果就会有贡献(x=x^y^y).
同样很显然,我们第i行,第j列的答案,其系数为C(i+j-2,j-1)【此时只考虑a的系数】
#include <iostream>
#include <cstdio>
#include<string.h>
using namespace std;
const int N = 2e6+10;
int a[N], b[N];
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项的时候
{
int nn=m+i-2,mm=i-1;
if((nn&mm)==mm)
{
for(int j=i; j<=n; j++) b[j]^=a[j-i+1];
}
}
for(int i=1; i<=n; i++) printf("%d%c",b[i],i==n?'
':' ');
}
return 0;
}