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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

template <typename T> using vector2 = vector<vector<T>>;
template <typename T> vector2<T> initVec2(size_t n0, size_t n1, T e = T()) {
  return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T> using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()) {
  return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

  int k, a, b;
  cin >> k >> a >> b;

  if (a - b <= 0 && a < k) {
    cout << -1 << endl;
    return 0;
  }

  int ans = 1;
  ans += (k - b - 1) / (a - b) * 2;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

#define int long long int
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9 + 7;

template <typename T> using vector2 = vector<vector<T>>;
template <typename T> vector2<T> initVec2(size_t n0, size_t n1, T e = T()) {
  return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T> using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()) {
  return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

  int k, a, b;
  cin >> k >> a >> b;

  if (a - b <= 0) {
    if (a >= k) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int ans = 1;
  ans += (k - b - 1) / (a - b) * 2;
  cout << ans << endl;

  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, a, b;
  cin >> k >> a >> b;
  if (k <= a)
    cout << 1 << endl;
  else if (a < b)
    cout << -1 << endl;
  else {
    ll ans = 1;
    k -= a;
    ans += (k + (a - b) - 1) / (a - b) * 2;
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k, a, b;
  cin >> k >> a >> b;
  if (k <= a)
    cout << 1 << endl;
  else if (a <= b)
    cout << -1 << endl;
  else {
    ll ans = 1;
    k -= a;
    ans += (k + (a - b) - 1) / (a - b) * 2;
    cout << ans << endl;
  }
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long LL;
int main() {
  LL K, A, B;
  scanf("%lld %lld %lld", &K, &A, &B);
  LL sum = A;
  if (sum < K && B >= A)
    printf("-1\n");
  else {
    LL gg;
    LL times = (K - sum) / (A - B);
    if ((K - sum) % (A - B) != 0)
      times++;
    gg = times * 2 + 1;
    printf("%lld\n", gg);
  }
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef long long LL;
int main() {
  LL K, A, B;
  scanf("%lld %lld %lld", &K, &A, &B);
  LL sum = A;
  if (sum >= K)
    printf("1\n");
  else if (sum < K && B >= A)
    printf("-1\n");
  else {
    LL gg;
    LL times = (K - sum) / (A - B);
    if ((K - sum) % (A - B) != 0)
      times++;
    gg = times * 2 + 1;
    printf("%lld\n", gg);
  }
  return 0;
}
Problem p03505 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

int main() {
  ll K, A, B;
  cin >> K >> A >> B;
  ll m = A - B;
  ll s = std::max(K - A, 0LL);
  if (s > 0 && m <= 0)
    cout << -1 << endl;
  else
    cout << 1 + 2 * ((s + m - 1) / m);
  return 0;
}

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

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

int main() {
  ll K, A, B;
  cin >> K >> A >> B;
  ll m = A - B;
  ll s = std::max(K - A, 0LL);
  if (s > 0) {
    if (m <= 0)
      cout << -1 << endl;
    else
      cout << 1 + 2 * ((s + m - 1) / m);
  } else
    cout << 1 << endl;

  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long k, a, b, s, l;
int main() {
  cin >> k >> a >> b;
  if (a < b) {
    cout << -1 << endl;
    return 0;
  }
  if (k % (a - b) != 0)
    l++;
  s = ((k - a) / (a - b)) * 2 + 1;
  cout << s + l * 2 << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
long long a, b, k;
main() {
  scanf("%lld%lld%lld", &k, &a, &b);
  if (k <= a)
    return puts("1"), 0; // 按思路特判
  else if (a <= b)
    return puts("-1"), 0; // 按思路特判
  else
    printf("%lld\n", ((k - a) / (a - b) + bool((k - a) % (a - b))) << 1 | 1);
  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f)                                                                 \
  { cout << (f ? "Yes" : "No") << endl; }
#define YES(f)                                                                 \
  { cout << (f ? "YES" : "NO") << endl; }
#define MPlus(x, y)                                                            \
  { x = (x + y) % M; }
#define MSub(x, y)                                                             \
  { x = (x - y + M) % M; }
#define MMul(x, y)                                                             \
  { x = (x * y) % M; }

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

  int k, a, b;
  cin >> k >> a >> b;
  if (a < b && a < k) {
    cout << -1 << endl;
  } else {
    int ans = 1;
    k -= a;
    if (k > 0) {
      ans += (k + a - b - 1) / (a - b) * 2;
    }
    cout << ans << endl;
  }
}

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f)                                                                 \
  { cout << (f ? "Yes" : "No") << endl; }
#define YES(f)                                                                 \
  { cout << (f ? "YES" : "NO") << endl; }
#define MPlus(x, y)                                                            \
  { x = (x + y) % M; }
#define MSub(x, y)                                                             \
  { x = (x - y + M) % M; }
#define MMul(x, y)                                                             \
  { x = (x * y) % M; }

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

  int k, a, b;
  cin >> k >> a >> b;
  if (a <= b && a < k) {
    cout << -1 << endl;
  } else {
    int ans = 1;
    k -= a;
    if (k > 0) {
      ans += (k + a - b - 1) / (a - b) * 2;
    }
    cout << ans << endl;
  }
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long int k, a, b;
  cin >> k >> a >> b;
  long long int ans = 1;
  long long int now = a;
  long long int kk = a - b;
  if (now >= k) {
    cout << ans << endl;
    return 0;
  }
  if (kk < 0) {
    cout << -1 << endl;
    return 0;
  }
  ans += ((k - now) / kk + ((k - now) % kk != 0 ? 1 : 0)) * 2;

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long int k, a, b;
  cin >> k >> a >> b;
  long long int ans = 1;
  long long int now = a;
  long long int kk = a - b;
  if (now >= k) {
    cout << ans << endl;
    return 0;
  }
  if (kk <= 0) {
    cout << -1 << endl;
    return 0;
  }
  ans += ((k - now) / kk + ((k - now) % kk != 0 ? 1 : 0)) * 2;

  cout << ans << endl;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const ll mod = 998244353, MAX = 1000001, INF = 1 << 30;

int main() {

  ll K, A, B;
  cin >> K >> A >> B;
  if (A < B) {
    if (K <= A)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  } else {
    if (K <= A)
      cout << 1 << endl;
    else {
      K -= A;
      ll need = (K + (A - B - 1)) / (A - B);
      cout << need * 2 + 1 << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const ll mod = 998244353, MAX = 1000001, INF = 1 << 30;

int main() {

  ll K, A, B;
  cin >> K >> A >> B;
  if (A <= B) {
    if (K <= A)
      cout << 1 << endl;
    else
      cout << -1 << endl;
  } else {
    if (K <= A)
      cout << 1 << endl;
    else {
      K -= A;
      ll need = (K + (A - B - 1)) / (A - B);
      cout << need * 2 + 1 << endl;
    }
  }
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, int> P;

long long int INF = 100000000000000000LL;

int main() {

  long long int K, A, B;
  cin >> K >> A >> B;

  if (K <= A) {
    cout << "1" << endl;
    return 0;
  }
  K -= A;

  if (A < B) {
    cout << "-1" << endl;
    return 0;
  }

  cout << (K / (A - B) + (K % (A - B) != 0)) * 2 + 1 << endl;

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, int> P;

long long int INF = 100000000000000000LL;

int main() {

  long long int K, A, B;
  cin >> K >> A >> B;

  if (K <= A) {
    cout << "1" << endl;
    return 0;
  }
  K -= A;

  if (A <= B) {
    cout << "-1" << endl;
    return 0;
  }

  cout << (K / (A - B) + (K % (A - B) != 0)) * 2 + 1 << endl;

  return 0;
}
Problem p03505 (C++)

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

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...)                                                               \
  while (false) {                                                              \
  }
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
typedef unsigned long long ull;

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

ll k, a, b;
ll ans;

void solve() {
  k -= a;
  if (k <= 0) {
    printf("1\n");
    return;
  }
  ll c = a - b;
  if (a <= 0) {
    printf("-1\n");
    return;
  }
  ans = 1 + 2 * ((k + c - 1) / c);
  printf("%lld\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  scanf("%lld %lld %lld", &k, &a, &b);
  solve();

#ifdef LOCAL
  err("ELAPSED TIME: %.3Lf\n", (ld)clock() / CLOCKS_PER_SEC);
#endif

  return 0;
}

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

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...)                                                               \
  while (false) {                                                              \
  }
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
typedef unsigned long long ull;

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

ll k, a, b;
ll ans;

void solve() {
  k -= a;
  if (k <= 0) {
    printf("1\n");
    return;
  }
  ll c = a - b;
  if (c <= 0) {
    printf("-1\n");
    return;
  }
  ans = 1 + 2 * ((k + c - 1) / c);
  printf("%lld\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  scanf("%lld %lld %lld", &k, &a, &b);
  solve();

#ifdef LOCAL
  err("ELAPSED TIME: %.3Lf\n", (ld)clock() / CLOCKS_PER_SEC);
#endif

  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;

signed main() {
  int k, a, b;
  cin >> k >> a >> b;
  int p = a - b;
  if (k <= a) {
    cout << 1 << endl;
    return 0;
  }
  if (p < 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans = (k - a) / p;
  if ((k - a) % p != 0)
    ans++;
  cout << ans * 2 + 1 << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;

signed main() {
  int k, a, b;
  cin >> k >> a >> b;
  int p = a - b;
  if (k <= a) {
    cout << 1 << endl;
    return 0;
  }
  if (p <= 0) {
    cout << -1 << endl;
    return 0;
  }
  int ans = (k - a) / p;
  if ((k - a) % p != 0)
    ans++;
  cout << ans * 2 + 1 << endl;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int main() {
  ll k, a, b;
  cin >> k >> a >> b;
  if (a > b || a >= k) {
    cout << (k - b - 1) / (a - b) * 2 + 1 << endl;
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int main() {
  ll k, a, b;
  cin >> k >> a >> b;
  if (a >= k) {
    cout << 1 << endl;
  } else if (a > b) {
    cout << (k - b - 1) / (a - b) * 2 + 1 << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long k, a, b;
  scanf("%lld%lld%lld", &k, &a, &b);
  long long d = a - b;
  if (d < 0) {
    if (a >= k) {
      puts("1");
      return 0;
    } else {
      puts("-1");
      return 0;
    }
  } else {
    long long ans = (k - a + d - 1) / d;
    printf("%lld\n", ans * 2 + 1);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long k, a, b;
  scanf("%lld%lld%lld", &k, &a, &b);
  long long d = a - b;
  if (d <= 0) {
    if (a >= k) {
      puts("1");
      return 0;
    } else {
      puts("-1");
      return 0;
    }
  } else {
    long long ans = (k - a + d - 1) / d;
    printf("%lld\n", ans * 2 + 1);
  }
  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  ll k, a, b;
  cin >> k >> a >> b;

  if (a >= k) {
    cout << 1 << endl;
    return 0;
  }
  if (b > a) {
    cout << -1 << endl;
    return 0;
  }
  cout << (k - a + a - b - 1) / (a - b) * 2 + 1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int main() {
  ll k, a, b;
  cin >> k >> a >> b;

  if (a >= k) {
    cout << 1 << endl;
    return 0;
  }
  if (b >= a) {
    cout << -1 << endl;
    return 0;
  }
  cout << (k - a + a - b - 1) / (a - b) * 2 + 1 << endl;

  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
// #include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;
typedef string::const_iterator State;

int dy[5] = {0, 0, 1, -1, 0};
int dx[5] = {1, -1, 0, 0, 0};

int main(void) {

  long long int k, a, b;
  long long int now = 0;
  long long int count = -1;

  cin >> k >> a >> b;

  now += a;

  if (a - b <= 0) {
    count = -1;
  }
  if (now >= k) {
    count = 1;
  } else {
    b = a - b;
    if ((k - now) % b == 0) {
      count = ((k - now) / b) * 2 + 1;
    } else {
      count = ((k - now) / b + 1) * 2 + 1;
    }
  }
  cout << count << endl;

  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
// #include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;
typedef string::const_iterator State;

int dy[5] = {0, 0, 1, -1, 0};
int dx[5] = {1, -1, 0, 0, 0};

int main(void) {

  long long int k, a, b;
  long long int now = 0;
  long long int count = -1;

  cin >> k >> a >> b;

  now += a;

  if (a - b <= 0) {
    count = -1;
  }
  if (now >= k) {
    count = 1;
  } else if (a - b > 0) {
    b = a - b;
    if ((k - now) % b == 0) {
      count = ((k - now) / b) * 2 + 1;
    } else {
      count = ((k - now) / b + 1) * 2 + 1;
    }
  }
  cout << count << endl;

  return 0;
}
Problem p03505 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
typedef long long LL;
int main() {
  LL N, A, B;
  scanf("%lld%lld%lld", &N, &A, &B);
  if (A >= N)
    printf("1\n");
  else if (A < B)
    printf("-1\n");
  else {
    N -= A;
    printf("%lld\n", (N + A - B - 1) / (A - B) * 2 + 1);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
typedef long long LL;
int main() {
  LL N, A, B;
  scanf("%lld%lld%lld", &N, &A, &B);
  if (A >= N)
    printf("1\n");
  else if (A <= B)
    printf("-1\n");
  else {
    N -= A;
    printf("%lld\n", (N + A - B - 1) / (A - B) * 2 + 1);
  }
  return 0;
}
Problem p03505 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007

int main() {
  ll k, a, b;
  cin >> k >> a >> b;
  if (a - b <= 0) {
    if (k > a) {
      cout << -1 << endl;
    } else {
      cout << 1 << endl;
    }
  }
  ll s = 0;
  ll ans = 0;
  ans = (k - a) / (a - b);
  s = (a - b) * ans;
  ans *= 2;
  for (ll i = 0; i < 1000000; i++) {
    if (i % 2 == 0) {
      ans++;
      s += a;
      if (s >= k) {
        cout << ans << endl;
        return 0;
      }
    } else {
      ans++;
      s -= b;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007

int main() {
  ll k, a, b;
  cin >> k >> a >> b;
  if (a - b <= 0) {
    if (k > a) {
      cout << -1 << endl;
    } else {
      cout << 1 << endl;
    }
    return 0;
  }
  ll s = 0;
  ll ans = 0;
  ans = (k - a) / (a - b);
  s = (a - b) * ans;
  ans *= 2;
  for (ll i = 0; i < 1000000; i++) {
    if (i % 2 == 0) {
      ans++;
      s += a;
      if (s >= k) {
        cout << ans << endl;
        return 0;
      }
    } else {
      ans++;
      s -= b;
    }
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int n, q, x, y;
int main() {
  scanf("%d%d", &n, &q);
  while (q--) {
    scanf("%d%d", &x, &y);
    --x;
    --y;
    while (x != y) {
      if (x > y)
        x = (x - 1) / n;
      else
        y = (y - 1) / n;
    }
    printf("%d\n", x + 1);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
int n, q, x, y;
int main() {
  scanf("%d%d", &n, &q);
  while (q--) {
    scanf("%d%d", &x, &y);
    if (n == 1) {
      printf("%d\n", x < y ? x : y);
      continue;
    }
    --x;
    --y;
    while (x != y) {
      if (x > y)
        x = (x - 1) / n;
      else
        y = (y - 1) / n;
    }
    printf("%d\n", x + 1);
  }
  return 0;
}
Problem p03506 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll n, q;

ll par(ll x) { return (x + n - 2) / n; }

int main() {
  cin >> n >> q;
  while (q--) {
    ll u, v;
    cin >> u >> v;
    while (u != v) {
      if (u > v)
        u = par(u);
      else
        v = par(v);
    }
    cout << u << endl;
  }
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

ll n, q;

ll par(ll x) { return (x + n - 2) / n; }

int main() {
  cin >> n >> q;
  if (n == 1) {
    while (q--) {
      ll u, v;
      cin >> u >> v;
      cout << min(u, v) << endl;
    }
    return 0;
  }
  while (q--) {
    ll u, v;
    cin >> u >> v;
    while (u != v) {
      if (u > v)
        u = par(u);
      else
        v = par(v);
    }
    cout << u << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb push_back
#define mp make_pair

using namespace std;

template <typename T> inline void upmin(T &x, T y) { y < x ? x = y : 0; }
template <typename T> inline void upmax(T &x, T y) { x < y ? x = y : 0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod pi = acos(-1);
const int oo = 1 << 30;
const LL OO = 1e18;

int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}

int n;
LL s[100];
inline int dep(int x) {
  for (int i = 0;; i++)
    if (x <= s[i])
      return i;
}
inline int solve(int a, int b) {
  if (n == 1)
    return min(a, b);
  while (dep(a) > dep(b))
    a = (a + n - 2) / n;
  while (dep(a) < dep(b))
    b = (b + n - 2) / n;
  while (a != b)
    a = (a + n - 2) / n, b = (b + n - 2) / n;
  return a;
}
int main() {
  n = gi();
  s[0] = 1;
  for (int i = 1; (s[i] = s[i - 1] * n + 1) <= 1e9; i++)
    ;
  for (int Q = gi(); Q--;)
    cout << solve(gi(), gi()) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb push_back
#define mp make_pair

using namespace std;

template <typename T> inline void upmin(T &x, T y) { y < x ? x = y : 0; }
template <typename T> inline void upmax(T &x, T y) { x < y ? x = y : 0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod pi = acos(-1);
const int oo = 1 << 30;
const LL OO = 1e18;

int gi() {
  int w = 0;
  bool q = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    q = 0, c = getchar();
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  return q ? w : -w;
}

int n;
LL s[100];
inline int dep(int x) {
  for (int i = 0;; i++)
    if (x <= s[i])
      return i;
}
inline int solve(int a, int b) {
  if (n == 1)
    return min(a, b);
  while (dep(a) > dep(b))
    a = (a + n - 2) / n;
  while (dep(a) < dep(b))
    b = (b + n - 2) / n;
  while (a != b)
    a = (a + n - 2) / n, b = (b + n - 2) / n;
  return a;
}
int main() {
  n = gi();
  if (n > 1) {
    s[0] = 1;
    for (int i = 1; (s[i] = s[i - 1] * n + 1) <= 1e9; i++)
      ;
  }
  for (int Q = gi(); Q--;)
    cout << solve(gi(), gi()) << endl;
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
int n, q;
int calc(int v, int w) {
  if (v == w)
    return w;
  if (v > w)
    return calc(w, v);
  return calc(v, (w - 1) / n);
}

int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int v, w;
    cin >> v >> w;
    v--;
    w--;
    cout << calc(v, w) + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
int n, q;
int calc(int v, int w) {
  if (v == w)
    return w;
  if (v > w)
    return calc(w, v);
  return calc(v, (w - 1) / n);
}

int main() {
  cin >> n >> q;
  if (n == 1) {
    for (int i = 0; i < q; i++) {
      int v, w;
      cin >> v >> w;
      cout << min(v, w) << endl;
    }
  } else {
    for (int i = 0; i < q; i++) {
      int v, w;
      cin >> v >> w;
      v--;
      w--;
      cout << calc(v, w) + 1 << endl;
    }
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long long ll;
typedef pair<ll, ll> p;
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 ll mod = 1000000007;
const ll linf = 1ll << 60;
const int inf = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int v, w;
    scanf("%d %d", &v, &w);
    while (v) {
      while (v < w) {
        w = (w + n - 2) / n;
      }
      if (v == w) {
        cout << w << "\n";
        break;
      }
      v = (v + n - 2) / n;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long long ll;
typedef pair<ll, ll> p;
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 ll mod = 1000000007;
const ll linf = 1ll << 60;
const int inf = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int v, w;
    scanf("%d %d", &v, &w);
    if (n == 1) {
      cout << min(v, w) << endl;
      continue;
    }
    while (v) {
      while (v < w) {
        w = (w + n - 2) / n;
      }
      if (v == w) {
        cout << w << "\n";
        break;
      }
      v = (v + n - 2) / n;
    }
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  tourist
 *    created: 12.12.2019 13:25:11
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, tt;
  cin >> n >> tt;
  while (tt--) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    while (a != b) {
      if (a > b) {
        a = (a - 1) / n;
      } else {
        b = (b - 1) / n;
      }
    }
    cout << a + 1 << '\n';
  }
  return 0;
}


Fixed submission:
/**
 *    author:  tourist
 *    created: 12.12.2019 13:25:11
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, tt;
  cin >> n >> tt;
  while (tt--) {
    int a, b;
    cin >> a >> b;
    if (n == 1) {
      cout << min(a, b) << '\n';
      continue;
    }
    --a;
    --b;
    while (a != b) {
      if (a > b) {
        a = (a - 1) / n;
      } else {
        b = (b - 1) / n;
      }
    }
    cout << a + 1 << '\n';
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using int64 = long long;

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

  int N, Q;
  std::cin >> N >> Q;

  for (int i = 0; i < Q; i++) {
    int v, w;
    std::cin >> v >> w;

    while (v != w) {
      if (v > w)
        std::swap(v, w);
      w = (w + N - 2) / N;
    }
    std::cout << v << std::endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using int64 = long long;

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

  int N, Q;
  std::cin >> N >> Q;

  for (int i = 0; i < Q; i++) {
    int v, w;
    std::cin >> v >> w;

    if (N == 1) {
      std::cout << std::min(v, w) << std::endl;
      continue;
    }

    while (v != w) {
      if (v > w)
        std::swap(v, w);
      w = (w + N - 2) / N;
    }
    std::cout << v << std::endl;
  }

  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;

int n, q;

int main() {
  cin >> n >> q;

  while (q--) {
    int v, w;
    set<int> st;

    cin >> v >> w;
    v--, w--;

    st.insert(0);
    while (v > 0) {
      st.insert(v);
      v = (v - 1) / n;
    }

    while (st.count(w) == 0) {
      w = (w - 1) / n;
    }

    cout << w + 1 << '\n';
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;

int n, q;

int main() {
  cin >> n >> q;

  while (q--) {
    int v, w;
    set<int> st;

    cin >> v >> w;
    v--, w--;

    if (n == 1) {
      cout << min(v, w) + 1 << endl;
      continue;
    }

    st.insert(0);
    while (v > 0) {
      st.insert(v);
      v = (v - 1) / n;
    }

    while (st.count(w) == 0) {
      w = (w - 1) / n;
    }

    cout << w + 1 << '\n';
  }

  return 0;
}
Problem p03506 (C++)

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

int n;
int par(int x) {
  if (x % n == 0)
    return x / n - 1;
  return x / n;
}

int main() {
  int q;
  cin >> n >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    while (u != v) {
      while (u < v) {
        v = par(v);
      }
      while (u > v) {
        u = par(u);
      }
    }
    cout << u + 1 << '\n';
  }
}

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

int n;
int par(int x) {
  if (x % n == 0)
    return x / n - 1;
  return x / n;
}

int main() {
  int q;
  cin >> n >> q;
  if (n == 1) {
    while (q--) {
      int u, v;
      cin >> u >> v;
      cout << min(u, v) << '\n';
    }
    return 0;
  }
  while (q--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    while (u != v) {
      while (u < v) {
        v = par(v);
      }
      while (u > v) {
        u = par(u);
      }
    }
    cout << u + 1 << '\n';
  }
}
Problem p03506 (C++)

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

//-----------------------------------------------------------------------------

int n, q;

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

  cin >> n >> q;
  REP(i, q) {
    int v, w;
    cin >> v >> w;
    v--, w--;
    if (n == 1) {
      cout << v + 1 << endl;
      continue;
    }
    while (w / n - !(w % n) >= v)
      w = w / n - !(w % n);
    while (w != v) {
      w = w / n - !(w % n), v = v / n - !(v % n);
    }
    cout << v + 1 << endl;
  }

  return 0;
}


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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

//-----------------------------------------------------------------------------

int n, q;

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

  cin >> n >> q;
  REP(i, q) {
    int v, w;
    cin >> v >> w;
    v--, w--;
    if (n == 1) {
      cout << v + 1 << endl;
      continue;
    }
    while (v != w) {
      if (v > w)
        v--, v /= n;
      else
        w--, w /= n;
    }
    cout << v + 1 << endl;
  }

  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int lca(const int B, int v, int w) {
  while (v != w) {
    while (v > w) {
      v = (v + B - 2) / B;
    }
    while (v < w) {
      w = (w + B - 2) / B;
    }
  }
  return v;
}

int main() {
  int N, Q;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  for (int j = 0; j < Q; ++j) {
    int v, w;
    cin >> v >> w;
    cout << lca(N, v, w) << endl;
  }
  return 0;
}


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

int lca(const int B, int v, int w) {
  if (B == 1)
    return min(v, w);
  while (v != w) {
    while (v > w) {
      v = (v + B - 2) / B;
    }
    while (v < w) {
      w = (w + B - 2) / B;
    }
  }
  return v;
}

int main() {
  int N, Q;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  for (int j = 0; j < Q; ++j) {
    int v, w;
    cin >> v >> w;
    cout << lca(N, v, w) << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
long long N, Q;
vector<long long> C1, C2;
long long nex(long long E) { return (E - 1) / N; }
int main() {
  cin >> N >> Q;
  for (int i = 0; i < Q; i++) {
    long long v, w;
    cin >> v >> w;
    v--;
    w--;
    C1.clear();
    C2.clear();
    while (v > 0) {
      C1.push_back(v);
      v = nex(v);
    }
    C1.push_back(0LL);
    while (w > 0) {
      C2.push_back(w);
      w = nex(w);
    }
    C2.push_back(0LL);
    long long minx = 0;
    for (int j = 0; j < C1.size(); j++) {
      for (int k = 0; k < C2.size(); k++) {
        if (C1[j] == C2[k])
          minx = max(minx, C1[j]);
      }
    }
    cout << minx + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
long long N, Q;
vector<long long> C1, C2;
long long nex(long long E) { return (E - 1) / N; }
int main() {
  cin >> N >> Q;
  for (int i = 0; i < Q; i++) {
    long long v, w;
    cin >> v >> w;
    v--;
    w--;
    if (N == 1) {
      cout << min(v, w) + 1 << endl;
      continue;
    }
    C1.clear();
    C2.clear();
    while (v > 0) {
      C1.push_back(v);
      v = nex(v);
    }
    C1.push_back(0LL);
    while (w > 0) {
      C2.push_back(w);
      w = nex(w);
    }
    C2.push_back(0LL);
    long long minx = 0;
    for (int j = 0; j < C1.size(); j++) {
      for (int k = 0; k < C2.size(); k++) {
        if (C1[j] == C2[k])
          minx = max(minx, C1[j]);
      }
    }
    cout << minx + 1 << endl;
  }
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int n, nq;

void run() {
  scanf("%d%d", &n, &nq);
  REP(qi, nq) {
    int a, b;
    scanf("%d%d", &a, &b);
    while (a != b) {
      if (a < b)
        swap(a, b);
      a = (a - 2) / n + 1;
    }
    printf("%d\n", a);
  }
}

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


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int n, nq;

void run() {
  scanf("%d%d", &n, &nq);
  REP(qi, nq) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (n == 1)
      a = b = min(a, b);
    while (a != b) {
      if (a < b)
        swap(a, b);
      a = (a - 2) / n + 1;
    }
    printf("%d\n", a);
  }
}

int main() {
  run();
  return 0;
}
Problem p03506 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "iostream"
#include "set"
#include "string"
#include "vector"

using namespace std;

const long long int MOD = 1000000007;

int N, M, K, H, W;

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

  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> H >> W;
    if (H < W) {
      swap(H, W);
    }
    while (H != W) {
      H = (H - 2) / N + 1;
      if (H < W) {
        swap(H, W);
      }
    }
    cout << H << endl;
  }
  return 0;
}

Fixed submission:
#include "algorithm"
#include "iostream"
#include "set"
#include "string"
#include "vector"

using namespace std;

const long long int MOD = 1000000007;

int N, M, K, H, W;

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

  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> H >> W;
    if (N == 1) {
      cout << min(H, W) << endl;
      continue;
    }
    if (H < W) {
      swap(H, W);
    }
    while (H != W) {
      H = (H - 2) / N + 1;
      if (H < W) {
        swap(H, W);
      }
    }
    cout << H << endl;
  }
  return 0;
}