Problem p02660 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <set>
#include <vector>

using namespace std;

set<int64_t> ans;

void decomPrime(int num) {

  int n = 2;
  while (num > n * n) {
    while (num % n == 0) {
      num /= n;
      cout << n << ",";
      ans.insert(n);
    }
    n++;
  }
  cout << num << endl;
  ans.insert(num);
}

vector<pair<int64_t, int>> decomPrime2(int64_t num) {

  vector<pair<int64_t, int>> ret;

  int n = 2;
  while (sqrt(num) >= n) {
    int a = 0;
    while (num % n == 0) {
      num /= n;
      a++;
      // cout << n << ",";
    }
    if (a != 0)
      ret.emplace_back(n, a);

    n++;
  }
  // cout << num << endl;

  if (num != 1)
    ret.emplace_back(num, 1);
  // ans.insert(num);

  return ret;
}

bool isPri(int64_t num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

vector<int64_t> vec;
bool element(int64_t i) {
  for (auto a : vec) {
    if (i == a)
      return true;
  }
  return false;
}

int64_t func(int64_t N) {

  int64_t z;
  for (int64_t i = 2; i <= N; i++) {
    if (isPri(i)) {
      z = 1;
      while (z <= N) {
        z *= i;
        if (N % z == 0) {
          if (element(z)) {
            continue;
          }
          return z;
        }
      }
    }
  }
  return 0;
}

int main() {
  int64_t N;
  int64_t z;
  cin >> N;

  // int count = 0;
  /*
  while ((z = func(N)) != 0)
  {
          count++;
          N /= z;
          vec.push_back(z);
  }
  cout << count;

  */
  int ans = 0;
  for (auto p : decomPrime2(N)) {
    // cout << p.first << "," << p.second << endl;
    int n = p.second;
    int x = 1;
    while (n >= x) {
      ans++;
      n -= x;

      x++;
    }
  }

  cout << ans;

  // cin >> N;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <set>
#include <vector>

using namespace std;

set<int64_t> ans;

void decomPrime(int num) {

  int n = 2;
  while (num > n * n) {
    while (num % n == 0) {
      num /= n;
      cout << n << ",";
      ans.insert(n);
    }
    n++;
  }
  cout << num << endl;
  ans.insert(num);
}

vector<pair<int64_t, int>> decomPrime2(int64_t num) {

  vector<pair<int64_t, int>> ret;

  int n = 2;
  while (num >= n * n) {
    int a = 0;
    while (num % n == 0) {
      num /= n;
      a++;
      // cout << n << ",";
    }
    if (a != 0)
      ret.emplace_back(n, a);

    n++;
  }
  // cout << num << endl;

  if (num != 1)
    ret.emplace_back(num, 1);
  // ans.insert(num);

  return ret;
}

bool isPri(int64_t num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false; // 偶数はあらかじめ除く

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      // 素数ではない
      return false;
    }
  }

  // 素数である
  return true;
}

vector<int64_t> vec;
bool element(int64_t i) {
  for (auto a : vec) {
    if (i == a)
      return true;
  }
  return false;
}

int64_t func(int64_t N) {

  int64_t z;
  for (int64_t i = 2; i <= N; i++) {
    if (isPri(i)) {
      z = 1;
      while (z <= N) {
        z *= i;
        if (N % z == 0) {
          if (element(z)) {
            continue;
          }
          return z;
        }
      }
    }
  }
  return 0;
}

int main() {
  int64_t N;
  int64_t z;
  cin >> N;

  // int count = 0;
  /*
  while ((z = func(N)) != 0)
  {
          count++;
          N /= z;
          vec.push_back(z);
  }
  cout << count;

  */
  int ans = 0;
  for (auto p : decomPrime2(N)) {
    // cout << p.first << "," << p.second << endl;
    int n = p.second;
    int x = 1;
    while (n >= x) {
      ans++;
      n -= x;

      x++;
    }
  }

  cout << ans;

  // cin >> N;

  return 0;
}