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

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

typedef unsigned long long ll;

ll get_upper(ll lower, ll a, ll b) {
  ll upper = a * b / lower;
  if (upper * lower == a * b)
    --upper;
  return upper;
}
// index が条件を満たすかどうか
bool isOK(ll lower, ll a, ll b) {
  ll upper = get_upper(lower, a, b);
  return lower <= upper;
}

int binary_search(ll a, ll b) {
  ll left = a;
  ll right = b - 1;

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, a, b))
      left = mid;
    else
      right = mid;
  }

  /* left は条件を満たす最大の値、right は条件を満たさない最小の値になっている
   */
  return left;
}

void solve(ll a, ll b) {
  if (a > b)
    swap(a, b);
  ll ans = 2 * (a - 1); // (a未満, bより大)の数
  ll lower = binary_search(a, b);
  ll upper = get_upper(lower, a, b);
  if (lower < a || upper >= b) {
  } else if (lower == a) {
    if (lower < upper && upper < b)
      ++ans;
  } else {
    ++ans; // (a, b-1)
    if (lower == upper)
      ans += (lower - a - 1) * 2 + 1;
    else
      ans += (lower - a) * 2;
  }
  // cout << a << " " << b << " " << lower << " " << upper << " " << ans <<
  // endl;
  cout << ans << endl;
}

int main() {
  ll q;
  cin >> q;
  for (ll i = 0; i < q; ++i) {
    ll a, b;
    cin >> a >> b;
    solve(a, b);
  }
}


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

typedef unsigned long long ll;

ll get_upper(ll lower, ll a, ll b) {
  ll upper = a * b / lower;
  if (upper * lower == a * b)
    --upper;
  return upper;
}
// index が条件を満たすかどうか
bool isOK(ll lower, ll a, ll b) {
  ll upper = get_upper(lower, a, b);
  return lower <= upper;
}

int binary_search(ll a, ll b) {
  ll left = a;
  ll right = b - 1;

  /* どんな二分探索でもここの書き方を変えずにできる！ */
  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (isOK(mid, a, b))
      left = mid;
    else
      right = mid;
  }

  /* left は条件を満たす最大の値、right は条件を満たさない最小の値になっている
   */
  return left;
}

void solve(ll a, ll b) {
  if (a > b)
    swap(a, b);
  ll ans = 2 * (a - 1); // (a未満, bより大)の数
  ll lower = binary_search(a, b);
  ll upper = get_upper(lower, a, b);
  if (lower < a || upper >= b) {
  } else if (lower == a) {
    if (lower < upper && upper < b)
      ++ans;
  } else {
    ++ans; // (a, b-1)
    if (lower == upper)
      ans += (lower - a - 1) * 2 + 1;
    else
      ans += (lower - a) * 2;
  }
  // cout << a << " " << b << " " << lower << " " << upper << " " << ans <<
  // endl;
  cout << ans << endl;
}

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;
    } else {
      solve(a, b);
    }
  }
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;
const ll MOD = 998244353;

