Strange Light Switches
시간 제한2초메모리 제한2048 MB
원형 이진 문자열에서 한 비트를 양옆 두 비트의 XOR 값으로 바꾸는 연산을 반복해 모두 0으로 만들 수 있는지 판정하고, 길이 3N 이하인 뒤집기 순서를 출력한다.
문제
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 of length where each digit corresponds to a light. A 1 at index means light is initially on and a 0 at index means light is initially off. Now, you can characterize the switching behavior with performing the following operation on the string. For any index , when you flip switch the following update occurs:
Here, the indexing is “wrap-around”, i.e. for we have that really means and for we have that really means .
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 (), the number of lights in the house. The following line consists of a string of 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 (at most ) indicating the number of steps in your solution. The second contains the space-separated indices of the sequence of switches you flip. It must be that for each index you output. This means you first flip , then , then , etc to turn off all lights. You are guaranteed that if there is a solution, there is one that uses at most 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 .