Problem p03506 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(X, Y) ((X) > (Y) ? X = (Y), true : false)
#define chmax(X, Y) ((X) < (Y) ? X = (Y), true : false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

#define N 50
#define M 1000000010
ll s[N], t[N];

int main() {
  ll n, q;
  cin >> n >> q;
  s[0] = 1;
  t[0] = 0;
  t[1] = 1;
  rep(i, N - 2) {
    s[i + 1] = s[i] * n;
    t[i + 2] = t[i + 1] + s[i + 1];
    if (t[i + 2] > M)
      break;
  }
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (a < b)
      swap(a, b);
    int p = 0;
    while (t[p] < a)
      p++;
    while (a != b) {
      if (a < b)
        swap(a, b);
      while (t[p - 1] >= a)
        p--;
      ll a2 = a - t[p - 1];
      a = t[p - 2] + 1 + (a2 - 1) / n;
    }
    cout << a << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(X, Y) ((X) > (Y) ? X = (Y), true : false)
#define chmax(X, Y) ((X) < (Y) ? X = (Y), true : false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

#define N 50
#define M 1000000010
ll s[N], t[N];

int main() {
  ll n, q;
  cin >> n >> q;
  s[0] = 1;
  t[0] = 0;
  t[1] = 1;
  rep(i, N - 2) {
    s[i + 1] = s[i] * n;
    t[i + 2] = t[i + 1] + s[i + 1];
    if (t[i + 2] > M)
      break;
  }
  while (q--) {
    ll a, b;
    cin >> a >> b;
    if (n == 1) {
      cout << min(a, b) << endl;
      continue;
    }
    if (a < b)
      swap(a, b);
    int p = 0;
    while (t[p] < a)
      p++;
    while (a != b) {
      if (a < b)
        swap(a, b);
      while (t[p - 1] >= a)
        p--;
      ll a2 = a - t[p - 1];
      a = t[p - 2] + 1 + (a2 - 1) / n;
    }
    cout << a << endl;
  }
  return 0;
}