int main() {
  int Q;
  cin >> Q;
  rep(i, Q) {
    ll A, B, ans, s;
    cin >> A >> B;
    ans = sqrt(A * B - 1);
    s = ans;
    ans *= 2;
    if (A <= s)
      ans--;
    if (B <= s)
      ans--;
    if (s == (A * B - 1) / s)
      ans--;
    cout << ans << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

using ll = long long;
const ll MOD = 998244353;

int main() {
  int Q;
  cin >> Q;
  rep(i, Q) {
    ll A, B, a, b, ans, C;
    cin >> a >> b;
    A = min(a, b);
    B = max(a, b);
    if (A == B) {
      //(1,2A-1)...(A-1,A+1),(A+1,A-1)...(2A-1,1)
      ans = 2 * A - 2;
    } else if (A + 1 == B) {
      //(1,2A)...(A-1,A+2),(A+2,A-1)...(2A,1)
      ans = 2 * A - 2;
    } else {
      C = sqrt(A * B);
      if (C * C == A * B)
        C--;
      if (C * (C + 1) >= A * B) {
        //(1,A+B+1)...(A-1,B+1),(A+1,2C-A-1)...(C,C)...(2C-1,1)
        ans = 2 * C - 2;
      } else {
        //(1,A+B+1)...(A-1,B+1),(A+1,2C-A)...(C,C+1)...(2C,1)
        ans = 2 * C - 1;
      }
    }
    cout << ans << endl;
  }
}
Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int MaxN = 1e5;

int main() {
  int q;
  long long a, b, ans, jh, sum, vis, biu;
  while (~scanf("%d", &q)) {
    while (q--) {
      ans = 0;
      scanf("%lld %lld", &a, &b);
      if (a > b) {
        jh = a;
        a = b;
        b = jh;
      }
      sum = a * b;
      ans = a + b - 2;
      while (b > a) {
        b--;
        a++;
        if (a * b >= sum) {
          biu = sum / a;
          if (a * biu == sum)
            biu--;
          ans -= b - biu;
          b = biu;
        } else {
          biu = sum / b;
          if (biu * b == sum)
            biu++;
          a = biu;
        }
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int MaxN = 1e5;

int main() {
  int q;
  long long a, b, ans, jh, sum, vis, biu;
  while (~scanf("%d", &q)) {
    while (q--) {
      ans = 0;
      scanf("%lld %lld", &a, &b);
      if (a > b) {
        jh = a;
        a = b;
        b = jh;
      }
      sum = a * b;
      jh = sqrt(sum);
      ans = jh * 2 - 1;
      if (a < jh)
        ans--;
      if (sum == jh * jh)
        ans--;
      else if (a == jh)
        ans--;
      if (jh * (jh + 1) < sum && b != jh + 1)
        ans++;
      printf("%lld\n", ans);
    }
  }
  return 0;
}
Problem p03388 (C++)

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

using namespace std;

#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

long sqr(long x) {
  long kir = 0, kan = 2e9;
  while (kir < kan) {
    long mid = (kir + kan + 1) / 2;
    if (mid * mid > x)
      kan = mid - 1;
    else
      kir = mid;
  }
  return kir;
}

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);
  int q;
  scanf("%d", &q);
  while (q--) {
    long a, b;
    scanf("%lld %lld", &a, &b);
    long x = a * b - 1;
    long sq = sqr(x);
    long res = 2 * sq - ((long)(sq == x / sq));
    if (a == b)
      printf("%lld\n", res);
    else
      printf("%lld\n", res - 1);
  }
}


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

using namespace std;

#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

long sqr(long x) {
  long kir = 0, kan = 2e9;
  while (kir < kan) {
    long mid = (kir + kan + 1) / 2;
    if (mid * mid > x)
      kan = mid - 1;
    else
      kir = mid;
  }
  return kir;
}

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);
  int q;
  scanf("%d", &q);
  while (q--) {
    long a, b;
    scanf("%lld %lld", &a, &b);
    if (a == 1 && b == 1) {
      printf("0\n");
      continue;
    }
    long x = a * b - 1;
    long sq = sqr(x);
    long res = 2 * sq - ((long)(sq == x / sq));
    if (a == b)
      printf("%lld\n", res);
    else
      printf("%lld\n", res - 1);
  }
}
Problem p03388 (C++)

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

int main() {
  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (a == b || a == b - 1) {
      cout << (2 * a - 2) << '\n';
    } else {
      ll c = a;
      while ((c + 1) * (c + 1) < a * b)
        c++;
      if (c * (c + 1) >= a * b) {
        cout << 2 * c - 2 << '\n';
      } else {
        cout << 2 * c - 1 << '\n';
      }
    }
  }
}


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

int main() {
  int q;
  cin >> q;
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    if (a == b || a == b - 1) {
      cout << (2 * a - 2) << '\n';
    } else {
      ll c = (ll)sqrt(a * b) - 3;
      while ((c + 1) * (c + 1) < a * b)
        c++;
      if (c * (c + 1) >= a * b) {
        cout << 2 * c - 2 << '\n';
      } else {
        cout << 2 * c - 1 << '\n';
      }
    }
  }
}
Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
// スタックサイズ: 100MB

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef int64_t ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
const ll INF = 1000000000000000000;
#define fori(i, a, b) for (ll i = (a); i < (b); ++i)
#define ford(i, a, b) for (ll i = (b - 1); (a) <= i; --i)
#define rep(i, n) fori(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define fst first
#define snd second
ll pw(ll x, ll y) {
  ll r = 1;
  rep(i, y) { r *= x; };
  return r;
}

int main() {

  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll a, b, A, B;
    cin >> a >> b;
    A = min(a, b), B = max(a, b);

    if (A == B)
      cout << 2 * A - 2 << endl;
    else if (A + 1 == B)
      cout << 2 * A - 2 << endl;
    else {
      ll C;
      for (C = min(A, B); pw(C + 1, 2) < A * B; ++C)
        ;
      if (A * B <= C * (C + 1))
        cout << (A - 1) + (2 * C - A - 1) << endl;
      else
        cout << (A - 1) + (2 * C - A) << endl;
    }
  }
}

Fixed submission:
// スタックサイズ: 100MB

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef int64_t ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
const ll INF = 1000000000000000000;
#define fori(i, a, b) for (ll i = (a); i < (b); ++i)
#define ford(i, a, b) for (ll i = (b - 1); (a) <= i; --i)
#define rep(i, n) fori(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define fst first
#define snd second
ll pw(ll x, ll y) {
  ll r = 1;
  rep(i, y) { r *= x; };
  return r;
}

int main() {

  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll a, b, A, B;
    cin >> a >> b;
    A = min(a, b), B = max(a, b);

    if (A == B)
      cout << 2 * A - 2 << endl;
    else if (A + 1 == B)
      cout << 2 * A - 2 << endl;
    else {
      ll C = 1e9, ng = -1;
      while (abs(C - ng) > 1) {
        ll mid = (C + ng) / 2;
        if (mid * mid >= A * B)
          C = mid;
        else
          ng = mid;
      }
      --C;
      if (A * B <= C * (C + 1))
        cout << (A - 1) + (2 * C - A - 1) << endl;
      else
        cout << (A - 1) + (2 * C - A) << endl;
    }
  }
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using vint = vector<int>;
using pint = pair<int, int>;

const double EPS = 1e-9;
const int inf = numeric_limits<int>::max();

#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; --i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define RREP(i, a, b) for (int i = int(b - 1); i >= int(a); --i)
#define SHOW(a) cout << #a << " = " << a << endl
#define ARR(a, n)                                                              \
  for (int i = 0; i < int(n); ++i)                                             \
  cout << #a << "[" << i << "]"                                                \
       << " = " << a[i] << endl

