Editorial for GlobeX Cup '18 S3 - Playing With Bits


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Ninjaclasher

For the first subtask, we can brute force all possible combinations of the array a, and check if they match the constraints.

Time Complexity: O(2KN)


For the rest of the subtasks (in general), we need to improve the time complexity.

Let us look at the xor operation first. A bit i in V is set if and only if there is an odd number of ajs with the ith bit set. A bit i in V is not set if there is an even number of ajs with the ith bit set. Thus, we need to look at each bit in V individually. If the ith bit is set, we need to calculate the number of ways to place an odd number of set bits in the array a of size N. Thus, (Nodd) (N choose an odd number). If the ith bit is not set, we need to calculate the number of ways to place an even number of set bits in the array a of size N. Thus, (Neven) (N choose an even number).

For the or operation, the ith bit in V is set if any of aj has set bit i. An ith bit in V is unset if all of aj has an unset bit i. Similar to the xor operation, we must loop through each bit. There are 2N1 ways to choose a combination for the N elements, such that at least one of them has a set bit i, and there is exactly 1 way to choose a combination for the N elements, such that all bit is are unset.

The and operation can be handled in the same manner as the or operation, and so we will leave it as an exercise for the reader.

With this, we can go through each bit i up to K, and multiply the number of ways for each bit accordingly. Be wary to modulo to prevent overflow.

It is trivial to calculate 2N1 with Exponentiation By Squaring. To calculate (Neven) and (Nodd) quickly requires a little more mathematical insight (which are the next 3 subtasks).


For the second subtask, we can precompute by looping up to N (0 to N inclusive), and calculate (Ni). Multiply all even i into a final variable, and all odd i into another variable. Note that we need to use modular division in order to prevent overflow. Noticing that M is a prime, we can use Fermat's Little Theorem to calculate the modular multiplicative inverse, and perform modular division accordingly (by multiplying by the modular multiplicative inverse).

Time Complexity: O(N+K)


For the third subtask, we notice that M is not a prime. Instead of performing modular division in order to compute (Ni), we need an alternative. We can use a segment tree or balanced binary search tree (similar to this problem) that stores the prime factors. When moving from (Ni) to (Ni+1), we need to remove some prime factors, and add some. For more details, refer to the editorial from that problem.

Time Complexity: O(Nlog2N+K) or better


For the last subtask, N109, meaning that even O(N) is too slow. We need a sublinear algorithm. With some mathematical insight, we can show that (Nodd)=(Neven)=2N1 (If you want a formal proof, open a ticket by clicking the "Report an issue" button at the bottom of the problem description). Thus, we can use Exponentiation By Squaring to precompute the values needed for the xor operation as well.

Time Complexity: O(logN+K)


Comments

There are no comments at the moment.