alexjercan/bugnet huggingface dataset:
Problem p00937 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int INF = 1000000000;
int n, m, a[3];
int go[110][60][60], ok[60];
int matrix[60][60], dp[60][3], dp2[60];
int main() {
  // freopen("in.txt", "r", stdin);
  scanf("%d %d %d %d %d", &n, &m, &a[0], &a[1], &a[2]);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    matrix[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i)
    go[0][i][i] = 1;
  for (int k = 1; k <= 100; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (go[k - 1][i][j] == 0)
          continue;
        for (int l = 1; l <= n; ++l) {
          // if (k == 1 && i == 8 && l == 1) printf("%d\\n", j);
          go[k][i][l] |= go[k - 1][i][j] & matrix[j][l];
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 3; ++j) {
      dp[i][j] = INF;
    }
    dp2[i] = INF;
  }

  queue<int> q;
  q.push(n);
  dp2[n] = 0;
  while (!q.empty()) {
    int h = q.front();
    q.pop();

    for (int i = 0; i < 3; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (go[a[i]][h][j]) {
          if (dp2[h] + 1 < dp[j][i]) {

            ok[j] |= 1 << i;
            dp[j][i] = dp2[h] + 1;

            if (ok[j] == 7) {
              dp2[j] = *max_element(dp[j], dp[j] + 3);
              q.push(j);
            }
          }
        }
      }
    }
  }
  // printf("%d\\n", go[0][8][1]);
  int ans = dp2[1];
  if (ans == INF)
    puts("IMPOSSIBLE");
  else {
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int INF = 1000000000;
int n, m, a[3];
vector<int> g[110];
int f[110][3];
int ok[110], vis[110];
queue<int> q;

void dfs(int u, int step, int num, int val) {
  if (step < 0)
    return;
  if (step == 0) {
    f[u][num] = min(f[u][num], val + 1);
    ok[u] |= 1 << num;
    if (ok[u] == 7 && vis[u] == 0) {
      ok[u] = 8;
      q.push(u);
      vis[u] = 1;
    }
    return;
  }
  for (int v : g[u]) {
    dfs(v, step - 1, num, val);
  }
}
int main() {
  // freopen("in.txt", "r", stdin);
  scanf("%d %d %d %d %d", &n, &m, &a[0], &a[1], &a[2]);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 3; ++j) {
      f[i][j] = INF;
    }
  }
  f[n][0] = f[n][1] = f[n][2] = 0;
  q.push(n);
  vis[n] = 1;
  ok[n] = 8;
  while (!q.empty() && vis[1] == 0) {
    int h = q.front();
    q.pop();
    assert(vis[h] == 1);
    int val = max({f[h][0], f[h][1], f[h][2]});
    for (int i = 0; i < 3; ++i) {
      dfs(h, a[i], i, val);
    }
  }
  // printf("%d\\n", f[][]);
  int ans = max({f[1][0], f[1][1], f[1][2]});
  if (ans >= INF) {
    puts("IMPOSSIBLE");
  } else {
    printf("%d\\n", ans);
  }
}
Problem p00937 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

vvi matmul(vvi a, vvi b) {
  int n = a.size();
  vvi d(n, vi(n, 0));
  rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j];
  return d;
}

static map<int, vvi> memo;
vvi matpow(vvi a, int p) {
  return memo[p] = memo.find(p) == memo.end()
                       ? p % 2 ? matmul(matpow(a, p - 1), a)
                               : matmul(matpow(a, p / 2), matpow(a, p / 2))
                       : memo[p];
}

