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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int sum[330][330], p[330][330];
signed main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) {
    string s;
    cin >> s;
    int m = s.size();
    int rest = 0;
    int tmp = 0;
    rep(j, m) {
      if (s[j] != '.') {
        tmp *= 10;
        tmp += (s[j] - '0');
      } else {
        rest = m - j - 1;
      }
    }
    rest = max(0ll, 9 - rest);
    while (rest--)
      tmp *= 10;
    a.push_back(tmp);
  }
  vector<int> ts(n), fs(n);
  rep(i, n) {
    int t = 0;
    while (a[i] % 2 == 0) {
      t++;
      a[i] /= 2;
    }
    int f = 0;
    while (a[i] % 5 == 0) {
      f++;
      a[i] /= 5;
    }
    p[t][f]++;
    ts[i] = t;
    fs[i] = f;
  }
  // int ret = 0;
  // rep(i,n)rep(j,i){
  // 	if(ts[i] + ts[j] >= 18 && fs[i] + fs[j] >= 18)ret++;
  // }
  // cout << ret << endl;
  // trace(a);
  // 1e18 == 0
  rep(i, 301) rep(j, 301) {
    sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + (p[i][j]);
  }
  int ret = 0;
  rep(i, n) {
    // trace(ts[i],fs[i],ret);
    int M = 300, A = max(0ll, 18 - ts[i]), B = max(0ll, 18 - fs[i]);
    // trace(A,B);
    ret += sum[M][M] - sum[A][M] - sum[M][B] + sum[A][B];
    if (ts[i] >= A && fs[i] >= B)
      ret--;
  }
  // trace(ts,fs);
  cout << ret / 2 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
int sum[330][330], p[330][330];
signed main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) {
    string s;
    cin >> s;
    int m = s.size();
    int rest = 0;
    int tmp = 0;
    rep(j, m) {
      if (s[j] != '.') {
        tmp *= 10;
        tmp += (s[j] - '0');
      } else {
        rest = m - j - 1;
      }
    }
    rest = max(0ll, 9 - rest);
    while (rest--)
      tmp *= 10;
    a.push_back(tmp);
  }
  vector<int> ts(n), fs(n);
  rep(i, n) {
    int t = 0;
    while (a[i] % 2 == 0) {
      t++;
      a[i] /= 2;
    }
    int f = 0;
    while (a[i] % 5 == 0) {
      f++;
      a[i] /= 5;
    }
    p[t][f]++;
    ts[i] = t;
    fs[i] = f;
  }
  int ret = 0;
  rep(i, n) rep(j, i) {
    if (ts[i] + ts[j] >= 18 && fs[i] + fs[j] >= 18)
      ret++;
  }
  cout << ret << endl;
  // //trace(a);
  // //1e18 == 0
  // rep(i,301)rep(j,301){
  // 	sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + (p[i][j]);
  // }
  // int ret = 0;
  // rep(i,n){
  // 	ret += sum[300][300] - sum[300][max(0ll,18 - ts[i])] - sum[max(0ll,18 -
  // fs[i])][300] + sum[max(0ll,18-ts[i])][max(0ll,18-fs[i])];
  // }
  // trace(ts,fs);
  // cout << ret << endl;

  return 0;
}