Problem p03710 (C++)

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

#define dig(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;

const int mod = 1e9 + 7;
const int maxn = 105;
const int N = 85;

int T;
LL f[maxn];
vector<pair<LL, LL>> excel[maxn];

void Init() {
  scanf("%d", &T);
  f[0] = 1;
  f[1] = 1;
  for (int i = 2; i <= N + 2; ++i) {
    f[i] = f[i - 1] + f[i - 2];
  }
}

int gcd_count(LL x, LL y) {
  int cnt = 0;
  if (x > y)
    swap(x, y);
  while (x) {
    x ^= y ^= x ^= y %= x;
    ++cnt;
  }
  return cnt;
}

void Prepare() {
  vector<pair<LL, LL>>::iterator it, p;
  LL x, y;
  excel[1].push_back(make_pair(1, 2));
  excel[1].push_back(make_pair(1, 3));
  for (int i = 1; i < N; ++i) {
    for (p = excel[i].begin(); p != excel[i].end(); ++p) {
      x = (*p).first;
      y = (*p).second;
      for (int j = 1; j <= 2; ++j) {
        if (y * j + x > f[i + 3])
          break;
        if (gcd_count(y, y * j + x) == i + 1) {
          excel[i + 1].push_back(make_pair(y, y * j + x));
        }
      }
    }
    sort(excel[i + 1].begin(), excel[i + 1].end());
    it = unique(excel[i + 1].begin(), excel[i + 1].end());
    excel[i + 1].resize(distance(excel[i + 1].begin(), it));
  }
  /*for (int i = 1; i <= N; ++i) {
          dig("%d\n", (int)excel[i].size());
          }*/
}

void Solve() {
  LL X, Y, x, y;
  int ans;
  LL sum = 0;
  vector<pair<LL, LL>>::iterator it;
  while (T--) {
    cin >> X >> Y;
    if (X > Y)
      swap(X, Y);
    for (int i = 1; i <= N; ++i) {
      if (X >= f[i] && Y >= f[i + 1]) {
        ans = i;
      } else {
        break;
      }
    }
    sum = 0;
    if (ans == 1) {
      sum = (X % mod * Y % mod) % mod;
    } else {
      for (it = excel[ans - 1].begin(); it != excel[ans - 1].end(); ++it) {
        x = (*it).first;
        y = (*it).second;
        if (X >= y) {
          (sum += (Y - x) / y) %= mod;
        }
        if (Y >= y) {
          (sum += (X - x) / y) %= mod;
        }
      }
    }
    cout << ans << ' ' << sum << endl;
  }
}

int main() {
  Init();
  Prepare();
  Solve();
  return 0;
}


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

#define dig(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;

const int mod = 1e9 + 7;
const int maxn = 105;
const int N = 85;

int T;
LL f[maxn];
vector<pair<LL, LL>> excel[maxn];

void Init() {
  scanf("%d", &T);
  f[0] = 1;
  f[1] = 1;
  for (int i = 2; i <= N + 2; ++i) {
    f[i] = f[i - 1] + f[i - 2];
  }
}

int gcd_count(LL x, LL y) {
  int cnt = 0;
  if (x > y)
    swap(x, y);
  while (x) {
    x ^= y ^= x ^= y %= x;
    ++cnt;
  }
  return cnt;
}

void Prepare() {
  vector<pair<LL, LL>>::iterator it, p;
  LL x, y;
  excel[1].push_back(make_pair(1, 2));
  excel[1].push_back(make_pair(1, 3));
  for (int i = 1; i < N; ++i) {
    for (p = excel[i].begin(); p != excel[i].end(); ++p) {
      x = (*p).first;
      y = (*p).second;
      for (int j = 1; j <= 2; ++j) {
        if (y * j + x > f[i + 3])
          break;
        if (gcd_count(y, y * j + x) == i + 1) {
          excel[i + 1].push_back(make_pair(y, y * j + x));
        }
      }
    }
    sort(excel[i + 1].begin(), excel[i + 1].end());
    it = unique(excel[i + 1].begin(), excel[i + 1].end());
    excel[i + 1].resize(distance(excel[i + 1].begin(), it));
  }
  /*for (int i = 1; i <= N; ++i) {
          dig("%d\n", (int)excel[i].size());
          }*/
}

void Solve() {
  LL X, Y, x, y;
  int ans;
  LL sum = 0;
  vector<pair<LL, LL>>::iterator it;
  while (T--) {
    cin >> X >> Y;
    if (X > Y)
      swap(X, Y);
    ans = 1;
    for (int i = 2; i <= N; ++i) {
      if (X >= f[i] && Y >= f[i + 1]) {
        ans = i;
      } else {
        break;
      }
    }
    sum = 0;
    if (ans == 1) {
      sum = (X % mod * Y % mod) % mod;
    } else {
      for (it = excel[ans - 1].begin(); it != excel[ans - 1].end(); ++it) {
        x = (*it).first;
        y = (*it).second;
        if (X >= y) {
          (sum += (Y - x) / y) %= mod;
        }
        if (Y >= y) {
          (sum += (X - x) / y) %= mod;
        }
      }
    }
    cout << ans << ' ' << sum << endl;
  }
}

int main() {
  Init();
  Prepare();
  Solve();
  return 0;
}