int main() {
  int n, m, a[3];
  while (cin >> n >> m >> a[0] >> a[1] >> a[2]) {
    vvi z(n, vi(n, 0)), e = z;
    rep(_, m) {
      int p, q;
      cin >> p >> q;
      e[p - 1][q - 1] = 1;
    }
    memo.clear();
    memo[1] = e;

    vector<vvi> d(3);
    rep(p, 3) d[p] = matpow(e, a[p]);

    vi g(n, 0), ng(n, 0);
    g[n - 1] = 1;
    rep(t, 1000) {
      bool next = 0;
      fill(all(ng), 0);
      rep(i, n) if (!g[i]) {
        bool ok = 1, r;
        rep(p, 3)[&] {
          r = 0;
          rep(j, n) r |= (g[j] and d[p][i][j]);
          ok &= r;
        }
        ();
        if (ok)
          ng[i] = next = 1;
      }
      if (!next) {
        cout << "IMPOSSIBLE" << endl;
        break;
      } else if (ng[0]) {
        cout << t + 1 << endl;
        break;
      }
      rep(i, n) g[i] |= ng[i];
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

vvi matmul(vvi a, vvi b) {
  int n = a.size();
  vvi d(n, vi(n, 0));
  rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j];
  return d;
}

static map<int, vvi> memo;
vvi matpow(vvi a, int p) {
  return memo[p] = memo.find(p) != memo.end()
                       ? p % 2 ? matmul(matpow(a, p - 1), a)
                               : matmul(matpow(a, p / 2), matpow(a, p / 2))
                       : memo[p];
}

int main() {
  int n, m, a[3];
  while (cin >> n >> m >> a[0] >> a[1] >> a[2]) {
    vvi z(n, vi(n, 0)), e = z;
    rep(_, m) {
      int p, q;
      cin >> p >> q;
      e[p - 1][q - 1] = 1;
    }
    memo.clear();
    memo[1] = e;

    vector<vvi> d(3);
    rep(p, 3) d[p] = matpow(e, a[p]);

    vi g(n, 0), ng(n, 0);
    g[n - 1] = 1;
    rep(t, 1000) {
      bool next = 0;
      fill(all(ng), 0);
      rep(i, n) if (!g[i]) {
        bool ok = 1, r;
        rep(p, 3)[&] {
          r = 0;
          rep(j, n) r |= (g[j] and d[p][i][j]);
          ok &= r;
        }
        ();
        if (ok)
          ng[i] = next = 1;
      }
      if (!next) {
        cout << "IMPOSSIBLE" << endl;
        break;
      } else if (ng[0]) {
        cout << t + 1 << endl;
        break;
      }
      rep(i, n) g[i] |= ng[i];
    }
  }
  return 0;
}
Problem p00937 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, m, a, b, c;
bool can[101][50][50];
int dp[50];

int main() {
  cin >> n >> m >> a >> b >> c;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    can[0][x][y] = 1;
  }
  rep(it, 100) rep(k, n) rep(i, n) rep(j, n) {
    if (can[it][i][k] && can[0][k][j])
      can[it + 1][i][j] = 1;
  }

  rep(it, 101) {
    rep(i, n - 1) {
      int x = inf, y = inf, z = inf;
      rep(j, n) {
        if (can[a - 1][i][j])
          x = min(x, dp[j] + 1);
        if (can[b - 1][i][j])
          y = min(y, dp[j] + 1);
        if (can[c - 1][i][j])
          z = min(z, dp[j] + 1);
      }
      dp[i] = max({dp[i], x, y, z});
    }
  }
  if (dp[0] >= n)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, m, a, b, c;
bool can[101][40][40];
int dp[50];

int main() {
  cin >> n >> m >> a >> b >> c;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    can[0][x][y] = 1;
  }
  rep(it, 100) rep(k, n) rep(i, n) rep(j, n) {
    if (can[it][i][k] && can[0][k][j])
      can[it + 1][i][j] = 1;
  }

  rep(it, 101) {
    rep(i, n - 1) {
      int x = inf, y = inf, z = inf;
      rep(j, n) {
        if (can[a - 1][i][j])
          x = min(x, dp[j] + 1);
        if (can[b - 1][i][j])
          y = min(y, dp[j] + 1);
        if (can[c - 1][i][j])
          z = min(z, dp[j] + 1);
      }
      dp[i] = max({dp[i], x, y, z});
    }
  }
  if (dp[0] >= n)
    cout << "IMPOSSIBLE" << endl;
  else
    cout << dp[0] << endl;

  return 0;
}
Problem p00937 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

constexpr int MAX = 114;
constexpr int INF = (1 << 29);

vector<int> G[MAX], G2[MAX][3];
bool visited[MAX][MAX];

bool dfs(int curr, int goal, int rem) {
  if (visited[curr][rem])
    return 0;
  visited[curr][rem] = 1;

  if (rem == 0) {
    return (curr == goal);
  }
  for (auto &next : G[curr]) {
    if (dfs(next, goal, rem - 1)) {
      return 1;
    }
  }
  return 0;
}

void solve(int N) {
  vector<int> d(N, INF);
  d[N - 1] = 0;

  bool update = 1;
  while (update) {
    update = 0;

    for (int i = 0; i < N; i++) {
      int cost = 0;
      for (int j = 0; j < 3; j++) {
        int mc = INF;
        for (auto &u : G2[i][j]) {
          mc = min(mc, d[u]);
        }
        cost = max(cost, mc + 1);
      }

      if (cost < d[i]) {
        d[i] = cost;
        update = 1;
      }
    }
  }

  if (d[0] == INF) {
    cout << "IMPOSSIBLE" << endl;
  } else {
    cout << d[0] << endl;
  }
}

int main() {
  int N, M, a[3];
  cin >> N >> M >> a[0] >> a[1] >> a[2];
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
  }

  for (int i = 0; i < 3; i++) {
    for (int from = 0; from < N; from++) {
      for (int to = 0; to < N; to++) {
        if (from == to)
          continue;
        memset(visited, 0, sizeof(visited));
        if (dfs(from, to, a[i])) {
          G2[from][i].emplace_back(to);
        }
      }
    }
  }
  solve(N);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

constexpr int MAX = 55;
constexpr int INF = (1 << 29);

vector<int> G[MAX], G2[MAX][3];

bool dfs(int curr, int goal, int rem) {
  if (rem == 0) {
    return (curr == goal);
  }
  for (auto &next : G[curr]) {
    if (dfs(next, goal, rem - 1)) {
      return 1;
    }
  }
  return 0;
}

void solve(int N) {
  vector<int> d(N, INF);
  d[N - 1] = 0;

  for (int t = 0; t < N; t++) {
    for (int i = 0; i < N; i++) {
      int cost = 0;
      for (int j = 0; j < 3; j++) {
        int mc = INF;
        for (auto &u : G2[i][j]) {
          mc = min(mc, d[u]);
        }
        cost = max(cost, mc + 1);
      }

      if (cost < d[i]) {
        d[i] = cost;
      }
    }
  }

  if (d[0] == INF) {
    cout << "IMPOSSIBLE" << endl;
  } else {
    cout << d[0] << endl;
  }
}

int main() {
  int N, M, a[3];
  cin >> N >> M >> a[0] >> a[1] >> a[2];
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].emplace_back(v);
  }

  for (int i = 0; i < 3; i++) {
    for (int from = 0; from < N; from++) {
      for (int to = 0; to < N; to++) {
        if (from == to)
          continue;
        if (dfs(from, to, a[i])) {
          G2[from][i].emplace_back(to);
        }
      }
    }
  }
  solve(N);
  return 0;
}
Problem p00937 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, m, a[3];
vector<int> rG[50];

