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

// #pragma comment(linker, "/STACK:134217728")

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef long long Int;
typedef pair<int, int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for (i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (i = (a)-1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair

int f(PII a, PII b) {
  Int m = Int(1e9);
  Int mm = m * m;

  Int x = Int(a.first) * Int(b.second) % m;
  Int y = Int(b.first) * Int(a.second) % m;
  Int z = Int(a.second) * Int(b.second) % mm;
  return (x * m + y * m + z) % mm == 0 ? 1 : 0;
}

void solve(int test) {
  int n;
  cin >> n;
  int i, j, k, l;
  int cnt[47][47];
  CLEAR(cnt, 0);
  FOR(i, 0, n) {
    double d;
    cin >> d;
    d *= 1e9;
    Int a = Int(d + 0.5);
    int cnt2 = 0;
    int cnt5 = 0;
    while (a % 2 == 0) {
      a /= 2;
      ++cnt2;
    }
    while (a % 5 == 0) {
      a /= 5;
      ++cnt5;
    }
    ++cnt[cnt2][cnt5];
  }

  Int res = 0;
  FOR(i, 0, 47)
  FOR(j, 0, 47)
  if (cnt[i][j] != 0) {
    if (i + i >= 18 && j + j >= 18)
      res += Int(cnt[i][j]) * Int(cnt[i][j] - 1) / 2;
    FOR(k, 0, 47)
    FOR(l, 0, 47)
    if (PII(i, j) > PII(k, l) && i + k >= 18 && j + l >= 18)
      res += Int(cnt[i][j]) * Int(cnt[k][l]);
  }

  cout << res << endl;
}

int main() {
  int T = 1, t;
  // cin >> T;
  FOR(t, 0, T) { solve(t); }
  return 0;
}


Error: TLE

Fixed submission:

// #pragma comment(linker, "/STACK:134217728")

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

typedef long long Int;
typedef pair<int, int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for (i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (i = (a)-1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair

int f(PII a, PII b) {
  Int m = Int(1e9);
  Int mm = m * m;

  Int x = Int(a.first) * Int(b.second) % m;
  Int y = Int(b.first) * Int(a.second) % m;
  Int z = Int(a.second) * Int(b.second) % mm;
  return (x * m + y * m + z) % mm == 0 ? 1 : 0;
}

void solve(int test) {
  int n;
  cin >> n;
  vector<PII> v(n);
  int i, j;
  FOR(i, 0, n) {
    string s;
    cin >> s;
    int pos = 0;
    while (pos != SIZE(s) && s[pos] != '.')
      ++pos;
    if (pos == SIZE(s))
      s.push_back('.');
    s[pos] = ' ';
    while (SIZE(s) - pos < 10)
      s.push_back('0');
    stringstream ss(s);
    ss >> v[i].first >> v[i].second;
  }

  int res = 0;
  FOR(i, 0, n)
  FOR(j, 0, i)
  if (f(v[i], v[j]) != 0)
    ++res;

  cout << res << endl;
}

int main() {
  int T = 1, t;
  // cin >> T;
  FOR(t, 0, T) { solve(t); }
  return 0;
}