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

#include <bits/stdc++.h>
#include <cmath>
#include <string>
#define pb push_back
#define x first
#define y second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
// void __print(long long x) {cerr << x;}
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

// look for  all edge cases
// search for a pattern

const int nax = 1e6 + 2;
int prime[nax];

void sieve() {

  for (int i = 1; i < nax; i++) {
    prime[i] = i;
  }

  for (int i = 2; i < nax; i++) {
    if (prime[i] == i) {
      for (int j = 2 * i; j < nax; j += i) {
        prime[j] = i;
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

  sieve();

  int n;
  cin >> n;

  int ans = 0;
  for (int i = n - 1; i > 1; i--) {
    // int cnt = 0;
    int val = 1;
    // unordered_set<int> s;
    int p = i;
    while (p != 1) {
      int cnt = 0;
      int ben = prime[p];
      while (p != 1 && p % ben == 0) {
        p = p / ben;
        cnt += 1;
      }
      val = val * (cnt + 1);
    }

    ans = ans + val;
    // debug(cnt , i , ans);
  }
  ans += 1;
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <string>
#define pb push_back
#define x first
#define y second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
// void __print(long long x) {cerr << x;}
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

// look for  all edge cases
// search for a pattern

const int nax = 1e6 + 2;
int prime[nax];

void sieve() {

  for (int i = 1; i < nax; i++) {
    prime[i] = i;
  }

  for (int i = 2; i < nax; i++) {
    if (prime[i] == i) {
      for (int j = 2 * i; j < nax; j += i) {
        prime[j] = min(i, prime[j]);
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

  sieve();

  int n;
  cin >> n;

  int ans = 0;
  for (int i = n - 1; i >= 1; i--) {
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        ans += 2;
      }
      if (j * j == i) {
        ans -= 1;
      }
    }
  }
  // ans += 1;
  cout << ans;

  return 0;
}