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

#include <bits/stdc++.h>

// Prerequisite:
// $ sudo apt-get install libboost-all-dev
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using bigint = boost::multiprecision::cpp_int;
using decfloat = boost::multiprecision::cpp_dec_float_50;

template <typename T> std::map<T, int> Factorize(T x) {
  std::map<T, int> m;
  for (T i = 2; i * i <= x;) {
    if (x % i == 0) {
      m[i]++;
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1)
    m[x]++;
  return m;
}

// #undef DEBUG
#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct N {
  int two, five;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  rd(ll, n);
  map<pair<ll, ll>, ll> m;
  // vector<pair<string, pair<ll, ll>>> v;
  rep(i, n) {
    rd(string, s);
    string l = s;
    ll two = 0, five = 0;
    rep(j, s.size()) if (s[j] == '.') {
      l = s.substr(0, j);
      string r = s.substr(j + 1);
      if (r.back() == '0') {
        int k = r.size() - 1;
      }
      while (r.back() == '0')
        r = r.substr(0, r.size() - 1);
      l += r;
      two -= r.size();
      five -= r.size();
    }
    // dbg(l);
    {
      stringstream ss;
      ss << l;
      ll x;
      ss >> x;
      // map<ll, int> fs = Factorize(x);
      while (x % 2 == 0) {
        ++two;
        x /= 2;
      }
      while (x % 5 == 0) {
        ++five;
        x /= 5;
      }
      // five += fs[5];
    }
    // a[i] = {two, five};
    ++m[{two, five}];
    // dbg(s, two, five);
    // v.push_back({s, {two, five}});
  }
  /*
  rep(i, v.size()) rep(j, i) {
    auto [a, b] = v[i].second;
    auto [c, d] = v[j].second;
    if (a + c >= 0 && b + d >= 0) {
      dbg("ok");
      // dbg(v[i].first, v[j].second);
    }
  }
  */
  dbg(m.size());
  ll ans = 0;
  for (auto [a, b] : m) {
    for (auto [c, d] : m) {
      auto [e, f] = a;
      auto [g, h] = c;
      if (e + g >= 0 && f + h >= 0) {
        dbg(e, f, ".", g, h, "|", b, d);
        if (a == c) {
          ans += b * (b - 1) / 2;
        } else {
          ans += b * d;
        }
      }
      if (a == c)
        break;
    }
  }
  wt(ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

// Prerequisite:
// $ sudo apt-get install libboost-all-dev
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>

using bigint = boost::multiprecision::cpp_int;
using decfloat = boost::multiprecision::cpp_dec_float_50;

template <typename T> std::map<T, int> Factorize(T x) {
  std::map<T, int> m;
  for (T i = 2; i * i <= x;) {
    if (x % i == 0) {
      m[i]++;
      x /= i;
    } else {
      ++i;
    }
  }
  if (x > 1)
    m[x]++;
  return m;
}

#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct N {
  int two, five;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  rd(ll, n);
  map<pair<ll, ll>, ll> m;
  // vector<pair<string, pair<ll, ll>>> v;
  rep(i, n) {
    rd(string, s);
    string l = s;
    ll two = 0, five = 0;
    rep(j, s.size()) if (s[j] == '.') {
      l = s.substr(0, j);
      string r = s.substr(j + 1);
      while (r.back() == '0')
        r = r.substr(0, r.size() - 1);
      l += r;
      two -= r.size();
      five -= r.size();
    }
    dbg(l);
    {
      stringstream ss;
      ss << l;
      ll x;
      ss >> x;
      map<ll, int> fs = Factorize(x);
      two += fs[2];
      five += fs[5];
    }
    // a[i] = {two, five};
    ++m[{two, five}];
    dbg(s, two, five);
    // v.push_back({s, {two, five}});
  }
  /*
  rep(i, v.size()) rep(j, i) {
    auto [a, b] = v[i].second;
    auto [c, d] = v[j].second;
    if (a + c >= 0 && b + d >= 0) {
      dbg("ok");
      // dbg(v[i].first, v[j].second);
    }
  }
  */
  ll ans = 0;
  for (auto [a, b] : m) {
    for (auto [c, d] : m) {
      auto [e, f] = a;
      auto [g, h] = c;
      if (e + g >= 0 && f + h >= 0) {
        dbg(e, f, ".", g, h, "|", b, d);
        if (a == c) {
          ans += b * (b - 1) / 2;
        } else {
          ans += b * d;
        }
      }
      if (a == c)
        break;
    }
  }
  wt(ans);
}