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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define IREP(it, array)                                                        \\
  for (vector<int>::iterator it = array.begin(); it != array.end(); ++it)
#define PREP(it, array)                                                        \\
  for (vector<P>::iterator it = array.begin(); it != array.end(); ++it)
#define SREP(it, array)                                                        \\
  for (vector<string>::iterator it = array.begin(); it != array.end(); ++it)

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()

const int INF = 1 << 29;
const double EPS = 1e-9;
double zero(double d) { return d < EPS ? 0.0 : d; }

typedef long long LL;
typedef pair<int, int> P;

int m;
int card[100 + 1];
int g;
int memo[1001][101];

int solve(int n, int u) {
  if (n == 0)
    return 1;
  if (n < 0 || u > 100)
    return 0;

  if (memo[n][u] != -1)
    return memo[n][u];

  int res = 0;
  REP(i, card[u] + 1) { res += solve(n - u * i, u + 1); }

  return memo[n][u] = res;
}

int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  while (cin >> m, m) {
    REP(i, 1001)
    fill(memo[i], memo[i] + 101, -1);
    memset(card, 0, sizeof(card));
    REP(_t, m) {
      int a, b;
      cin >> a >> b;
      card[a] = b;
    }

    cin >> g;
    REP(_t, g) {
      int n;
      cin >> n;
      cout << solve(n, 0) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define IREP(it, array)                                                        \\
  for (vector<int>::iterator it = array.begin(); it != array.end(); ++it)
#define PREP(it, array)                                                        \\
  for (vector<P>::iterator it = array.begin(); it != array.end(); ++it)
#define SREP(it, array)                                                        \\
  for (vector<string>::iterator it = array.begin(); it != array.end(); ++it)

#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(), (x).end()

const int INF = 1 << 29;
const double EPS = 1e-9;
double zero(double d) { return d < EPS ? 0.0 : d; }

typedef long long LL;
typedef pair<int, int> P;

int m;
int card[100 + 1];
int g;
int memo[1001][101];

int solve(int n, int u) {
  if (n == 0)
    return 1;
  if (n < 0 || u > 100)
    return 0;

  if (memo[n][u] != 0)
    return memo[n][u];

  int res = 0;
  REP(i, card[u] + 1) { res += solve(n - u * i, u + 1); }

  return memo[n][u] = res;
}

int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  while (cin >> m, m) {
    memset(memo, 0, sizeof(memo));
    memset(card, 0, sizeof(card));
    REP(_t, m) {
      int a, b;
      cin >> a >> b;
      card[a] = b;
    }

    cin >> g;
    REP(_t, g) {
      int n;
      cin >> n;
      cout << solve(n, 0) << endl;
    }
  }

  return 0;
}