There is a room with
n lights which are turned on initially and 4 buttons on the wall. After performing exactly
m unknown operations towards buttons, you need to return how many different kinds of status of the
n lights could be.
n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:
Input: n = 1, m = 1. Output: 2 Explanation: Status can be: [on], [off]
Input: n = 2, m = 1. Output: 3 Explanation: Status can be: [on, off], [off, on], [off, off]
Input: n = 3, m = 1. Output: 4 Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].
m both fit in range [0, 1000].
As the search space is very large ( states of lights, naively operation sequences), let us try to reduce it.\n
The first 6 lights uniquely determine the rest of the lights. This is because every operation that modifies the -th light also modifies the -th light.\n
Also, operations commute: doing operation A followed by B is the same as doing operation B followed by A. So we can assume we do all the operations in order.\n
Finally, doing the same operation twice in a row is the same as doing nothing. So we only need to consider whether each operation was done 0 or 1 times.\n
Say we do the -th operation times. Let\'s first figure out what sets of residues are possible: that is, what sets ( ) are possible.\n
Because and , if , or if , it isn\'t possible. Otherwise, it is possible by a simple construction: do the operations specified by , then do operation number 1 with the even number of operations you have left.\n
For each possible set of residues, let\'s simulate and remember what the first 6 lights will look like, storing it in a Set structure
seen. At the end, we\'ll return the size of this set.
In Java, we make use of bit manipulations to manage the state of lights, where in Python we simulate it directly.\n\n
Time Complexity: . Our checks are bounded by a constant.\n
Space Complexity: , the size of the data structures used.\n
Intuition and Algorithm\n
As before, the first 6 lights uniquely determine the rest of the lights. This is because every operation that modifies the -th light also modifies the -th light, so the -th light is always equal to the -th light.\n
Actually, the first 3 lights uniquely determine the rest of the sequence, as shown by the table below for performing the operations a, b, c, d:\n
So that (modulo 2):\n
The above justifies taking without loss of generality. The rest is now casework.\n
Let\'s denote the state of lights by the tuple . The transitions are to XOR by or .\n
When , all the lights are on, and there is only one state . The answer in this case is always 1.\n
When , we could get states , , , or . The answer in this case is either for respectively.\n
When , we can manually check that we can get 7 states: all of them except for . The answer in this case is either for respectively.\n
When , we can get all 8 states. The answer in this case is either for respectively.\n\n
Analysis written by: @awice.\n