Problem p02938 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define dbug(x) cout << #x << "=" << x << endl
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = (1e9) + 7;
int mx = 63;
int d[70], e[70];
ll dp[70][2][2][3];
void update(ll &x, ll y) {
  x += y;
  x %= mod;
}
ll solve(ll L, ll R) {
  for (int i = 0; i < mx; i++) {
    if (R & (1LL << i))
      d[i] = 1;
    else
      d[i] = 0;
    if (L & (1LL << i))
      e[i] = 1;
    else
      e[i] = 0;
  }
  // for (int i=0;i<mx;i++) printf("%d",d[i]); printf("\n");
  // for (int i=0;i<mx;i++) printf("%d",e[i]); printf("\n");
  memset(dp, 0, sizeof(dp));
  dp[mx][1][1][0] = 1;
  int C;
  for (int i = mx; i >= 0; i--) {
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 1; k++)
        for (int B = 0; B <= 2; B++) {
          // printf("%d %d %d %d %lld\n",i,j,k,B,dp[i][j][k][B]);
          for (int a = 0; a <= 1; a++)
            for (int b = 0; b <= 1; b++) {
              if (j && a > d[i - 1])
                continue;
              if (k && b > e[i - 1])
                continue;
              if (a < b)
                continue;
              if (B == 2)
                C = 2;
              else {
                if (b || B)
                  C = 1;
                else if (a - b)
                  C = 2;
                else
                  C = 0;
              }
              update(dp[i - 1][j & (a == d[i - 1])][k & (b == e[i - 1])][C],
                     dp[i][j][k][B]);
            }
        }
  }
  ll res = 0;
  for (int j = 0; j <= 1; j++)
    for (int k = 0; k <= 1; k++)
      update(res, dp[0][j][k][1]);
  // printf("%lld\n",res);
  return res;
}
int main() {
  // freopen("1.txt","r",stdin);
  // freopen("1.out","w",stdout);
  ll L, R;
  read(L);
  read(R);
  ll ans = solve(R, R) - solve(L - 1, R);
  ans = (ans % mod + mod) % mod;
  printf("%lld\n", ans);
  /*ans=0;
  for (int x=1;x<=R;x++)
  for (int y=x;y<=R;y++)
          if (y%x==(x^y)) ans++;
  printf("%lld\n",ans);*/
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define dbug(x) cout << #x << "=" << x << endl
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const ll mod = (1e9) + 7;
int mx = 63;
int d[70], e[70];
ll dp[70][2][2][3];
void update(ll &x, ll y) {
  x += y;
  x %= mod;
}
ll solve(ll L, ll R) {
  for (int i = 0; i < mx; i++) {
    if (R & (1LL << i))
      d[i] = 1;
    else
      d[i] = 0;
    if (L & (1LL << i))
      e[i] = 1;
    else
      e[i] = 0;
  }
  // for (int i=0;i<mx;i++) printf("%d",d[i]); printf("\n");
  // for (int i=0;i<mx;i++) printf("%d",e[i]); printf("\n");
  memset(dp, 0, sizeof(dp));
  dp[mx][1][1][0] = 1;
  int C;
  for (int i = mx; i >= 1; i--) {
    for (int j = 0; j <= 1; j++)
      for (int k = 0; k <= 1; k++)
        for (int B = 0; B <= 2; B++) {
          // printf("%d %d %d %d %lld\n",i,j,k,B,dp[i][j][k][B]);
          for (int a = 0; a <= 1; a++)
            for (int b = 0; b <= 1; b++) {
              if (j && a > d[i - 1])
                continue;
              if (k && b > e[i - 1])
                continue;
              if (a < b)
                continue;
              if (B == 2)
                C = 2;
              else {
                if (b || B)
                  C = 1;
                else if (a - b)
                  C = 2;
                else
                  C = 0;
              }
              update(dp[i - 1][j & (a == d[i - 1])][k & (b == e[i - 1])][C],
                     dp[i][j][k][B]);
            }
        }
  }
  ll res = 0;
  for (int j = 0; j <= 1; j++)
    for (int k = 0; k <= 1; k++)
      update(res, dp[0][j][k][1]);
  // printf("%lld\n",res);
  return res;
}
int main() {
  // freopen("1.txt","r",stdin);
  // freopen("1.out","w",stdout);
  ll L, R;
  read(L);
  read(R);
  ll ans = solve(R, R) - solve(L - 1, R);
  ans = (ans % mod + mod) % mod;
  printf("%lld\n", ans);
  /*ans=0;
  for (int x=1;x<=R;x++)
  for (int y=x;y<=R;y++)
          if (y%x==(x^y)) ans++;
  printf("%lld\n",ans);*/
  return 0;
}