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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
typedef long long ll;
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define per(i, n, m) for (int i = (n - 1); i >= (m); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
int main(void) {
  ll N, val, z = 0, len, sum = 0;
  cin >> N;
  vector<ll> data;
  for (ll i = 2; i <= sqrt(N); i++) {
    if (N % i == 0) {
      data.push_back(i);
      data.push_back(N / i);
    }
  }
  sort(all(data));
  len = sz(data);
  for (ll i = 0; i < len; i++) {
    int count = 0;
    ll x;
    for (ll j = 0; j < i; j++) {
      if (count > 0 && data[i] % data[j] == 0 && data[j] % x != 0) {
        count++;
      }
      if (count == 0 && data[i] % data[j] == 0) {
        x = data[j];
        count++;
      }
    }
    if ((count == 0 || count == 1) && N / data[i] >= 1 && N % data[i] == 0) {
      sum++;
      N /= data[i];
    }
  }
  if (N > 1 && len == 0)
    sum++;
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
typedef long long ll;
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define per(i, n, m) for (int i = (n - 1); i >= (m); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
int main(void) {
  ll N, val, z = 0, len, sum = 0;
  cin >> N;
  val = N;
  vector<ll> data;
  for (ll i = 2; i <= val; i++) {
    if (val % i == 0) {
      int count = 0;
      ll x;
      for (ll j = 0; j < sz(data); j++) {
        if (count > 0 && i % data[j] == 0 && data[j] % x != 0) {
          count++;
        }
        if (count == 0 && i % data[j] == 0) {
          x = data[j];
          count++;
        }
      }
      if (count == 0 || count == 1) {
        data.push_back(i);
        val /= i;
      }
    }
    if (sz(data) == 0 && i >= sqrt(N))
      i = val + 1;
  }
  len = sz(data);
  if (N > 1 && len == 0)
    sum++;
  else
    sum = len;
  cout << sum << endl;
}