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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  // kazde cislo zo vstupu sa da zapisat
  // ako x/(2^a*5^b)
  // Kazde x mozme upravit do takehoto tvaru pomocou gcd.
  // Nech b[i] = 10^9 * a[i]. potom a[i] = b[i]/(2^9*5^9)
  // Zaujimaju nas len mocniny 2 a 5 v b.
  vector<pair<ll, ll>> a(n);
  const ll c = 9;
  vector<vector<ll>> f(c * 2 + 7, vector<ll>(c * 2 + 7, 0));
  ll good = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int b = -1;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '.') {
        b = j;
        break;
      }
    }
    if (b == -1) {
      s.push_back(b);
      b = s.size() - 1;
    }
    s.resize(b + 10, '0');
    s.erase(s.begin() + b);
    ll num = stoll(s);
    // cout << num << endl;
    a[i].first = -c;
    a[i].second = -c;
    while (num % 2 == 0) {
      num /= 2;
      a[i].first++;
    }
    while (num % 5 == 0) {
      num /= 5;
      a[i].second++;
    }
    a[i].first = min(a[i].first, c);   // viac ako 9 nebudeme potrebovat
    a[i].second = min(a[i].second, c); // viac ako 9 nebudeme potrebovat
    for (int c2 = -c; c2 <= c; c2++) {
      for (int c5 = -c; c5 <= c; c5++) {
        if (a[i].first + c2 >= 0 && a[i].second + c5 >= 0) {
          good += f[c2 + c][c5 + c];
        }
      }
    }
    f[a[i].first + c][a[i].second + c]++;
  }
  cout << good << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  // kazde cislo zo vstupu sa da zapisat
  // ako x/(2^a*5^b)
  // Kazde x mozme upravit do takehoto tvaru pomocou gcd.
  // Nech b[i] = 10^9 * a[i]. potom a[i] = b[i]/(2^9*5^9)
  // Zaujimaju nas len mocniny 2 a 5 v b.
  vector<pair<ll, ll>> a(n);
  multiset<pair<ll, ll>> m;
  const ll c = 9;
  ll good = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    int b = -1;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '.') {
        b = j;
        break;
      }
    }
    if (b == -1) {
      s.push_back(b);
      b = s.size() - 1;
    }
    s.resize(b + 10, '0');
    s.erase(s.begin() + b);
    ll num = stoll(s);
    // cout << num << endl;
    a[i].first = -c;
    a[i].second = -c;
    while (num % 2 == 0) {
      num /= 2;
      a[i].first++;
    }
    while (num % 5 == 0) {
      num /= 5;
      a[i].second++;
    }
    a[i].first = min(a[i].first, c);   // viac ako 9 nebudeme potrebovat
    a[i].second = min(a[i].second, c); // viac ako 9 nebudeme potrebovat
    for (int c2 = -c; c2 <= c; c2++) {
      for (int c5 = -c; c5 <= c; c5++) {
        if (a[i].first + c2 >= 0 && a[i].second + c5 >= 0) {
          good += m.count(make_pair(c2, c5));
        }
      }
    }
    m.insert(a[i]);
  }
  cout << good << "\\n";
  return 0;
}