fork download
  1. /*
  2. ==> Don't stop when you're tired, stop when you're done.
  3. ==> From the river to the sea, Palestine will be free.
  4. --> @author: MIDORIYA_
  5. */
  6. //*==============================================================
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. typedef long long ll;
  10. typedef double db;
  11. typedef long double ld;
  12. typedef pair<int, int> pii;
  13. typedef pair<ll, ll> pll;
  14. typedef vector<int> vi;
  15. typedef vector<ll> vll;
  16. typedef vector<db> vd;
  17. typedef vector<ld> vld;
  18. typedef vector<bool> vb;
  19. typedef vector<vector<ll>> vvl;
  20. typedef vector<vector<int>> vvi;
  21. typedef vector<pii> vii;
  22. typedef set<int> si;
  23. typedef set<ll> sl;
  24. #define pb push_back
  25. #define pf push_front
  26. #define popb pop_back
  27. #define popf pop_front
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define rall(x) x.rbegin(), x.rend()
  32. #define endl "\n"
  33. const int MOD = 998244353, mod = 1e9 + 7, maxA = 1e5 + 5;
  34. #define time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs";
  35. //*===================>>>FastIO - FileIO<<<=========
  36. void fastIO()
  37. {
  38. ios_base::sync_with_stdio(false);
  39. cin.tie(nullptr);
  40. cout.tie(nullptr);
  41. }
  42. void fileIO()
  43. {
  44. #ifndef ONLINE_JUDGE
  45. freopen("in.txt", "r", stdin);
  46. freopen("out.txt", "w", stdout);
  47. #endif
  48. }
  49. //*===================>>>ONE-FOR-ALL<<<=============
  50. void OneForAll()
  51. {
  52. ll a, b;
  53. cin >> a >> b;
  54.  
  55. ll maxEvenSum = -1;
  56. if (b & 1)
  57. {
  58. if (a & 1)
  59. {
  60. cout << a * b + 1 << endl;
  61. }
  62. else
  63. {
  64. cout << "-1" << endl;
  65. }
  66. return;
  67. }
  68.  
  69. vector<ll> vec;
  70.  
  71. vec.pb(1);
  72. if (b > 1)
  73. vec.pb(b);
  74.  
  75. if (b % 2 == 0)
  76. {
  77. vec.pb(2);
  78. vec.pb(b / 2);
  79. }
  80.  
  81. for (ll k = 1; k * k <= b && k <= 1000; k++)
  82. {
  83. if (b % k == 0)
  84. {
  85. vec.pb(k);
  86. if (k != b / k)
  87. {
  88. vec.pb(b / k);
  89. }
  90. }
  91. }
  92. sort(all(vec));
  93. vec.erase(unique(all(vec)), vec.end());
  94.  
  95. for (ll k : vec)
  96. {
  97. ll sum = a * k + b / k;
  98. if (sum % 2 == 0)
  99. {
  100. maxEvenSum = max(maxEvenSum, sum);
  101. }
  102. }
  103.  
  104. cout << maxEvenSum << endl;
  105. }
  106.  
  107. int main()
  108. {
  109. fastIO();
  110. // fileIO();
  111.  
  112. ll tc = 1, t = 1;
  113. cin >> t;
  114. while (t--)
  115. {
  116. // cout << "Case " << tc++ << ": ";
  117. OneForAll();
  118. }
  119. time;
  120. return 0;
  121. }
Success #stdin #stdout #stderr 0.01s 5324KB
stdin
Standard input is empty
stdout
23328962683090
stderr
Time Taken: 0.006038 Secs