Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

vector<pair<int, int>> vp;
int N;
int dp[10099][40];

pair<int, int> find_repr(string &s) {
  int n = s.size();
  ll num = 0;
  int di = -1;

  for (int i = 0; i < n; i++) {
    if (s[i] == '.') {
      di = i;
      continue;
    }
    num = num * 10 + int(s[i] - '0');
  }

  int j = (di == -1 ? 0 : n - di - 1);
  for (; j < 9; j++) {
    num *= 10;
  }

  // debug(num);

  int x = -9, y = -9;
  while (num % 2 == 0) {
    x++;
    num /= 2;
  }
  while (num % 5 == 0) {
    y++;
    num /= 5;
  }

  return {x, y};
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(10) << fixed;

  string s;
  cin >> N;
  rep(i, N) {
    cin >> s;
    vp.pb(find_repr(s));
  }

  sort(vp.begin(), vp.end());

  // for(auto x : vp) {
  //   cout<<x.F<<","<<x.S<<" ";
  // }

  for (int i = vp.size() - 1; i >= 0; i--) {
    dp[i][vp[i].S + 10] = 1;
    for (int j = 0; j <= 20; j++)
      dp[i][j] += dp[i + 1][j];
  }

  ll ans = 0;
  for (int i = 0; i < vp.size(); i++) {
    int x = -vp[i].F, y = -vp[i].S;
    int j = lower_bound(vp.begin(), vp.end(), mp(x, -999)) - vp.begin();
    if (x <= 0)
      j = i + 1;
    for (int k = 10 + y; k <= 20; k++)
      ans += dp[j][k];
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

vector<pair<int, int>> vp;
int N;
int dp[200099][40];

pair<int, int> find_repr(string &s) {
  int n = s.size();
  ll num = 0;
  int di = -1;

  for (int i = 0; i < n; i++) {
    if (s[i] == '.') {
      di = i;
      continue;
    }
    num = num * 10 + int(s[i] - '0');
  }

  int j = (di == -1 ? 0 : n - di - 1);
  for (; j < 9; j++) {
    num *= 10;
  }

  // debug(num);

  int x = -9, y = -9;
  while (num % 2 == 0) {
    x++;
    num /= 2;
  }
  while (num % 5 == 0) {
    y++;
    num /= 5;
  }

  return {x, y};
}

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(10) << fixed;

  string s;
  cin >> N;
  rep(i, N) {
    cin >> s;
    vp.pb(find_repr(s));
  }

  sort(vp.begin(), vp.end());

  // for(auto x : vp) {
  //   cout<<x.F<<","<<x.S<<" ";
  // }

  for (int i = vp.size() - 1; i >= 0; i--) {
    dp[i][vp[i].S + 10] = 1;
    for (int j = 0; j <= 20; j++)
      dp[i][j] += dp[i + 1][j];
  }

  ll ans = 0;
  for (int i = 0; i < vp.size(); i++) {
    int x = -vp[i].F, y = -vp[i].S;
    int j = lower_bound(vp.begin(), vp.end(), mp(x, -999)) - vp.begin();
    if (x <= 0)
      j = i + 1;
    for (int k = 10 + y; k <= 20; k++)
      ans += dp[j][k];
  }
  cout << ans << endl;

  return 0;
}