Official

A - スイッチの切り替え / Flipping Switches Editorial by admin Editorial for printing

DeepSeek V3

Overview

Given the initial state of each light and the number of times a switch is pressed, determine the final state. Pressing the switch toggles the state of the light.

Analysis

For each light, if the number of switch presses \(K_i\) is odd, the state is toggled from the initial state. If it is even, the state remains the same as the initial state. This follows from the property that pressing the switch once toggles the state, and pressing it twice returns it to the original state. In other words, by focusing on the remainder of \(K_i\) divided by 2, we can efficiently determine the final state.

Algorithm

  1. For each light, represent the initial state numerically: 1 if “Yes”, 0 if “No”.
  2. Determine whether the number of switch presses \(K_i\) is odd or even.
  3. If \(K_i\) is odd, toggle the initial state (1 becomes 0, 0 becomes 1). If even, keep the initial state as is.
  4. Convert the final state to “Yes” or “No” and output it.

Complexity

  • Time complexity: \(O(N)\) The processing for each light takes constant time, so it is proportional to the input size \(N\).
  • Space complexity: \(O(N)\) The size of the list to store the results is \(N\).

Implementation Notes

  • By converting the initial state from a string to a numerical value (0 or 1), toggling the state becomes straightforward.

  • The modulo operation (% 2) is used to determine whether the number of switch presses is odd or even.

  • Toggling the state can be simply expressed with the formula 1 - initial_state.

    Source Code

import sys

def main():
    data = sys.stdin.read().splitlines()
    n = int(data[0])
    results = []
    for i in range(1, n + 1):
        parts = data[i].split()
        s = parts[0]
        k = int(parts[1])
        
        initial_state = 1 if s == "Yes" else 0
        total_flips = k
        final_state = initial_state
        
        if total_flips % 2 == 1:
            final_state = 1 - initial_state
            
        results.append("Yes" if final_state == 1 else "No")
    
    for res in results:
        print(res)

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

posted:
last update: