Problem p01549 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 256
typedef pair<int, int> pii;

int n;
vector<string> e;
map<string, pii> mp;

int op_num(char c) {
  switch (c) {
  case '+':
    return 0;
  case '-':
    return 1;
  case '*':
    return 2;
  case '/':
    return 3;
  }
  return -1;
}

int s2i(string &s) {
  int res;
  stringstream ss(s);
  ss >> res;
  return res;
}

int calc(int a, int op, int b) {
  if (op == 0) {
    return (a + b) % 256;
  } else if (op == 1) {
    return (a - b + 256) % 256;
  } else if (op == 2) {
    return (a * b) % 256;
  } else {
    return (a / b) % 256;
  }
}

bool zero_division_checker() {
  stack<int> st;
  stack<vector<int>> st2;
  for (int i = 0; i < n; i++) {
    if (isdigit(e[i][0])) {
      st.push(s2i(e[i]));
    } else {
      int op = op_num(e[i][0]);
      if (op >= 0) {
        int b = st.top();
        st.pop();
        int a = st.top();
        st.pop();
        vector<int> v;
        if (a == MAX) {
          if (b == MAX) {
            vector<int> vb = st2.top();
            st2.pop();
            vector<int> va = st2.top();
            st2.pop();
            for (int j = 0; j < (int)va.size(); j++) {
              for (int k = 0; k < (int)vb.size(); k++) {
                if (op == 3 && vb[k] == 0) {
                  return false;
                }
                v.push_back(calc(va[j], op, vb[k]));
              }
            }
          } else {
            if (op == 3 && b == 0) {
              return false;
            }
            vector<int> va = st2.top();
            st2.pop();
            for (int j = 0; j < (int)va.size(); j++) {
              v.push_back(calc(va[j], op, b));
            }
          }
          sort(v.begin(), v.end());
          v.erase(unique(v.begin(), v.end()), v.end());
          st.push(MAX);
          st2.push(v);
        } else {
          if (b == MAX) {
            vector<int> vb = st2.top();
            st2.pop();
            for (int j = 0; j < (int)vb.size(); j++) {
              if (op == 3 && vb[j] == 0) {
                return false;
              }
              v.push_back(calc(a, op, vb[j]));
            }
            sort(v.begin(), v.end());
            v.erase(unique(v.begin(), v.end()), v.end());
            st.push(MAX);
            st2.push(v);
          } else {
            if (op == 3 && b == 0) {
              return false;
            }
            st.push(calc(a, op, b));
          }
        }
      } else {
        vector<int> v;
        pii p = mp[e[i]];
        for (int j = p.first; j <= p.second; j++) {
          v.push_back(j);
        }
        st.push(MAX);
        st2.push(v);
      }
    }
  }
  return true;
}

int main() {
  int m;
  cin >> m;
  string name;
  int lb, ub;
  for (int i = 0; i < m; i++) {
    cin >> name >> lb >> ub;
    mp[name] = pii(lb, ub);
  }
  cin >> n;
  e.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> e[i];
  }
  cout << (zero_division_checker() ? "correct" : "error") << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 500
typedef pair<int, int> pii;

int n;
vector<string> e;
map<string, pii> mp;

int op_num(char c) {
  switch (c) {
  case '+':
    return 0;
  case '-':
    return 1;
  case '*':
    return 2;
  case '/':
    return 3;
  }
  return -1;
}

int s2i(string &s) {
  int res;
  stringstream ss(s);
  ss >> res;
  return res;
}

int memo[MAX][MAX][MAX];

bool zero_division_checker(int x, vector<int> vec) {
  if (x == n) {
    return true;
  }
  int l = vec.size();
  int v = (l <= 1 ? MAX - 1 : vec[l - 2]);
  int v2 = (l == 0 ? MAX - 1 : vec[l - 1]);
  if (memo[x][v][v2] != -1) {
    return memo[x][v];
  }
  bool res = 1;
  if (isdigit(e[x][0])) {
    vec.push_back(s2i(e[x]));
    res = zero_division_checker(x + 1, vec);
    vec.pop_back();
  } else {
    int op = op_num(e[x][0]);
    if (op >= 0) {
      int b = vec[l - 1];
      int a = vec[l - 2];
      vec.pop_back();
      vec.pop_back();

      int c = -1;
      if (op == 0) {
        c = (a + b) % 256;
      } else if (op == 1) {
        c = (a - b + 256) % 256;
      } else if (op == 2) {
        c = (a * b) % 256;
      } else {
        if (b == 0) {
          return false;
        }
        c = (a / b) % 256;
      }
      vec.push_back(c);
      res = zero_division_checker(x + 1, vec);
      vec.pop_back();
      vec.push_back(a);
      vec.push_back(b);
    } else {
      pii p = mp[e[x]];
      for (int i = p.first; i <= p.second; i++) {
        vec.push_back(i);
        res &= zero_division_checker(x + 1, vec);
        vec.pop_back();
      }
    }
  }
  return (memo[x][v][v2] = res);
}

int main() {
  int m;
  cin >> m;
  string name;
  int lb, ub;
  for (int i = 0; i < m; i++) {
    cin >> name >> lb >> ub;
    mp[name] = pii(lb, ub);
  }
  cin >> n;
  e.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> e[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << (zero_division_checker(0, vector<int>()) ? "correct" : "error")
       << endl;
  return 0;
}