Problem p02588 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

struct Data {
  int two, five, under;
};

int main() {
  int n;
  cin >> n;
  vector<string> avec(n);
  for (int i = 0; i < n; ++i)
    cin >> avec[i];

  vector<Data> Adata(n);
  // 各avecについてData変換
  for (int i = 0; i < n; ++i) {
    string ch = avec[i];
    int chlen = ch.length();
    bool flg = false;
    int cnt = 0;
    string no;
    for (int j = 0; j < chlen; ++j) {
      if (ch[j] == '.') {
        flg = true;
        continue;
      }
      if (flg)
        cnt++;
      no.push_back(ch[j]);
    }
    // 後ろから0が何個あるか
    int nolen = no.length();
    int zero = 0;
    for (int j = nolen - 1; j >= 0; --j) {
      if (no[j] == '0')
        zero++;
      else
        break;
    }
    cnt -= zero; // cnt調節
    no = no.substr(0, nolen - zero);
    ll num = stoll(no);
    // 小数点を左にずらすことができた
    int tch = 0, fch = 0;
    // 素因数2, 5の数をそれぞれ見つける
    while (1) {
      if (num % 2 == 0) {
        tch++;
        num /= 2;
      } else
        break;
    }
    while (1) {
      if (num % 5 == 0) {
        fch++;
        num /= 5;
      } else
        break;
    }

    Adata[i].two = tch;
    Adata[i].five = fch;
    Adata[i].under = cnt;
    Adata[i].two -= Adata[i].under;
    Adata[i].five -= Adata[i].under;
    Adata[i].under = min(Adata[i].two, Adata[i].five);
  }

  vector<vector<ll>> tfmap(20, vector<ll>(20, 0));
  for (int i = 0; i < n; ++i) {
    int tch = Adata[i].two + 9;
    int fch = Adata[i].five + 9;
    tfmap[min(19, tch)][min(19, fch)]++;
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int trest = max(0, 0 - Adata[i].two + 9);
    int frest = max(0, 0 - Adata[i].five + 9);
    int rmt = min(19, Adata[i].two + 9);
    int rmf = min(19, Adata[i].five + 9);
    tfmap[rmt][rmf]--;

    for (int j = trest; j < 20; ++j) {
      for (int k = frest; k < 20; ++k) {
        ans += tfmap[j][k];
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

struct Data {
  int two, five, under;
};

int main() {
  int n;
  cin >> n;
  vector<string> avec(n);
  for (int i = 0; i < n; ++i)
    cin >> avec[i];

  vector<Data> Adata(n);
  // 各avecについてData変換
  for (int i = 0; i < n; ++i) {
    string ch = avec[i];
    int chlen = ch.length();
    bool flg = false;
    int cnt = 0;
    string no;
    for (int j = 0; j < chlen; ++j) {
      if (ch[j] == '.') {
        flg = true;
        continue;
      }
      if (flg)
        cnt++;
      no.push_back(ch[j]);
    }
    // 後ろから0が何個あるか
    int nolen = no.length();
    int zero = 0;
    for (int j = nolen - 1; j >= 0; --j) {
      if (no[j] == '0')
        zero++;
      else
        break;
    }
    cnt -= zero; // cnt調節
    no = no.substr(0, nolen - zero);
    ll num = stoll(no);
    // 小数点を左にずらすことができた
    int tch = 0, fch = 0;
    // 素因数2, 5の数をそれぞれ見つける
    while (1) {
      if (num % 2 == 0) {
        tch++;
        num /= 2;
      } else
        break;
    }
    while (1) {
      if (num % 5 == 0) {
        fch++;
        num /= 5;
      } else
        break;
    }

    Adata[i].two = tch;
    Adata[i].five = fch;
    Adata[i].under = cnt;
    Adata[i].two -= Adata[i].under;
    Adata[i].five -= Adata[i].under;
    Adata[i].under = min(Adata[i].two, Adata[i].five);
  }

  /*for(int i = 0; i < n; ++i) {
      cout << Adata[i].two << " " << Adata[i].five << endl;
  }*/

  vector<vector<int>> tfmap(20, vector<int>(20, 0));
  for (int i = 0; i < n; ++i) {
    int tch = Adata[i].two + 9;
    int fch = Adata[i].five + 9;
    tfmap[min(19, tch)][min(19, fch)]++;
  }

  /*for(int i = 0; i < 20; ++i) {
      for(int j = 0; j < 20; ++j) {
          cout << tfmap[i][j] << " ";
      }
      cout << endl;
  }*/

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int trest = 0 - Adata[i].two + 9;
    int frest = 0 - Adata[i].five + 9;

    for (int j = trest; j < 20; ++j) {
      for (int k = frest; k < 20; ++k) {
        if (j == Adata[i].two + 9 && k == Adata[i].five + 9) {
          ans += tfmap[j][k] - 1;
        } else
          ans += tfmap[j][k];
      }
    }
    int rmt = min(19, Adata[i].two + 9);
    int rmf = min(19, Adata[i].five + 9);
    // cout << "rm " << rmt << " : " << rmf << endl;
    tfmap[rmt][rmf]--;
    // cout << "ans " << ans << endl;
  }
  cout << ans << endl;
}