• Source
    1. def lineal(a,d)
    2. #p "lineal: #{a},#{d}"
    3. q=[[a,0]]
    4. until q.empty?
    5. #p q
    6. node,dist=q.shift
    7. dist+=1
    8. i=1
    9. loop{
    10. break if (i+=1)**2>node
    11. next if 0!=node%i
    12. [i,node/i].each{|j|
    13. t=j+1
    14. return dist if t==d
    15. q.push([t,dist]) if t>d
    16. }
    17. }
    18. end
    19. nil
    20. end
    21. def maketree(root,min)
    22. just=[]
    23. #p min
    24. t=(f=->current,parent{
    25. #p "f: #{current}"
    26. ret=[current,parent,children=[]]
    27. just.push(ret) if current==min
    28. return ret if current<=min
    29. i=1
    30. loop {
    31. break if (i+=1)**2>current
    32. next if 0!=current%i
    33. children.push(f[current/i+1,ret])
    34. children.push(f[i+1,ret]) if i+1>=min
    35. }
    36. ret
    37. })[root,nil]
    38. just
    39. end
    40. def solve(input)
    41. root,*c=input.split(/\D/).map(&:to_i)
    42. small,big=c.sort
    43. min=lineal(big,small)
    44. return min if min==1||big==root
    45. just=maketree(root,big)
    46. just.each{|leaf|
    47. node,i=leaf,0
    48. while node=node[1]
    49. i+=1
    50. dist=lineal(node[0],small) or next
    51. min=i+dist if !min||min>i+dist
    52. end
    53. }
    54. min
    55. end
    56.  
    57. def test(n,input,expected)
    58. actual=solve(input).to_s
    59. puts "#{n}: "+(actual==expected ? "ok":"ng ( #{actual} against #{expected} for #{input} )")
    60. end
    61.  
    62. test( 0, "50:6,3", "1" )
    63. test( 1, "98:5,11", "4" )
    64. test( 2, "1000:33,20", "7" )
    65. test( 3, "514:9,18", "8" )
    66. test( 4, "961:5,4", "3" )
    67. test( 5, "1369:1369,3", "2" )
    68. test( 6, "258:16,12", "5" )
    69. test( 7, "235:13,3", "2" )
    70. test( 8, "1096:19,17", "8" )
    71. test( 9, "847:7,17", "6" )
    72. test( 10, "1932:3,5", "2" )
    73. test( 11, "2491:4,8", "3" )
    74. test( 12, "840:421,36", "2" )
    75. test( 13, "1430:37,111", "3" )
    76. test( 14, "496:17,9", "2" )
    77. test( 15, "891:6,10", "1" )
    78. test( 16, "1560:196,21", "2" )
    79. test( 17, "516:20,12", "5" )
    80. test( 18, "696:30,59", "2" )
    81. test( 19, "1760:5,441", "2" )
    82. test( 20, "1736:11,26", "5" )
    83. test( 21, "1518:17,34", "4" )
    84. test( 22, "806:63,16", "5" )
    85. test( 23, "1920:3,97", "2" )
    86. test( 24, "1150:13,22", "4" )
    87. test( 25, "920:116,5", "1" )
    88. test( 26, "2016:7,337", "2" )
    89. test( 27, "408:9,25", "2" )
    90. test( 28, "735:36,8", "2" )
    91. test( 29, "470:5,31", "2" )
    92. test( 30, "2100:12,351", "3" )
    93. test( 31, "870:36,10", "1" )
    94. test( 32, "1512:253,13", "2" )
    95. test( 33, "697:12,15", "3" )
    96. test( 34, "1224:5,14", "2" )
    97. test( 35, "986:125,17", "3" )
    98. test( 36, "864:12,13", "3" )
    99. test( 37, "500:21,51", "2" )
    100. test( 38, "819:33,21", "4" )
    101. test( 39, "594:55,3", "2" )
    102. test( 40, "638:17,24", "3" )
    103.