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

//------ © daxkofficial™ ------//

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define srt(x) sort((x).begin(), (x).end());
#define clr(x) memset(x, 0, sizeof(x))
#define mem(a, val) memset(a, val, sizeof(a))
#define trace(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << "\\n";
#define sumv(x, w)                                                             \\
  for (auto it : x)                                                            \\
    w += it;
#define traca(A, n)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << A[i] << ' ';                                                       \\
  cout << "\\n";
#define pll pair<ll, ll>
#define vll vector<ll>
#define vvll vector<vll>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define cy                                                                     \\
  cout << "YES"                                                                \\
       << "\\n";
#define cn                                                                     \\
  cout << "NO"                                                                 \\
       << "\\n";
#define nl cout << "\\n";
#define chk cout << "*\\n";
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

/*-----------------------------Modular Arithmetic---------------------*/
inline ll add(ll x, ll y, ll mod) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
inline ll sub(ll x, ll y, ll mod) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
inline ll mul(ll x, ll y, ll mod) { return (x * 1ll * y) % mod; }
inline ll power(ll x, ll y, ll mod) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = mul(ans, x, mod);
    x = mul(x, x, mod);
    y >>= 1;
  }
  return ans;
}
inline ll inv(ll x, ll mod) { return power(x, mod - 2, mod); }

/*------------------------ Important Functions ----------------------------*/

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

/*------------------------ Trouble begins here ----------------------------*/

const ll mod = 1e9 + 7, M2 = 998244353;
const ll N = 1e5 + 100;

ll factmod(ll n, ll p) {
  ll res = 1;
  while (n > 1) {
    res = (res * ((n / p) % 2 ? p - 1 : 1)) % p;
    for (ll i = 2; i <= n % p; ++i)
      res = (res * i) % p;
    n /= p;
  }
  return res % p;
}

ll largestPower(ll n, ll p) {
  ll x = 0;
  while (n) {
    n /= p;
    x += n;
  }
  return x;
}

ll modFact(ll n, ll p) {
  if (n >= p)
    return 0;
  ll res = 1;
  bool isPrime[n + 1];
  memset(isPrime, 1, sizeof(isPrime));
  for (ll i = 2; i * i <= n; i++) {
    if (isPrime[i]) {
      for (ll j = 2 * i; j <= n; j += i)
        isPrime[j] = 0;
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (isPrime[i]) {
      ll k = largestPower(n, i);
      res = (res * power(i, k, p)) % p;
    }
  }
  return res;
}

ll fastpower(ll n, ll p, ll m) {
  if (p == 0)
    return 1;

  ll t = fastpower(n, p / 2, m);
  ll ans = (t * t) % m;

  if (p % 2 != 0) {
    ans = (ans * n % m) % m;
  }
  return ans;
}

ll countWays(vll arr, ll m, ll N) {
  ll count[N + 1];
  memset(count, 0, sizeof(count));

  // base case
  count[0] = 1;

  // count ways for all values up
  // to 'N' and store the result
  for (ll i = 1; i <= N; i++)
    for (ll j = 0; j < m; j++)

      // if i >= arr[j] then
      // accumulate count for value 'i' as
      // ways to form value 'i-arr[j]'
      if (i >= arr[j]) {
        count[i] += count[i - arr[j]];
        count[i] %= mod;
      }

  // required number of ways
  return count[N];
}

ll count(ll n) {
  // table[i] will store count
  // of solutions for value i.
  if (n == 3)
    return 1;
  else if (n == 6)
    return 2;
  else if (n == 9)
    return 4;

  ll table[n + 1], i;

  // Initialize all table
  // values as 0
  for (i = 0; i < n + 1; i++)
    table[i] = 0;

  // Base case (If given value
  // is 0, 1, 2, or 4)
  table[0] = 0;
  table[3] = 1;
  table[6] = 2;
  table[9] = 4;

  for (i = 8; i <= n; i = i + 2) {

    table[i] = table[i - 2] + table[i - 4] + table[i - 6];
  }

  return table[n];
}

void solve() {
  ll n, i;
  cin >> n;

  vll arr;
  for (i = 3; i <= n; i++)
    arr.push_back(i);
  ll x = arr.size();
  cout << countWays(arr, x, n) % mod;

  nl return;
}

int main() {

  fastio

      ll tc = 1;
  // cin>>tc;
  while (tc--) {

    // cout<<"ch\\n";
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

//------ © daxkofficial™ ------//

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define srt(x) sort((x).begin(), (x).end());
#define clr(x) memset(x, 0, sizeof(x))
#define mem(a, val) memset(a, val, sizeof(a))
#define trace(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << ' ';                                                         \\
  cout << "\\n";
#define sumv(x, w)                                                             \\
  for (auto it : x)                                                            \\
    w += it;
#define traca(A, n)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << A[i] << ' ';                                                       \\
  cout << "\\n";
#define pll pair<ll, ll>
#define vll vector<ll>
#define vvll vector<vll>
#define vb vector<bool>
#define vc vector<char>
#define vpll vector<pll>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define cy                                                                     \\
  cout << "YES"                                                                \\
       << "\\n";
#define cn                                                                     \\
  cout << "NO"                                                                 \\
       << "\\n";
#define nl cout << "\\n";
#define chk cout << "*\\n";
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

/*-----------------------------Modular Arithmetic---------------------*/
inline ll add(ll x, ll y, ll mod) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
inline ll sub(ll x, ll y, ll mod) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
inline ll mul(ll x, ll y, ll mod) { return (x * 1ll * y) % mod; }
inline ll power(ll x, ll y, ll mod) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = mul(ans, x, mod);
    x = mul(x, x, mod);
    y >>= 1;
  }
  return ans;
}
inline ll inv(ll x, ll mod) { return power(x, mod - 2, mod); }

/*------------------------ Important Functions ----------------------------*/

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  a %= b;
  return gcd(b, a);
}

ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }

