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

#include <bits/stdc++.h>
#include <utility>
using namespace std;
using ll = long long;

ll vm[200001][22][22];

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  for (ll i = 0; i < n; i++)
    cin >> s[i];
  vector<pair<ll, ll>> vp(n);
  for (ll i = 0; i < n; i++) {

    pair<ll, ll> p = pair<ll, ll>(0, 0); // 5,2
    for (ll j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '.') {
        ll x = s[i].size() - j - 1;
        p.first -= x;
        p.second -= x;
      }
    }
    ll x = 0;
    for (ll j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '.')
        continue;
      x *= 10;
      x += s[i][j] - '0';
    }
    while (x % 5 == 0) {
      p.first++;
      x /= 5;
    }
    while (x % 2 == 0) {
      p.second++;
      x /= 2;
    }

    if (p.first > 9)
      p.first = 10;
    if (p.second > 9)
      p.second = 10;
    vp[i] = p;
    // cout<<p.first<<' ' << p.second<<endl;
    p.first += 10;
    p.second += 10;
    vm[i][p.first][p.second]++;
  }
  for (ll i = n - 1; i > 0; i--) {
    for (ll f = -10; f < 12; f++) {
      for (ll t = -10; t < 12; t++) {
        pair<ll, ll> now = pair<ll, ll>(f, t);
        now.first += 10;
        now.second += 10;
        vm[i - 1][now.first][now.second] += vm[i][now.first][now.second];
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    auto now = vp[i];
    now.first *= -1;
    now.second *= -1;
    for (ll f = now.first; f < 12; f++) {
      for (ll t = now.second; t < 12; t++) {

        pair<ll, ll> tmp = make_pair(f, t);
        tmp.first += 10;
        tmp.second += 10;
        ans += vm[i + 1][tmp.first][tmp.second];
      }
    }
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <utility>
using namespace std;
using ll = long long;

ll vm[200001][21][47];

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  for (ll i = 0; i < n; i++)
    cin >> s[i];
  vector<pair<int, int>> vp(n);
  for (ll i = 0; i < n; i++) {

    pair<ll, ll> p = pair<ll, ll>(0, 0); // 5,2
    for (ll j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '.') {
        ll x = s[i].size() - j - 1;
        p.first -= x;
        p.second -= x;
      }
    }
    ll x = 0;
    for (ll j = 0; j < s[i].size(); j++) {
      if (s[i][j] == '.')
        continue;
      x *= 10;
      x += s[i][j] - '0';
    }
    while (x % 5 == 0) {
      p.first++;
      x /= 5;
    }
    while (x % 2 == 0) {
      p.second++;
      x /= 2;
    }

    vp[i] = p;
    p.first += 9;
    p.second += 9;
    vm[i][p.first][p.second]++;
  }
  for (ll i = n - 1; i > 0; i--) {
    for (ll f = -9; f < 12; f++) {
      for (ll t = -9; t < 37; t++) {
        pair<ll, ll> now = pair<ll, ll>(f, t);
        now.first += 9;
        now.second += 9;
        vm[i - 1][now.first][now.second] += vm[i][now.first][now.second];
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    auto now = vp[i];
    now.first *= -1;
    now.second *= -1;
    for (ll f = now.first; f < 12; f++) {
      for (ll t = now.second; t < 37; t++) {
        pair<ll, ll> tmp = make_pair(f, t);
        tmp.first += 9;
        tmp.second += 9;
        ans += vm[i + 1][tmp.first][tmp.second];
      }
    }
  }
  cout << ans << endl;
}