Deftly Dodging the Discrete Diagonal

Start at the point ( 0 , 0 ) (0, 0) on the cartesian plane. At any point, one can move either up one unit, or right one unit. The ultimate destination is the point ( 1 0 6 , 1 0 6 ) (10^6, 10^6) .

Calculate the number of unique paths there are from the origin to the destination, and answer it modulo 1 0 9 + 7 10^9 + 7 . But, before you do, there's a little catch:

Points of the form ( 1 0 5 n , 1 0 5 n ) (10^5 n, 10^5 n) are off limits for all even n n , with the implicit exceptions of n { 0 , 10 } n \in \{0, 10\} . Paths that touch any of these points may not be counted towards your total.


Details:

  • This problem is not entirely original. It is based on an old Google Code Jam problem. The link will be posted in the solution, so as not to tempt solvers here!
  • Programming skills make this problem easier to solve, but are far from required. Pencil, paper, and Wolfram (or some other similarly featured calculator) are more than sufficient to compute the solution. Pencil and paper alone, however, are most certainly not enough.
  • Yes, 1 0 9 + 7 10^9 + 7 is prime. This is important.

Image Credit: https://play.google.com/store/apps/details?id=com.bgm.chezzgame&hl=th


The answer is 530023407.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Daniel Ploch
Aug 8, 2014

We'll set P = 1 0 9 + 7 P = 10^9 + 7 for brevity.


Let f ( d ) f(d) be the number of different paths from any point ( x , y ) (x, y) to another point ( x + d , y + d ) (x+d, y+d) , ignoring the forbidden points for now. Since the total number of moves that need to be made is 2 d 2d , and exactly d d of them need to be rightward moves, we have:

f ( d ) = ( 2 d d ) f(d) = \binom{2d}{d}

It's important to note that we can compute f ( d ) f(d) in limited space modulo P P - something which Wolfram will automagically do for you, but that you'll have to implement yourself if using a lesser calculator. This can be accomplished via three observations:

  • ( n k ) mod P n ! k ! ( n k ) ! mod P n ! ( k ! ( n k ) ! ) 1 mod P \binom{n}{k}\:\text{mod}\:P \equiv \frac{n!}{k! \cdot (n-k)!}\:\text{mod}\:P \equiv n! \cdot (k! \cdot (n-k)!)^{-1}\:\text{mod}\:P

  • x 1 mod P x P 2 m o d P x^{-1}\:\text{mod}\:P \equiv x^{P-2} \mod P Euler's Theorem

  • a b mod P a^b\:\text{mod}\:P can be computed in O ( log 2 b ) O(\log_2 b) steps, using exponentiation by squaring

These three things let us compute f ( d ) mod P f(d)\:\text{mod}\:P relatively quickly.


Now, we need to account for the forbidden points. We can do this through a simple application of the inclusion-exclusion principle .

More succinctly, let's define g ( n ) g(n) to be the number of ways to get from ( 0 , 0 ) (0, 0) to ( 2 n 1 0 5 , 2 n 1 0 5 ) (2n \cdot 10^5, 2n \cdot 10^5) , without hitting any forbidden points in between (we ignore the fact that the destination might be a forbidden point for this calculation), and let h ( n ) h(n) be the same as g ( n ) g(n) , except ignoring the forbidden point restriction entirety. Considering all unique-to-order ways there are to sum to 5 5 , the inclusion-exclusion principle gives us:

h ( n ) = f ( 2 1 0 5 n ) h(n) = f(2 \cdot 10^5 \cdot n)

g ( 5 ) = h ( 5 ) 2 h ( 4 ) h ( 1 ) 2 h ( 3 ) h ( 2 ) + 3 h ( 3 ) h 2 ( 1 ) + 3 h 2 ( 2 ) h ( 1 ) 4 h ( 2 ) h 3 ( 1 ) + h 5 ( 1 ) g(5) = h(5) - 2h(4)h(1) - 2h(3)h(2) + 3h(3)h^2(1) + 3h^2(2)h(1) - 4h(2)h^3(1) + h^5(1)

We can use a calculator to get h ( n ) mod P h(n)\:\text{mod}\:P for all necessary n n . Then, we can plug into the above formula, and arrive at the answer:

g ( 5 ) 530023407 mod P g(5) \equiv 530023407\:\text{mod}\:P


The inspiration for this problem can be found here . It is quite a bit more difficult than this one, and requires some strong programming skills to solve!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...