/*------------------------ Trouble begins here ----------------------------*/

const ll mod = 1e9 + 7, M2 = 998244353;
const ll N = 1e5 + 100;

ll factmod(ll n, ll p) {
  ll res = 1;
  while (n > 1) {
    res = (res * ((n / p) % 2 ? p - 1 : 1)) % p;
    for (ll i = 2; i <= n % p; ++i)
      res = (res * i) % p;
    n /= p;
  }
  return res % p;
}

ll largestPower(ll n, ll p) {
  ll x = 0;
  while (n) {
    n /= p;
    x += n;
  }
  return x;
}

ll modFact(ll n, ll p) {
  if (n >= p)
    return 0;
  ll res = 1;
  bool isPrime[n + 1];
  memset(isPrime, 1, sizeof(isPrime));
  for (ll i = 2; i * i <= n; i++) {
    if (isPrime[i]) {
      for (ll j = 2 * i; j <= n; j += i)
        isPrime[j] = 0;
    }
  }
  for (ll i = 2; i <= n; i++) {
    if (isPrime[i]) {
      ll k = largestPower(n, i);
      res = (res * power(i, k, p)) % p;
    }
  }
  return res;
}

ll fastpower(ll n, ll p, ll m) {
  if (p == 0)
    return 1;

  ll t = fastpower(n, p / 2, m);
  ll ans = (t * t) % m;

  if (p % 2 != 0) {
    ans = (ans * n % m) % m;
  }
  return ans;
}

ll countWays(ll arr[], ll m, ll N) {
  ll count[N + 1];
  memset(count, 0, sizeof(count));

  // base case
  count[0] = 1;

  // count ways for all values up
  // to 'N' and store the result
  for (ll i = 1; i <= N; i++)
    for (ll j = 0; j < m; j++)

      // if i >= arr[j] then
      // accumulate count for value 'i' as
      // ways to form value 'i-arr[j]'
      if (i >= arr[j]) {
        count[i] += count[i - arr[j]];
        count[i] %= mod;
      }

  // required number of ways
  return count[N];
}

ll count(ll n) {
  // table[i] will store count
  // of solutions for value i.
  if (n == 3)
    return 1;
  else if (n == 6)
    return 2;
  else if (n == 9)
    return 4;

  ll table[n + 1], i;

  // Initialize all table
  // values as 0
  for (i = 0; i < n + 1; i++)
    table[i] = 0;

  // Base case (If given value
  // is 0, 1, 2, or 4)
  table[0] = 0;
  table[3] = 1;
  table[6] = 2;
  table[9] = 4;

  for (i = 8; i <= n; i = i + 2) {

    table[i] = table[i - 2] + table[i - 4] + table[i - 6];
  }

  return table[n];
}

void solve() {
  ll n;
  cin >> n;

  ll arr[] = {3, 4, 5, 6, 7, 8, 9};

  cout << countWays(arr, n, 7);

  nl return;
}

int main() {

  fastio

      ll tc = 1;
  // cin>>tc;
  while (tc--) {

    // cout<<"ch\\n";
    solve();
  }

  return 0;
}