UVa 10120

From Algorithmist
Jump to navigation Jump to search

UVa 10120 - Gift?![edit]


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 meters ( rocks, or going from to or .
    • He cannot reach [left bank] or [righr bank] at any time after his first jump.

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


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 and at step . Simplify: or , provided that .
  • 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[edit]


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

(1.1) When for positive integer k, there's always a solution:

  • Note that, . Thus, Frank can arrive at 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 , then Frank can arrive at R[x+2] (for ):

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:

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 () is even and :

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 , and by the fundatmental theorem of arithmetics (?) there's a smallest such (k+2), and as a result .

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[], ..., R[M],...., R[],....

The above list is not accurate when , but when that is the case, we simply use strategy (1.1).

Since is even and , we can express , or M = x + 2y, where and .

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

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

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

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

(2.2.3) We need a condition for k such that is true: Or


Now, since , we'll have:


For if I substract a bigger quantity () 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 :


Solve the quadratic inequality:

or .

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

In other words, the strategy apply when .

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


  • Anything special to note about the problem.


Notes/Hints on actual implementation here.


Optimizations here.


9 5
12 2
0 0


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


  1. Reference 1