XORSUB - Editorial

PROBLEM LINK:

Practice
Contest

Author: Lalit Kundu
Tester 1: Minako Kojima
Tester 2: Shiplu Hawlader
Editorialist: Pawel Kacprzak

DIFFICULTY:

SIMPLE

PREREQUISITES:

DP, bits

PROBLEM:

You are given an array A of N integers and an integer K. Your task is to return the maximum possible value of F(P) xor K, where P is a subset of A and F(P) is defined as a xor of all values in P. If P is empty, then F(P) = 0.

QUICK EXPLANATION:

Since each element of A has a value of at most 1000, we can use dynamic programming dp[i][j] := 1 if and only if there exists a subset P of A[1ā€¦i] such that F(P) = j. In order to get the result, we return max 1 <= j <= 1023 { dp[n][j] * (j ^ k) }

EXPLANATION:

Let dp[i][j] := 1 if and only if there exists a subset P of A[1ā€¦i] such that F(P) = j, otherwise 0

The first observation is that F(P) can be at most 1023 since any input number is at most 1000.

Initially we set all dp[i][j] := 0.

Next, we set dp[0][0] := 1, since a xor of the empty set is 0.

We iterate over all elements of A from left to right. For each A[i], we iterate over all possible values of the xor function i.e. a range from 0 to 1023 inclusive and update these values as follows:

for i = 1 to N:
    for j = 0 to 1023:
        dp[i][j] = dp[i - 1][j] | dp[i - 1][j ^ a[i]]

The reason for this is that if there exists a subset P of A[1ā€¦i - 1] such that F(P) = j then there exists also a subset of A[1ā€¦i] such that F(P) = j or if there exists a subset P of A[1ā€¦i - 1] such that F(P) = j ^ a[i], then F(P) ^ a[i] = j, so there exists a subset Pā€™ of A[1ā€¦i] such that F(Pā€™) = j

At the end we have dp[n][j] for all 0 <= j <= 1023, and we can return a maximum value of dp[n][j] * (j ^ k) for all j.

Time Complexity:

The time complexity per one testcase is O(N * 1023);

AUTHORā€™S AND TESTERā€™S SOLUTIONS:

To be uploaded soon.

RELATED PROBLEMS:

To be uploaded soon.

20 Likes

I have a quick question. Information that A is at most 1000 was written in problem statement from the beginning? I wrote a comment about this during the contest but it was not approved. I assumed, that A is up to 2^30, so I solved this problem almost as the last one (when I finally checked the problem statement again)ā€¦

6 Likes

Iā€™ve managed to solve the problem without DP. Iā€™ve used C++ STLā€™s set and maintained all possible xors in it. Since all numbers are in [0, 1000], any xor of them will lie in [0, 1023]. So, set can have at max of 1024 elements at any point of time. So the complexity should be little less than O(n * 1024 + 1024 * log(1024)) where second term is for the inserts in the worst case.

Hereā€™s my solution: CodeChef: Practical coding for everyone

Iā€™ve also solved this using Guassian Elimination, you can view my code here: CodeChef: Practical coding for everyone

Also, the problem does not even need a 2 Dimensional DP, it can be made simpler.
Hereā€™s my DP solution: CodeChef: Practical coding for everyone

38 Likes

I just inserted k in the array and used the solution of the problem XMAX on SPOJ which is quite similar.
Obviously thatā€™s the wrong logic I assumed in the beginning but I got AC in all the subtasks except 4 in subtask 2.

1 Like

