题意:
给你一个数列, m次变换, 每次变换b[i] = a[1]~a[i]的异或值。 求最后的序列。
思路:
一个找规律的题目。
考虑 每一个数对后面的数的贡献。
先考虑a1:
第一次变换: 1 1 1 1 1
第二次变换: 1 2 3 4 5
第三次变换: 1 3 6 10 15
第四次变换: 1 4 10 20 35
不难发现 每一行是 杨辉三角的斜边。
所以 f(x,y) = C(x+y-2,y-1) 其中f(x,y)表示变换x次, a1 对第y 个位置数的贡献值。
那么只考虑 这个组合数是奇数还是偶数即可。
有个简单的公式 C(x,y) 是奇数当且仅当 (x & y) == y 的时候满足。
然后在考虑怎么把所有贡献算出来, 因为我们算的是a1的贡献, a2,a3,a4 的贡献相当于从a1的位置向右挪了挪, 在挪回来即可。
所以我们枚举a1的贡献值,当是奇数的时候,我们在计算后面的贡献。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 200000 + 10;
int a[maxn], ans[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);
ans[i] = 0;
}
for (int i = 1; i <= n; ++i){
int x = i + m - 2;
int y = i-1;
if ((x&y)==y){
for (int j = i; j <= n; ++j){
ans[j] ^= a[j-i+1];
}
}
}
for (int i = 1; i <= n; ++i){
if (i > 1)putchar(' ');
printf("%d", ans[i]);
}
putchar('\n');
}
return 0;
}
Just do it
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1113 Accepted Submission(s): 648
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×105,1≤m≤109) .
The second line contains n nonnegative integers a1...n(0≤ai≤230−1) .
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 nonnegative integers, denoting the final sequence.
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
Recommend