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

#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;

  ll a[n];
  ll res = 0;
  map<pii, ll> mp;
  for (ll i = 0; i < n; i++) {

    string s;
    cin >> s;
    ll cnt = 0;
    ll sm = 0;
    ll flag = 0;

    for (ll j = 0; j < s.size(); j++) {
      if (s[j] == '.') {
        flag = 1;
        continue;
      }

      if (flag)
        cnt++;

      ll x = (ll)(s[j] - '0');

      sm *= 10;
      sm += x;
    }

    ll p, q;
    p = sm;
    q = 1;

    for (ll kk = 0; kk < cnt; kk++)
      q *= 10;

    ll p_2 = 0;
    ll p_5 = 0;

    ll q_2 = 0;
    ll q_5 = 0;

    while (p % 2 == 0) {
      p_2++;
      p /= 2;
    }

    while (p % 5 == 0) {
      p_5++;
      p /= 5;
    }

    while (q % 2 == 0) {
      q_2++;
      q /= 2;
    }

    while (q % 5 == 0) {
      q_5++;
      q /= 5;
    }

    ll del2 = q_2 - p_2;
    ll del5 = q_5 - p_5;

    ll ans = 0;

    for (ll j = del2; j <= 30; j++) {
      for (ll h = del5; h <= 9; h++) {
        ans += mp[{j, h}];
      }
    }

    res += ans;
    mp[{p_2 - q_2, p_5 - q_5}]++;
  }

  cout << res << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;

  ll a[n];
  ll res = 0;
  map<pii, ll> mp;
  for (ll i = 0; i < n; i++) {

    string s;
    cin >> s;
    ll cnt = 0;
    ll sm = 0;
    ll flag = 0;

    for (ll j = 0; j < s.size(); j++) {
      if (s[j] == '.') {
        flag = 1;
        continue;
      }

      if (flag)
        cnt++;

      ll x = (ll)(s[j] - '0');

      sm *= 10;
      sm += x;
    }

    ll p, q;
    p = sm;
    q = 1;

    for (ll kk = 0; kk < cnt; kk++)
      q *= 10;

    ll p_2 = 0;
    ll p_5 = 0;

    ll q_2 = 0;
    ll q_5 = 0;

    while (p % 2 == 0) {
      p_2++;
      p /= 2;
    }

    while (p % 5 == 0) {
      p_5++;
      p /= 5;
    }

    while (q % 2 == 0) {
      q_2++;
      q /= 2;
    }

    while (q % 5 == 0) {
      q_5++;
      q /= 5;
    }

    ll del2 = q_2 - p_2;
    ll del5 = q_5 - p_5;

    ll ans = 0;

    for (ll j = del2; j <= 30; j++) {
      for (ll h = del5; h <= 30; h++) {
        ans += mp[{j, h}];
      }
    }

    res += ans;
    mp[{p_2 - q_2, p_5 - q_5}]++;
  }

  cout << res << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}