Problem p03849 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int bsr(int x) { return 31 - __builtin_clz(x); }

template <uint MD> struct ModInt {
  uint v;
  ModInt() : v{0} {}
  ModInt(ll v) : v{normS(v % MD + MD)} {}
  static uint normS(const uint &x) { return (x < MD) ? x : x - MD; };
  static ModInt make(const uint &x) {
    ModInt m;
    m.v = x;
    return m;
  }
  ModInt operator+(const ModInt &r) const { return make(normS(v + r.v)); }
  ModInt operator-(const ModInt &r) const { return make(normS(v + MD - r.v)); }
  ModInt operator*(const ModInt &r) const { return make((ull)v * r.v % MD); }
  ModInt &operator+=(const ModInt &r) { return *this = *this + r; }
  ModInt &operator-=(const ModInt &r) { return *this = *this - r; }
  ModInt &operator*=(const ModInt &r) { return *this = *this * r; }
  static ModInt inv(const ModInt &x) { return pow(ModInt(x), MD - 2); }
};
using Mint = ModInt<TEN(9) + 7>;

map<ll, Mint> dp;
Mint calc(ll N) {
  if (N == 0)
    return 1;
  if (dp.count(N))
    return dp[N];
  Mint ans = 0;
  for (int i = 0; i <= 2; i++) {
    if (N < i)
      break;
    ans += calc((N - i) / 2);
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  cout << calc(n).v << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int bsr(int x) { return 31 - __builtin_clz(x); }

template <uint MD> struct ModInt {
  uint v;
  ModInt() : v{0} {}
  ModInt(ll v) : v{normS(v % MD + MD)} {}
  static uint normS(const uint &x) { return (x < MD) ? x : x - MD; };
  static ModInt make(const uint &x) {
    ModInt m;
    m.v = x;
    return m;
  }
  ModInt operator+(const ModInt &r) const { return make(normS(v + r.v)); }
  ModInt operator-(const ModInt &r) const { return make(normS(v + MD - r.v)); }
  ModInt operator*(const ModInt &r) const { return make((ull)v * r.v % MD); }
  ModInt &operator+=(const ModInt &r) { return *this = *this + r; }
  ModInt &operator-=(const ModInt &r) { return *this = *this - r; }
  ModInt &operator*=(const ModInt &r) { return *this = *this * r; }
  static ModInt inv(const ModInt &x) { return pow(ModInt(x), MD - 2); }
};
using Mint = ModInt<TEN(9) + 7>;

map<ll, Mint> dp;
Mint calc(ll N) {
  if (N == 0)
    return 1;
  if (dp.count(N))
    return dp[N];
  Mint ans = 0;
  for (int i = 0; i <= 2; i++) {
    if (N < i)
      break;
    ans += calc((N - i) / 2);
  }
  return dp[N] = ans;
}

int main() {
  ll n;
  cin >> n;
  cout << calc(n).v << endl;
  return 0;
}