fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <assert.h>
  5. using namespace std;
  6.  
  7. long long a, b;
  8. long long S, P;
  9.  
  10. long long sqrt (long long s) {
  11. long long d = 0, c = (long long)(1e8), res = -1;
  12. while (d <= c) {
  13. long long g = (d + c) / 2;
  14. if (g * g <= s) {
  15. res = g;
  16. d = g + 1;
  17. } else c = g - 1;
  18. }
  19. return res;
  20. }
  21.  
  22. int main() {
  23. cin >> a >> b;
  24. S = a + b;
  25. long long root = sqrt(S);
  26. long long root_of_a = sqrt(a);
  27. long long root_of_b = sqrt(b);
  28. P = (long long)(1e18);
  29.  
  30.  
  31. vector < long long > diva;
  32. vector < long long > divb;
  33.  
  34. for (long long i = 1; i <= root_of_a; i++)
  35. if (a % i == 0)
  36. diva.push_back(i);
  37.  
  38. for (long long i = 1; i <= root_of_b; i++)
  39. if (b % i == 0)
  40. divb.push_back(i);
  41.  
  42. for (long long i = 1; i <= root; i++) {
  43. if (S % i == 0) {
  44. auto it = lower_bound(diva.begin(), diva.end(), i);
  45. if(it != diva.end()) {
  46. if(*it > i && it != diva.begin()) it--;
  47. if(*it <= i && a / *it <= S/i)
  48. P = min(P, 1LL * 2*(i + S/i));
  49. }
  50. else {
  51. it--;
  52. if (a / *it <= S / i)
  53. P = min(P, 1LL * 2*(i + S/i));
  54. }
  55.  
  56. it = lower_bound(divb.begin(), divb.end(), i);
  57. if(it != divb.end()) {
  58. if(*it > i && it != divb.begin()) it--;
  59. if(*it <= i && b / *it <= S/i)
  60. P = min(P, 1LL * 2*(i + S/i));
  61. }
  62. else {
  63. it--;
  64. if (b / *it <= S / i)
  65. P = min(P, 1LL * 2*(i + S/i));
  66. }
  67. }
  68. }
  69.  
  70. cout << P << '\n';
  71. return 0;
  72. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
1000000000000000000