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

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define black_tree                                                             \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define pair_black_tree                                                        \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>
#define gp __gnu_pbds
#define INF 1000000000
#define MOD 1000000007
#define MAX 1000000
#define endl '\\n'
#define ll long long
#define ld long double
#define lli long long int
#define ull unsigned long long
#define ulli unsigned long long int
#define pb push_back
#define pf push_front
#define ook order_of_key
#define fbo find_by_order
#define np next_permutation
#define mp make_pair
#define eb emplace_back
#define mae max_element
#define mie min_element
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
using namespace std;
using namespace gp;
ll n, ans;
ll check(ll n) {
  ll ans = 1, cnt;
  for (int i = 2; i <= sqrt(n); i++) {
    cnt = 1;
    while (n % i == 0) {
      n /= i;
      cnt++;
    }
    ans *= cnt;
  }
  if (n > 1) {
    ans *= 2;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    ans += check(i);
  }
  cout << ans;
}

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define black_tree                                                             \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define pair_black_tree                                                        \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>
#define gp __gnu_pbds
#define INF 1000000000
#define MOD 1000000007
#define MAX 1000000
#define endl '\\n'
#define ll long long
#define ld long double
#define lli long long int
#define ull unsigned long long
#define ulli unsigned long long int
#define pb push_back
#define pf push_front
#define ook order_of_key
#define fbo find_by_order
#define np next_permutation
#define mp make_pair
#define eb emplace_back
#define mae max_element
#define mie min_element
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
using namespace std;
using namespace gp;
ll n, ans, primes[MAX];
ll check(ll n) {
  ll ans = 1, cnt;
  for (int i = 2; i <= n; i++) {
    if (primes[i] == 0) {
      cnt = 1;
      while (n % i == 0) {
        n /= i;
        cnt++;
      }
      ans *= cnt;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 2; i * i < n; i++) {
    if (primes[i] == 0) {
      for (int j = i * 2; j < n; j += i) {
        primes[j] = 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    ans += check(i);
  }
  cout << ans;
}