Does anyone know how to solve this problem?
At the entrance to a cave is a rotating round table. On top of the table are $n$ identical barrels, evenly spaced along its circumference. Inside each barrel is a herring either with its head up or its head down. In a move, Ali Baba chooses from $1$ to $n$ of the barrels and turns them upside down. Then the table spins around. When it stops, it is impossible to tell which barrels have been turned over. The cave will open if the heads of the herrings in all $n$ barrels are up or are all down. Determine all values of $n$ for which Ali Baba can open the cave in a finite number of moves.
International Mathematics TOURNAMENT OF THE TOWNS Senior A-Level Paper Fall 2009.
I'm not sure I've properly understood your description of the problem. As I understand it, it appears to be equivalent to the following:
The last bullet point is the part that I'm not entirely sure about, since it ignores the seemingly redundant specification that the barrels are on a rotating circular platform. I'm assuming that on each round you will have a barrel in front of you, and that barrel is the only one whose switch you're able to press. I'm also assuming, however, that you know the value of $\ N\ $. If that's the case, then you can simply label the barrels $\ B_1,$$\,B_2,$$\ \dots,$$\, B_N\ $ in the order in which they come to you, and as long as you keep count of the number of rounds that have passed, you will always know the label borne by the barrel currently in front of you. When $\ B_i\ $ is the next barrel whose switch you want to press, you simply have to wait until it rotates around to the position in front of you.
Assuming my understanding of the problem is correct, then there's always a sequence of switch presses (for any value of $\ N\ $) which guarantees that all the lights will eventually come on. You can represent the statuses of both the switches and the lights by $\ 1\times N\ $ binary vectors $\ s\ $ and $\ \ell\ $, respectively, where \begin{align} s_i&=\cases{0&if you've pressed the switch on $\ B_i\ $ an even\\ &number of times\\ 1&if you've pressed it an odd number of times}\\ \ell_i&=\cases{0&if the light in $\ B_i\ $ is off\\ 1&if it's on} \end{align} Although you never know the value of $\ \ell\ $ until it reaches $\ (1,1,\dots,1)\ $, you do know that $\ \ell_i\equiv\ell(0)_i+s_i\pmod{2}\ $, where $\ \ell(0)\ $ is the initial status of the lights and $\ s\ $ is the current status of the switches. Therefore, to guarantee that the lights will all eventually come on, you merely need to cycle the status of the switches through all $\ 2^N\ $ of its possible values. When $\ s_i\equiv\ell(0)_i+1\pmod{2}\ $ for all $\ i\ $, then all the lights will be on.
The most convenient way of cycling through all $\ 2^N\ $ values of the switches' status is to do it in Gray code order: if you have pressed a total of $\ n\ $ switches, and $\ n+1=2^rk\ $, where $\ r\ $ is a non-negative integer, and $\ k\ $ is an odd positive integer, then the next switch you press should be the one on $\ B_{r+1}\ $ ($\ r\ $ is uniquely determined).