prithivMLmods/Coder-Stat huggingface dataset:
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
#define inf 0x3f3f3f3f

int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll x = sqrt(a * b) + 1;
    while (x * x >= a * b)
      x--;
    ll ans = 2 * x;
    if (x >= a)
      ans--;
    if (x == (a * b - 1) / x)
      ans--;
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
#define inf 0x3f3f3f3f

int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll x = sqrt(a * b) + 1;
    while (x * x >= a * b)
      x--;
    ll ans = 2 * x;
    if (x >= a)
      ans--;
    if (x != 0 && x == (a * b - 1) / x)
      ans--;
    cout << ans << endl;
  }

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int main() {
  int q;
  cin >> q;
  REP(i, q) {
    ll a, b;
    cin >> a >> b;
    if (a < b)
      swap(a, b);
    ll sum = b - 1;
    ll n = 1;
    ll oldk = 0;
    while (true) {
      ll k = n * a / (n + b) + 1;
      if (k == oldk)
        break;
      ++sum;
      oldk = k;
      ++n;
    }
    sum += a - oldk - 1;
    cout << sum << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int main() {
  int q;
  cin >> q;
  REP(i, q) {
    ll a, b;
    cin >> a >> b;
    if (a < b)
      swap(a, b);
    ll s = sqrt(a * b);
    while (s * s < a * b)
      ++s;
    while (s * s >= a * b)
      --s;
    ll ans = 2 * s;
    if (s * (s + 1) >= a * b)
      --ans;
    if (b <= s)
      --ans;
    cout << ans << endl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define pd pair<double, double>
#define ld long double
#define pld pair<ld, ld>
#define lg length()
#define sz size()
#define pb push_back
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005

ll q, a, b;

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a >> b;
    ll ans = sqrt(a * b - 1);
    if ((a * b - 1) / ans == ans)
      ans = 2ll * ans - 1;
    else
      ans = 2ll * ans;
    if (a == b)
      ans++;
    cout << ans - 1 << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int, int>
#define pl pair<ll, ll>
#define pd pair<double, double>
#define ld long double
#define pld pair<ld, ld>
#define lg length()
#define sz size()
#define pb push_back
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005

ll q, a, b;

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a >> b;
    if (a == 1 && b == 1) {
      cout << 0 << '\n';
      continue;
    }
    ll ans = a * b - 1;
    ll l = 0, r = 1000000000;
    while (l != r) {
      ll mid = (l + r + 1) / 2;
      if (mid * mid <= ans)
        l = mid;
      else
        r = mid - 1;
    }
    ans = l;
    if ((a * b - 1) / ans == ans)
      ans = 2ll * ans - 1;
    else
      ans = 2ll * ans;
    if (a == b)
      ans++;
    cout << ans - 1 << '\n';
  }
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define PI 3.1415926535897932384626433832795l

#define pb push_back
#define mp make_pair
#define lb lower_bound // First element which is not less than val
#define ub upper_bound // First element greater than val
#define f first
#define s second

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define endl '\n'
#define re return
#define repz(i, a) for (ll i = 0; i < (a); i++)
#define vecin(n, v)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> v[i];
#define full(v) v.begin(), v.end()

void solve() {
  ll a, b;
  cin >> a >> b;
  ll prod = a * b;

  ll cnt = sqrt(prod);
  if (prod == cnt * cnt)
    cnt--;

  ll ans = 2 * cnt;

  ll res = prod / cnt - (prod % cnt ? 0 : 1);

  if (res == cnt)
    ans--;

  if (a <= cnt)
    ans--;
  if (b <= cnt)
    ans--;

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define PI 3.1415926535897932384626433832795l

#define pb push_back
#define mp make_pair
#define lb lower_bound // First element which is not less than val
#define ub upper_bound // First element greater than val
#define f first
#define s second

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define endl '\n'
#define re return
#define repz(i, a) for (ll i = 0; i < (a); i++)
#define vecin(n, v)                                                            \
  for (ll i = 0; i < n; i++)                                                   \
    cin >> v[i];
#define full(v) v.begin(), v.end()

void solve() {
  ll a, b;
  cin >> a >> b;
  ll prod = a * b;

  if (a == 1 && b == 1) {
    cout << 0 << endl;
    re;
  }

  ll cnt = sqrt(prod);
  if (prod == cnt * cnt)
    cnt--;

  ll ans = 2 * cnt;

  ll res = prod / cnt - (prod % cnt ? 0 : 1);

  if (res == cnt)
    ans--;

  if (a <= cnt)
    ans--;
  if (b <= cnt)
    ans--;

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a == b)
      cout << 2 * a - 2 << endl;
    else {
      ll c = min(a, b);
      ll d = max(a, b);
      ll x;
      rep2(i, c, d) {
        if (i * i >= a * b) {
          x = i;
          break;
        }
      }
      x--;
      if (x * (x + 1) < a * b)
        cout << 2 * x - 1 << endl;
      else
        cout << 2 * x - 2 << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
struct edge {
  ll to;
  ll cost;
};
using graph = vector<vector<edge>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout << fixed << setprecision(20);
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
double pi = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a == b)
      cout << 2 * a - 2 << endl;
    else {
      ll x = sqrt(double(a * b));

      if (a * b == x * x)
        x--;
      if (x * (x + 1) < a * b)
        cout << 2 * x - 1 << endl;
      else
        cout << 2 * x - 2 << endl;
    }
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    if (a < b)
      swap(a, b);
    ll ans = b - 1, lb = 0, rb = a - 1, sk = a * b - 1;
    while (rb - lb > 1) {
      ll mid = (lb + rb) / 2;
      if (mid * mid + mid <= sk) {
        lb = mid;
      } else
        rb = mid;
    }
    ans += lb;
    ans += (sk / rb) - b;
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    if (a == 1 && b == 1) {
      cout << 0 << endl;
      continue;
    }
    if (a < b)
      swap(a, b);
    ll ans = b - 1, lb = 0, rb = a - 1, sk = a * b - 1;
    while (rb - lb > 1) {
      ll mid = (lb + rb) / 2;
      if (mid * mid + mid <= sk) {
        lb = mid;
      } else
        rb = mid;
    }
    ans += lb;
    ans += (sk / rb) - b;
    cout << ans << endl;
  }
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

long long Q, A, B;

long long solve(long long V, long long D) {
  long long L = 0, R = (1LL << 30), M, maxn = 0;
  for (int i = 0; i < 40; i++) {
    M = (L + R) / 2;
    if (M * M <= V) {
      L = M;
      maxn = max(maxn, M);
    } else {
      R = M;
    }
  }
  long long B = maxn, K = 0LL;
  if (B < D)
    K = 1LL;
  if (B == V / B)
    return (B - 1) * 2LL + K + 0LL;
  return (B - 1) * 2LL + K + 1LL;
}

int main() {
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> A >> B;
    cout << solve(A * B - 1LL, min(A, B)) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

long long Q, A, B;

long long solve(long long V, long long D) {
  long long L = 0, R = (1LL << 30), M, maxn = 0;
  for (int i = 0; i < 40; i++) {
    M = (L + R) / 2;
    if (M * M <= V) {
      L = M;
      maxn = max(maxn, M);
    } else {
      R = M;
    }
  }
  long long B = maxn, K = 0LL;
  if (B < D)
    K = 1LL;
  if (B == V / B)
    return (B - 1) * 2LL + K + 0LL;
  return (B - 1) * 2LL + K + 1LL;
}

int main() {
  cin >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> A >> B;
    if (A == 1 && B == 1) {
      cout << "0" << endl;
    } else
      cout << solve(A * B - 1LL, min(A, B)) << endl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#define int long long
using namespace std;

int f(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == b)
    return 2 * a - 2;
  int x = 2 * sqrt((long double)a * b - 1.0 + 1e-9) - 2;
  int st = 0, ed = 2000000000, mid; // oooxxx, [st, ed)
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if ((a * b - 1) / mid >= b)
      st = mid;
    else
      ed = mid;
  }
  if ((a * b - 1) / st == b)
    x--;
  return x;
}

signed main() {
  int q, a, b;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << f(a, b) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#define int long long
using namespace std;

int f(int a, int b) {
  if (a > b)
    swap(a, b);
  if (a == b)
    return 2 * a - 2;
  int x = 2 * sqrt((long double)a * b - 0.5) - 2;
  int st = 1, ed = 2000000000, mid; // oooxxx, [st, ed)
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if ((a * b - 1) / mid >= b)
      st = mid;
    else
      ed = mid;
  }
  if ((a * b - 1) / st == b)
    x--;
  return x;
}

signed main() {
  int q, a, b;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << f(a, b) << endl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

inline int read() {
  int ret = 0;
  char c = getchar();
  while (c < 48 || c > 57)
    c = getchar();
  while (c >= 48 && c <= 57)
    ret = ret * 10 + c - 48, c = getchar();
  return ret;
}
int ans, t;
long long i, a, b, u, c, d, e, f, g, h;
int main() {
  freopen("3.in", "r", stdin);
  freopen("4.out", "w", stdout);
  t = read();
  while (t--) {
    a = read();
    b = read();
    if (a < b)
      swap(a, b);
    u = (a * b - 1) / (b + 1);
    if (u <= b + 1)
      ans = u;
    else {

      h = u + b + 1;
      if ((h / 2 * (h - h / 2)) <= a * b - 1)
        ans = u;
      else {
        //			g=sqrt(a*b-0.9);

        // tepan//da xiao// 0 1 2 ... //
        g = sqrtl((long double)a * b - 0.9);
        // sqrt
        //(long double)

        // 二分套三分

        ans = min(u - g + 1, g - (b + 1) + 1);
        if (u - g + 1 > g - (b + 1) + 1 && g * (g + 1) <= a * b - 1)
          ++ans;

        //            int v=b+1;
        //            for(long long i=u; i>=g; --i){
        //            	if(i*v<=a*b-1)
        //            	{
        //            	    ++ans;
        //            	    ++v;
        //            	    if(v==292703660)
        //					int hh=0;
        //                }
        //			}

        ans += g - 1;
      }
    }
    ans += b - 1;
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

inline int read() {
  int ret = 0;
  char c = getchar();
  while (c < 48 || c > 57)
    c = getchar();
  while (c >= 48 && c <= 57)
    ret = ret * 10 + c - 48, c = getchar();
  return ret;
}
int ans, t;
long long i, a, b, u, c, d, e, f, g, h;
int main() {
  //	freopen("3.in","r",stdin);
  //	freopen("4.out","w",stdout);
  t = read();
  while (t--) {
    a = read();
    b = read();
    if (a < b)
      swap(a, b);
    u = (a * b - 1) / (b + 1);
    if (u <= b + 1)
      ans = u;
    else {

      h = u + b + 1;
      if ((h / 2 * (h - h / 2)) <= a * b - 1)
        ans = u;
      else {
        //			g=sqrt(a*b-0.9);

        // tepan//da xiao// 0 1 2 ... //
        g = sqrtl((long double)a * b - 0.9);
        // sqrt
        //(long double)

        // 二分套三分

        ans = min(u - g + 1, g - (b + 1) + 1);
        if (u - g + 1 > g - (b + 1) + 1 && g * (g + 1) <= a * b - 1)
          ++ans;

        //            int v=b+1;
        //            for(long long i=u; i>=g; --i){
        //            	if(i*v<=a*b-1)
        //            	{
        //            	    ++ans;
        //            	    ++v;
        //            	    if(v==292703660)
        //					int hh=0;
        //                }
        //			}

        ans += g - 1;
      }
    }
    ans += b - 1;
    printf("%d\n", ans);
  }
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int a, b;

inline void up(int &x, int y) {
  if (y <= x)
    ++x;
}

ll mn(int m) {
  int ma1 = m + 1 >> 1, ma2 = m + 2 >> 1, mb1 = ma1, mb2 = ma2;
  up(ma1, a);
  up(ma2, a);
  up(mb1, b);
  up(mb2, b);
  return max((ll)ma1 * mb2, (ll)ma2 * mb1);
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &a, &b);
    int l = 0, r = 2e9 + 100;
    while (r > l) {
      int m = l + r + 1 >> 1;
      if (mn(m) < (ll)a * b)
        l = m;
      else
        r = m - 1;
    }
    printf("%d\n", l);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int a, b;

inline void up(int &x, int y) {
  if (y <= x)
    ++x;
}

ll mn(int m) {
  int ma1 = m + 1 >> 1, ma2 = m + 2 >> 1, mb1 = ma1, mb2 = ma2;
  up(ma1, a);
  up(ma2, a);
  up(mb1, b);
  up(mb2, b);
  return max((ll)ma1 * mb2, (ll)ma2 * mb1);
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &a, &b);
    int l = 0, r = 2e9 + 100;
    while (r > l) {
      int m = (ll)l + r + 1 >> 1;
      if (mn(m) < (ll)a * b)
        l = m;
      else
        r = m - 1;
    }
    printf("%d\n", l);
  }
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
// ARC094B.cpp
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll Q, n = 1e12, a, b;

int main() {
  scanf("%lld", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll ans = 0;
    if (a == b || a + 1 == b)
      ans += 2LL * a - 2;
    else {
      ll C = 0;
      while ((C + 1) * (C + 1) < 1LL * a * b)
        C++;
      if (C * (C + 1) >= 1LL * a * b)
        ans = 2LL * C - 2;
      else
        ans = 2LL * C - 1;
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
// ARC094B.cpp
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll Q, n = 1e12, a, b;

int main() {
  scanf("%lld", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll ans = 0;
    if (a == b || a + 1 == b)
      ans += 2LL * a - 2;
    else {
      ll C = sqrt(1LL * a * b) - 1;
      while ((C + 1) * (C + 1) < 1LL * a * b)
        C++;
      if (C * (C + 1) >= 1LL * a * b)
        ans = 2LL * C - 2;
      else
        ans = 2LL * C - 1;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long int LLI;

int Q;
LLI A, B;

LLI Sqrt(LLI x) {
  LLI low = -1;
  LLI high = 1e9 + 7;
  while (high - low > 1) {
    LLI mid = (high + low) / 2;
    if (mid * mid <= x)
      low = mid;
    else
      high = mid;
  }
  return low;
}

int main() {
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld%lld", &A, &B);
    LLI k = Sqrt(A * B - 1);
    LLI ans = k * 2;
    LLI t = A * B - 1;
    if (t / A != 0 && t / (t / A) == A)
      ans--;
    if (t / B != 0 && t / (t / B) == B)
      ans--;
    if (t / k != 0 && t / k == k)
      ans--;
    printf("%lld\n", ans);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long int LLI;

int Q;
LLI A, B;

LLI Sqrt(LLI x) {
  LLI low = -1;
  LLI high = 1e9 + 7;
  while (high - low > 1) {
    LLI mid = (high + low) / 2;
    if (mid * mid <= x)
      low = mid;
    else
      high = mid;
  }
  return low;
}

int main() {
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld%lld", &A, &B);
    LLI k = Sqrt(A * B - 1);
    LLI ans = k * 2;
    LLI t = A * B - 1;
    if (t / A != 0 && t / (t / A) == A)
      ans--;
    if (t / B != 0 && t / (t / B) == B)
      ans--;
    if (k != 0 && t / k != 0 && t / k == k)
      ans--;
    printf("%lld\n", ans);
  }
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; /*10^12*/
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    ll ans = 2 * (a - 1);
    ll p = (ll)sqrt(a * b);
    if (a < b - 1) {
      if (a == p) {
        ans += 1;
      } else {
        ans += 1;
        ans += 2 * (p - a - 1);
        if (p * (p + 1) < a * b)
          ans += 2;
        else if (p * p < a * b)
          ans += 1;
        if ((a * b - 1) / (a - 2) == b - 2)
          ans--;
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; /*10^12*/
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    ll ans = 2 * (a - 1);
    ll p = (ll)sqrt(a * b);
    if (a < b - 1) {
      if (a == p) {
        ans += 1;
      } else {
        ans += 1;
        ans += 2 * (p - a - 1);
        if (p * (p + 1) < a * b)
          ans += 2;
        else if (p * p < a * b)
          ans += 1;
        if ((a * b - 1) / (a + 2) == b - 2)
          ans--;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
int Q;
LL a, b;
LL LLsqrt(LL x) {
  LL L = 0, R = 1e9, mid, ans = -1;
  while (L <= R) {
    mid = (L + R) >> 1;
    if (mid * mid <= x)
      L = mid + 1, ans = mid;
    else
      R = mid - 1;
  }
  return ans;
}
int main() {
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    LL c = a * b - 1;
    LL d = LLsqrt(c);
    LL ans = d + d;
    if (a != b)
      ans--;
    if (d == c / d)
      ans--;
    printf("%lld\n", max(ans, 0LL));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
int Q;
LL a, b;
LL LLsqrt(LL x) {
  LL L = 0, R = 1e9, mid, ans = -1;
  while (L <= R) {
    mid = (L + R) >> 1;
    if (mid * mid <= x)
      L = mid + 1, ans = mid;
    else
      R = mid - 1;
  }
  return ans;
}
int main() {
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    LL c = a * b - 1;
    LL d = LLsqrt(c);
    LL ans = d + d;
    if (a != b)
      ans--;
    if (d != 0)
      if (d == c / d)
        ans--;
    printf("%lld\n", max(ans, 0LL));
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)

const int MXN = 2e5 + 1;
const long long MNN = 5e3 + 100;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

int n;
int t, q;

int main() {
  cin >> q;
  while (q--) {
    llong a, b;
    cin >> a >> b;
    llong sq = sqrt(a * b);
    llong k = sq;
    if (sq * sq == a * b) {
      sq--;
    }
    llong ans = sq * 2;
    if ((a * b - 1) / sq == sq) {
      ans--;
    }
    if (a <= sq) {
      ans--;
    }
    if (b <= sq) {
      ans--;
    }
    cout << ans << nl;
  }
  return 0;
}


Fixed submission:
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for (int i = start; i <= finish; i += k)

const int MXN = 2e5 + 1;
const long long MNN = 5e3 + 100;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

int n;
int t, q;

int main() {
  cin >> q;
  while (q--) {
    llong a, b;
    cin >> a >> b;
    llong sq = sqrt(a * b);
    llong k = sq;
    if (sq * sq == a * b) {
      sq--;
    }
    llong ans = sq * 2;
    if (sq != 0) {
      if ((a * b - 1) / sq == sq) {
        ans--;
      }
    }
    if (a <= sq) {
      ans--;
    }
    if (b <= sq) {
      ans--;
    }
    cout << ans << nl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    ll w = a * b - 1;
    ll k = sqrtl(w);
    while (w / k != w / (k + 1))
      ++k;
    cout << k + w / k - 2 + (a == b) << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a == b && b == 1) {
      cout << "0\n";
      continue;
    }
    ll w = a * b - 1;
    ll k = sqrtl(w);
    while (w / k != w / (k + 1))
      ++k;
    cout << k + w / k - 2 + (a == b) << '\n';
  }
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long MOD = 1000000007;

int main() {

  int Q;
  cin >> Q;
  vector<long long> A(Q);
  for (int i = 0; i < Q; i++) {
    long long a, b, c, d;
    cin >> a >> b;
    c = a * b - 1;
    d = static_cast<long long>(sqrt(c));

    long long x = max(d - 2, 1LL);
    for (; x <= c / x; x++) {
    }
    x--;
    long long ans = x * 2;
    if (x == c / x) {
      ans--;
    }

    if (x >= a) {
      ans--;
    }
    if (x >= b) {
      ans--;
    }

    A[i] = ans;
  }
  for (int i = 0; i < Q; i++) {
    cout << A[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

long long MOD = 1000000007;

int main() {

  int Q;
  cin >> Q;
  vector<long long> A(Q);
  for (int i = 0; i < Q; i++) {
    long long a, b, c, d;
    cin >> a >> b;
    c = a * b - 1;
    d = static_cast<long long>(sqrt(c));

    long long x = max(d - 2, 1LL);
    for (; x <= c / x; x++) {
    }
    x--;
    long long ans = x * 2;

    if (ans == 0) {
      A[i] = 0;
      continue;
    }

    if (x == c / x) {
      ans--;
    }

    if (x >= a) {
      ans--;
    }
    if (x >= b) {
      ans--;
    }

    A[i] = ans;
  }
  for (int i = 0; i < Q; i++) {
    cout << A[i] << endl;
  }

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long ll;
int q, a, b, ans;

inline ll calc_sqrt(ll n) {
  ll l = 0, r = sqrt(n) + 100;
  while (l < r) {
    ll mid = l + r + 1 >> 1;
    if (mid * mid <= n)
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}

int main() {
  cin >> q;
  while (q--) {
    cin >> a >> b;
    ll n = (ll)a * b - 1, x = calc_sqrt(n);
    if (n / x == x)
      ans = 2 * x - 1;
    else
      ans = 2 * x;
    if (a <= x)
      --ans;
    else if (n / a && x - n / a >= n / (n / a) - x)
      --ans;
    if (b <= x)
      --ans;
    else if (n / b && x - n / b >= n / (n / b) - x)
      --ans;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long ll;
int q, a, b, ans;

inline ll calc_sqrt(ll n) {
  ll l = 0, r = sqrt(n) + 100;
  while (l < r) {
    ll mid = l + r + 1 >> 1;
    if (mid * mid <= n)
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}

int main() {
  cin >> q;
  while (q--) {
    cin >> a >> b;
    if (a == 1 && b == 1) {
      cout << 0 << endl;
      continue;
    }
    ll n = (ll)a * b - 1, x = calc_sqrt(n);
    if (n / x == x)
      ans = 2 * x - 1;
    else
      ans = 2 * x;
    if (a <= x)
      --ans;
    else if (n / a && x - n / a >= n / (n / a) - x)
      --ans;
    if (b <= x)
      --ans;
    else if (n / b && x - n / b >= n / (n / b) - x)
      --ans;
    cout << ans << endl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int q;
  ll a[110], b[110];

  cin >> q;

  REP(i, q)
  cin >> a[i] >> b[i];

  REP(i, q) {
    ll t = a[i] * b[i];
    t--;

    ll l = 0;
    ll r = LLONG_MAX;

    while (r - l > 1) {
      ll mid = (l + r) / 2;

      if (t / mid >= mid)
        l = mid;
      else
        r = mid;
    }

    ll res = l * 2;

    if (t / l == l)
      res--;

    if (a[i] <= l)
      res--;

    if (b[i] <= l)
      res--;

    cout << res << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int q;
  ll a[110], b[110];

  cin >> q;

  REP(i, q)
  cin >> a[i] >> b[i];

  REP(i, q) {
    ll t = a[i] * b[i];
    t--;

    ll l = 0;
    ll r = LLONG_MAX;

    while (r - l > 1) {
      ll mid = (l + r) / 2;

      if (t / mid >= mid)
        l = mid;
      else
        r = mid;
    }

    if (l == 0) {
      cout << 0 << endl;
      continue;
    }

    ll res = l * 2;

    if (t / l == l)
      res--;

    if (a[i] <= l)
      res--;

    if (b[i] <= l)
      res--;

    cout << res << endl;
  }

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ff first
#define ss second
#define nl '\n'
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */

int main() {
  ios::sync_with_stdio(0);
  srand(time(0));
  cin.tie(0);
  cout.tie(0);

  int t;
  cin >> t;
  while (t--) {
    ll p, q;
    cin >> p >> q;
    ll z = p * q;
    ll rt = 1;
    for (; rt * rt + rt + rt + 1 < z; rt++)
      ;
    ll a;
    if (rt * rt + rt < z)
      a = 2 * rt;
    else
      a = 2 * rt - 1;
    if (min(p, q) <= rt)
      a--;
    cout << a << nl;
  }

  return 0;
}

/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ff first
#define ss second
#define nl '\n'
/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */

int main() {
  ios::sync_with_stdio(0);
  srand(time(0));
  cin.tie(0);
  cout.tie(0);

  int t;
  cin >> t;
  while (t--) {
    ll p, q;
    cin >> p >> q;
    ll z = p * q;
    ll lo = 1, hi = max(p, q);
    while (lo < hi) {
      ll mid = (lo + hi + 1) / 2;
      if (mid * mid < z)
        lo = mid;
      else
        hi = mid - 1;
    }
    ll rt = lo;
    ll a;
    if (rt * rt + rt < z)
      a = 2 * rt;
    else
      a = 2 * rt - 1;
    if (min(p, q) <= rt)
      a--;
    cout << a << nl;
  }

  return 0;
}

/* -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- -*- */
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int main() {
  int q;
  scanf("%d", &q);
  for (int x = 0; x < q; x++) {
    long long int a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b) {
      long long int c = b;
      b = a;
      a = c;
    }
    // a<=b
    long long int ans = 0;
    ans += a - 1;
    if (a == b) {
      ans += a - 1;
    } else {
      for (long long int i = a; 0 == 0; i++) {
        if (i * i >= a * b - 1) {
          int x = i - 1;
          if (i * i == a * b - 1)
            x++;
          if ((a * b - 1) / x == x) {
            ans += (2 * x - a - 1);
          } else {
            ans += (2 * x - a);
          }
          break;
        }
        if ((i + 10) * (i + 10) < a * b - 1) {
          i += 10;
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
int main() {
  int q;
  scanf("%d", &q);
  for (int x = 0; x < q; x++) {
    long long int a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b) {
      long long int c = b;
      b = a;
      a = c;
    }
    // a<=b
    long long int ans = 0;
    ans += a - 1;
    if (a == b) {
      ans += a - 1;
    } else {
      for (long long int i = a; 0 == 0; i++) {
        if (i * i >= a * b - 1) {
          int x = i - 1;
          if (i * i == a * b - 1)
            x++;
          if ((a * b - 1) / x == x) {
            ans += (2 * x - a - 1);
          } else {
            ans += (2 * x - a);
          }
          break;
        }
        if ((i + 100) * (i + 100) < a * b - 1) {
          i += 100;
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;

int a, b;

bool check(int p) {
  int mid = (p + 1) / 2;

  ll mx = 0;
  for (int i = mid - 10; i <= mid + 10; i++) {
    if (i <= 0 || i > p + 1 || i == a)
      continue;

    int below = i - (i >= a);
    int j = p + 1 + (p >= b) - below;
    j -= (j <= b && p >= b);

    if (j <= 0 || j > p + 1 || j == b)
      continue;
    mx = max(mx, 1ll * i * j);
  }
  return mx < 1ll * a * b;
}

void test_case() {
  cin >> a >> b;
  if (a > b)
    swap(a, b);

  int l = 0, r = 2e9 + 5;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << l << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tcase;
  cin >> tcase;
  while (tcase--) {
    test_case();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;

int a, b;

bool check(int p) {
  int mid = (p + 1) / 2;

  ll mx = 0;
  for (int i = mid - 10; i <= mid + 10; i++) {
    if (i <= 0 || i > p + 1 || i == a)
      continue;

    int below = i - (i >= a);
    int j = p + 1 + (p >= b) - below;
    j -= (j <= b && p >= b);

    if (j <= 0 || j > p + 1 || j == b)
      continue;
    mx = max(mx, 1ll * i * j);
  }
  return mx < 1ll * a * b;
}

void test_case() {
  cin >> a >> b;
  if (a > b)
    swap(a, b);

  int l = 0, r = 2e9 + 5;
  while (l + 1 < r) {
    int mid = (1ll * l + r) >> 1;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << l << endl;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tcase;
  cin >> tcase;
  while (tcase--) {
    test_case();
  }

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
int Q;
LL a, b;
int main() {
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    LL m = sqrt(a * b - 1), t = (a * b - 1) / m - 1;
    printf("%lld\n", m + t - 1);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
int Q;
LL a, b;
int main() {
  scanf("%d", &Q);
  while (Q--) {
    scanf("%lld%lld", &a, &b);
    if (a == b)
      printf("%lld\n", a * 2 - 2);
    else {
      if (a > b)
        swap(a, b);
      LL m = sqrt(a * b), t = (a * b - 1) / m - 1;
      printf("%lld\n", m - 1 + t);
    }
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
int main() {
  int T = read();
  while (T--) {
    int A = read(), B = read();
    ll sum = 1ll * A * B;
    sum = (ll)sqrt(sum - 1);
    printf("%lld\n", 2ll * sum - ((A <= sum) + (B <= sum) +
                                  (sum == (1ll * A * B - 1) / sum)));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
int main() {
  int T = read();
  while (T--) {
    int A = read(), B = read();
    ll sum = 1ll * A * B;
    if (sum <= 27)
      sum = (ll)sqrt(sum - 1);
    else {
      sum = (ll)sqrt(sum) - 5;
      while ((sum + 1) * (sum + 1) <= 1ll * A * B - 1)
        ++sum;
    }
    if (sum == 0) {
      printf("0\n");
      continue;
    }
    printf("%lld\n", 2ll * sum - ((A <= sum) + (B <= sum) +
                                  (sum == (1ll * A * B - 1) / sum)));
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#define ll long long
using namespace std;
int main() {
  ll n;
  scanf("%lld", &n);
  while (n--) {
    ll q, w;
    scanf("%lld %lld", &q, &w);
    if (q < w)
      swap(q, w);
    ll dap = w - 1;
    ll ch = (q * w);
    ch = sqrt(ch);
    dap += (ch - 1);
    ll ri = (q * w - 1) / ch;
    ll le = (q * w - 1) / (q - 1);
    le = max(w + 1, le);
    if (ri >= le)
      dap += (ri - le + 1);
    printf("%lld\n", dap);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#define ll long long
using namespace std;
int main() {
  ll n;
  scanf("%lld", &n);
  while (n--) {
    ll q, w;
    scanf("%lld %lld", &q, &w);
    if (q == 1 && w == 1) {
      puts("0");
      continue;
    }
    if (q < w)
      swap(q, w);
    ll dap = w - 1;
    ll ch = (q * w);
    ch = sqrt(ch);
    dap += (ch - 1);
    ll ri = (q * w - 1) / ch;
    ll le = (q * w - 1) / (q - 1);
    le = max(w + 1, le);
    if (ri >= le)
      dap += (ri - le + 1);
    printf("%lld\n", dap);
  }
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int _max(int x, int y) { return x > y ? x : y; }
const int N = 200001;
const int mod = 924844033;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int main() {
  int T = read();
  while (T--) {
    int a = read(), b = read();
    LL hh = (LL)a * b - 1;
    int l = 1, r = 1e9, uu = 0;
    while (l <= r) {
      int mid = (l + r) / 2;
      if ((LL)mid * mid <= hh)
        l = mid + 1, uu = mid;
      else
        r = mid - 1;
    }
    int ans = uu * 2;
    if (uu == hh / uu)
      ans--;
    if (a == uu && b == uu)
      ans--;
    else {
      if (a <= uu)
        ans--;
      if (b <= uu)
        ans--;
    }
    put(ans), puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int _max(int x, int y) { return x > y ? x : y; }
const int N = 200001;
const int mod = 924844033;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int main() {
  int T = read();
  while (T--) {
    int a = read(), b = read();
    LL hh = (LL)a * b - 1;
    if (hh == 0) {
      puts("0");
      continue;
    }
    int l = 1, r = 1e9, uu = 0;
    while (l <= r) {
      int mid = (l + r) / 2;
      if ((LL)mid * mid <= hh)
        l = mid + 1, uu = mid;
      else
        r = mid - 1;
    }
    int ans = uu * 2;
    if (uu == hh / uu)
      ans--;
    if (a == uu && b == uu)
      ans--;
    else {
      if (a <= uu)
        ans--;
      if (b <= uu)
        ans--;
    }
    put(ans), puts("");
  }
  return 0;
}
Problem p03392 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, mod = 998244353;
char s[N];
int f[N][3][3];
int power(int x, int y) {
  int s = 1;
  for (; y; y /= 2, x = 1ll * x * x % mod)
    if (y & 1)
      s = 1ll * s * x % mod;
  return s;
}
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  if (n == 2) {
    printf("%d\n", 1 + (s[1] != s[2]));
    return 0;
  }
  if (n == 3) {
    if (s[1] == s[2] && s[2] == s[3])
      puts("1");
    else if (s[1] == s[2] || s[2] == s[3])
      puts("6");
    else if (s[1] == s[3])
      puts("7");
    else
      puts("3");
    return 0;
  }
  int fl = 1;
  for (int i = 1; i < n; i++)
    if (s[i] != s[i + 1])
      fl = 0;
  if (fl) {
    puts("1");
    return 0;
  }
  int ans = power(3, n - 1);
  int v = 0;
  for (int i = 1; i <= n; i++)
    v = (v + s[i] - 'a') % 3;
  for (int i = 0; i < 3; i++)
    f[1][i][i] = 1;
  for (int i = 2; i <= n; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        for (int l = 0; l < 3; l++)
          if (k != l)
            f[i][(j + l) % 3][l] =
                (f[i][(j + l) % 3][l] + f[i - 1][j][k]) % mod;
  for (int i = 0; i < 3; i++)
    ans = (ans + mod - f[n][v][i]) % mod;
  fl = 1;
  for (int i = 1; i < n; i++)
    if (s[i] == s[i + 1])
      fl = 0;
  printf("%d\n", (ans + fl) % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, mod = 998244353;
char s[N];
int f[N][3][3];
int power(int x, int y) {
  int s = 1;
  for (; y; y /= 2, x = 1ll * x * x % mod)
    if (y & 1)
      s = 1ll * s * x % mod;
  return s;
}
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  if (n == 2) {
    printf("%d\n", 1 + (s[1] != s[2]));
    return 0;
  }
  if (n == 3) {
    if (s[1] == s[2] && s[2] == s[3])
      puts("1");
    else if (s[1] == s[2] || s[2] == s[3])
      puts("6");
    else if (s[1] == s[3])
      puts("7");
    else
      puts("3");
    return 0;
  }
  int fl = 1;
  for (int i = 1; i < n; i++)
    if (s[i] != s[i + 1])
      fl = 0;
  if (fl) {
    puts("1");
    return 0;
  }
  int ans = power(3, n - 1);
  int v = 0;
  for (int i = 1; i <= n; i++)
    v = (v + s[i] - 'a') % 3;
  for (int i = 0; i < 3; i++)
    f[1][i][i] = 1;
  for (int i = 2; i <= n; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        for (int l = 0; l < 3; l++)
          if (k != l)
            f[i][(j + l) % 3][l] =
                (f[i][(j + l) % 3][l] + f[i - 1][j][k]) % mod;
  for (int i = 0; i < 3; i++)
    ans = (ans + mod - f[n][v][i]) % mod;
  fl = 1;
  for (int i = 1; i < n; i++)
    if (s[i] == s[i + 1])
      fl = 0;
  printf("%d\n", (ans + fl) % mod);
  return 0;
}