ll num(ll A, ll B) {
  ll score = A * B - 1;
  ll i_max = sqrt(score);

  ll res = i_max * 2;
  if (i_max >= min(A, B))
    res--;
  if (i_max == score / i_max)
    res--;

  return res;
}

void solve() {
  int Q;
  cin >> Q;
  int A[105], B[105];
  rep(i, Q) { cin >> A[i] >> B[i]; }

  rep(i, Q) { cout << num(A[i], B[i]) << endl; }
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using vint = vector<int>;
using pint = pair<int, int>;

const double EPS = 1e-9;
const int inf = numeric_limits<int>::max();

#define rep(i, n) for (ll i = 0; i < ll(n); ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; --i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define RREP(i, a, b) for (int i = int(b - 1); i >= int(a); --i)
#define SHOW(a) cout << #a << " = " << a << endl
#define ARR(a, n)                                                              \
  for (int i = 0; i < int(n); ++i)                                             \
  cout << #a << "[" << i << "]"                                                \
       << " = " << a[i] << endl

ll num(ll A, ll B) {
  if (A > B) {
    ll tmp = A;
    A = B;
    B = tmp;
  }
  if (A == B || A + 1 == B) {
    return 2 * A - 2;
  }
  ll C = sqrt(A * B);
  if (C * C == A * B) {
    C--;
  }
  if (C * (C + 1) >= A * B) {
    return 2 * C - 2;
  }
  return 2 * C - 1;
}

void solve() {
  int Q;
  cin >> Q;
  int A[105], B[105];
  rep(i, Q) { cin >> A[i] >> B[i]; }

  rep(i, Q) { cout << num(A[i], B[i]) << endl; }
}

int main() {
  solve();
  return 0;
}
Problem p03388 (C++)

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

using namespace std;

typedef unsigned long long ull;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  freopen("AtCoder2/input.txt", "r", stdin);
  freopen("AtCoder2/output.txt", "w", stdout);

  int q;
  ull t;
  cin >> q;
  ull a[q], b[q];
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < q; i++) {
    t = sqrt(a[i] * b[i]);
    if (a[i] == b[i]) {
      cout << 2 * (a[i] - 1) << endl;
    } else if (t * t == a[i] * b[i]) {
      cout << 2 * t - 3 << endl;
    } else if (t * (t + 1) >= a[i] * b[i]) {
      cout << 2 * t - 2 << endl;
    } else {
      cout << 2 * t - 1 << endl;
    }
  }
  return 0;
}

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

using namespace std;

typedef unsigned long long ull;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  // freopen("AtCoder2/input.txt", "r", stdin);
  // freopen("AtCoder2/output.txt", "w", stdout);

  int q;
  ull t;
  cin >> q;
  ull a[q], b[q];
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < q; i++) {
    t = sqrt(a[i] * b[i]);
    if (a[i] == b[i]) {
      cout << 2 * (a[i] - 1) << endl;
    } else if (t * t == a[i] * b[i]) {
      cout << 2 * t - 3 << endl;
    } else if (t * (t + 1) >= a[i] * b[i]) {
      cout << 2 * t - 2 << endl;
    } else {
      cout << 2 * t - 1 << endl;
    }
  }
  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using VI = vector<long>;

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long a, b, aa, bb;
    cin >> a >> b;
    aa = min(a, b);
    bb = max(a, b);
    if (a == b) {
      cout << 2 * a - 2 << endl;
      continue;
    }
    if (aa + 1 == bb) {
      cout << 2 * aa - 2 << endl;
    } else {
      long c = sqrt(aa * bb - 1);
      assert(c * c < a * b);
      assert((c + 1) * (c + 1) >= a * b);
      if (c * (c + 1) >= aa * bb) {
        cout << 2 * c - 2 << endl;
      } else {
        cout << 2 * c - 1 << endl;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using VI = vector<long>;

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    long a, b, aa, bb;
    cin >> a >> b;
    aa = min(a, b);
    bb = max(a, b);
    if (a == b) {
      cout << 2 * a - 2 << endl;
      continue;
    }
    if (aa + 1 == bb) {
      cout << 2 * aa - 2 << endl;
    } else {
      long c = sqrt(aa * bb);
      if (c * c == a * b) {
        c--;
      }
      assert(c * c < a * b);
      assert((c + 1) * (c + 1) >= a * b);
      if (c * (c + 1) >= aa * bb) {
        cout << 2 * c - 2 << endl;
      } else {
        cout << 2 * c - 1 << endl;
      }
    }
  }
}
Problem p03388 (C++)

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

const int mod = (int)1e9 + 7;

int A[200005];
long long dpPos[200005], dpCol[200005];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);

  int Q;
  cin >> Q;

  while (Q--) {
    long long a, b;
    cin >> a >> b;

    if (a > b)
      swap(a, b);

    long long pro = a * b - 1;
    long long sq = sqrt((long double)pro);

    if (pro / sq == sq)
      cout << sq * 2 - 1 - (a <= sq) << "\n";
    else
      cout << sq * 2 - (a <= sq) << "\n";
  }

  return 0;
}


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

const int mod = (int)1e9 + 7;

