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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<pair<ll, ll>> cnt(n);
  vector<vector<ll>> acnt(50, vector<ll>(50, 0));
  rep(i, n) {
    string s;
    cin >> s;
    ll period = -1, tmp = 0;
    rep(j, len(s)) {
      if (s[j] == '.') {
        if (period == -1)
          period = j;
        continue;
      }
      tmp = tmp * 10 + (s[j] - '0');
    }
    ll rem = (period == -1) ? 9 : (9 - (len(s) - period - 1));
    rep(j, rem) tmp *= 10;
    ll two = 0, fiv = 0;
    while (tmp % 2 == 0) {
      two++;
      tmp /= 2;
    }
    while (tmp % 5 == 0) {
      fiv++;
      tmp /= 5;
    }
    cnt[i] = make_pair(two, fiv);
    acnt[two][fiv]++;
  }
  ll ans = 0;
  rep(i, n) {
    ll two = max(0LL, 18 - cnt[i].first);
    ll fiv = max(0LL, 18 - cnt[i].second);
    reps(j, two, 50) {
      reps(k, fiv, 50) {
        ll mns = ((cnt[i].first == j) && (cnt[i].second == k)) ? -1 : 0;
        ans += acnt[j][k] + mns;
      }
    }
  }
  cout << (ans / 2) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for (ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, e, n) for (ll i = (n)-1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\\n"

template <class T> map<T, int> factorize2(T n) {
  map<T, int> res;
  for (long long i = 2; (i * i) <= n; i++) {
    if (n % i)
      continue;
    res[i] = 0;
    while ((n % i) == 0) {
      n /= i;
      res[i]++;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());
  ll n;
  cin >> n;
  vector<pair<ll, ll>> cnt(n);
  vector<vector<ll>> acnt(50, vector<ll>(50, 0));
  rep(i, n) {
    string s;
    cin >> s;
    ll period = -1, tmp = 0;
    rep(j, len(s)) {
      if (s[j] == '.') {
        if (period == -1)
          period = j;
        continue;
      }
      tmp = tmp * 10 + (s[j] - '0');
    }
    ll rem = (period == -1) ? 9 : (9 - (len(s) - period - 1));
    rep(j, rem) tmp *= 10;
    auto fac = factorize2(tmp);
    cnt[i] = make_pair(fac[2], fac[5]);
    acnt[fac[2]][fac[5]]++;
  }
  ll ans = 0;
  rep(i, n) {
    ll two = max(0LL, 18 - cnt[i].first);
    ll fiv = max(0LL, 18 - cnt[i].second);
    reps(j, two, 50) {
      reps(k, fiv, 50) {
        ll mns = ((cnt[i].first == j) && (cnt[i].second == k)) ? -1 : 0;
        ans += acnt[j][k] + mns;
      }
    }
  }
  cout << (ans / 2) << endl;
  return 0;
}