Problem p02555 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end()  // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal

#define endl "\\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH {
  size_t operator()(const pair<int, int> &x) const {
    return hash<long long>()(((long long)x.first) ^
                             (((long long)x.second) << 32));
  }
};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < (int)v.size(); ++i) {
    os << v[i];
    if (i != (int)v.size() - 1)
      os << ", ";
  }
  os << "]\\n";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\\n";
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it.first << " : " << it.second;

    if (it != *v.rbegin())
      os << ", ";
  }

  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it.first << " : " << it.second;

    if (it != *v.rbegin())
      os << ", ";
  }

  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  os << "\\n";
  return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and
// range [l, r]. Find x in range such that x is smallest value for which
// predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is
// largest value for which predicate holds true.

// int x = 0;
// for(int s = max_step; s >= 1; s /= 2)
//{
//     while(predicate(x+s)) x += s;
// }
//  after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l < r)
//{
//     ll mid = l + (r-l) / 2;
//     if(P(mid)) {
//         r = mid;
//         ans = mid;
//     }
//     else
//     {
//         l = mid + 1;
//     }
// }
//
//  AND
//
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l <= r)
//{
//     ll mid = l + (r-l) / 2;
//     if(P(mid)) {
//         r = mid-1;
//         ans = mid;
//     }
//     else
//     {
//         l = mid + 1;
//     }
// }
//
//  ARE EQUIVALENT

#define ull unsigned long long

void solve() {
  int s;
  cin >> s;
  if (s == 1) {
    cout << 0 << endl;
    return;
  }

  vector<int> a(s + 1 - 3);
  for (int i = 0; i < a.size(); i++) {
    a[i] = 3 + i;
  }

  vector<ll> dp(s + 1, 0);
  dp[0] = 1;
  for (int ss = 3; ss <= s; ss++) {
    for (int j = 0; j < a.size(); j++) {
      if (ss - a[j] >= 0) {
        dp[ss] += dp[ss - a[j]];
        dp[ss] %= MOD;
      }
    }
  }
  cout << dp[s] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end()  // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal

#define endl "\\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH {
  size_t operator()(const pair<int, int> &x) const {
    return hash<long long>()(((long long)x.first) ^
                             (((long long)x.second) << 32));
  }
};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < (int)v.size(); ++i) {
    os << v[i];
    if (i != (int)v.size() - 1)
      os << ", ";
  }
  os << "]\\n";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\\n";
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it.first << " : " << it.second;

    if (it != *v.rbegin())
      os << ", ";
  }

  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  os << "{";
  for (const auto &it : v) {
    os << it.first << " : " << it.second;

    if (it != *v.rbegin())
      os << ", ";
  }

  os << "}\\n";
  return os;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  os << "\\n";
  return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and
// range [l, r]. Find x in range such that x is smallest value for which
// predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is
// largest value for which predicate holds true.

// int x = 0;
// for(int s = max_step; s >= 1; s /= 2)
//{
//     while(predicate(x+s)) x += s;
// }
//  after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l < r)
//{
//     ll mid = l + (r-l) / 2;
//     if(P(mid)) {
//         r = mid;
//         ans = mid;
//     }
//     else
//     {
//         l = mid + 1;
//     }
// }
//
//  AND
//
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l <= r)
//{
//     ll mid = l + (r-l) / 2;
//     if(P(mid)) {
//         r = mid-1;
//         ans = mid;
//     }
//     else
//     {
//         l = mid + 1;
//     }
// }
//
//  ARE EQUIVALENT

#define ull unsigned long long

void solve() {
  int s;
  cin >> s;

  // dp[i][j] = ways to get j having i elements
  int n = s / 3 + 1;
  vector<int> a(s + 1 - 3);
  for (int i = 0; i < a.size(); i++) {
    a[i] = 3 + i;
  }

  vector<ll> dp(s + 1, 0);
  dp[0] = 1;
  for (int ss = 3; ss <= s; ss++) {
    for (int j = 0; j < a.size(); j++) {
      if (ss - a[j] >= 0) {
        dp[ss] += dp[ss - a[j]];
        dp[ss] %= MOD;
      }
    }
  }
  cout << dp[s] << endl;
  //    dp[0][0] = 1;
  //    for(int i = 1; i <= a.size(); i++) {
  //        for(int j = 0; j <= s; j++) {
  //            dp[i][j] = dp[i-1][j];
  //            if(j - a[i-1] >= 0) {
  //                dp[i][j] += dp[i-1][j-a[i-1]];
  //            }
  //            dp[i][j] %= MOD;
  //        }
  //    }
  //    cout << dp[a.size()][s] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}