Problem p03849 (C++)

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

typedef long long i64;

const int MOD = 1e9 + 7;
int dp[64][2][3];

int main() {
#ifdef LOCAL_RUN
  freopen("task.in", "r", stdin);
  freopen("task.out", "w", stdout);
// freopen("task.err", "w", stderr);
#endif // ONLINE_JUDGE
  ios::sync_with_stdio(false);
  cin.tie(0);

  int64_t n;
  cin >> n;

  dp[63][0][0] = 1;
  for (int k = 62; k >= 0; --k) {
    int64_t r = 1LL << k;
    for (int64_t cput = 0; cput <= r; cput += r) {
      for (int i = 0; i < 2; ++i) {
        for (int pi = 0; pi < 2; ++pi) {
          if (i == 0 && pi == 1) {
            continue;
          }
          if (i == 1 && pi == 0 && !((n & r) != 0 && cput == 0)) {
            continue;
          }
          if (i == 0 && pi == 0 && cput != (r & n)) {
            continue;
          }
          if (cput == 0) {
            if (n & r) {
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][1] += dp[k + 1][pi][0];
              dp[k][i][1] %= MOD;
            } else {
              dp[k][i][0] += dp[k + 1][pi][0];
              dp[k][i][0] %= MOD;
              dp[k][i][0] += dp[k + 1][pi][1];
              dp[k][i][0] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            }
          } else {
            if (n & r) {
              dp[k][i][0] += dp[k + 1][pi][0];
              dp[k][i][0] %= MOD;
              dp[k][i][1] += dp[k + 1][pi][1];
              dp[k][i][1] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            } else {
              dp[k][i][1] += dp[k + 1][pi][1];
              dp[k][i][1] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            }
          }
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      ans = (ans + dp[0][i][j]) % MOD;
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
using namespace std;

typedef long long i64;

const int MOD = 1e9 + 7;
int dp[64][2][3];

int main() {
#ifdef LOCAL_RUN
  freopen("task.in", "r", stdin);
  freopen("task.out", "w", stdout);
// freopen("task.err", "w", stderr);
#endif // ONLINE_JUDGE
  ios::sync_with_stdio(false);
  cin.tie(0);

  int64_t n;
  cin >> n;

  dp[62][0][0] = 1;
  for (int k = 61; k >= 0; --k) {
    int64_t r = 1LL << k;
    for (int64_t cput = 0; cput <= r; cput += r) {
      for (int i = 0; i < 2; ++i) {
        for (int pi = 0; pi < 2; ++pi) {
          if (i == 0 && pi == 1) {
            continue;
          }
          if (i == 1 && pi == 0 && !((n & r) != 0 && cput == 0)) {
            continue;
          }
          if (i == 0 && pi == 0 && cput != (r & n)) {
            continue;
          }
          if (cput == 0) {
            if (n & r) {
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][1] += dp[k + 1][pi][0];
              dp[k][i][1] %= MOD;
            } else {
              dp[k][i][0] += dp[k + 1][pi][0];
              dp[k][i][0] %= MOD;
              dp[k][i][0] += dp[k + 1][pi][1];
              dp[k][i][0] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][1];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            }
          } else {
            if (n & r) {
              dp[k][i][0] += dp[k + 1][pi][0];
              dp[k][i][0] %= MOD;
              dp[k][i][1] += dp[k + 1][pi][1];
              dp[k][i][1] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            } else {
              dp[k][i][1] += dp[k + 1][pi][1];
              dp[k][i][1] %= MOD;
              dp[k][i][2] += dp[k + 1][pi][2];
              dp[k][i][2] %= MOD;
            }
          }
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      ans = (ans + dp[0][i][j]) % MOD;
    }
  }
  cout << ans << '\n';
}