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

#include <bits/stdc++.h>

using namespace std;

std::vector<pair<unsigned long long, unsigned long long>>
prime_factors(unsigned long long number) {
  std::vector<pair<unsigned long long, unsigned long long>> result;
  unsigned long long root = static_cast<unsigned long long>(std::sqrt(number));
  int count = 0;
  int leng = 0;
  int flag = 0;
  while (number % 2 == 0) {
    if (count == 0) {
      result.push_back(pair<unsigned long long, unsigned long long>(2, 0));
      count += 1;
      flag = 1;
    }
    result[leng].second += 1;
    number = number / 2;
  }
  if (flag == 1) {
    leng++;
    flag = 0;
  }

  for (unsigned int i = 3; i <= root; i += 2) {
    count = 0;
    while (number % i == 0) {
      if (count == 0) {
        result.push_back(pair<unsigned long long, unsigned long long>(i, 0));
        count += 1;
        flag = 1;
      }
      result[leng].second += 1;
      number = number / i;
    }
    if (flag == 1) {
      leng++;
      flag = 0;
    }
  }

  if (number > 2) {
    result.push_back(pair<unsigned long long, unsigned long long>(number, 1));
  }

  return result;
}

int main(void) {
  unsigned long long N;
  cin >> N;
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  int result = 0;
  vector<pair<unsigned long long, unsigned long long>> l = prime_factors(N);

  /*
  for(int i = 0;i < (int)l.size();i++) {
      cout << l[i].first << " " << l[i].second << endl;
  }
  */

  for (int i = 0; i < (int)l.size(); i++) {
    for (unsigned long long dif = 1;; dif++) {
      // cout << dif << " " << l[i] << endl;
      if (l[i].second >= dif) {
        l[i].second = l[i].second - dif;
        result++;
      } else {
        break;
      }
    }
  }
  cout << result << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

std::vector<pair<unsigned long long, unsigned long long>>
prime_factors(unsigned long long number) {
  std::vector<pair<unsigned long long, unsigned long long>> result;
  unsigned long long root = static_cast<unsigned long long>(std::sqrt(number));
  int count = 0;
  int leng = 0;
  while (number % 2 == 0) {
    if (count == 0) {
      result.push_back(pair<unsigned long long, unsigned long long>(2, 0));
    }
    result[leng].second += 1;
    number = number / 2;
    count += 1;
  }

  for (unsigned int i = 3; i <= root; i += 2) {
    count = 0;
    leng++;
    while (number % i == 0) {
      if (count == 0) {
        result.push_back(pair<unsigned long long, unsigned long long>(i, 0));
        count += 1;
      }
      result[leng].second += 1;
      number = number / i;
    }
  }

  if (number > 2) {
    result.push_back(pair<unsigned long long, unsigned long long>(number, 1));
  }

  return result;
}

int main(void) {
  unsigned long long N;
  cin >> N;
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  int result = 0;
  vector<pair<unsigned long long, unsigned long long>> l = prime_factors(N);
  for (int i = 0; i < (int)l.size(); i++) {
    for (unsigned long long dif = 1;; dif++) {
      // cout << dif << " " << l[i] << endl;
      if (l[i].second >= dif) {
        l[i].second = l[i].second - dif;
        result++;
      } else {
        break;
      }
    }
  }
  cout << result << endl;

  return 0;
}