Problem p02660 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
Author: Babin Dutta
*/

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
// #define endl "\n"
#define inf 1e18
#define mm 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll A, ll M) { return power(A, M - 2, M); }

/*
bool pp[1000001];

void func(){
        for(int i=2;i*i<=1000000;i++){
                if(pp[i]){
                        for(int j=i*i;j<=1000000;j+=i) pp[j] = false;
                }
        }
}
*/

vector<ll> v;

void solve() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  ll z = n;
  set<ll> s;
  ll m = 0;
  while (n % 2 == 0) {
    n /= 2;
    m++;
  }
  for (ll k = 1; k <= m; k++) {
    v.pb(pow(2, k));
  }
  for (ll i = 3; i * i <= n; i += 2) {
    m = 0;
    while (n % i == 0) {
      n /= i;
      m++;
    }
    for (ll k = 1; k <= m; k++) {
      v.pb(pow(i, k));
    }
  }
  if (n > 2)
    v.pb(n);
  sort(all(v));
  ll y = 1;
  while (z > 1) {
    for (ll i = 0; i < v.size(); i++) {
      if (v[i] < y)
        continue;
      if (z % v[i] == 0 && z > 1) {
        z /= v[i];
        s.insert(v[i]);
        y = v[i];
      }
    }
  }
  cout << s.size() << endl;
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  boost;
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++)
    solve();
  return 0;
}

Fixed submission:
/*
Author: Babin Dutta
*/

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
#define mp make_pair
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define f(i, n) for (int i = 0; i < n; i++)
#define ff(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
// #define endl "\n"
#define inf 1e18
#define mm 998244353

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll modInverse(ll A, ll M) { return power(A, M - 2, M); }

/*
bool pp[1000001];

void func(){
        for(int i=2;i*i<=1000000;i++){
                if(pp[i]){
                        for(int j=i*i;j<=1000000;j+=i) pp[j] = false;
                }
        }
}
*/

vector<ll> v;

void solve() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  ll z = n;
  set<ll> s;
  ll m = 0;
  while (n % 2 == 0) {
    n /= 2;
    m++;
  }
  for (ll k = 1; k <= m; k++) {
    v.pb(pow(2, k));
  }
  for (ll i = 3; i * i <= n; i += 2) {
    m = 0;
    while (n % i == 0) {
      n /= i;
      m++;
    }
    for (ll k = 1; k <= m; k++) {
      v.pb(pow(i, k));
    }
  }
  if (n > 2)
    v.pb(n);
  sort(all(v));
  ll y = 1;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] <= y)
      continue;
    if (z % v[i] == 0) {
      z /= v[i];
      s.insert(v[i]);
      y = v[i];
    }
  }
  cout << s.size() << endl;
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  boost;
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++)
    solve();
  return 0;
}