Problem p03710 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, x, y) for (int i = (int)x; i <= (int)y; i++)
#define FOR(i, x, y) for (int i = (int)x; i < (int)y; i++)
#define PER(i, x, y) for (int i = (int)x; i >= (int)y; i--)
#define fi first
#define se second
#define mk make_pair
#define pb push_back
using namespace std;
typedef long long LL;
const int P = 1e9 + 7;

LL Fib[10005];
int dp[55][55], mx[55][55];
pair<LL, LL> bas[105][105];

void work(LL A, LL B) {
  if (min(A, B) == 1) {
    printf("1 %lld\n", max(A, B) % P);
    return;
  }
  if (A == 2 && B == 2) {
    printf("1 4\n");
    return;
  }
  int ans = 2;
  LL res = 0;
  for (ans = 2; Fib[ans] <= B && Fib[ans + 1] <= A; ans++)
    ;
  ans--;
  printf("%d ", ans);
  REP(k, 1, ans) {
    LL u = bas[ans][k].fi, v = bas[ans][k].se;
    // du <= Xi
    // dv + dku <= Yi
    LL C = A, D = B;
    for (LL d = 1; d * u <= C; d++) {
      LL lim = (D - d * v);
      if (lim < 0)
        break;
      res += (lim / (d * u) + 1) % P;
      res %= P;
    }
    swap(C, D);
    for (LL d = 1; d * u <= C; d++) {
      LL lim = (D - d * v);
      if (lim < 0)
        break;
      res += (lim / (d * u) + 1) % P;
      res %= P;
    }
  }
  printf("%lld\n", res);
}

int main() {
  Fib[1] = 1;
  Fib[0] = 1;
  REP(i, 2, 86) Fib[i] = Fib[i - 1] + Fib[i - 2];
  bas[2][1] = mk(2LL, 3LL);
  bas[2][2] = mk(3LL, 4LL);
  REP(i, 3, 86) {
    FOR(j, 1, i) {
      LL u = bas[i - 1][j].fi, v = bas[i - 1][j].se;
      bas[i][j] = mk(v, v + u);
      if (j == 1)
        bas[i][i] = mk(v + u, v + u + u);
    }
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    LL X, Y;
    scanf("%lld%lld", &X, &Y);
    if (X < Y)
      swap(X, Y);
    work(X, Y);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, x, y) for (int i = (int)x; i <= (int)y; i++)
#define FOR(i, x, y) for (int i = (int)x; i < (int)y; i++)
#define PER(i, x, y) for (int i = (int)x; i >= (int)y; i--)
#define fi first
#define se second
#define mk make_pair
#define pb push_back
using namespace std;
typedef long long LL;
const int P = 1e9 + 7;

LL Fib[10005];
int dp[55][55], mx[55][55];
pair<LL, LL> bas[105][105];

void work(LL A, LL B) {
  if (min(A, B) == 1) {
    printf("1 %lld\n", max(A, B) % P);
    return;
  }
  if (A == 2 && B == 2) {
    printf("1 4\n");
    return;
  }
  int ans = 2;
  LL res = 0;
  for (ans = 2; Fib[ans] <= B && Fib[ans + 1] <= A; ans++)
    ;
  ans--;
  printf("%d ", ans);
  REP(k, 1, ans) {
    LL u = bas[ans][k].fi, v = bas[ans][k].se;
    // du <= Xi
    // dv + dku <= Yi
    LL C = A, D = B;
    for (LL d = 1; d * u <= C; d++) {
      LL lim = (D - d * v);
      if (lim < 0)
        break;
      res += (lim / (d * u) + 1) % P;
      res %= P;
    }
    swap(C, D);
    for (LL d = 1; d * u <= C; d++) {
      LL lim = (D - d * v);
      if (lim < 0)
        break;
      res += (lim / (d * u) + 1) % P;
      res %= P;
    }
  }
  printf("%lld\n", res);
}

int main() {
  Fib[1] = 1;
  Fib[0] = 1;
  REP(i, 2, 87) Fib[i] = Fib[i - 1] + Fib[i - 2];
  bas[2][1] = mk(2LL, 3LL);
  bas[2][2] = mk(3LL, 4LL);
  REP(i, 3, 86) {
    FOR(j, 1, i) {
      LL u = bas[i - 1][j].fi, v = bas[i - 1][j].se;
      bas[i][j] = mk(v, v + u);
      if (j == 1)
        bas[i][i] = mk(v + u, v + u + u);
    }
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    LL X, Y;
    scanf("%lld%lld", &X, &Y);
    if (X < Y)
      swap(X, Y);
    work(X, Y);
  }
  return 0;
}