Problem p03117 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  tourist
 *    created: 27.01.2020 01:09:52
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long r, b;
  cin >> r >> b;
  auto Count = [&](long long p, long long q, long long bound) {
    long long sx = 0;
    long long sy = 0;
    long long cnt = 0;
    for (long long x = 0; x * p <= bound; x++) {
      long long y = (bound - x * p) / q;
      sx += x * (y + 1);
      sy += y * (y + 1) / 2;
      cnt += y + 1;
    }
    return make_tuple(sx, sy, cnt);
  };
  auto Test = [&](long long p, long long q) {
    bool inv = false;
    if (p < q) {
      swap(p, q);
      swap(r, b);
      inv = true;
    }
    long long low = 0, high = 1;
    while (true) {
      auto got = Count(p, q, high);
      if (get<0>(got) * p + get<1>(got) * q > r * p + b * q) {
        break;
      }
      low = high;
      high <<= 1;
    }
    while (low + 1 < high) {
      long long mid = (low + high) >> 1;
      auto got = Count(p, q, mid);
      if (get<0>(got) * p + get<1>(got) * q > r * p + b * q) {
        high = mid;
      } else {
        low = mid;
      }
    }
    auto got = Count(p, q, low);
    long long sx = get<0>(got);
    long long sy = get<1>(got);
    long long cnt = get<2>(got);
    long long s = sx * p + sy * q;
    long long add = (r * p + b * q - s) / high;
    long long x = 0;
    while ((high - x * p) % q != 0) {
      ++x;
    }
    sx += x * add + q * (add * (add - 1) / 2);
    long long y = (high - x * p) / q;
    while (y >= p) {
      x += q;
      y -= p;
    }
    sy += y * add + p * (add * (add - 1) / 2);
    cnt += add;
    if (sx <= r && sy <= b) {
      cout << cnt - 1 << '\\n';
      exit(0);
    }
    bool ret = (sx > r) ^ inv;
    if (inv) {
      swap(r, b);
    }
    return ret;
  };
  long long p0 = 0, q0 = 1;
  long long p1 = 1, q1 = 0;
  while (true) {
    if (Test(p0 + p1, q0 + q1)) {
      p0 = p0 + p1;
      q0 = q0 + q1;
    } else {
      p1 = p0 + p1;
      q1 = q0 + q1;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

/**
 *    author:  tourist
 *    created: 27.01.2020 01:09:52
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long r, b;
  cin >> r >> b;
  auto Count = [&](long long p, long long q, long long bound) {
    long long sx = 0;
    long long sy = 0;
    long long cnt = 0;
    for (long long x = 0; x * p <= bound; x++) {
      long long y = (bound - x * p) / q;
      sx += x * (y + 1);
      sy += y * (y + 1) / 2;
      cnt += y + 1;
    }
    return make_tuple(sx, sy, cnt);
  };
  auto Test = [&](long long p, long long q) {
    long long low = 0, high = 1;
    while (true) {
      auto got = Count(p, q, high);
      if (get<0>(got) * p + get<1>(got) * q > r * p + b * q) {
        break;
      }
      low = high;
      high <<= 1;
    }
    while (low + 1 < high) {
      long long mid = (low + high) >> 1;
      auto got = Count(p, q, mid);
      if (get<0>(got) * p + get<1>(got) * q > r * p + b * q) {
        high = mid;
      } else {
        low = mid;
      }
    }
    auto got = Count(p, q, low);
    long long sx = get<0>(got);
    long long sy = get<1>(got);
    long long cnt = get<2>(got);
    long long s = sx * p + sy * q;
    long long add = (r * p + b * q - s) / high;
    long long x = 0;
    while ((high - x * p) % q != 0) {
      ++x;
    }
    sx += x * add + q * (add * (add - 1) / 2);
    long long y = (high - x * p) / q;
    while (y >= p) {
      x += q;
      y -= p;
    }
    sy += y * add + p * (add * (add - 1) / 2);
    cnt += add;
    if (sx <= r && sy <= b) {
      cout << cnt - 1 << '\\n';
      exit(0);
    }
    return (sx > r);
  };
  long long p0 = 0, q0 = 1;
  long long p1 = 1, q1 = 0;
  while (true) {
    if (Test(p0 + p1, q0 + q1)) {
      p0 = p0 + p1;
      q0 = q0 + q1;
    } else {
      p1 = p0 + p1;
      q1 = q0 + q1;
    }
  }
  return 0;
}