Just do it点击打开链接
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1411 Accepted Submission(s): 823
Problem Description
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
Input
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×105,1≤m≤109).
The second line contains
n nonnegative integers
a1...n(0≤ai≤230−1).
Output
For each test case:
A single line contains
n
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
【分析】:
if((n&m)==m) 奇数
else 偶数
m次操作之后,只需计算每个元素对后面元素的贡献。
若贡献为偶数次,由于是亦或,可忽略
若贡献为奇数次,那么只需亦或一次。
试着写了写m=1,2,3,4,....的时候,得出规律:
第一个元素对第i个元素的贡献次数为C(i+m-2,m-1)我们只用到他的奇偶性。
【代码】:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[202020],b[202020];
int e[202020];
int T,n;
ll h;
int main()
{
cin>>T;
while(T--)
{
cin>>n>>h;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int top=0;
ll x=h-1,y=h-1;
for(int i=1;i<=n;i++,x++)
{
if((x&y)==y)e[top++]=i;//i点组合数为奇数
}
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
for(int j=0; i+e[j]-1<=n&&j<top; j++)
b[i+e[j]-1]^=a[i];
if(i<n)
printf("%d ",b[i]);
else printf("%d\n",b[i]);
}
}
return 0;
}