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

#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define all(c) c.begin(), c.end()
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define debug(x) #x << " = " << (x) << " (L" << __LINE__ << ")"

typedef long long ll;
typedef vector<int> vi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &val) {
  os << "[ ";
  for (typename vector<T>::const_iterator it = val.begin(); it != val.end();
       ++it) {
    os << *it << " ";
  }
  os << "]";
  return os;
}

pair<int, int> solve(ll a, ll b, ll d) {
  int mini = 100000000;
  int minf = 100000000;
  int ra = -1, rb = -1;
  for (int u_a = -10000; u_a <= 10000; u_a++) {
    int td = d + a * u_a;
    if (td < 0)
      td = -td;
    if (td % b == 0) {
      int u_b = td / b;
      int a_u_a = abs(u_a);
      int a_u_b = abs(u_b);
      int used = a_u_a + a_u_b;
      if (mini > used or (mini == used and minf > a * a_u_a + b * a_u_b)) {
        mini = used;
        minf = a * a_u_a + b * a_u_b;
        ra = a_u_a;
        rb = a_u_b;
        // cerr << debug(a_u_a) << debug(a_u_b) << endl;
      }
    }
  }
  return make_pair(ra, rb);
}

int main() {
  while (true) {
    int a, b, d;
    cin >> a >> b >> d;
    if (a == 0 and b == 0 and d == 0)
      break;
    pair<int, int> ret = solve(a, b, d);
    cout << ret.first << " " << ret.second << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define all(c) c.begin(), c.end()
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

typedef long long ll;
typedef vector<int> vi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &val) {
  os << "[ ";
  for (typename vector<T>::const_iterator it = val.begin(); it != val.end();
       ++it) {
    os << *it << " ";
  }
  os << "]";
  return os;
}

struct State {
  int left, right, diff;
  State(int a_left, int a_right, int a_diff)
      : left(a_left), right(a_right), diff(a_diff) {}
};

ostream &operator<<(ostream &os, const State &s) {
  os << "[" << s.left << "," << s.right << "," << s.diff << "]";
  return os;
}
int main() {
  while (true) {
    int a, b, d;
    cin >> a >> b >> d;
    if (a == 0 and b == 0 and d == 0)
      break;
    queue<State> que;
    que.push(State(0, 0, d));
    State ret(-1, -1, -1);
    set<int> used;
    while (not que.empty()) {
      State t = que.front();
      que.pop();
      if (used.find(t.diff) != used.end())
        continue;
      used.insert(t.diff);
      if (t.diff == 0) {
        ret = t;
        break;
      }
      que.push(State(t.left + 1, t.right, t.diff + a));
      que.push(State(t.left + 1, t.right, t.diff - a));
      que.push(State(t.left, t.right + 1, t.diff + b));
      que.push(State(t.left, t.right + 1, t.diff - b));
    }
    cout << ret.left << " " << ret.right << endl;
  }
  return 0;
}