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

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
template <class T> using Graph = vector<vector<T>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int n;
  cin >> n;

  vector<Int> d{
      93668,    201177,   313925,   430062,   548725,   669422,   791780,
      915548,   1040630,  1166750,  1293935,  1421996,  1550902,  1680578,
      1810953,  1942002,  2073666,  2205986,  2338763,  2472113,  2606008,
      2740315,  2875085,  3010305,  3145958,  3281973,  3418435,  3555195,
      3692338,  3829833,  3967622,  4105787,  4244274,  4382991,  4522065,
      4661460,  4801042,  4940922,  5081120,  5221472,  5362138,  5503108,
      5644159,  5785585,  5927210,  6069000,  6211021,  6353304,  6495814,
      6638449,  6781292,  6924353,  7067636,  7211106,  7354711,  7498504,
      7642514,  7786669,  7931004,  8075504,  8220199,  8364997,  8510049,
      8655190,  8800484,  8945974,  9091590,  9237328,  9383292,  9529316,
      9675504,  9821898,  9968324,  10114972, 10261748, 10408621, 10555635,
      10702823, 10850034, 10997454, 11145096, 11292653, 11440411, 11588376,
      11736313, 11884447, 12032740, 12181098, 12329547, 12478206, 12626845,
      12775675, 12924562, 13073587, 13222720, 13372007, 13521286, 13670825,
      13820360};
  Int ans = 0;
  if (1 <= (n - 1) / 10000) {
    ans = d[((n - 1) / 10000) - 1];
  }
  for (Int i = ((n - 1) / 10000) * 10000 + 1; i < n; i++) {
    ans += divisor(i).size();
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
template <class T> using Graph = vector<vector<T>>;

const Ld pi = M_PI;
const Int MOD = 1000000007;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Int n;
  cin >> n;

  Int ans = 1;
  for (Int i = 2; i < n; i++) {
    auto v = divisor(i);
    ans += v.size();
  }

  cout << ans << endl;

  return 0;
}