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

#include <bits/stdc++.h>
using namespace std;

int multiplicityOfDivisor(long long number, long long divisor) {
  int multiplicity = 0;
  while (number % divisor == 0) {
    number /= divisor;
    ++multiplicity;
  }
  return multiplicity;
}

void solve() {
  int N;
  cin >> N;
  vector<long long> numbers(N);
  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;
    const int pos = s.find(".");

    if (pos == -1) {
      numbers[i] = stoll(s) * 1000000000LL;
    } else {
      // Before "."
      long long before = stoll(s.substr(0, pos)) * 1000000000LL;
      // After "."
      string after_string = s.substr(pos + 1);
      long long after = 0;
      long long multiplier = 100000000LL;
      for (int j = 0; j < after_string.size(); ++j) {
        after += (after_string[j] - '0') * multiplier;
        multiplier /= 10;
      }
      numbers[i] = before + after;
    }
  }

  // numberOfElementsWithMultiplicity[i][j] = number of elements
  // that have exactly i as multiplicity for 2, and j as multiplicity for 5.
  long long answer = 0;
  vector<vector<int>> numberOfElementsWithMultiplicity(50, vector<int>(20, 0));
  for (int i = 0; i < N; ++i) {
    const int multiplicity_2 = multiplicityOfDivisor(numbers[i], 2);
    const int multiplicity_5 = multiplicityOfDivisor(numbers[i], 5);

    // Other element should have at least this multiplicity.
    const int target_multiplicity_2 = 18 - multiplicity_2;
    const int target_multiplicity_5 = 18 - multiplicity_5;

    // Query.
    for (int j = max(0, target_multiplicity_2); j < 50; ++j) {
      for (int k = max(0, target_multiplicity_5); k < 20; ++k) {
        answer += numberOfElementsWithMultiplicity[j][k];
      }
    }

    // Update "hash table".
    ++numberOfElementsWithMultiplicity[multiplicity_2][multiplicity_5];
  }
  cout << answer << endl;
}

int main() {
  // generateTestCases();
  // return 0;
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int multiplicityOfDivisor(long long number, int divisor) {
  int multiplicity = 0;
  while (number % divisor == 0) {
    number /= divisor;
    ++multiplicity;
  }
  return multiplicity;
}

void solve() {
  int N;
  cin >> N;
  vector<long long> numbers(N);
  for (int i = 0; i < N; ++i) {
    double number_decimal;
    cin >> number_decimal;
    numbers[i] = 1000000000LL * number_decimal;
  }

  // numberOfElementsWithMultiplicity[i][j] = number of elements
  // that have exactly i as multiplicity for 2, and j as multiplicity for j.
  long long answer = 0;
  vector<vector<int>> numberOfElementsWithMultiplicity(50, vector<int>(20, 0));
  for (int i = 0; i < N; ++i) {
    const int multiplicity_2 = multiplicityOfDivisor(numbers[i], 2);
    const int multiplicity_5 = multiplicityOfDivisor(numbers[i], 5);

    // Other element should have at least this multiplicity.
    const int target_multiplicity_2 = 18 - multiplicity_2;
    const int target_multiplicity_5 = 18 - multiplicity_5;

    // Query.
    for (int j = target_multiplicity_2; j < 50; ++j) {
      for (int k = target_multiplicity_5; k < 20; ++k) {
        answer += numberOfElementsWithMultiplicity[j][k];
      }
    }

    // Update "hash table".
    ++numberOfElementsWithMultiplicity[multiplicity_2][multiplicity_5];
  }
  cout << answer << endl;
}

// void generateTestCase() {
//   freopen("testcase.txt", "w", stdout);
//   const int N = 200000;
//   cout << N << endl;
//   for (int i = 0; i < N; ++i) {
//     cout << 0.4 << endl;
//   }
// }

int main() { solve(); }