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

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

ll get_ans(map<pair<ll, ll>, ll> &m, ll a, ll b) {
  ll cnt = 0;
  for (ll i = a; i <= 34; i++) {
    for (ll j = b; j <= 34; j++) {
      if (m.find({i, j}) != m.end()) {
        cnt += m[{i, j}];
      }
    }
  }
  return cnt;
}

void update_v(vector<pair<ll, ll>> &v, ll idx, ll val) {
  ll a = 0, b = 0, cnt = 0;
  //	cout<<val<<endl;
  while (val > 0 && val % 2 == 0) {
    cnt++;
    val /= 2;
  }

  a = cnt;
  while (val > 0 && val % 5 == 0) {
    b++;
    val /= 5;
  }

  v[idx] = {a - 9, b - 9};
  //	cout<<v[idx].ff<<" "<<v[idx].ss<<endl;
  return;
}

int main() {
  io;
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v(n);
  long double k = 1e9;
  ll ans = 0;
  map<pair<ll, ll>, ll> m;
  for (ll i = 0; i < n; i++) {
    long double val;
    cin >> val;
    ll t = llround(val * k);
    //	cout<<val<<" "<<t<<endl;
    update_v(v, i, t);
    m[{v[i].ff, v[i].ss}]++;
  }

  map<pair<ll, ll>, ll>::iterator it1;
  map<pair<ll, ll>, ll>::iterator it2;

  for (it1 = m.begin(); it1 != m.end(); it1++) {
    for (it2 = m.begin(); it2 != m.end(); it2++) {
      pair<pair<ll, ll>, ll> p1 = *it1, p2 = *it2;
      if (p1 < p2) {
        if (p1.ff.ff + p2.ff.ff >= 0 && p1.ff.ss + p2.ff.ss >= 0) {
          ans += p2.ss * p1.ss;
        }
      } else if (p1 == p2) {
        if (p1.ff.ff + p2.ff.ff >= 0 && p1.ff.ss + p2.ff.ss >= 0) {
          ans += ((p1.ss * (p2.ss - 1)) / 2);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

ll get_ans(map<pair<ll, ll>, ll> &m, ll a, ll b) {
  ll cnt = 0;
  for (ll i = a; i <= 34; i++) {
    for (ll j = b; j <= 34; j++) {
      if (m.find({i, j}) != m.end()) {
        cnt += m[{i, j}];
      }
    }
  }
  return cnt;
}

void update_v(vector<pair<ll, ll>> &v, ll idx, ll val) {
  ll a = 0, b = 0, cnt = 0;
  //	cout<<val<<endl;
  while (val > 0 && val % 2 == 0) {
    cnt++;
    val /= 2;
  }

  a = cnt;
  while (val > 0 && val % 5 == 0) {
    b++;
    val /= 5;
  }

  v[idx] = {a - 9, b - 9};
  //	cout<<v[idx].ff<<" "<<v[idx].ss<<endl;
  return;
}

int main() {
  io;
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v(n);
  long double k = 1e9;
  // cout<<n<<endl;
  for (ll i = 0; i < n; i++) {
    long double val;
    cin >> val;
    ll t = llround(val * k);
    //	cout<<val<<" "<<t<<endl;
    update_v(v, i, t);
  }

  ll ans = 0;
  map<pair<ll, ll>, ll> m;
  for (ll i = n - 1; i >= 0; i--) {
    ll mina = 0 - v[i].ff;
    ll minb = 0 - v[i].ss;
    ans += get_ans(m, mina, minb);
    //	cout<<mina<<" "<<minb<<" "<<ans<<endl;
    m[{v[i].ff, v[i].ss}]++;
  }
  cout << ans << endl;
  return 0;
}