Problem p01970 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(), (x).end())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x, n)                                                            \\
  for (int yui = 0; yui < n; yui++) {                                          \\
    cout << x[yui] << " ";                                                     \\
  }                                                                            \\
  cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9 + 7;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> inline T sq(T a) { return a * a; }

ll in() {
  long long int x;
  scanf("%lld", &x);
  return x;
}
char yuyushiki[1000010];
string stin() {
  scanf("%s", yuyushiki);
  return yuyushiki;
}

// head

struct Mod {
  unsigned n;
  Mod() : n(0) {}
  Mod(ll x) {
    if (x < 0)
      n = x % MOD + MOD;
    else
      n = x % MOD;
  }
};
Mod operator+(Mod a, Mod b) { return Mod(a.n + b.n); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-(Mod a) { return Mod(MOD - a.n); }
Mod operator-(Mod a, Mod b) { return Mod(a.n + MOD - b.n); }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator*(Mod a, Mod b) { return Mod((ll)a.n * b.n); }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod modpow(Mod x, ll k) {
  Mod res = 1;
  while (k) {
    if (k & 1)
      res *= x;
    k /= 2;
    x *= x;
  }
  return res;
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
  return d;
}
Mod inv(Mod a) {
  ll x, y;
  assert(extgcd(a.n, MOD, x, y) == 1);
  return Mod(x);
}
Mod operator/(Mod a, Mod b) { return Mod((ll)a.n * inv(b).n); }
Mod operator/=(Mod &a, Mod b) { return a = a / b; }

vb isp;
int max_p;
void init_p(int mp = 1000010) {
  max_p = mp;
  isp.resize(max_p, true);
  isp[0] = isp[1] = false;
  FOR(i, 2, max_p) {
    if (isp[i]) {
      for (int j = i * 2; j < max_p; j += i)
        isp[j] = false;
    }
  }
}
bool is_prime(int x) { return isp[x]; }

int n;

int a[100010];

Mod dp[100010][2];

int main() {
  init_p();

  n = in();
  a[0] = -1;
  REP(i, n) { a[i + 1] = in(); }

  dp[0][0] = 1;
  REP(i, n) {
    REP(j, 2) {
      if (dp[i][j].n != 0) {
        if (a[i - j] < a[i + 1] && is_prime(a[i + 1])) {
          // cout << "* " << i+1 << " " << 0 << " " << dp[i+1][0].n << " " <<
          // dp[i][j].n << endl;
          dp[i + 1][0] += dp[i][j];
          if (i + 2 <= n && a[i + 2] >= 2) {
            dp[i + 2][1] += dp[i][j];
            // cout << "* " << i+2 << " " << 1 << " " << dp[i+1][0].n << " " <<
            // dp[i][j].n << endl;
          }
        }
      }
    }
  }

  /*
  REP( i , n+1 ){
    cout << dp[i][0].n << " " << dp[i][1].n << endl;
  }
  */

  Mod ans = dp[n][0] + dp[n][1];

  cout << ans.n << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(), (x).end())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x, n)                                                            \\
  for (int yui = 0; yui < n; yui++) {                                          \\
    cout << x[yui] << " ";                                                     \\
  }                                                                            \\
  cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9 + 7;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> inline T sq(T a) { return a * a; }

ll in() {
  long long int x;
  scanf("%lld", &x);
  return x;
}
char yuyushiki[1000010];
string stin() {
  scanf("%s", yuyushiki);
  return yuyushiki;
}

// head

struct Mod {
  unsigned n;
  Mod() : n(0) {}
  Mod(ll x) {
    if (x < 0)
      n = x % MOD + MOD;
    else
      n = x % MOD;
  }
};
Mod operator+(Mod a, Mod b) { return Mod(a.n + b.n); }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator-(Mod a) { return Mod(MOD - a.n); }
Mod operator-(Mod a, Mod b) { return Mod(a.n + MOD - b.n); }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator*(Mod a, Mod b) { return Mod((ll)a.n * b.n); }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod modpow(Mod x, ll k) {
  Mod res = 1;
  while (k) {
    if (k & 1)
      res *= x;
    k /= 2;
    x *= x;
  }
  return res;
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
  return d;
}
Mod inv(Mod a) {
  ll x, y;
  assert(extgcd(a.n, MOD, x, y) == 1);
  return Mod(x);
}
Mod operator/(Mod a, Mod b) { return Mod((ll)a.n * inv(b).n); }
Mod operator/=(Mod &a, Mod b) { return a = a / b; }

vi ps;
vb isp;
int max_p;
void init_p(int mp = 1000010) {
  max_p = mp;
  isp.resize(max_p, true);
  isp[0] = isp[1] = false;
  FOR(i, 2, max_p) {
    if (isp[i]) {
      ps.pb(i);
      for (int j = i * 2; j < max_p; j += i)
        isp[j] = false;
    }
  }
}
bool is_prime(int x) {
  if (max_p <= x)
    init_p(x + 1);
  return isp[x];
}

int n;

int a[100010];

Mod dp[100010][2];

int main() {

  n = in();
  a[0] = -1;
  REP(i, n) { a[i + 1] = in(); }

  dp[0][0] = 1;
  REP(i, n) {
    REP(j, 2) {
      if (dp[i][j].n != 0) {
        if (a[i - j] < a[i + 1] && is_prime(a[i + 1])) {
          // cout << "* " << i+1 << " " << 0 << " " << dp[i+1][0].n << " " <<
          // dp[i][j].n << endl;
          dp[i + 1][0] += dp[i][j];
          if (i + 2 <= n && a[i + 2] >= 2) {
            dp[i + 2][1] += dp[i][j];
            // cout << "* " << i+2 << " " << 1 << " " << dp[i+1][0].n << " " <<
            // dp[i][j].n << endl;
          }
        }
      }
    }
  }

  /*
  REP( i , n+1 ){
    cout << dp[i][0].n << " " << dp[i][1].n << endl;
  }
  */

  Mod ans = dp[n][0] + dp[n][1];

  cout << ans.n << endl;

  return 0;
}