I thought something similar to the XMAX solution as mentioned above by @h1ashdr@gon . What i did was: divided the whole array in two subarrays, the first that doesnā€™t have any of the bits set as of k and the other that have atleast one bit set. In my maximum, xor of all elements in the first set has a fair chance of giving me the maximum. My task was reduced to finding the maximum of ((xor of set1^k), (xor of set2^k), (set1^set2^k). But then I got stuck in finding the maximum for set2 which is nothing but one of the subproblem of the original question(I tired many approaches though). So, it was just DP i should have thought about or any other way out in this approach??

ā€œThe first observation is that FĀ§ can be at most 1023 since any input number is at most 1000ā€.
How can we conclude that ?

I used Gaussian elimination to transform the input array A into the lower echelon form which is an equivalent representation of the above array (in which the array of bits [1101, 1001, 1010, 111, 11, 1] for example gets transformed to something like [1111,101, 1] ā€“ decreasing order of bits) Now it is easy to maximize the XOR value greedily. Start iterating with max = k from the left, and include the element only if the XOR value increases, else move on. Output max at the end of the iteration.

3 Likes

I tried doing this in the problem. This is the link:: discrete mathematics - Maximization with xor operator - Mathematics Stack Exchange

This is same as what @adijo said, I believe. But, the main idea behind this approach is setting one bit of each number one and then xoring with the numbers below in the series if they have that specific bit set, just to ensure no 2 numbers have the same MSB (most significant bit) as 1.

If this is to follow, then since we were given a k, and it also had an MSB, so shouldnā€™t we have xored k with every element in the series which had its bit corresponding to the MSB as 1.

But, it gave me a wrong answer. :confused:

1 Like

I made a recursive function for solving this.
Considering that the biggest number we can make is 1023 and in any case, we can always use an empty set to get the answer as K^0 = K, the biggest number that can be attained is 1023, and the smallest (to be checked) is K.
So the answer lies in the range from K to 1023.

To take the initial input, I made an array with size 1001, using which I could directly set array[i]=1 if i was present in the input, else the value of array[i] would be 0.
One more thing I did was to go through this array and make a new array which only holds all the elements in descending order.

Now we basically need to check for each number from 1023 to K+1, whether that number can somehow be made with the other numbers present in the set.
The most obvious(and ultimately required) case would be if the number is directly present in the set.

The basic idea is that if some number r is required, and isnā€™t directly present in the set, take a number say p from the set, and again call the function for finding r^p in the remaining set. This forms the basic recursion.

But that would give a TLE, so I needed some constraints.
I was using the array in decreasing order, so the first number would be the greatest, from this, one simple constraint I was able to make was that if the required numberā€™s Most significant bit(MSB) is greater than the currently largest numberā€™s MSB, then there is no way to make the required number.

Using all this I was able to get AC with time 0.00 in all but 1 case, which was giving TLE.
For further refinement I used the concept that, considering a certain subset of a larger set, if a certain required number could not be made using the larger set, then it can definitely not be made using this smaller subset.

With this I was finally able to get AC that one case too, with a time 0.02

Here is my solution:
http://www.codechef.com/viewsolution/5597629

4 Likes

I tried couple of times.Task# 8-15 passed for Subtask 2. Subtask 1 completely failed. I am still wondering what did i miss in my solution. Here is my solution http://www.codechef.com/viewsolution/5532642

any insight will be a great help.

Thank you.

I have tried this problem and 9 testcases are not passed . This is my solution link :
http://www.codechef.com/viewsolution/5518114

Could anyone help me out .

Which is better for large inputs - Gaussian elimination or dp?

Can anyone tell me how this solution works? I think it doesnā€™t cover all possibilities?

Can anyone explain me this plz

dp[i][j] = dp[i - 1][j] | dp[i - 1][j ^ a[i]]

can any one explain why gaussian method work.?? i mean after take echleon form of the matrix what happend that it is giving me sum of each subset from the given setā€¦

I got AC by using gaussian elimination.

A simple explanation :

First do the gaussian elimination, now traverse through each row. having the value of k, do k xor the first row and if the result is bigger than k, assign the value of the result to k, otherwise donot change k.

Similarly iterate over all the rows

and the final k is the answer

my link to answer :


[1]


  [1]: http://www.codechef.com/viewsolution/5569197

@king of hackerā€¦

i know ā€¦ i am asking as to why this method worked?

Instead of using 2-d array solution can be easily solved using 1-D array. I found that solution from submission list. Really nice logic.

http://www.codechef.com/viewplaintext/5531874

Can anyone please explain how Gaussian Elimination is applied on this problem. I saw the stack exchange link but couldnā€™t make out anything from that.

did it using gaussian elimination but dp also seems really cool trick hereā€¦

loved the editorial (thnx codechef)

I would like to explain the Gaussian Elimination method which I have used and got AC in 0.00 :slight_smile:
The link to my solution. (sorry for some dubugging statements in it).

  1. The idea here is to choose a no. which has MSB with maximum value. With a little thinking, we will get the max(array) will have this feature. Say, maximum of array is a k-bit no. and that no. is M. Now, there can be multiple no.s which are k-bit no.s and have the same highest-value bit as ā€˜1ā€™. So in that case we will EX-OR each of them with the M and put them again input array (ideally, we can choose any one of those k-bit no.s as M and put others in array after ex-oring with it).
    At the same time we will keep the no. M in some other array say x[].

  2. We will keep doing step 1 until we get 0 as maximum in the array i.e. this loop will run for iterations = no. of bit the maximum of array is.

  3. Now will have a x[] array which will be of size = no. of bit the maximum of array is

  4. Now we will initialize the answer variable to given K and loop for each value in array x[], if value of answer variable is going to increase with the inclusion of ith element, we will update the answer variable with the new value as answer^x[i], else we will keep it as is.

  5. Finally returning the value of answer solves our problem.

About my solution:

Bucket[i] contains all i-bit no.s

the M chosen is first element of bucket i.e. Bucket[i][0]

x[] is modified_array[]

4 Likes