UVa 10120 - Gift?!

UVa 10120 - Gift?!

 * http://acm.uva.es/p/v101/10120.html

Summary
A river is laid out with rocks (R) as follows:
 * [left bank] R[1] R[2] R[3] R[4] ... R[N] [right bank]
 * The distance between [left bank] and R[1], R[N] and [right bank], R[i] and R[i+1] (1<=i<N), are all exactly 1 meter.
 * A frog, Frank, is trying to reach R[M] starting from the left bank, such that:
 * The first jump he mades jumps from the [left bank] onto R[1],
 * Each subsequent jump he must jump over 2*i-1 meters (2*i rocks, or going from R[k] to R[k+2*i-1] or R[k-(2*i-1)].
 * He cannot reach [left bank] or [righr bank] at any time after his first jump.

Is it possible for Frank it reach R[M]?

Explanation
You can do this with simulation for small number of N (In this problem 2<=N<=10^6, 2<=M<=N). If N is small (N<=100), we can apply the following induction:

At each step, we have a set of locations that Frank can reach.


 * At step 1, Frank can reach {1}.
 * For each location x Frank can reach at step i, Frank can reach x+(2*(i+1)-1) and x-(2*(i+1)-1) at step (i+1). Simplify: x+2*i+1 or x-2*i-1, provided that 1<=x<=N.


 * Thus, we'll have an array of sets telling us precisely at each step the locations that Frank can be on. If M appears in any of those sets, then it means that at some step i, Frank can be on R[M] (it is possible).


 * Note that for a set on step i>1 to be non-empty, set on step (i-1) must be non-empty. Since at one point of the simulation, Frank will be getting "out of bounce" no matter how he jumps (he jumps too far to be remained on some rock), and thus the simulation can terminate since no more rocks will be reachable from that point on.


 * For bigger N, the simulation will get TLE. It is conjectured that for N>=49, it is always possible to reach to the other side. (If anyone knows the prove, please enlighten your fellow solvers!).

Gotchas

 * Any points one can easily overlook?
 * The correct way to understand ambiguous formulations?

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
Input Here

Output
Output Here