This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

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.

Suppose `n`

lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are given below:

- Flip all the lights.
- Flip lights with even numbers.
- Flip lights with odd numbers.
- Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...

**Example 1:**

Input:n = 1, m = 1.Output:2Explanation:Status can be: [on], [off]

**Example 2:**

Input:n = 2, m = 1.Output:3Explanation:Status can be: [on, off], [off, on], [off, off]

**Example 3:**

Input:n = 3, m = 1.Output:4Explanation:Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].

**Note:**
`n`

and `m`

both fit in range [0, 1000].

b'

\n\n#### Approach #1: Reduce Search Space [Accepted]

\n

\n#### Approach #2: Mathematical [Accepted]

\n

\n

'
**Intuition**

As the search space is very large ( states of lights, naively operation sequences), let us try to reduce it.

\nThe 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.

\nAlso, 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.

\nFinally, 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**Algorithm**

Say we do the -th operation times. Let\'s first figure out what sets of residues are possible: that is, what sets ( ) are possible.

\nBecause 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.

\nFor 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**Complexity Analysis**

- \n
- \n
Time Complexity: . Our checks are bounded by a constant.

\n \n - \n
Space Complexity: , the size of the data structures used.

\n \n

\n

**Intuition and Algorithm**

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.

\nActually, 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- \n
- Light 1 = 1 + a + c + d \n
- Light 2 = 1 + a + b \n
- Light 3 = 1 + a + c \n
- Light 4 = 1 + a + b + d \n
- Light 5 = 1 + a + c \n
- Light 6 = 1 + a + b \n

So that (modulo 2):

\n- \n
- Light 4 = (Light 1) + (Light 2) + (Light 3) \n
- Light 5 = Light 3 \n
- Light 6 = Light 2 \n

The above justifies taking without loss of generality. The rest is now casework.

\nLet\'s denote the state of lights by the tuple . The transitions are to XOR by or .

\nWhen , all the lights are on, and there is only one state . The answer in this case is always 1.

\nWhen , we could get states , , , or . The answer in this case is either for respectively.

\nWhen , we can manually check that we can get 7 states: all of them except for . The answer in this case is either for respectively.

\nWhen , we can get all 8 states. The answer in this case is either for respectively.

\n\n**Complexity Analysis**

- \n
- Time and Space Complexity: . The entire program uses constants. \n

\n

Analysis written by: @awice.

\n