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 $$2i-1$$ meters ($$2i$$ rocks, or going from $$R[k]$$ to $$R[k+2i-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+2i+1$$ or $$x-2i-1$$, provided that $$1\leq x\leq 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, the 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. We should next prove that for N>=49, any problem instance P(N,M) (49<=N and M<=N) should return true.

Prove Tautological Property for N>=49
Proof:

(1) Let's look at some specific strategies:

(1.1) When $$M = k^2$$ for positive integer k, there's always a solution:


 * Note that, $$1+3+5+7+...+(2k-1) = k^2$$. Thus, Frank can arrive at $$k^2$$ by simply keep jumping forward.

(1.2) If at any time Frank is at R[x], and the next jump is (2k+1) meters for some k, then:

If $$x-(2k+1)>=1$$, then Frank can arrive at R[x+2] (for $$x+2<=M$$):

Proof 1.2:

Assume Frank is at R[x] and the next jump is (2k+1).

Furthermore, assume that x-(2k+1)>=1:

Frank can jump to the left, arriving at (x-(2k+1)) and then right, arriving at (x-(2k+1)+(2k+3)) = (x+2). That is, Frank can jump left to arrive at some rock (x-(2k+1)), then jump right to arrive at R[x+2]. Since (x-(2k+1))>=1, and x+2<=N, Frank's moves are valid.

(1.3) Similarly, if we are at R[x], and our next jump is 2k+1, and x-((2k+1)+(y-1)*2)>=1, then we can travel from R[x] to R[x+2y] (0<=y and x+2y<=N):

Why x - ((2k+1)+(y-1)*2)>=1 ? Because if we use the strategy of jump left, then right; the first pair of jumps give us left boundary x-(2k+1), and each addition pair of jumps pushes our left boundary further left by 2.

Proof 1.3: Assume x-((2k+1)+(y-1)*2)>=1, 0<=y and x+2y<=N:

For y = 0, x = x + 2y, we are there!

For y = 1, x - (2k+1) = x-((2k+1)+(y-1)*2) >= 1. Thus, by (1.2), we can travel from x to x + 2 = x + 2y.

For y>1:

By induction, assume that we can travel from x to x + 2(y-1) = x+2y-2.

By assumption, x-((2k+1)+(y-1)*2)>=1.

If the next jump after x is 2k+1, then the next jump after x+2(y-1) is 2*(k+2(y-1))+1, because we need 2 jumps to advance from x to x+2 using our strategy from (1.2), i.e. same number of jumps for the number of advances (in multiples of 2).

In order for our induction to be valid, we must have (current location) - (next jump) >=1, or in this case (x+2(y-1)) - (2*(k+2(y-1))+1) >=1:

Rearrange the inequality:

$$ x+2(y-1)-(2k+4(y-1)+1)>=1 $$

$$ x+2(y-1)-2k-4(y-1)-1>=1$$

$$ x-2k-1-2(y-1)>=1$$

$$ x-(2k+1+2(y-1))>=1$$

which is equivalent to our assumption, thus our induction is valid.

(2) Now, we propose a general strategy for going to R[M]:

First, we list out the rocks:

R[1], R[2], ... ,R[M],...

(2.1) There exist a k such that ($$M-k^2$$) is even and $$k^2<=M<(k+2)^2$$:

If M is odd, we let k be odd, and since the sequence of squares of k (1,9,25,49,81,...) are strictly increasing, we can find a proper k. Or, there's always a (k+2) such that $$ (k+2)^2 > M $$, and by the fundatmental theorem of arithmetics (?) there's a smallest such (k+2), and as a result $$k^2<=M$$.

Similar argument applies when M is even.

(2.2) Now, find such a k described in (2.1):

Our Rocks:

R[1], R[2], ..., R[$$k^2$$], ..., R[M],...., R[$$(k+2)^2$$],....

The above list is not accurate when $$ k^2 = M $$, but when that is the case, we simply use strategy (1.1).

Since $$M-k^2$$ is even and $$k^2<=M$$, we can express $$M = k^2 + 2r$$, or M = x + 2y, where $$x = k^2$$ and $$y = (M-k^2)/2$$.

Thus, by (1.3), if we can arrive at R[$$k^2$$], and our next jump is 2k+1, and $$k^2-((2k+1)+2(y-1))>=1$$, then we can arrive at R[M].

(2.2.1) We can always arrive at R[$$k^2$$], by using strategy in (1.1).

(2.2.2) When we arrive at R[$$k^2$$], our next jump will be 2k+1:

Recall $$k^2 = 1+3+5+...+(2k-1)$$, thus when we are at $$k^2$$, the next avaliable jump (after 2k-1) is 2k+1.

(2.2.3) We need a condition for k such that $$k^2-((2k+1)+2(y-1))>=1$$ is true: Or $$k^2-((2k+1)+2((M-k^2)/2-1))>=1$$

rearrange:

$$k^2-(2k+1+M-k^2-2)>=1$$

$$k^2-2k-1-M+k^2+2>=1$$

$$2k^2-2k+1-M>=1$$

Now, since $$(k+2)^2>M$$, we'll have:

$$2k^2-2k+1-(k+2)^2>=1$$ implies  $$2k^2-2k+1-M>=1$$

For if I substract a bigger quantity ($$(k+2)^2$$) and will be larger then 1, then substracting a smaller quantity (M), the result should still be larger then 1.

we now need to prove that $$2k^2-2k+1-(k+2)^2>=1$$:

rearrange:

$$2k^2-2k+1-(k^2+4k+4)>=1$$

$$2k^2-2k+1-k^2-4k-4>=1$$

$$k^2-6k-3>=1$$

$$k^2-6k-4>=0$$

Solve the quadratic inequality:

$$k<= (6-sqrt(36+16))/2$$ or $$k>=(6+sqrt(36+16))/2$$.

Which means, our strategy applies when k >= 6.7 > (6+sqrt(36+16))/2

In other words, the strategy apply when $$k >= 7 = 49$$.

Thus, for M>=49, there's always a solution.

For M<49,

if N>=49, there's always a solution, since when N = 49, we can reach 1..49

if N<49, then we can use the above simulation approach to solve the problem.

Gotchas

 * N/A

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
Input Here

Output
Output Here