Strange Light Switches

시간 제한2초메모리 제한2048 MB

요약
원형 이진 문자열에서 한 비트를 양옆 두 비트의 XOR 값으로 바꾸는 연산을 반복해 모두 0으로 만들 수 있는지 판정하고, 길이 3N 이하인 뒤집기 순서를 출력한다.
난이도

어려움10점 중 8점

유형
구현, 시뮬레이션, 수학
정답자
아직 제출이 없습니다

문제

You are the best electrician around and are now working on understanding the wiring in a customer’s house. But you aren’t able tomake heads or tails of it, or even turn them all off! You really want to turn them off so you can start rewiring the place.

All lights in the customer’s house are arranged in a circle. After working on it for a while, you notice a pattern; whenever you flip a switch, the light may or may not change its on/off status - it depends on it’s direct neighbours. That is, whenever the two neighbours of a light are in different on/off states, the switched light turns on (or reamins on if it was already on), and when the two neighbours are both on or both off, the switched light turns off (or remains off if it was already off). You recall is exactly the exclusive or (XOR) function.

Looking for a programmatic solution, you first represent the light switches as a binary string aa of length NN where each digit corresponds to a light. A 1 at index ii means light ii is initially on and a 0 at index ii means light ii is initially off. Now, you can characterize the switching behavior with performing the following operation on the string. For any index 0≤i\<N0≤i\<N, when you flip switch ii the following update occurs:

a\[i]=a\[i−1] XOR a\[i+1]a\[i]=a\[i-1] \text{ XOR } a\[i+1]

Here, the indexing is “wrap-around”, i.e. for i=0i=0 we have that a\[i−1]a\[i-1] really means a\[N−1]a\[N-1] and for i=N−1i=N-1 we have that a\[i+1]a\[i+1] really means a\[0]a\[0].

Your task is to write a program to determine if you can zero the string or not, and report the steps of a solution it is possible.

입력

The first line of input contains the integer NN (3≤N≤1033≤N≤10^3), the number of lights in the house. The following line consists of a string of NN binary digits the initial on/off state of all the lights. A 1 denotes the light being on, and a 0 denotes the light being off. At least one light will initially be on.

출력

If it is possible to turn off all lights, you should output two lines. The first contains a single integer MM (at most 3⋅N3\cdot N) indicating the number of steps in your solution. The second contains the space-separated indices b_1,b_2,…,b_Mb\_1,b\_2,\dots ,b\_M of the sequence of switches you flip. It must be that 0≤b_i≤N−10≤b\_i≤N-1 for each index b_ib\_i you output. This means you first flip b_1b\_1, then b_2b\_2, then b_3b\_3, etc to turn off all lights. You are guaranteed that if there is a solution, there is one that uses at most 3⋅N3\cdot N steps. If there are multiple solutions, you may output any.

If it is not possible to turn off all lights, you should output just a single line containing the integer −1-1.

예제4

  1. 예제 1

    입력
    6
    010101
    
    예상 출력
    3
    1
    3
    5
    
  2. 예제 2

    입력
    5
    11011
    
    예상 출력
    6
    4
    3
    4
    0
    4
    1
    
  3. 예제 3

    입력
    3
    111
    
    예상 출력
    -1
    
  4. 예제 4

    입력
    6
    101101
    
    예상 출력
    -1