Problem p03048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define Z class
#define ln cout << '\n'
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
// ASCII a=97, A=65
//  int H[N]; rep(i,N) scanf("%d", &H[i]);
//  int max_x = *std::max_element(x.begin(), x.end());
//  int min_y = *std::min_element(y.begin(), y.end());
//  sort(vec.begin(), vec.end()); std::greater<>()
//  reverse(vec.begin(), vec.end());
//  scanf("%s",s);
//  printf("%d\n",end-start+1);
//  ceil (a + (b - 1)) / b

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <Z A> void pr(A a) {
  cout << a;
  ln;
}
template <Z A, Z B> void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
const long long INF = 1LL << 60;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  int res = 0;
  for (int i = 0; i <= D; i += A) {
    for (int j = 0; j <= D; j += B) {
      for (int k = 0; k <= D; k += C) {
        if (i + j + k == D)
          res++;
      }
    }
  }
  pr(res);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define Z class
#define ln cout << '\n'
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
// ASCII a=97, A=65
//  int H[N]; rep(i,N) scanf("%d", &H[i]);
//  int max_x = *std::max_element(x.begin(), x.end());
//  int min_y = *std::min_element(y.begin(), y.end());
//  sort(vec.begin(), vec.end()); std::greater<>()
//  reverse(vec.begin(), vec.end());
//  scanf("%s",s);
//  printf("%d\n",end-start+1);
//  ceil (a + (b - 1)) / b

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <Z A> void pr(A a) {
  cout << a;
  ln;
}
template <Z A, Z B> void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <Z A, Z B, Z C> void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <Z A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  ln;
}

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
const long long INF = 1LL << 60;

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  int res = 0;
  for (int i = 0; i <= D; i += A) {
    for (int j = 0; j <= D; j += B) {
      if (D - i - j >= 0 && (D - i - j) % C == 0)
        res++;
    }
  }
  pr(res);
}