UVa 10120

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 ${\displaystyle 2i-1}$ meters (${\displaystyle 2i}$ rocks, or going from ${\displaystyle R[k]}$ to ${\displaystyle R[k+2i-1]}$ or ${\displaystyle 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 ${\displaystyle x+(2(i+1)-1)}$ and ${\displaystyle x-(2(i+1)-1)}$ at step ${\displaystyle (i+1)}$. Simplify: ${\displaystyle x+2i+1}$ or ${\displaystyle x-2i-1}$, provided that ${\displaystyle 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 ${\displaystyle M=k^{2}}$ for positive integer k, there's always a solution:

• Note that, ${\displaystyle 1+3+5+7+...+(2k-1)=k^{2}}$. Thus, Frank can arrive at ${\displaystyle 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 ${\displaystyle x-(2k+1)>=1}$, then Frank can arrive at R[x+2] (for ${\displaystyle 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:

${\displaystyle x+2(y-1)-(2k+4(y-1)+1)>=1}$

${\displaystyle x+2(y-1)-2k-4(y-1)-1>=1}$

${\displaystyle x-2k-1-2(y-1)>=1}$

${\displaystyle 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 (${\displaystyle M-k^{2}}$) is even and ${\displaystyle 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 ${\displaystyle (k+2)^{2}>M}$, and by the fundatmental theorem of arithmetics (?) there's a smallest such (k+2), and as a result ${\displaystyle 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[${\displaystyle k^{2}}$], ..., R[M],...., R[${\displaystyle (k+2)^{2}}$],....

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

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

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

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

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

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

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

rearrange:

${\displaystyle k^{2}-(2k+1+M-k^{2}-2)>=1}$

${\displaystyle k^{2}-2k-1-M+k^{2}+2>=1}$

${\displaystyle 2k^{2}-2k+1-M>=1}$

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

${\displaystyle 2k^{2}-2k+1-(k+2)^{2}>=1}$ implies ${\displaystyle 2k^{2}-2k+1-M>=1}$

For if I substract a bigger quantity (${\displaystyle (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 ${\displaystyle 2k^{2}-2k+1-(k+2)^{2}>=1}$:

rearrange:

${\displaystyle 2k^{2}-2k+1-(k^{2}+4k+4)>=1}$

${\displaystyle 2k^{2}-2k+1-k^{2}-4k-4>=1}$

${\displaystyle k^{2}-6k-3>=1}$

${\displaystyle k^{2}-6k-4>=0}$

${\displaystyle k<=(6-sqrt(36+16))/2}$ or ${\displaystyle 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 ${\displaystyle 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.

• N/A

Notes

• Anything special to note about the problem.

Implementations

Notes/Hints on actual implementation here.

Optimizations

Optimizations here.

Input

9 5
12 2
0 0


Output

Don't make fun of me!
Let me try!


1. Reference 1