Problem p03048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define rp(i, l, m) for (int i = l; i < m; ++i)
#define Rp(i, l, m) for (int i = l; i <= m; i++)
#define rpD(i, l, m) for (int i = m; i > l; i--)
#define RpD(i, m, l) for (int i = m; i >= l; i--)
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define sz(a) int((a).size())
#define is insert
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = (int)1e9 + 7;
const ld PI = acos((ld)-1);
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll Pow(int n, int k) {
  int r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n;
    n = n * n;
  }
  return r;
}
#define N 5005

int ans, r, b, n, g, s1, s2, s3;

inline void input() { cin >> r >> g >> b >> n; }

inline void solve() {
  for (int i = 0; i <= n / r; i++) {
    s1 = r * i;
    if (s1 == n) {
      ans++;
      break;
    }
    if (s1 > n)
      break;
    int s2;
    for (int j = 0; j <= (n - s1) / g; j++) {
      s2 = s1 + j * g;
      if (s2 == n) {
        ans++;
        break;
      }
      if (s2 > n)
        break;
      int s3;
      for (int k = 0; k <= (n - s2) / b; k++) {
        s3 = s2 + k * b;
        if (s3 == n) {
          ans++;
          break;
        }
        if (s3 > n)
          break;
      }
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define rp(i, l, m) for (int i = l; i < m; ++i)
#define Rp(i, l, m) for (int i = l; i <= m; i++)
#define rpD(i, l, m) for (int i = m; i > l; i--)
#define RpD(i, m, l) for (int i = m; i >= l; i--)
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define sz(a) int((a).size())
#define is insert
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = (int)1e9 + 7;
const ld PI = acos((ld)-1);
inline ll gcd(ll a, ll b) {
  ll r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline ll fpow(ll n, ll k, int p = MOD) {
  ll r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline ll Pow(int n, int k) {
  int r = 1;
  for (; k; k >>= 1) {
    if (k & 1)
      r = r * n;
    n = n * n;
  }
  return r;
}
#define N 5005

int ans, r, b, n, g, s1, s2, s3;

inline void input() { cin >> r >> g >> b >> n; }

inline void solve() {
  for (int i = 0; i <= n / r; i++) {
    s1 = r * i;
    if (s1 == n) {
      ans++;
      break;
    }
    if (s1 > n)
      break;
    int s2;
    for (int j = 0; j <= (n - s1) / g; j++) {
      s2 = s1 + j * g;
      if (s2 == n) {
        ans++;
        break;
      }
      if (s2 > n)
        break;
      if ((n - s2) % b == 0)
        ans++;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
  return 0;
}