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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt[20][20];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    string s;
    cin >> s;
    int idx = 0;
    while (idx < s.size() && s[idx] != '.') {
      a[i] *= 10;
      a[i] += s[idx] - '0';
      idx++;
    }
    idx++;
    rep(_, 9) {
      a[i] *= 10;
      if (idx < s.size()) {
        a[i] += s[idx] - '0';
        idx++;
      }
    }
  }

  rep(i, n) {
    array<ll, 2> res = {0, 0};
    while (a[i] % 2 == 0) {
      res[0]++;
      a[i] /= 2;
    }
    while (a[i] % 5 == 0) {
      res[1]++;
      a[i] /= 5;
    }
    chmin(res[0], 18LL);
    chmin(res[1], 18LL);
    cnt[res[0]][res[1]]++;
  }
  ll ans = 0;
  rep(i, 20) {
    rep(j, 20) {
      rep(k, 20) {
        rep(l, 20) {
          if (i + k >= 18 && j + l >= 18) {
            ll res = cnt[i][j] * cnt[k][l];
            if (i == k && j == l)
              res = cnt[i][j] * (cnt[i][j] - 1);
            ans += res;
            // if (cnt[i][j] && cnt[k][l]) cout << i << ' ' << j << ' ' << k <<
            // ' ' << l << ' ' << res << '\\n';
          }
        }
      }
    }
  }
  // rep(i, 20) rep(j, 20) if (cnt[i][j]) cout << i << ' ' << j << ' ' <<
  // cnt[i][j] << '\\n';
  cout << ans / 2 << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

ll cnt[20][20];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    string s;
    cin >> s;
    int idx = 0;
    while (idx < s.size() && s[idx] != '.') {
      a[i] *= 10;
      a[i] += s[idx] - '0';
      idx++;
    }
    idx++;
    rep(_, 9) {
      a[i] *= 10;
      if (idx < s.size()) {
        a[i] += s[idx] - '0';
        idx++;
      }
    }
  }

  rep(i, n) {
    map<ll, ll> mp = prime_factor(a[i]);
    chmin(mp[2], (ll)18);
    chmin(mp[5], (ll)18);
    cnt[mp[2]][mp[5]]++;
  }
  ll ans = 0;
  rep(i, 20) {
    rep(j, 20) {
      rep(k, 20) {
        rep(l, 20) {
          if (i + k >= 18 && j + l >= 18) {
            ll res = cnt[i][j] * cnt[k][l];
            if (i == k && j == l)
              res = cnt[i][j] * (cnt[i][j] - 1);
            ans += res;
            // if (cnt[i][j] && cnt[k][l]) cout << i << ' ' << j << ' ' << k <<
            // ' ' << l << ' ' << res << '\\n';
          }
        }
      }
    }
  }
  // rep(i, 20) rep(j, 20) if (cnt[i][j]) cout << i << ' ' << j << ' ' <<
  // cnt[i][j] << '\\n';
  cout << ans / 2 << '\\n';

  return 0;
}