int A[200005];
long long dpPos[200005], dpCol[200005];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL), cout.tie(NULL);

  int Q;
  cin >> Q;

  while (Q--) {
    long long a, b;
    cin >> a >> b;

    if (a == 1 && b == 1) {
      cout << 0 << "\n";
      continue;
    }

    if (a > b)
      swap(a, b);

    long long pro = a * b - 1;
    long long sq = sqrt((long double)pro);

    if (pro / sq == sq)
      cout << sq * 2 - 1 - (a <= sq) << "\n";
    else
      cout << sq * 2 - (a <= sq) << "\n";
  }

  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iterator>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MOD 1000000007
using namespace std;

int main(int argc, char *argv[]) {
  long qq;
  scanf("%ld", &qq);

  vector<long> a(qq);
  vector<long> b(qq);

  long i;
  for (i = 0; i < qq; i++) {
    scanf("%ld %ld", &(a[i]), &(b[i]));
  }

  for (i = 0; i < qq; i++) {
    ll x = MIN(a[i], b[i]);
    ll y = MAX(a[i], b[i]);

    ll ans = x - 1;
    if (x == y) {
      ans += (x - 1);
    } else {
      double s = sqrt((double)x * y);
      ll tmp = (ll)s;
      tmp--;
      while (1) {
        ll tmp2 = (ll)x * y / tmp;
        if (tmp2 * tmp == x * y)
          tmp2--;
        if (tmp2 < tmp) {
          ans += (tmp - x);
          ans += (tmp2 - 1);
          break;
        }
        tmp++;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iterator>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MOD 1000000007
using namespace std;

int main(int argc, char *argv[]) {
  long qq;
  scanf("%ld", &qq);

  vector<long> a(qq);
  vector<long> b(qq);

  long i;
  for (i = 0; i < qq; i++) {
    scanf("%ld %ld", &(a[i]), &(b[i]));
  }

  for (i = 0; i < qq; i++) {
    ll x = MIN(a[i], b[i]);
    ll y = MAX(a[i], b[i]);

    ll ans = x - 1;
    if (x == y) {
      ans += (x - 1);
    } else {
      double s = sqrt((double)x * y);
      ll tmp = (ll)s;
      tmp--;
      if (tmp <= x)
        tmp = x;
      while (1) {
        ll tmp2 = (ll)x * y / tmp;
        if (tmp2 * tmp == x * y)
          tmp2--;
        if (tmp2 < tmp) {
          ans += (tmp - x);
          ans += (tmp2 - 1);
          break;
        }
        tmp++;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric> // accumulate(v.begin(), v.end(), 0)
#include <vector>

using namespace std;
#define ll long long

int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll A, B;
    cin >> A >> B;
    if (A > B) {
      ll tmp = A;
      A = B;
      B = tmp;
    }
    ll M = A * B;
    ll root = floor(sqrt(M - 1));
    ll ret = root * 2;
    if (root == (M - 1) / root)
      ret--;
    if (A <= root)
      ret--;
    // if((M-1) % B == 0 && (M-1)/B <= root) ret--;
    cout << ret << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric> // accumulate(v.begin(), v.end(), 0)
#include <vector>

using namespace std;
#define ll long long

int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll A, B;
    cin >> A >> B;
    if (A > B) {
      ll tmp = A;
      A = B;
      B = tmp;
    }
    if (A == B)
      cout << 2 * A - 2 << endl;
    else if (A + 1 == B)
      cout << 2 * A - 2 << endl;
    else {
      // ll C = floor(sqrt(A * B - 1));
      ll C = sqrt(A * B);
      if (C * C == A * B)
        C--;
      if (C * (C + 1) >= A * B)
        cout << 2 * C - 2 << endl;
      else
        cout << 2 * C - 1 << endl;
    }
  }
  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll A, B, ans = 0;
    cin >> A >> B;
    if (A > B)
      swap(A, B);
    ans += (A - 1);
    ll C = floor(sqrt(A * B)), now = A;
    for (ll i = B - 1; i >= C; i--) {
      ll j = (A * B - 1) / i;
      if (j != now)
        ans++;
      now = j;
    }
    ans += (C - 1);
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll A, B, ans = 0;
    cin >> A >> B;
    if (A > B)
      swap(A, B);
    ans += (A - 1);
    ll C = floor(sqrt(A * B));
    ans += (C - A);
    if (C * (C + 1) < A * B)
      ans++;
    if (A == B)
      ans++;
    if (C * C == A * B)
      ans--;
    ans += (C - 1);
    cout << ans << endl;
  }
}
Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long a, b, c, d, e;
    scanf("%lld%lld", &a, &b);
    if (a == b)
      printf("%lld\n", 2 * a - 2);
    else {
      d = a > b ? a : b;
      e = a < b ? a : b;
      if (d - e == 1)
        printf("%lld\n", 2 * e - 2);
      else {
        for (c = 0; c < sqrt(a * b); c++)
          ;
        c--;
        if (c * (c + 1) >= b * a)
          printf("%lld\n", 2 * c - 2);
        else
          printf("%lld\n", 2 * c - 1);
      }
    }
  }
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long a, b, c, d, e;
    scanf("%lld%lld", &a, &b);
    if (a == b)
      printf("%lld\n", 2 * a - 2);
    else {
      d = a > b ? a : b;
      e = a < b ? a : b;
      if (d - e == 1)
        printf("%lld\n", 2 * e - 2);
      else {
        for (c = sqrt(a * b); c * c < a * b; c++)
          ;
        c--;
        if (c * (c + 1) >= b * a)
          printf("%lld\n", 2 * c - 2);
        else
          printf("%lld\n", 2 * c - 1);
      }
    }
  }
  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -std=c++11 -o2 -Wall ABC093D.cpp -o ./out/ABC093D.out
./out/ABC093D.out
*/

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

typedef long long ll;

int q;
ll a, b;

int main() {
  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    if (a > b) {
      swap(a, b);
    }
    auto check = [&](int _mid) -> bool {
      for (int j = _mid / 2 - 2; j <= _mid / 2 + 2; ++j) {
        if (j < 0 || b - a < j)
          continue;
        if ((a + j) * (a + _mid - j) >= a * b)
          return false;
      }
      return true;
    };
    ll mid;
    ll lb, ub;
    lb = 0;
    ub = b - a + 1;
    while (ub - lb > 1) {
      mid = (lb + ub) / 2;
      if (check(mid)) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    cout << a * 2 - 2 + lb << "\n";
    //      cout << a << " " << lb << "\n";
  }
}


Fixed submission:
/*
g++ -std=c++11 -o2 -Wall ABC093D.cpp -o ./out/ABC093D.out
./out/ABC093D.out
*/

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

typedef long long ll;

int q;
ll a, b;

int main() {
  //    freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    if (a > b) {
      swap(a, b);
    }
    auto check = [&](int _mid) -> bool {
      for (int j = _mid / 2 - 2; j <= _mid / 2 + 2; ++j) {
        if (j < 0 || b - a < j)
          continue;
        if ((a + j) * (a + _mid - j) >= a * b)
          return false;
      }
      return true;
    };
    ll mid;
    ll lb, ub;
    lb = 0;
    ub = b - a + 1;
    while (ub - lb > 1) {
      mid = (lb + ub) / 2;
      if (check(mid)) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    cout << a * 2 - 2 + lb << "\n";
    //      cout << a << " " << lb << "\n";
  }
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
string S;
ll Q;
int main() {
  cin >> Q;
  for (ll i = 0; i < Q; i++) {
    ll A, B;
    cin >> A >> B;
    ll border;
    if (A == B) {
      border = A - 1;
    } else {
      border = sqrt(A * B);
    }
    ll x = (A * B % border == 0 ? -1 : 0);
    ll y = (A * B % border == 0 ? -1 : 0);
    cout << (A * B / border + x) - (min(A, B) + 1) + border + min(A, B) - 1
         << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
string S;
ll Q;
int main() {
  cin >> Q;
  for (ll i = 0; i < Q; i++) {
    ll A, B;
    cin >> A >> B;
    ll border;
    if (A == B) {
      border = A - 1;
    } else {
      border = sqrt(A * B);
    }
    if (border == 0) {
      cout << 0 << endl;
      continue;
    }
    ll x = (A * B % border == 0 ? -1 : 0);
    ll y = (A * B % border == 0 ? -1 : 0);
    cout << (A * B / border + x) - (min(A, B) + 1) + border + min(A, B) - 1
         << endl;
  }

  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int64_t A, B;
    cin >> A >> B;
    int64_t a = min(A, B);
    int64_t b = max(A, B);
    if (a == b)
      cout << 2 * a - 2 << endl;
    else {
      int64_t c = a * b - 1;
      int64_t J = 0;
      for (int j = 0; j * (j + 1) <= c; j++)
        J = j + 1;
      int64_t ans = J + (c / J) - 2;
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int64_t A, B;
    cin >> A >> B;
    int64_t a = min(A, B);
    int64_t b = max(A, B);
    if (a == b)
      cout << 2 * a - 2 << endl;
    else {
      int64_t c = a * b - 1;
      int64_t J = powl(c, 0.5);
      if (J * (J + 1) <= c)
        J++;
      int64_t ans = J + (c / J) - 2;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  REP(i, N) {
    ll a, b;
    cin >> a >> b;

    ll ok = 1, ng = a * b;
    while (ng - ok > 1) {
      ll mid = (ok + ng) / 2;

      if ((long double)(a)*b / mid <= mid) {
        ng = mid;
      } else {
        ok = mid;
      }
    }

    ll ans = 2 * (ng - 1);

    if (a < ng)
      ans--;
    if (b < ng)
      ans--;
    if ((a * b - 1) / (ng - 1) == (ng - 1))
      ans--;
    print(ans);
  }
}


Fixed submission:

#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() { cerr << endl; }

template <typename H, typename... T> void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}

template <typename T> void print(T x) { cout << x << endl; }

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  REP(i, N) {
    ll a, b;
    cin >> a >> b;

    ll ok = 1, ng = a * b;
    while (ng - ok > 1) {
      ll mid = (ok + ng) / 2;

      if ((long double)(a)*b / mid <= mid) {
        ng = mid;
      } else {
        ok = mid;
      }
    }

    if (ng == 1) {
      print(0);
      continue;
    }

    ll ans = 2 * (ng - 1);

    if (a < ng)
      ans--;
    if (b < ng)
      ans--;
    if ((a * b - 1) / (ng - 1) == (ng - 1))
      ans--;
    print(ans);
  }
}
Problem p03388 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const double PI = acos(-1.0);
// cout << fixed << setprecision(14);

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int Q;
  cin >> Q;
  rep(loop, Q) {
    int ans;
    int a, b;
    cin >> a >> b;
    int c = a * b;
    int left = 0, right = MOD;
    int mid;
    while (right - left > 1) {
      mid = (left + right) / 2;
      if (mid * mid >= c)
        right = mid;
      else
        left = mid;
    }
    int maxi = left;
    if ((c - 1) / maxi > maxi) {
      ans = maxi * 2;
      if (a <= maxi)
        --ans;
      if (b <= maxi)
        --ans;
    } else {
      ans = maxi * 2 - 1;
      if (a <= maxi)
        --ans;
      if (b <= maxi)
        --ans;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int, int> P;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const double PI = acos(-1.0);
// cout << fixed << setprecision(14);

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int Q;
  cin >> Q;
  rep(loop, Q) {
    int ans;
    int a, b;
    cin >> a >> b;
    int c = a * b;
    int left = 0, right = MOD;
    int mid;
    while (right - left > 1) {
      mid = (left + right) / 2;
      if (mid * mid >= c)
        right = mid;
      else
        left = mid;
    }
    int maxi = left;
    if (maxi == 0) {
      cout << 0 << endl;
      continue;
    }
    if ((c - 1) / maxi > maxi) {
      ans = maxi * 2;
      if (a <= maxi)
        --ans;
      if (b <= maxi)
        --ans;
    } else {
      ans = maxi * 2 - 1;
      if (a <= maxi)
        --ans;
      if (b <= maxi)
        --ans;
    }
    cout << ans << endl;
  }
}
Problem p03389 (C++)

Buggy submission (status: Time Limit Exceeded):
// by sdfzchy
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const int inf = (1 << 30);
inline int in() {
  char ch = getchar();
  int f = 1, tmp = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = (tmp << 1) + (tmp << 3) + (ch - '0');
    ch = getchar();
  }
  return tmp * f;
}

int n, m;
int A, B, C;
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  //	freopen("out.out","w",stdout);
#endif
  A = in(), B = in(), C = in();
  int ret = 0;
  if (A > B)
    swap(A, B);
  if (A > C)
    swap(A, C);
  if (B > C)
    swap(B, C);
  ret += C - B;
  A += C - B;
  if ((C - A) & 1)
    ret += (C - A + 1) / 2 + 1;
  else
    ret += (C - A) / 2;
  cout << ret << endl;
  return 0;
}


Fixed submission:
// by sdfzchy
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const int inf = (1 << 30);
inline int in() {
  char ch = getchar();
  int f = 1, tmp = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    tmp = (tmp << 1) + (tmp << 3) + (ch - '0');
    ch = getchar();
  }
  return tmp * f;
}

int n, m;
int A, B, C;
int main() {
  A = in(), B = in(), C = in();
  int ret = 0;
  if (A > B)
    swap(A, B);
  if (A > C)
    swap(A, C);
  if (B > C)
    swap(B, C);
  ret += C - B;
  A += C - B;
  if ((C - A) & 1)
    ret += (C - A + 1) / 2 + 1;
  else
    ret += (C - A) / 2;
  cout << ret << endl;
  return 0;
}
Problem p03389 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int a[3];
  cin >> a[0] >> a[1] >> a[2];
  sort(a, a + 3);
  int count = 0;
  count += a[2] - a[1];
  a[0] += count;
  a[1] += count;

  count += (a[2] - a[0]) / 2;
  count += (a[2] % a[0]) == 1 ? 2 : 0;

  cout << count << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int a[3];
  cin >> a[0] >> a[1] >> a[2];
  sort(a, a + 3);
  int count = 0;
  count += a[2] - a[1];
  a[0] += count;
  a[1] += count;

  count += (a[2] - a[0]) / 2;
  count += (a[2] - a[0]) % 2 == 1 ? 2 : 0;

  cout << count << endl;
}
Problem p03389 (C++)

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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

int main() {
  int a, b, c;
  int x, y, z;
  cin >> a >> b >> c;
  x = min(a, min(b, c));
  z = max(a, max(b, c));
  y = a + b + c - x - z;
  if ((y - x) % 2)
    printf("%d\n", ((z + 1) * 3 - a - b - c) / 2);
  else
    printf("%d\n", (y - z) / x + z - y);
  return 0;
}


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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

int main() {
  int a, b, c;
  int x, y, z;
  cin >> a >> b >> c;
  x = min(a, min(b, c));
  z = max(a, max(b, c));
  y = a + b + c - x - z;
  if ((y - x) % 2)
    printf("%d\n", ((z + 1) * 3 - a - b - c) / 2);
  else
    printf("%d\n", (y - x) / 2 + z - y);
  return 0;
}
Problem p03389 (C++)

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

const int N = 51;

int f[N][N][N];

int a, b, c;

int main() {
  scanf("%d%d%d", &a, &b, &c);
  int mx = max(max(a, b), c);
  for (int i = a; i <= mx + 2; i++)
    for (int j = b; j <= mx + 2; j++)
      for (int k = c; k <= mx + 2; k++) {
        if (i == a && j == b && k == c)
          continue;
        f[i][j][k] = 1000;
        if (i - 1 >= a && j - 1 >= b)
          f[i][j][k] = min(f[i][j][k], f[i - 1][j - 1][k] + 1);
        if (i - 1 >= a && k - 1 >= c)
          f[i][j][k] = min(f[i][j][k], f[i - 1][j][k - 1] + 1);
        if (j - 1 >= b && k - 1 >= c)
          f[i][j][k] = min(f[i][j][k], f[i][j - 1][k - 1] + 1);
        if (i - 2 >= a)
          f[i][j][k] = min(f[i][j][k], f[i - 2][j][k] + 1);
        if (j - 2 >= b)
          f[i][j][k] = min(f[i][j][k], f[i][j - 2][k] + 1);
        if (k - 2 >= c)
          f[i][j][k] = min(f[i][j][k], f[i][j][k - 2] + 1);
      }
  int ans = 1001;
  for (int i = mx; i <= mx + 2; i++)
    ans = min(ans, f[i][i][i]);
  printf("%d", ans);
}

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

const int N = 101;

int f[N][N][N];

int a, b, c;

int main() {
  scanf("%d%d%d", &a, &b, &c);
  int mx = max(max(a, b), c);
  for (int i = a; i <= mx + 2; i++)
    for (int j = b; j <= mx + 2; j++)
      for (int k = c; k <= mx + 2; k++) {
        if (i == a && j == b && k == c)
          continue;
        f[i][j][k] = 1000;
        if (i - 1 >= a && j - 1 >= b)
          f[i][j][k] = min(f[i][j][k], f[i - 1][j - 1][k] + 1);
        if (i - 1 >= a && k - 1 >= c)
          f[i][j][k] = min(f[i][j][k], f[i - 1][j][k - 1] + 1);
        if (j - 1 >= b && k - 1 >= c)
          f[i][j][k] = min(f[i][j][k], f[i][j - 1][k - 1] + 1);
        if (i - 2 >= a)
          f[i][j][k] = min(f[i][j][k], f[i - 2][j][k] + 1);
        if (j - 2 >= b)
          f[i][j][k] = min(f[i][j][k], f[i][j - 2][k] + 1);
        if (k - 2 >= c)
          f[i][j][k] = min(f[i][j][k], f[i][j][k - 2] + 1);
      }
  int ans = 1001;
  for (int i = mx; i <= mx + 2; i++)
    ans = min(ans, f[i][i][i]);
  printf("%d", ans);
}
Problem p03389 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**6)

debug_mode = True if len(sys.argv) > 1 and sys.argv[1] == "-d" else False
if debug_mode:
    import os

    infile = open(os.path.basename(__file__).replace(".py", ".in"))

    def input():
        return infile.readline()


# ==============================================================


def main():
    a, b, c = list(map(int, input().strip().split()))
    s = sum(a, b, c)
    x = min(a, b, c)
    steps = 0
    while True:
        if s + steps * 2 == 3 * x:
            break
        elif s + steps * 2 < 3 * x:
            steps += 1
        elif s + steps * 2 > 3 * x:
            x += 1
    print(steps)


main()

# ==============================================================

if debug_mode:
    infile.close()


Fixed submission:
import sys

sys.setrecursionlimit(10**6)

debug_mode = True if len(sys.argv) > 1 and sys.argv[1] == "-d" else False
if debug_mode:
    import os

    infile = open(os.path.basename(__file__).replace(".py", ".in"))

    def input():
        return infile.readline()


# ==============================================================


def main():
    a, b, c = list(map(int, input().strip().split()))
    s = sum((a, b, c))
    x = max(a, b, c)
    steps = 0
    while True:
        if s + steps * 2 == 3 * x:
            break
        elif s + steps * 2 < 3 * x:
            steps += 1
        elif s + steps * 2 > 3 * x:
            x += 1
    print(steps)


main()

# ==============================================================

if debug_mode:
    infile.close()
Problem p03389 (C++)

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

using namespace std;

struct all {
  int a, b, c;
  all(int a, int b, int c) : a(a), b(b), c(c) {}
};

queue<all> q;
bool vis[500][500][500];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
#endif

  int a, b, c;
  cin >> a >> b >> c;

  q.push(all(a, b, c));
  vis[a][b][c] = true;

  int cost = 0, size;
  while (!q.empty()) {
    size = q.size();

    while (size-- != 0) {
      all fr = q.front();
      q.pop();

      if (fr.a == fr.b && fr.b == fr.c) {
        cout << cost << endl;
        return 0;
      }

      if (!vis[fr.a + 1][fr.b + 1][fr.c]) {
        vis[fr.a + 1][fr.b + 1][fr.c] = true;
        q.push(all(fr.a + 1, fr.b + 1, fr.c));
      }

      if (!vis[fr.a + 1][fr.b][fr.c + 1]) {
        vis[fr.a + 1][fr.b][fr.c + 1] = true;
        q.push(all(fr.a + 1, fr.b, fr.c + 1));
      }

      if (!vis[fr.a][fr.b + 1][fr.c + 1]) {
        vis[fr.a][fr.b + 1][fr.c + 1] = true;
        q.push(all(fr.a, fr.b + 1, fr.c + 1));
      }

      if (!vis[fr.a + 2][fr.b][fr.c]) {
        vis[fr.a + 2][fr.b][fr.c] = true;
        q.push(all(fr.a + 2, fr.b, fr.c));
      }

      if (!vis[fr.a][fr.b + 2][fr.c]) {
        vis[fr.a][fr.b + 2][fr.c] = true;
        q.push(all(fr.a, fr.b + 2, fr.c));
      }

      if (!vis[fr.a][fr.b][fr.c + 2]) {
        vis[fr.a][fr.b][fr.c + 2] = true;
        q.push(all(fr.a, fr.b, fr.c + 2));
      }
    }

    ++cost;
  }

  cout << cost << endl;

  return 0;
}


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

