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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

void solve(const vi &a, const vi &b, int p, int sum, vi &c) {
  if (p == a.size())
    c[sum]++;
  else
    rep(i, b[p] + 1) solve(a, b, p + 1, sum + a[p] * i, c);
}

int main() {
  for (int m; cin >> m, m;) {
    vi a(m), b(m);
    rep(i, m) cin >> a[i] >> b[i];

    vi c(7001);
    solve(a, b, 0, 0, c);
    int g;
    cin >> g;
    rep(i, g) {
      int n;
      cin >> n;
      cout << c[n] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int solve(vi &a, vi &b, int n, vi &c, int p) {
  if (p == a.size()) {
    int sum = 0;
    rep(i, a.size()) sum += a[i] * c[i];
    return sum == n;
  } else {
    int res = 0;
    rep(i, b[p] + 1) {
      c[p] = i;
      res += solve(a, b, n, c, p + 1);
    }
    return res;
  }
}

int main() {
  for (int m; cin >> m, m;) {
    vi a(m), b(m);
    rep(i, m) cin >> a[i] >> b[i];

    int g;
    cin >> g;
    rep(i, g) {
      int n;
      cin >> n;
      vi c(m);
      cout << solve(a, b, n, c, 0) << endl;
    }
  }

  return 0;
}