Google Treasure Hunt 2008Robot Problem

 

Question

A robot is located at the top-left corner of a 6545  grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Note: The grid below is 7x3, and is used to illustrate the problem. It is not drawn to scale.

 

 


 

*Image not to scale.

How many possible unique paths are there?
(Note: Answer must be an exact, decimal representation of the number.)


For a M*N grid,the result is as simple as  (N+M-2)!/(N-1)!/(M-1)!

    1. Function  G_C_D(ByVal x As LongByVal y As LongAs Long    'GET Greatest Common Divisor
    2.     Dim temp As Long
    3.     If x > y Then temp = x: x = y: y = temp   'LET X < Y
    4.     Do
    5.         temp = y Mod x
    6.         If temp = 0 Then G_C_D = x: Exit Function
    7.         y = x
    8.         x = temp
    9.     Loop
    10. End Function
    11. Function Robot(ByVal N As LongByVal m As LongAs String
    12.     If m = 1 Then Robot = 1: Exit Function
    13.     Dim S() As String, result() As Long, nn(), nm(), i As Long, j As Long, k As Long, temp As Long, numlen As Long, last As Long
    14.     ReDim nn(1 To m - 1)
    15.     ReDim nm(1 To m - 1)
    16.     For i = 1 To m - 1
    17.         nn(i) = N + m - 1 - i    ' (n+m-2)(n+m-3)*....(n-1)
    18.         nm(i) = i    ' 1*2*3*...*(m-1)
    19.     Next
    20.     j = 1
    21.     Do While j < m - 1
    22.         j = j + 1
    23.         For i = 1 To m - 1
    24.             temp = G_C_D(nm(j), nn(i))    'Greatest Common Divisor
    25.             If temp > 1 Then nn(i) = nn(i) / temp: nm(j) = nm(j) / temp
    26.             If nm(j) = 1 Then Exit For
    27.         Next
    28.     Loop
    29.     k = 0
    30.     For i = 1 To m - 1
    31.         If nn(i) > 1 Then
    32.             k = k + 1
    33.             nn(k) = nn(i)
    34.         End If
    35.     Next
    36.     ReDim Preserve nn(1 To k)
    37.     numlen = 1
    38.     ReDim result(1 To numlen)
    39.     result(1) = 1
    40.     i = 0
    41.     Do While i < k
    42.         i = i + 1
    43.         last = 0
    44.         For j = 1 To numlen
    45.             temp = result(j) * nn(i) + last
    46.             result(j) = temp Mod 100000
    47.             last = temp / 100000
    48.         Next
    49.         Do While Not last = 0
    50.             ReDim Preserve result(1 To numlen + 1)
    51.             result(numlen + 1) = last Mod 100000
    52.             last = last / 100000
    53.             numlen = UBound(result)
    54.         Loop
    55.     Loop
    56.     ReDim S(1 To numlen)
    57.     For i = 2 To numlen
    58.         S(i) = Format(result(numlen + 1 - i), "00000")
    59.     Next
    60.     S(1) = result(numlen)
    61.     Erase nn()
    62.     Robot = Join(S, "")
    63. End Function
    64. Private Sub Command1_Click()
    65. MsgBox Robot(65, 45)
    66. End Sub

 

It returns:

3927192747782241611458841012775

 

 

Later , I use 3 lines python codes to solve it:

 

  1. fac=lambda n:[1,0][n>0or fac(n-1)*n
  2. robot=lambda m,n: fac(m+n-2)/(fac(m-1)*fac(n-1)) 
  3. print robot(65,45

 

Conclusion:

 

It seems VB is the worst choice for numbers calculating.

原文地址:https://www.cnblogs.com/fengju/p/6336233.html