int d[50], c[50];
bool dp[101][50][50];

void init(int si) {
  dp[0][si][si] = true;
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < n; j++) {
      if (!dp[i][si][j])
        continue;
      for (int k = 0; k < (int)rG[j].size(); k++) {
        int w = rG[j][k];
        dp[i + 1][si][w] = true;
      }
    }
  }
}

void solve() {
  memset(c, 0, sizeof(c));
  memset(d, -1, sizeof(d));
  queue<int> Q;
  Q.push(n - 1);
  d[n - 1] = 0;
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    if (pos == 0) {
      cout << d[pos] << endl;
      return;
    }
    for (int i = 0; i < 3; i++) {
      for (int to = 0; to < n; to++) {
        if (!dp[a[i]][pos][to])
          continue;
        c[to] |= (1 << i);
        if (c[to] == 7 && d[to] == -1) {
          d[to] = d[pos] + 1;
          Q.push(to);
        }
      }
    }
  }
  cout << "IMPOSSIBLE" << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--, to--;
    rG[to].push_back(from);
  }
  for (int i = 0; i < n; i++)
    init(i);

  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, a[3];
vector<int> rG[50];

int d[50], c[50];
bool dp[101][50][50];

void init(int si) {
  dp[0][si][si] = true;
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j < n; j++) {
      if (!dp[i][si][j])
        continue;
      for (int k = 0; k < (int)rG[j].size(); k++) {
        int w = rG[j][k];
        dp[i + 1][si][w] = true;
      }
    }
  }
}

