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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007

bool check_prime(long long n) {
  int flag = 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (n == 1)
    return false;
  else if (flag == 0 || n == 2 || n == 3) {
    return true;
  } else {
    return false;
  }
}

int BE(int x, int n, int m) // function to calculate x raise to power n modulo m
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x % m;
    x = x * x % m;
    n = n / 2;
  }
  return result;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________

  int n;
  cin >> n;
  /*sieve();
  vector<int>p = getFactorization(n);
  int cnt = 0;
  int x;
  //um <int, int>mt;
  set<int>s;
  for (int i = 0; i < p.size(); i++)
  {
          s.insert(p[i]);
  }
  for (int y : s)
  {
          x = y;
          while (n % x == 0)
          {
                  n = n / x;
                  //cout << x << en;
                  cnt++;
                  x *= y;
          }
          //mt[p[i]]++;
  }
  //cout << p.size() << en;
  cout << cnt  << en;*/
  int cnt = 0;
  // Print the number of 2s that divide n
  int m = n;
  int x = 2;
  while (n % 2 == 0) {
    n = n / 2;
    cnt++;
  }

  int var = 1;
  int result = 0;
  // cout << cnt << en;
  rep(i, 1, cnt) {
    if (((i + 1) * i / 2) > cnt) {
      break;
    } else {
      result++;
    }
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    // x = i;
    cnt = 0;
    // var = 1;
    //  While i divides n, print i and divide n
    while (n % i == 0) {
      n = n / i;
      cnt++;
    }
    rep(j, 1, cnt) {
      if (((j + 1) * j / 2) > cnt) {
        break;
      } else {
        result++;
      }
    }
  }
  if (check_prime(n)) {
    result++;
  }
  // cout << primeFactors(n, n) << en;
  cout << result << en;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007

bool check_prime(long long n) {
  int flag = 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (n == 1)
    return false;
  else if (flag == 0 || n == 2 || n == 3) {
    return true;
  } else {
    return false;
  }
}

int BE(int x, int n, int m) // function to calculate x raise to power n modulo m
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x % m;
    x = x * x % m;
    n = n / 2;
  }
  return result;
}

#define MAXN 100009

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-_______________________________________________

  int n;
  cin >> n;
  sieve();
  vector<int> p = getFactorization(n);
  int cnt = 0;
  int x;
  // um <int, int>mt;
  set<int> s;
  for (int i = 0; i < p.size(); i++) {
    s.insert(p[i]);
  }
  for (int y : s) {
    x = y;
    while (n % x == 0) {
      n = n / x;
      // cout << x << en;
      cnt++;
      x *= y;
    }
    // mt[p[i]]++;
  }
  // cout << p.size() << en;
  cout << cnt << en;
  return 0;
}