fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int main() {
  6. long long n, x, y;
  7. cin >> n >> x >> y;
  8.  
  9. if (x > y) {
  10. swap(x, y); // Ensure x is always the faster one
  11. }
  12.  
  13. // Binary search over time to find the minimum time required
  14. long long low = 0, high = (n - 1) * y; // Maximum time possible
  15. long long answer = high;
  16.  
  17. while (low <= high) {
  18. long long mid = (low + high) / 2;
  19.  
  20. // How many copies can be made in 'mid' seconds
  21. long long copies = mid / x + mid / y;
  22.  
  23. // We already have the original copy, so we need (n - 1) more copies
  24. if (copies >= n - 1) {
  25. answer = mid;
  26. high = mid - 1; // Try to find a smaller time
  27. } else {
  28. low = mid + 1; // Increase the time
  29. }
  30. }
  31.  
  32. // Add the time for the first copy
  33. cout << answer + x << endl;
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5300KB
stdin
5 1 2
stdout
4