void solve() {
  memset(c, 0, sizeof(c));
  memset(d, -1, sizeof(d));
  queue<int> Q;
  Q.push(n - 1);
  d[n - 1] = 0;
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    if (pos == 0) {
      cout << d[pos] << endl;
      return;
    }
    for (int i = 0; i < 3; i++) {
      for (int to = 0; to < n; to++) {
        if (!dp[a[i]][pos][to])
          continue;
        c[to] |= (1 << i);
        if (c[to] == 7 && d[to] == -1) {
          d[to] = d[pos] + 1;
          Q.push(to);
        }
      }
    }
  }
  cout << "IMPOSSIBLE" << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < 3; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    int from, to;
    cin >> from >> to;
    from--, to--;
    rG[to].push_back(from);
  }
  for (int i = 0; i < n; i++)
    init(i);

  solve();
  return 0;
}
Problem p00937 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;

void listup(int cur, int lim, int mark, vector<vector<int>> &adj,
            vector<int> &ret) {
  set<int> vlist;
  vlist.insert(cur);
  set<int>::iterator itr;
  for (int i = 0; i < lim; i++) {
    set<int> newset;
    for (itr = vlist.begin(); itr != vlist.end(); itr++) {
      int node = *itr;
      for (int j = 0; j < (int)adj[node].size(); j++) {
        newset.insert(adj[node][j]);
      }
    }
    vlist = newset;
  }
  for (itr = vlist.begin(); itr != vlist.end(); itr++) {
    ret[*itr] |= mark;
  }
}

