Problem p00939 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

string d;
int n;
int sumD = 0;
ll prodD = 1;

const int SUM = 130;
// unknown, small, large
unordered_map<ll, ll> dp[15][SUM][3];
ll dfs(int x, int sum, int sl, ll prod) {
  if (x == n) {
    if (sum < sumD)
      return 1;
    else if (sum == sumD) {
      if (prod < prodD)
        return 1;
      else if (prod == prodD) {
        if (sl == 1)
          return 1;
      }
    }
    return 0;
  }

  if (dp[x][sum][sl].count(prod))
    return dp[x][sum][sl][prod];

  ll ret = 0;
  rep(i, 10) {
    int nsl = sl;
    if (sl == 0) {
      int v = d[x] - '0';
      if (i < v)
        nsl = 1;
      if (i > v)
        nsl = 2;
    }

    ret += dfs(x + 1, sum + i, nsl, prod * (i + 1));
  }
  return dp[x][sum][sl][prod] = ret;
}

int main() {
  cin >> d;
  n = d.size();

  rep(i, n) {
    int v = d[i] - '0';
    sumD += v;
    prodD *= (v + 1);
  }

  cout << dfs(0, 0, 0, 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

const int SUM = 130;
// unknown, small, large
map<vector<int>, ll> dp[SUM][3], nx[SUM][3];

const int D[4] = {2, 3, 5, 7};
vector<int> F[11];
vector<int> factor(int n) {
  vector<int> ret(4, 0);
  rep(i, 4) {
    while (n % D[i] == 0) {
      n /= D[i];
      ++ret[i];
    }
  }

  return ret;
}

ll calc(const vector<int> &v) {
  ll ret = 1;
  rep(i, 4) { rep(j, v[i]) ret *= D[i]; }
  return ret;
}

int main() {
  rep(i, 10) F[i + 1] = factor(i + 1);

  string d;
  cin >> d;

  int n = d.size();

  dp[0][0][vector<int>({0, 0, 0, 0})] = 1;
  rep(i, n) {
    rep(j, SUM) rep(k, 3) {
      for (const auto &p : dp[j][k]) {
        vector<int> nv(p.fi);

        rep(x, 10) {
          int nk = k;
          if (k == 0) {
            int v = d[i] - '0';
            if (x < v)
              nk = 1;
            if (x > v)
              nk = 2;
          }

          rep(ff, 4) nv[ff] += F[x + 1][ff];
          nx[j + x][nk][nv] += p.se;
          rep(ff, 4) nv[ff] -= F[x + 1][ff];
        }
      }
    }

    rep(j, SUM) rep(k, 3) {
      dp[j][k] = nx[j][k];
      nx[j][k].clear();
    }
  }

  ll sumD = 0, prodD = 1;
  rep(i, n) {
    int v = d[i] - '0';
    sumD += v;
    prodD *= (v + 1);
  }

  ll ans = 0;
  rep(i, SUM) rep(j, 3) {
    // if(!dp[i][j].empty()) dbg(dp[i][j].size());
    for (const auto &p : dp[i][j]) {
      if (i < sumD)
        ans += p.se;
      else if (i == sumD) {
        ll prod = calc(p.fi);
        if (prod < prodD)
          ans += p.se;
        else if (prod == prodD) {
          if (j == 1)
            ans += p.se;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}