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

#include <bits/stdc++.h>
using namespace std;

#pragma region Macros
using ll = long long;
#define int ll
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define SORT(name) sort(name.begin(), name.end())
#define RSORT(name)                                                            \\
  SORT(name);                                                                  \\
  reverse(all(name));
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> inline void print(T x) { cout << x << '\\n'; }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
#pragma endregion

#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

const int MOD = 1000000007;
const int MAX_N = 10000010;

// [1, n] の範囲の各値について、約数の個数を vector に格納して返す
// ret[i]: i の約数の個数
// O(n log log n)
vector<int> DivCount(int n) {
  vector<int> ret(n + 1, 1);
  int cnt = 0;
  for (int i = 2; i <= n; ++i) {
    for (int j = i; j <= n; j += i) {
      ret[j]++;
      cnt++;
    }
  }
  return ret;
}

int N;

signed main() {
  IOS;

  cin >> N;

  int ans = 0;
  auto div = DivCount(N);
  for (int i = 1; i <= N; ++i) {
    ans += i * div[i];
  }
  print(ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#pragma region Macros
using ll = long long;
#define int ll
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define SORT(name) sort(name.begin(), name.end())
#define RSORT(name)                                                            \\
  SORT(name);                                                                  \\
  reverse(all(name));
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> inline void print(T x) { cout << x << '\\n'; }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const double DINF = std::numeric_limits<double>::infinity();
#pragma endregion

#if 1
#define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

// 高速素因数分解(Osa_k 法)
// 構築 O(N log log N), 導出 O(log N)
class SmartSieve {
public:
  SmartSieve(ll l, ll r) { Init(l, r); }
  // [L, R) の範囲を素因数分解することにする
  // 事前準備 O(N log log N)
  void Init(ll l, ll r) {
    m_L = l;
    m_R = r;
    m_M = sqrt(m_R) + 1;
    m_Small.resize(m_M);
    iota(m_Small.begin(), m_Small.end(), 0);
    m_Large.resize(m_R - m_L);
    m_Aux.assign(m_R - m_L, 1);

    for (ll i = 2; i * i < m_R; ++i) {
      if (m_Small[i] < i) {
        continue;
      }
      m_Small[i] = i;
      for (ll j = i * i; j < m_M; j += i) {
        if (m_Small[j] == j) {
          m_Small[j] = i;
        }
      }
      for (ll j = (m_L + i - 1) / i * i; j < m_R; j += i) {
        ll k = j;
        do {
          // m_Aux[j - m_L] > m_M で判定した方がいいかも？
          // j / m_Aux[j - m_L] < m_M の方がいい？（割り算したくない）
          if (m_Aux[j - m_L] * m_Aux[j - m_L] > m_R) {
            break;
          }

          m_Large[j - m_L].push_back(i);
          m_Aux[j - m_L] *= i;
          k /= i;
        } while (k % i == 0);
      }
    }
  }

  // n の素因数分解結果を返す O(log N)
  // first: 素因数 second: それが登場する回数
  map<ll, int> Factor(ll n) {
    assert(m_L <= n && n < m_R);
    map<ll, int> res;
    for (auto &tmp : m_Large[n - m_L]) {
      res[tmp]++;
    }
    n /= m_Aux[n - m_L];
    if (n >= m_M) {
      // この場合，n は素数となることが示せる（はず）
      // n*n >= R だとオーバーフローしそう？
      res[n]++;
      return res;
    }
    while (n > 1) {
      res[m_Small[n]]++;
      n /= m_Small[n];
    }
    return res;
  }

public:
  ll m_L, m_R, m_M;
  vector<int> m_Small;        // 小さい篩
  vector<vector<ll>> m_Large; // 大きい篩
  vector<ll> m_Aux;           // m_Aux[i] := m_Large[i] の素因数の積
};

const int MOD = 1000000007;
const int MAX_N = 10000010;

int N;

signed main() {
  IOS;

  cin >> N;

  SmartSieve ss(1, MAX_N);
  int ans = 0;
  for (int i = 1; i <= N; ++i) {
    auto res = ss.Factor(i);
    int cand = 1;
    for (auto kv : res) {
      cand *= (kv.second + 1);
    }
    ans += i * cand;
  }
  print(ans);

  return 0;
}