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

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline int msbp(int x) { return 31 - __builtin_clz(x); }
inline int msb(int x) { return 1 << msbp(x); }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;
// const ll INF = LLONG_MAX;
// const ll nINF = LLONG_MIN;

/////////////////////////////////////////////////////////////////////

const int N = 2e5 + 10;

int n;
ll ans;
pii p[N];
map<pii, int> m;

signed main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  cin >> n;
  REP(i, n) {
    string a;
    cin >> a;
    int id = a.find(".");
    int two = 0, five = 0;
    ll num;
    if (id != -1) {
      num = stoll(a.substr(0, id) + a.substr(id + 1));
      int tens = a.size() - 1 - id;
      two = -tens, five = -tens;
    } else {
      num = stoll(a);
    }
    while (num % 2 == 0) {
      ++two;
      num /= 2;
    }
    while (num % 5 == 0) {
      ++five;
      num /= 5;
    }
    for (auto [pr, freq] : m) {
      auto [j, k] = pr;
      if (j + two >= 0 && k + five >= 0) {
        ans += freq;
      }
    }
    p[i] = {two, five};
    ++m[p[i]];
  }
  cout << ans << endl;

  return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE) CHECK IF IT SHOULD BE 2*N!!!!
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - negative arithmetic, ceiling division, modulo (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function
// (WA)
// - not clearing DS between test cases EVEN FOR INPUT (WA, RTE, TLE, MLE)


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline int msbp(int x) { return 31 - __builtin_clz(x); }
inline int msb(int x) { return 1 << msbp(x); }
typedef long long ll;
typedef pair<int, int> pii;
// const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int INF = 0x3f3f3f3f;
// const int INF = INT_MAX;
// const int nINF = INT_MIN;
// const ll INF = LLONG_MAX;
// const ll nINF = LLONG_MIN;

/////////////////////////////////////////////////////////////////////

const int N = 2e5 + 10;

int n;
ll ans;
pii p[N];
map<pii, int> m;

signed main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  cin >> n;
  REP(i, n) {
    string a;
    cin >> a;
    int id = a.find(".");
    int two = 0, five = 0;
    ll num;
    if (id != -1) {
      num = stoll(a.substr(0, id) + a.substr(id + 1));
      int tens = a.size() - 1 - id;
      two = -tens, five = -tens;
    } else {
      num = stoll(a);
    }
    while (num % 2 == 0) {
      ++two;
      num /= 2;
    }
    while (num % 5 == 0) {
      ++five;
      num /= 5;
    }
    p[i] = {two, five};
    ++m[p[i]];
  }
  REP(i, n) {
    auto [two, five] = p[i];
    FOR(j, -two, 20) {
      FOR(k, -five, 20) { ans += m[{j, k}]; }
    }
    if (two >= 0 && five >= 0)
      --ans;
  }
  cout << ans / 2 << endl;

  return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE) CHECK IF IT SHOULD BE 2*N!!!!
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - negative arithmetic, ceiling division, modulo (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting max to 0 instead of -INF/min to 0 instead of INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)
// - round announcements (WA)
// - multiset::erase (WA)
// - forgetting to rename recursive call when copy/pasting recursive function
// (WA)
// - not clearing DS between test cases EVEN FOR INPUT (WA, RTE, TLE, MLE)