I participated in one algorithmic competition. I got stuck in one problem, I am asking the same here.
Problem Statement
XOR-sum array is to XOR
As far as I can tell, the code at the link that you provided is not the best solution, nor even a working solution. The code which you copied from that link seems to make sense, but previous to the copied code, when the values are read into A, they are XORed by the value read in before them:
for (int i = 1; i <= N; i++)
{
scanf("%d", &A[i]);
A[i] ^= A[i - 1];
}
...meaning the following input:
1
4
1 2 3 4
...gets stored into A like so:
A[0]: 00000000000000000000000000000000 = 0
A[1]: 00000000000000000000000000000001 = 1
A[2]: 00000000000000000000000000000011 = 3
A[3]: 00000000000000000000000000000000 = 0
A[4]: 00000000000000000000000000000100 = 4
For the previous input, the correct answer should be:
F(1, 1) + F(1, 2) + F(2, 2) + F(1, 3) + F(2, 3) + F(3, 3) + F(1, 4) + F(2, 4) + F(3, 4) + F(4, 4)
= 1 + 3 + 2 + 2 + 1 + 3 + 5 + 6 + 7 + 4
= 34
...but here's what we get using the algorithm you posted as the "best one" (sum of c * (N - c + 1) * 2 ^ i from i = 0 to i = 29)
2 * (4 - 2 + 1) * 1 + 1 * (4 - 1 + 1) * 2 + 1 * (4 - 1 + 1) * 4
= 6 + 8 + 16
= 30
So it's off by four, and therefore isn't a working solution to the problem, let alone the best working solution.
Note that if the values hadn't been XORed when they were read in, here's what would be in A:
A[0]: 00000000000000000000000000000000 = 0
A[1]: 00000000000000000000000000000001 = 1
A[2]: 00000000000000000000000000000010 = 2
A[3]: 00000000000000000000000000000011 = 3
A[4]: 00000000000000000000000000000100 = 4
So then the formula sum of c * (N - c + 1) * 2 ^ i from i = 0 to i = 29 would give:
2 * (4 - 2 + 1) * 1 + 2 * (4 - 2 + 1) * 2 + 1 * (4 - 1 + 1) * 4
= 6 + 12 + 16
= 34
...which is the correct answer, according to the problem statement on the website you linked. I think this is why we've seen answers so far that agree with the code you posted - the code you posted makes sense, even if the preceding code (found at the page to which you linked) makes the entire program erroneous.