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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  int N;
  string S;
  long long p;
  long long q;
  long long r;
  long long aa;

  cin >> N;
  vector<int> a0(5);
  vector<int> a1(11);
  vector<int> a2(N);
  vector<int> a5(N);
  vector<vector<int>> x(25, vector<int>(25));

  rep(i, N) {
    rep(j, 5) a0.at(j) = 0;
    rep(j, 10) a1.at(j) = 0;
    cin >> S;
    p = 0;
    q = -1;
    aa = 0;
    r = S.size();
    rep(j, r) {
      if (S.at(j) == '.')
        q = 0;
      else if (q < 0) {
        a0.at(p) = S.at(j) - '0';
        p++;
      } else {
        a1.at(q) = S.at(j) - '0';
        q++;
      }
    }
    if (p > 0) {
      rep(j, p) {
        aa *= 10;
        aa += a0.at(j);
      }
    }
    rep(j, 9) {
      aa *= 10;
      aa += a1.at(j);
    }
    p = 0;
    rep(j, 20) {
      if (aa % 2 > 0)
        break;
      aa /= 2;
      p++;
    }
    a2.at(i) = p;
    p = 0;
    rep(j, 20) {
      if (aa % 5 > 0)
        break;
      aa /= 5;
      p++;
    }
    a5.at(i) = p;
  }

  rep(i, 25) {
    rep(j, 25) { x.at(i).at(j) = 0; }
  }
  rep(i, N) { x.at(a2.at(i)).at(a5.at(i))++; }

  p = 0;
  rep(i1, 21) {
    rep(i2, 21) {
      if (i1 + i2 >= 18) {
        rep(j1, 21) {
          rep(j2, 21) {
            if (j1 + j2 >= 18) {
              q = x.at(i1).at(j1);
              r = x.at(i2).at(j2);
              p += q * r;
              if ((i1 == i2) && (j1 == j2))
                p -= q;
            }
          }
        }
      }
    }
  }
  cout << p / 2 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  int N;
  string S;
  int p;
  int q;
  int r;
  long long aa;
  int p2;
  int p5;
  int p7;

  cin >> N;
  vector<int> a0(5);
  vector<int> a1(11);
  vector<int> a2(N);
  vector<int> a5(N);

  p2 = 0;
  p5 = 0;

  rep(i, N) {
    rep(j, 5) a0.at(j) = 0;
    rep(j, 10) a1.at(j) = 0;
    cin >> S;
    p = 0;
    q = -1;
    aa = 0;
    r = S.size();
    rep(j, r) {
      if (S.at(j) == '.')
        q = 0;
      else if (q < 0) {
        a0.at(p) = S.at(j) - '0';
        p++;
      } else {
        a1.at(q) = S.at(j) - '0';
        q++;
      }
    }
    if (p > 0) {
      rep(j, p) {
        aa *= 10;
        aa += a0.at(j);
      }
    }
    rep(j, 9) {
      aa *= 10;
      aa += a1.at(j);
    }
    p = 0;
    rep(j, 20) {
      if (aa % 2 > 0)
        break;
      aa /= 2;
      p++;
    }
    a2.at(i) = p;
    if (p > p2)
      p2 = p;
    p = 0;
    rep(j, 20) {
      if (aa % 5 > 0)
        break;
      aa /= 5;
      p++;
    }
    a5.at(i) = p;
    if (p > p5)
      p5 = p;
  }
  p7 = 0;
  rep(i, N) if (p7 < a2.at(i) + a5.at(i)) p7 = a2.at(i) + a5.at(i);

  p = 0;
  rep(i, N - 1) {
    if (a2.at(i) < 18 - p2)
      continue;
    if (a5.at(i) < 18 - p5)
      continue;
    if (a2.at(i) + a5.at(i) < 36 - p7)
      continue;
    rep2(j, i + 1, N) {
      if (a2.at(i) + a2.at(j) >= 18) {
        if (a5.at(i) + a5.at(j) >= 18)
          p++;
      }
    }
  }
  cout << p << endl;
  return 0;
}