[Poj 6129 Just do it](http://acm.hdu.edu.cn/showproblem.php?pid=6129)
###题解:
####官方题解(深奥, 看得我一知半解,恕我太菜,233~)
我的理解:
这题求前缀异或和,但由于要进行m次操作,所以完全暴力会T;
所以我们得找规律:
对于a[0],我们来看随着操作次数的变化,它对a[0] - a[n] 的贡献:
m/i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | … |
2 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … |
3 | 1 | 3 | 6 | 10 | 15 | 21 | 28 | 36 | … |
4 | 1 | 4 | 10 | 20 | 35 | 56 | 84 | 120 | … |
… | … | … | … | … | … | … | … | … | … |
我们可以发现上表是一个扬辉三角形,从而可以归纳出对于a[0]在第m次操作时对其他数的贡献为C(m+i-2,i-1)(i = 1 时是对其本身的贡献 1<=i<=n)
因为是^,所以我们只需要判断C(x,y)的奇偶性,由lucas定理可知:当且仅当 x&y == y 时 C(x,y) 为奇数。
把i作为相对位置进行剪枝,优化算法。
AC代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
const int maxn = 2e5 + 10;
using namespace std;
typedef long long LL;
int n,m;
int a[maxn],b[maxn];
int main()
{
int T;
cin >> T;
while(T--)
{
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
memset(b,0,sizeof(b));
for(int i = 1; i <= n; i++)
{
int y = m + i - 2;
int x = i - 1;
if((y&x) == x)
{
for(int j = i-1; j < n; j++)
b[j] ^= a[j-i+1];
}
}
for(int i = 0; i < n; i++)
printf("%d%c",b[i],i==n-1? '\n':' ');
}
return 0;
}