#672

Bulb Switcher II

specialist · 925 · lc medium +32 · verified · 50% accepted · 196 likes · top 38%

Description

A room has n bulbs (labeled 1 to n), all initially on, and four buttons:

- Button 1: Flips all bulbs.

- Button 2: Flips bulbs with even labels (i.e., 2, 4, ...).

- Button 3: Flips bulbs with odd labels (i.e., 1, 3, ...).

- Button 4: Flips bulbs at positions j = 3k + 1 for k = 0, 1, 2, ... (i.e., 1, 4, 7, 10, ...).

You must press buttons exactly presses times in total. Given n and presses, return the number of distinct final light configurations that are achievable.

Example 1:

Input: n = 1, presses = 1
Output: 2
Explanation: Status can be:
- [off] by pressing button 1
- [on] by pressing button 2

Example 2:

Input: n = 2, presses = 1
Output: 3
Explanation: Status can be:
- [off, off] by pressing button 1
- [on, off] by pressing button 2
- [off, on] by pressing button 3

Example 3:

Input: n = 3, presses = 1
Output: 4
Explanation: Status can be:
- [off, off, off] by pressing button 1
- [on, off, on] by pressing button 2
- [off, on, off] by pressing button 3
- [off, on, on] by pressing button 4

Code

1
2
3