Problem p03847 (C++)

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

typedef long long LL;
typedef long double LB;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const LB eps = 1e-8;
const LL MOD = 1e9 + 7;
const int DIGS = 70;

template <typename T> inline bool scan_d(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0; // EOF
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <typename T> void print(T x) {
  static char s[33], *s1;
  s1 = s;
  if (!x)
    *s1++ = '0';
  if (x < 0)
    putchar('-'), x = -x;
  while (x)
    *s1++ = (x % 10 + '0'), x /= 10;
  while (s1-- != s)
    putchar(*s1);
}
template <typename T> void println(T x) {
  print(x);
  putchar('\n');
}

LL n;
// LL dig[DIGS];
//
// void init() {
//     dig[0] = 0;
//     while(n > 0) {
//         dig[++ dig[0]] = n & 1;
//         n >>= 1;
//     }
// }
map<LL, LL> ans;
LL calc(LL x) {
  if (x == 0)
    return 1;
  if (x == 1)
    return 2;
  if (x == 2)
    return 4;
  LL ret = ans[x];
  if (!ret) {
    ret += calc(x >> 1);
    if (ret >= MOD)
      ret -= MOD;
    ret += calc((x - 1) >> 1);
    if (ret >= MOD)
      ret -= MOD;
    ret += calc((x - 2) >> 1);
    if (ret >= MOD)
      ret -= MOD;
  }
  return ret;
}

int main() {
#ifdef ___LOCAL_WONZY___
//    freopen("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
  scan_d(n);
  //        init();
  println(calc(n));
  return 0;
}


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

typedef long long LL;
typedef long double LB;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const LB eps = 1e-8;
const LL MOD = 1e9 + 7;
const int DIGS = 70;

template <typename T> inline bool scan_d(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0; // EOF
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <typename T> void print(T x) {
  static char s[33], *s1;
  s1 = s;
  if (!x)
    *s1++ = '0';
  if (x < 0)
    putchar('-'), x = -x;
  while (x)
    *s1++ = (x % 10 + '0'), x /= 10;
  while (s1-- != s)
    putchar(*s1);
}
template <typename T> void println(T x) {
  print(x);
  putchar('\n');
}

LL n;
// LL dig[DIGS];
//
// void init() {
//     dig[0] = 0;
//     while(n > 0) {
//         dig[++ dig[0]] = n & 1;
//         n >>= 1;
//     }
// }
map<LL, LL> ans;
LL calc(LL x) {
  if (x == 0)
    return 1;
  if (x == 1)
    return 2;
  if (x == 2)
    return 4;
  LL ret = ans[x];
  if (!ret) {
    ret += calc(x >> 1);
    if (ret >= MOD)
      ret -= MOD;
    ret += calc((x - 1) >> 1);
    if (ret >= MOD)
      ret -= MOD;
    ret += calc((x - 2) >> 1);
    if (ret >= MOD)
      ret -= MOD;
    ans[x] = ret;
  }
  return ret;
}

int main() {
#ifdef ___LOCAL_WONZY___
//    freopen("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
  scan_d(n);
  //        init();
  println(calc(n));
  return 0;
}