Official

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

GPT 5.2 High

Overview

Each light’s final state is determined solely by “whether the number of presses is even or odd,” so this is a problem of flipping Yes/No based on the parity of \(K_i\).

Analysis

Each light is independent, and its state toggles with each press of the switch. The key insight here is that repeated toggling has a period of \(2\).

  • Pressing \(K_i\) times means toggling \(K_i\) times
  • Toggling twice returns to the original state (ON→OFF→ON, OFF→ON→OFF)

Therefore, - If \(K_i\) is even: the final state is the same as the initial state - If \(K_i\) is odd: the final state is the opposite of the initial state

Naively “looping \(K_i\) times to toggle” would not work since \(K_i\) can be up to \(10^9\), meaning even a single light would be too slow (TLE). Since we only need to check the parity (\(K_i \bmod 2\)), each line can be processed in \(O(1)\).

Concrete examples: - Initial Yes (ON), \(K=3\) (odd):
Yes → No → Yes → No, so the final result is No - Initial No (OFF), \(K=4\) (even):
After 4 toggles it returns to the original state, so the final result is No

Algorithm

For each light \(i\), do the following:

  1. Read string \(S_i\) (Yes or No) and integer \(K_i\)
  2. Compute \(K_i \bmod 2\)
    • If \(K_i\) is even, output \(S_i\) as is
    • If \(K_i\) is odd, output the opposite of \(S_i\)
      • Yes → No
      • No → Yes

Repeat this for all \(N\) lights.

Complexity

  • Time complexity: \(O(N)\) (each light is processed once)
  • Space complexity: \(O(N)\) (if storing output in an array and printing at the end; \(O(1)\) is also possible with sequential output)

Implementation Notes

  • Do not actually toggle \(K_i\) times; always just check \(K_i \bmod 2\) (parity).

  • Since the input can be up to \(10^5\) lines, in Python it is safer to read input quickly using sys.stdin.buffer.read().split() or similar methods.

  • Flipping Yes/No can be easily written with a conditional ("No" if s=="Yes" else "Yes").

    Source Code

import sys

def main():
    it = iter(sys.stdin.buffer.read().split())
    n = int(next(it))
    out = []
    for _ in range(n):
        s = next(it).decode()
        k = int(next(it))
        if k % 2 == 0:
            out.append(s)
        else:
            out.append("No" if s == "Yes" else "Yes")
    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.