Physical experiments
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
As you already know, Zenyk and Marichka are preparing for the ZNO. This time — in physics.
Our heroes have an electrical circuit with \(n\) lamps, numbered from 1 to \(n\). Initially, some of them are on, and some are off.
Since Zenyk believes they have prepared enough, he wants to turn off all the lamps and finally go home.
To turn off all the lamps, Zenyk must make exactly \(n\) moves; on the \(i\)-th move, he chooses exactly \(i\) lamps and toggles all of them simultaneously.
Is it possible to turn off all the lamps using such moves?
Input
The first line contains a number \(n\) — the number of lamps.
In the second line, a string \(s\) of \(n\) characters is given — the initial state of the lamps (0 means the corresponding lamp is off, 1 means it is on).
Output
If it is impossible to turn off all the lamps, output No
on a single line.
If the answer exists, output Yes on the first line.
Then, in each of the next \(n\) lines,
output the numbers of the lamps (1-indexed) that Zenyk must toggle on
this move. If there are several answers, output any of them.
Constraints
\(20\%\) of tests: \(1 \le n \le 10\).
\(20\%\) of tests: \(n \le 300\), initially all lamps are in the same state.
\(60\%\) of tests: \(n \le 300\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 101 | Yes 3 2 3 1 2 3 |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 00 | No |
Notes
In the first example, after the first move, the lamps will be in state \(100\), after the second — \(111\), and after the third — \(000\).
In the second example, it can be shown that no matter how Zenyk acts, he will not be able to turn off all the lamps, and therefore he has to study physics further...
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Physical experiments - Editorial
You are given \(n\) lamps, each initially either off or on. You need to make exactly \(n\) moves; on the \(i\)-th of them you choose exactly \(i\) lamps and switch their state to the opposite. Can all lamps be turned off?
As in the previous problem, we describe two solutions: a constructive one and a dynamic programming one.
First, the dynamic programming solution. Let \(dp[i][j]\) be the number of lamps that had to be turned off on the \(i\)-th step so that exactly \(j\) lamps are on after it. Note that there may be several such numbers; in that case \(dp[i][j]\) is any of them. If it is impossible to have exactly \(j\) lamps on after the \(i\)-th step, we keep a special mark in \(dp[i][j]\) (for example, -1).
If after the \(i\)-th step exactly
\(j\) lamps are on and on the \((i + 1)\)-th step we turn off \(k\) lamps, then we turn on \((i + 1 - k)\) lamps. Then the number of
lamps on becomes \((j - k + i + 1 -
k)\). That is, if the state \(dp[i][j]\) is reachable, we can update
\(dp[i + 1][j - k + i + 1 - k]\) with
the value \(k\). If the state \(dp[n][0]\) is unreachable, the answer is
No; otherwise it is Yes. For each state we
know how many lamps had to be turned off on the previous step, so we can
simulate the process. Complexity: \(O(n^3)\) time and \(O(n^2)\) memory.
The constructive solution. Let’s show that from the initial state we can always reach a state where all lamps (except possibly one) are off. First note that the order of the moves does not matter, so we first make \(n\) switches, then \(n - 1\), and so on. We maintain the invariant: after the move with \(i\) switches, at most \(i\) lamps are on. Initially this clearly holds. On each step we first turn off all lamps that are on, and then turn on lamps that are off. If on some step we must switch \(i\) lamps and at its start at most \(i + 1\) lamps were on, this process leaves at most \(i\) lamps on.
Now let’s show that from no state can we reach both a state where all lamps are off with some moves and a state where all but exactly one lamp are off with other moves. This follows from parity: the parities of the total numbers of switches of all lamps in these two final states differ, which is impossible, since the total number of switches to reach each of them is the same, namely \(1 + 2 + \ldots + n = \frac{n \cdot (n + 1)}{2}\), where \(n\) is the initial number of lamps.
So we carry out the process described in the first part, and if all lamps are off in the final state, this is the solution; otherwise there is no solution.
This solution takes \(O(n^2)\) time and memory.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|