Question
A robot is located at the top-left corner of a 65 x 45 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)!
-
- Function G_C_D(ByVal x As Long, ByVal y As Long) As Long 'GET Greatest Common Divisor
- Dim temp As Long
- If x > y Then temp = x: x = y: y = temp 'LET X < Y
- Do
- temp = y Mod x
- If temp = 0 Then G_C_D = x: Exit Function
- y = x
- x = temp
- Loop
- End Function
- Function Robot(ByVal N As Long, ByVal m As Long) As String
- If m = 1 Then Robot = 1: Exit Function
- 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
- ReDim nn(1 To m - 1)
- ReDim nm(1 To m - 1)
- For i = 1 To m - 1
- nn(i) = N + m - 1 - i ' (n+m-2)(n+m-3)*....(n-1)
- nm(i) = i ' 1*2*3*...*(m-1)
- Next
- j = 1
- Do While j < m - 1
- j = j + 1
- For i = 1 To m - 1
- temp = G_C_D(nm(j), nn(i)) 'Greatest Common Divisor
- If temp > 1 Then nn(i) = nn(i) / temp: nm(j) = nm(j) / temp
- If nm(j) = 1 Then Exit For
- Next
- Loop
- k = 0
- For i = 1 To m - 1
- If nn(i) > 1 Then
- k = k + 1
- nn(k) = nn(i)
- End If
- Next
- ReDim Preserve nn(1 To k)
- numlen = 1
- ReDim result(1 To numlen)
- result(1) = 1
- i = 0
- Do While i < k
- i = i + 1
- last = 0
- For j = 1 To numlen
- temp = result(j) * nn(i) + last
- result(j) = temp Mod 100000
- last = temp / 100000
- Next
- Do While Not last = 0
- ReDim Preserve result(1 To numlen + 1)
- result(numlen + 1) = last Mod 100000
- last = last / 100000
- numlen = UBound(result)
- Loop
- Loop
- ReDim S(1 To numlen)
- For i = 2 To numlen
- S(i) = Format(result(numlen + 1 - i), "00000")
- Next
- S(1) = result(numlen)
- Erase nn()
- Robot = Join(S, "")
- End Function
- Private Sub Command1_Click()
- MsgBox Robot(65, 45)
- End Sub
It returns:
3927192747782241611458841012775
Later , I use 3 lines python codes to solve it:
- fac=lambda n:[1,0][n>0] or fac(n-1)*n
- robot=lambda m,n: fac(m+n-2)/(fac(m-1)*fac(n-1))
- print robot(65,45)
Conclusion:
It seems VB is the worst choice for numbers calculating.