Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

int main() {
  int in(q);
  rep(loop, q) {
    long long in(a, b);
    long long n = a * b;
    long long v = (long long)sqrt(n);
    long long result = 2 * v - 1;
    if (v * v == n) {
      if (v == a and v == b)
        result += 1;
      result -= 2;
      v -= 1;
    } else if (v * (n / v) == n and abs(n / v - v) == 1) {
      result -= 1;
    }

    if (n / v == v) {
      result -= 1;
    }
    out(result);
  }
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

int main() {
  int in(q);
  rep(loop, q) {
    long long in(a, b);
    if (a == 1 and b == 1) {
      out(0);
      continue;
    }
    long long n = a * b;
    long long v = (long long)sqrt(n);
    long long result = 2 * v - 1;
    if (v * v == n) {
      if (v == a and v == b)
        result += 1;
      result -= 2;
      v -= 1;
    } else if (v * (n / v) == n and abs(n / v - v) == 1) {
      result -= 1;
    }

    if (n / v == v) {
      result -= 1;
    }
    out(result);
  }
}