using namespace std;

struct all {
  int a, b, c;
  all(int a, int b, int c) : a(a), b(b), c(c) {}
};

queue<all> q;
bool vis[500][500][500];

int main() {
  int a, b, c;
  cin >> a >> b >> c;

  q.push(all(a, b, c));
  vis[a][b][c] = true;

  int cost = 0, size;
  while (!q.empty()) {
    size = q.size();

    while (size-- != 0) {
      all fr = q.front();
      q.pop();

      if (fr.a == fr.b && fr.b == fr.c) {
        cout << cost << endl;
        return 0;
      }

      if (!vis[fr.a + 1][fr.b + 1][fr.c]) {
        vis[fr.a + 1][fr.b + 1][fr.c] = true;
        q.push(all(fr.a + 1, fr.b + 1, fr.c));
      }

      if (!vis[fr.a + 1][fr.b][fr.c + 1]) {
        vis[fr.a + 1][fr.b][fr.c + 1] = true;
        q.push(all(fr.a + 1, fr.b, fr.c + 1));
      }

      if (!vis[fr.a][fr.b + 1][fr.c + 1]) {
        vis[fr.a][fr.b + 1][fr.c + 1] = true;
        q.push(all(fr.a, fr.b + 1, fr.c + 1));
      }

      if (!vis[fr.a + 2][fr.b][fr.c]) {
        vis[fr.a + 2][fr.b][fr.c] = true;
        q.push(all(fr.a + 2, fr.b, fr.c));
      }

      if (!vis[fr.a][fr.b + 2][fr.c]) {
        vis[fr.a][fr.b + 2][fr.c] = true;
        q.push(all(fr.a, fr.b + 2, fr.c));
      }

      if (!vis[fr.a][fr.b][fr.c + 2]) {
        vis[fr.a][fr.b][fr.c + 2] = true;
        q.push(all(fr.a, fr.b, fr.c + 2));
      }
    }

    ++cost;
  }

  cout << cost << endl;

  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

inline ll num(const ll P, const ll a) {
  const ll maxi = P / a;
  const ll mini = (P + a + 1) / (a + 1);
  return maxi - mini + 1;
}

inline ll bin(const ll P) {
  ll inf = 0;
  ll sup = P + 1;
  while (sup - inf > 1) {
    const ll mid = (inf + sup) / 2;
    if (P >= (__int128_t)mid * (mid - 1)) {
      inf = mid;
    } else {
      sup = mid;
    }
  }
  return inf;
}

int main() {
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    ll A, B;
    cin >> A >> B;
    const ll P = A * B - 1;
    if (A < B) {
      swap(A, B);
    }
    const ll u = bin(P);
    const ll v = P / u;
    ll ans = v + u - 1;
    if (P >= A and A >= u and num(P, P / A) == 1) {
      ans--;
    }
    const ll b = P / B;
    if (b > 0 and num(P, B) > 0 and P / b - P / (b + 1) <= 1) {
      ans--;
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007LL;
template <typename T> constexpr T INF = numeric_limits<T>::max() / 10;

inline ll num(const ll P, const ll a) {
  const ll maxi = P / a;
  const ll mini = (P + a + 1) / (a + 1);
  return maxi - mini + 1;
}

inline ll bin(const ll P) {
  ll inf = 0;
  ll sup = P + 1;
  while (sup - inf > 1) {
    const ll mid = (inf + sup) / 2;
    if (P >= (__int128_t)mid * (mid - 1)) {
      inf = mid;
    } else {
      sup = mid;
    }
  }
  return inf;
}

int main() {
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    ll A, B;
    cin >> A >> B;
    if (A == 1 and B == 1) {
      cout << 0 << endl;
      continue;
    }
    const ll P = A * B - 1;
    if (A < B) {
      swap(A, B);
    }
    const ll u = bin(P);
    const ll v = P / u;
    ll ans = v + u - 1;
    if (P >= A and A >= u and num(P, P / A) == 1) {
      ans--;
    }
    const ll b = P / B;
    if (b > 0 and num(P, B) > 0 and P / b - P / (b + 1) <= 1) {
      ans--;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03390 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pa pair<int, int>
#define mo 1000000007
#define ll long long
#define se second
#define fi first
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll x = a * b;
    ll v = max(0, (int)sqrt(x) - 2);
    for (; (v + 1) * (v + 1) < x; v++)
      ;
    printf("%lld\n",
           2 * v - (a <= v) - (b <= v) - (x / v == v && a != v && b != v));
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define pa pair<int, int>
#define mo 1000000007
#define ll long long
#define se second
#define fi first
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    if (a > b)
      swap(a, b);
    ll x = a * b;
    ll v = max(0, (int)sqrt(x) - 2);
    for (; (v + 1) * (v + 1) < x; v++)
      ;
    ll ans = 2 * v - (a <= v) - (b <= v);
    if (v && (((x - 1) / v == v) || ((x - 1) / v == b)))
      ans--;
    // if (v&&d/v==v)
    printf("%lld\n", ans);
  }
}