Google Treasure Hunt 2008--Robot Problem

Question

A robot is located at the top-left corner of a65 x45 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.)

Your answer:

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

    1. Function G_C_D(ByValxAsLong,ByValyAsLong)AsLong'GETGreatestCommonDivisor
    2. DimtempAsLong
    3. Ifx>yThentemp=x:x=y:y=temp'LETX<Y
    4. Do
    5. temp=yModx
    6. Iftemp=0ThenG_C_D=x:ExitFunction
    7. y=x
    8. x=temp
    9. Loop
    10. EndFunction
    11. FunctionRobot(ByValNAsLong,ByValmAsLong)AsString
    12. Ifm=1ThenRobot=1:ExitFunction
    13. DimS()AsString,result()AsLong,nn(),nm(),iAsLong,jAsLong,kAsLong,tempAsLong,numlenAsLong,lastAsLong
    14. ReDimnn(1Tom-1)
    15. ReDimnm(1Tom-1)
    16. Fori=1Tom-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. DoWhilej<m-1
    22. j=j+1
    23. Fori=1Tom-1
    24. temp=G_C_D(nm(j),nn(i))'GreatestCommonDivisor
    25. Iftemp>1Thennn(i)=nn(i)/temp:nm(j)=nm(j)/temp
    26. Ifnm(j)=1ThenExitFor
    27. Next
    28. Loop
    29. k=0
    30. Fori=1Tom-1
    31. Ifnn(i)>1Then
    32. k=k+1
    33. nn(k)=nn(i)
    34. EndIf
    35. Next
    36. ReDimPreservenn(1Tok)
    37. numlen=1
    38. ReDimresult(1Tonumlen)
    39. result(1)=1
    40. i=0
    41. DoWhilei<k
    42. i=i+1
    43. last=0
    44. Forj=1Tonumlen
    45. temp=result(j)*nn(i)+last
    46. result(j)=tempMod100000
    47. last=temp\100000
    48. Next
    49. DoWhileNotlast=0
    50. ReDimPreserveresult(1Tonumlen+1)
    51. result(numlen+1)=lastMod100000
    52. last=last\100000
    53. numlen=UBound(result)
    54. Loop
    55. Loop
    56. ReDimS(1Tonumlen)
    57. Fori=2Tonumlen
    58. S(i)=Format(result(numlen+1-i),"00000")
    59. Next
    60. S(1)=result(numlen)
    61. Erasenn()
    62. Robot=Join(S,"")
    63. EndFunction
    64. PrivateSubCommand1_Click()
    65. MsgBoxRobot(65,45)
    66. EndSub

It returns:

3927192747782241611458841012775

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

  1. fac=lambdan:[1,0][n>0]orfac(n-1)*n
  2. robot=lambdam,n:fac(m+n-2)/(fac(m-1)*fac(n-1))
  3. printrobot(65,45)

Conclusion:

It seemsVB is the worst choice for numbers calculating.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值