• Source
    1. #include <iostream>
    2. #include <vector>
    3. #include <algorithm>
    4. #include <math.h>
    5. using namespace std;
    6.  
    7. long long a, b;
    8. vector <long long> uocA;
    9. vector <long long> uocB;
    10.  
    11. void khoitaoUocA (long long n)
    12. {
    13. if (sqrt (n)*sqrt (n)==n) uocA.push_back(sqrt(n));
    14. for (int i=1; i<sqrt (n); i++)
    15. {
    16. if (n%i==0)
    17. {
    18. uocA.push_back(i);
    19. uocA.push_back(n/i);
    20. }
    21. }
    22. }
    23.  
    24. void khoitaoUocB (long long n)
    25. {
    26. if (sqrt (n)*sqrt (n)==n) uocB.push_back(sqrt(n));
    27. for (int i=1; i<sqrt (n); i++)
    28. {
    29. if (n%i==0)
    30. {
    31. uocB.push_back(i);
    32. uocB.push_back(n/i);
    33. }
    34. }
    35. }
    36.  
    37. int main ()
    38. {
    39. cin>>a>>b;
    40. if (a==b) cout<<"0";
    41. else
    42. {
    43. khoitaoUocA (a);
    44. khoitaoUocB (b);
    45. int kq=2000000;
    46. for (int i=uocB.size()-1; i>=0; i--)
    47. {
    48. for (int j=uocA.size()-1; j>=0; j--)
    49. {
    50. if (uocB[i]==uocA[j])
    51. {
    52. long long p1=uocB[i];
    53. long long p2=uocA[j];
    54. long long p1c=b/p1;
    55. long long p2c=a/p2;
    56.  
    57. int demB1=0;
    58. while (1)
    59. {
    60. if (p1c%5==0)
    61. {
    62. p1c=p1c/5;
    63. demB1++;
    64. }
    65. else if (p1c%3==0)
    66. {
    67. p1c=p1c/3;
    68. demB1++;
    69. }
    70. else if (p1c%2==0)
    71. {
    72. p1c=p1c/2;
    73. demB1++;
    74. }
    75. else break;
    76. }
    77. int demB2=0;
    78. while (1)
    79. {
    80. if (p2c%5==0)
    81. {
    82. p2c=p2c/5;
    83. demB2++;
    84. }
    85. else if (p2c%3==0)
    86. {
    87. p2c=p2c/3;
    88. demB2++;
    89. }
    90. else if (p2c%2==0)
    91. {
    92. p2c=p2c/2;
    93. demB2++;
    94. }
    95. else break;
    96. }
    97. if (p1c==1 && p2c==1) kq = min (kq, demB1+demB2);
    98. break;
    99. }
    100. }
    101. }
    102. if (kq==2000000) cout<<"-1";
    103. else cout<<kq;
    104. }
    105.  
    106. return 0;
    107. }