int main() {
  int n, m, a, b, c;
  cin >> n >> m >> a >> b >> c;
  vector<vector<int>> revadj(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    revadj[v].push_back(u);
  }

  vector<bool> used(n + 1, false), isnew(n + 1, false);
  used[n] = true;
  isnew[n] = true;
  int ans = -1;
  int turn = 0;
  vector<int> cand(n + 1, 0);
  while (1) {
    if (used[1]) {
      ans = turn;
      break;
    }
    for (int i = 1; i <= n; i++) {
      if (!isnew[i])
        continue;
      listup(i, a, 1, revadj, cand);
      listup(i, b, 2, revadj, cand);
      listup(i, c, 4, revadj, cand);
    }
    isnew = vector<bool>(n + 1, false);
    bool update = false;
    for (int i = 1; i <= n; i++) {
      if (cand[i] == 7 && !used[i]) {
        used[i] = true;
        update = true;
        isnew[i] = true;
      }
    }
    if (!update)
      break;
    turn++;
  }

  if (ans == -1) {
    cout << "IMPOSSIBLE" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

void listup(int cur, int lim, int mark, vector<vector<int>> &adj,
            vector<int> &ret) {
  if (lim == 0) {
    ret[cur] |= mark;
    return;
  }

  for (int i = 0; i < (int)adj[cur].size(); i++) {
    listup(adj[cur][i], lim - 1, mark, adj, ret);
  }
}

int main() {
  int n, m, a, b, c;
  cin >> n >> m >> a >> b >> c;
  vector<vector<int>> revadj(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    revadj[v].push_back(u);
  }

  vector<bool> used(n + 1, false);
  used[n] = true;
  int ans = -1;
  int turn = 0;
  ;
  while (1) {
    if (used[1]) {
      ans = turn;
      break;
    }
    vector<int> cand(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      if (!used[i])
        continue;
      listup(i, a, 1, revadj, cand);
      listup(i, b, 2, revadj, cand);
      listup(i, c, 4, revadj, cand);
    }
    bool update = false;
    for (int i = 1; i <= n; i++) {
      if (cand[i] == 7 && !used[i]) {
        used[i] = true;
        update = true;
      }
    }
    if (!update)
      break;
    turn++;
  }

  if (ans == -1) {
    cout << "IMPOSSIBLE" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
Problem p00939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
string S;
map<tuple<int, int, int64, int>, int64> dp;
int length;
int64 weight;

int64 dfs(int idx, int num, int64 seki, int flag) {
  if (num - (length - idx) * 9 > 0)
    return (0);
  if (idx == length) {
    if (seki != weight)
      return (seki < weight);
    return (flag == 1);
  }
  if (dp.find(make_tuple(idx, num, seki, flag)) != dp.end())
    return (dp[make_tuple(idx, num, seki, flag)]);
  int64 ret = 0;
  for (int i = 0; i <= min(num, 9); i++) {
    int nextflag = flag;
    if (flag == 0) {
      if (i < S[idx] - '0')
        nextflag = 1;
      else if (i > S[idx] - '0')
        nextflag = 2;
    }
    ret += dfs(idx + 1, num - i, seki * (i + 1), nextflag);
  }
  return (dp[make_tuple(idx, num, seki, flag)] = ret);
}

int main() {
  cin >> S;
  length = S.size();
  int sum = accumulate(S.begin(), S.end(), 0) - '0' * S.size();
  if (sum == 0) {
    cout << 0 << endl;
  } else {
    vector<int64> poyo(sum, 0);
    poyo[0] = 1;
    for (int i = 0; i < length; i++) {
      for (int j = sum - 1; j >= 0; j--) {
        for (int k = j + 1; k <= min(sum - 1, j + 9); k++) {
          poyo[k] += poyo[j];
        }
      }
    }
    weight = 1;
    for (int i = 0; i < S.size(); i++)
      weight *= (S[i] - '0' + 1);
    cout << accumulate(poyo.begin(), poyo.end(), 0LL) + dfs(0, sum, 1, 0)
         << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
string S;
map<tuple<int, int, int64, int>, int64> dp;
int length;
int64 weight;

int64 dfs(int idx, int num, int64 seki, int flag) {
  if (num - (length - idx) * 9 > 0)
    return (0);
  if (idx == length) {
    if (seki != weight)
      return (seki < weight);
    return (flag == 1);
  }
  if (dp.find(make_tuple(idx, num, seki, flag)) != dp.end())
    return (dp[make_tuple(idx, num, seki, flag)]);
  int64 ret = 0;
  for (int i = 0; i <= min(num, 9); i++) {
    int nextflag = flag;
    if (flag == 0) {
      if (i < S[idx] - '0')
        nextflag = 1;
      else if (i > S[idx] - '0')
        nextflag = 2;
    }
    ret += dfs(idx + 1, num - i, seki * (i + 1), nextflag);
  }
  return (dp[make_tuple(idx, num, seki, flag)] = ret);
}

int main() {
  cin >> S;
  length = S.size();
  int sum = accumulate(S.begin(), S.end(), 0) - '0' * S.size();
  weight = 1;

  vector<int64> poyo(sum, 0);
  poyo[0] = 1;
  for (int i = 0; i < length; i++) {
    for (int j = sum - 1; j >= 0; j--) {
      for (int k = j + 1; k <= min(sum - 1, j + 9); k++) {
        poyo[k] += poyo[j];
      }
    }
  }
  for (int i = 0; i < S.size(); i++)
    weight *= (S[i] - '0' + 1);
  cout << accumulate(poyo.begin(), poyo.end(), 0LL) + dfs(0, sum, 1, 0) << endl;
}
Problem p00939 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;

string s;
int sum, mul;

map<P1, P> memo;

P dfs(int x, int S, int X) {

  if (S > sum || (S == sum && X > mul))
    return P(0, 0);

  if (x == s.size()) {
    if (S < sum)
      return P(1, 0);
    if (S == sum && X < mul)
      return P(0, 1);
    return P(0, 0);
  }

  if (memo.count(P1(x, P(S, X))))
    return memo[P1(x, P(S, X))];

  P res = P(0, 0);

  for (int i = 0; i < 10; i++) {
    P r = dfs(x + 1, S + i, X * (i + 1));
    res.first += r.first;
    res.second += r.second;
  }

  return memo[P1(x, P(S, X))] = res;
}

map<P, int> memo2[500][3];

int dfs2(int x, int S, int X, int flag) {

  if (S > sum || X > mul)
    return 0;
  if (S + 9 * (s.size() - x) < sum)
    return 0;

  if (x == s.size()) {
    if (S == sum && X == mul && flag)
      return 1;
    return 0;
  }

  if (memo2[S][flag].count(P(x, X)))
    return memo2[S][flag][P(x, X)];

  int res = 0;

  for (int i = 0; i < 10; i++) {
    if (flag == 0 && i > s[x] - '0')
      continue;
    if (flag == 0 && i < s[x] - '0')
      res += dfs2(x + 1, S + i, X * (i + 1), 1);
    else
      res += dfs2(x + 1, S + i, X * (i + 1), flag);
  }

  return memo2[S][flag][P(x, X)] = res;
}

signed main() {

  cin >> s;

  mul = 1;
  for (int i = 0; i < s.size(); i++) {
    sum += s[i] - '0';
    mul *= (s[i] - '0' + 1);
  }

  P ans = dfs(0, 0, 1);

  cout << ans.first + ans.second + dfs2(0, 0, 1, 0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;

string s;
int sum, mul;

map<P1, P> memo;

P dfs(int x, int S, int X) {

  if (S > sum || (S == sum && X > mul))
    return P(0, 0);

  if (x == s.size()) {
    if (S < sum)
      return P(1, 0);
    if (S == sum && X < mul)
      return P(0, 1);
    return P(0, 0);
  }

  if (memo.count(P1(x, P(S, X))))
    return memo[P1(x, P(S, X))];

  P res = P(0, 0);

  for (int i = 0; i < 10; i++) {
    P r = dfs(x + 1, S + i, X * (i + 1));
    res.first += r.first;
    res.second += r.second;
  }

  return memo[P1(x, P(S, X))] = res;
}

map<P2, int> memo2;

int dfs2(int x, int S, int X, int flag) {

  if (S > sum || X > mul)
    return 0;

  if (x == s.size()) {
    if (S == sum && X == mul && flag)
      return 1;
    return 0;
  }

  if (memo2.count(P2(P(x, S), P(X, flag))))
    return memo2[P2(P(x, S), P(X, flag))];

  int res = 0;

  for (int i = 0; i < 10; i++) {
    if (flag == 0 && i > s[x] - '0')
      continue;
    if (flag == 0 && i < s[x] - '0')
      res += dfs2(x + 1, S + i, X * (i + 1), 1);
    else
      res += dfs2(x + 1, S + i, X * (i + 1), flag);
  }

  return memo2[P2(P(x, S), P(X, flag))] = res;
}

signed main() {

  cin >> s;

  mul = 1;
  for (int i = 0; i < s.size(); i++) {
    sum += s[i] - '0';
    mul *= (s[i] - '0' + 1);
  }

  P ans = dfs(0, 0, 1);

  cout << ans.first + ans.second + dfs2(0, 0, 1, 0) << endl;

  return 0;
}