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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author https://github.com/rishabhdeepsingh/competitive
 */

// Created by White Knife on 15/04/20.
// doot diddly donger cuckerino Hahahahahah

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC target("popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

// @formatter:off
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
#define sim template <typename T
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<T>(0) x 1, debug &>::type operator<<(T i)
sim > struct rge {
  T b, e;
};
sim > rge<T> rang(T i, T j) { return rge<T>{i, j}; }
sim > auto dud(T *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) {
    cerr << boolalpha << i;
    ris;
  }
  eni(==) { ris << rang(all(i)); }
  sim, typename U dor(pair<U, T> d) {
    ris << "(" << d.first << ", " << d.second << ")";
  }
  sim dor(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
  }
#else
  sim dor(const T &) { ris; }
#endif
};
#define pr(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;
using ull = unsigned long long;
using ld = long double;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;

using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
template <typename T>
using ordered_set =
    tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

const ld PI = acos(-1);

template <typename T> inline T mini(T &x, T y) { return x = min(x, y); }
template <typename T> inline T maxi(T &x, T y) { return x = max(x, y); }
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
template <typename T> T rand(T x, T y) { return rand() % (y - x + 1) + x; }
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T> T gcd(T x, T y) {
  return (y == T(0)) ? x : gcd(y, x % y);
}
// @formatter:on

const long long MOD7 = 1000000007;
const long long MOD9 = 1000000009;
const long long MODF = 998244353;

long long MOD = MOD7;

template <class T> inline T add(T &x, T y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  return x;
}

template <class T> inline T sub(T &x, T y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}

template <typename T> T mul(T x, T y) {
  x %= MOD, y %= MOD;
  return (x * (long long)y) % MOD;
}

template <typename T, typename U> T power(T base, U exp, T mod) {
  T res = 1;
  while (exp > 0) {
    if (exp & 1) {
      res = mul(res, base);
    }
    base = mul(base, base);
    exp >>= 1;
  }
  return res % mod;
}

template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> T power(const T &a, long long b) {
  if (b == 0) {
    return 1;
  }
  if ((b & 1) == 0) {
    T res = power(a, b >> 1);
    return res * res;
  } else {
    return power(a, b - 1) * a;
  }
}

template <typename T> T factorial(int n) {
  T result = 1;
  for (int i = 2; i <= n; i++) {
    result *= i;
  }
  return result;
}

template <typename T> std::vector<T> factorials(int length) {
  std::vector<T> result(length);
  if (length > 0) {
    result[0] = 1;
  }
  for (int i = 1; i < length; i++) {
    result[i] = result[i - 1] * i;
  }
  return result;
}

template <typename T> std::vector<T> powers(T base, int length) {
  std::vector<T> result(length);
  if (length > 0) {
    result[0] = 1;
  }
  for (int i = 1; i < length; i++) {
    result[i] = result[i - 1] * base;
  }
  return result;
}

struct Sieve {
  int maxn;

public:
  std::vector<long long> spf;
  std::vector<long long> primes;
  std::vector<long long> mobius;
  std::vector<long long> phi;
  std::vector<bool> isPrime;

  Sieve(int _n = 2e6 + 7) : maxn(_n + 1) {
    spf.resize(maxn, 1);
    isPrime.resize(maxn, true);
    primes.clear();

    isPrime[0] = isPrime[1] = false;

    for (long long i = 2; i < maxn; ++i) {
      if (isPrime[i]) {
        primes.push_back(i);
        spf[i] = i;
      }
      for (long long j = 0; j < (int)primes.size() && (i * primes[j]) < maxn &&
                            primes[j] <= spf[i];
           ++j) {
        isPrime[i * primes[j]] = false;
        spf[i * primes[j]] = primes[j];
      }
    }
  }

  void genPhi() {
    phi.resize(maxn + 1, 0);
    phi[1] = 1;
    for (int i = 2; i <= maxn; i++)
      phi[i] = i - 1;

    for (int i = 2; i <= maxn; i++)
      for (int j = 2 * i; j <= maxn; j += i)
        phi[j] -= phi[i];
  }

  void genMobius() {
    mobius.clear();
    mobius.resize(maxn, 0);
    mobius[1] = 1;
    for (ll i = 2; i < maxn; ++i) {
      if (spf[i / spf[i]] == spf[i]) {
        mobius[i] = 0;
      } else {
        mobius[i] = -mobius[i / spf[i]];
      }
    }
  }

  long long nextPrime(long long x, bool strict = false) {
    auto it = upper_bound(primes.begin(), primes.end(), x);
    if (!strict and isPrime[x])
      return x;
    next(it);
    return *it;
  }

  long long numDivisors(long long val) {
    auto div = primeFactors(val);
    long long ans = 1;
    for (const auto &p : div) {
      ans *= p.second + 1;
    }
    return ans;
  }

  vector<pll> divisorPair(long long x) {
    vector<pll> res;
    for (long long i = 1; i * i <= x; i++) {
      if (x % i == 0) {
        if (x / i == i) {
          res.emplace_back(i, i);
        } else {
          res.emplace_back(i, x / i);
        }
      }
    }
    return res;
  }

  map<long long, long long> primeFactors(ll val) {
    if (val >= maxn)
      return primeFactorBrute(val);
    map<long long, long long> fac;
    while (val > 1) {
      int pf = spf[val];
      ll cnt = 0;
      while (val % pf == 0) {
        cnt++;
        val /= pf;
      }
      fac[pf] = cnt;
    }
    return fac;
  }

private:
  map<long long, long long> primeFactorBrute(ll val) {
    map<long long, long long> mp;
    for (ll i = 2; i * i <= val; i += 2) {
      while (val % i == 0) {
        mp[i]++;
        val /= i;
      }
    }
    if (val > 2) {
      mp[val]++;
    }
    return mp;
  }

  set<long long> divisorsBrute(long long val) {
    set<long long> res;
    for (long long i = 1; i * i <= val; i++) {
      if (val % i == 0) {
        if ((val / i) == i) {
          res.insert(i);
        } else {
          res.insert(i);
          res.insert(val / i);
        }
      }
    }
    return res;
  }
};

class DSumOfDivisors {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int n;
    cin >> n;
    Sieve sieve(n);
    ll ans = 0;
    for (ll i = 1; i <= n; ++i) {
      ans += i * sieve.numDivisors(i);
    }
    cout << ans << '\\n';
  }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  DSumOfDivisors solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author https://github.com/rishabhdeepsingh/competitive
 */

// Created by White Knife on 15/04/20.
// doot diddly donger cuckerino Hahahahahah

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC target("popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

// @formatter:off
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
#define sim template <typename T
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<T>(0) x 1, debug &>::type operator<<(T i)
sim > struct rge {
  T b, e;
};
sim > rge<T> rang(T i, T j) { return rge<T>{i, j}; }
sim > auto dud(T *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) {
    cerr << boolalpha << i;
    ris;
  }
  eni(==) { ris << rang(all(i)); }
  sim, typename U dor(pair<U, T> d) {
    ris << "(" << d.first << ", " << d.second << ")";
  }
  sim dor(rge<T> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
      *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
  }
#else
  sim dor(const T &) { ris; }
#endif
};
#define pr(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;
using ull = unsigned long long;
using ld = long double;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;

using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
template <typename T>
using ordered_set =
    tree<T, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;

const ld PI = acos(-1);

template <typename T> inline T mini(T &x, T y) { return x = min(x, y); }
template <typename T> inline T maxi(T &x, T y) { return x = max(x, y); }
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
template <typename T> T rand(T x, T y) { return rand() % (y - x + 1) + x; }
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template <typename T> T gcd(T x, T y) {
  return (y == T(0)) ? x : gcd(y, x % y);
}
// @formatter:on

const long long MOD7 = 1000000007;
const long long MOD9 = 1000000009;
const long long MODF = 998244353;

long long MOD = MOD7;

template <class T> inline T add(T &x, T y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  return x;
}

template <class T> inline T sub(T &x, T y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}

template <typename T> T mul(T x, T y) {
  x %= MOD, y %= MOD;
  return (x * (long long)y) % MOD;
}

template <typename T, typename U> T power(T base, U exp, T mod) {
  T res = 1;
  while (exp > 0) {
    if (exp & 1) {
      res = mul(res, base);
    }
    base = mul(base, base);
    exp >>= 1;
  }
  return res % mod;
}

template <typename T> T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
    T t = m / a;
    m -= t * a;
    swap(a, m);
    u -= t * v;
    swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> T power(const T &a, long long b) {
  if (b == 0) {
    return 1;
  }
  if ((b & 1) == 0) {
    T res = power(a, b >> 1);
    return res * res;
  } else {
    return power(a, b - 1) * a;
  }
}

template <typename T> T factorial(int n) {
  T result = 1;
  for (int i = 2; i <= n; i++) {
    result *= i;
  }
  return result;
}

template <typename T> std::vector<T> factorials(int length) {
  std::vector<T> result(length);
  if (length > 0) {
    result[0] = 1;
  }
  for (int i = 1; i < length; i++) {
    result[i] = result[i - 1] * i;
  }
  return result;
}

template <typename T> std::vector<T> powers(T base, int length) {
  std::vector<T> result(length);
  if (length > 0) {
    result[0] = 1;
  }
  for (int i = 1; i < length; i++) {
    result[i] = result[i - 1] * base;
  }
  return result;
}

struct Sieve {
  int maxn;
  map<long long, map<long long, long long>> memo;

public:
  std::vector<long long> spf;
  std::vector<long long> primes;
  std::vector<long long> mobius;
  std::vector<long long> phi;
  std::vector<bool> isPrime;

  Sieve(int _n = 2e6 + 7) : maxn(_n + 1) {
    spf.resize(maxn, 1);
    isPrime.resize(maxn, true);
    primes.clear();

    isPrime[0] = isPrime[1] = false;

    for (long long i = 2; i < maxn; ++i) {
      if (isPrime[i]) {
        primes.push_back(i);
        spf[i] = i;
      }
      for (long long j = 0; j < (int)primes.size() && (i * primes[j]) < maxn &&
                            primes[j] <= spf[i];
           ++j) {
        isPrime[i * primes[j]] = false;
        spf[i * primes[j]] = primes[j];
      }
    }
  }

  void genPhi() {
    phi.resize(maxn + 1);
    phi[0] = 0;
    phi[1] = 1;
    for (int i = 2; i <= maxn; i++)
      phi[i] = i - 1;

    for (int i = 2; i <= maxn; i++)
      for (int j = 2 * i; j <= maxn; j += i)
        phi[j] -= phi[i];
  }

  void genMobius() {
    mobius.clear();
    mobius.resize(maxn, 0);
    mobius[1] = 1;
    for (ll i = 2; i < maxn; ++i) {
      if (spf[i / spf[i]] == spf[i]) {
        mobius[i] = 0;
      } else {
        mobius[i] = -mobius[i / spf[i]];
      }
    }
  }

  long long nextPrime(long long x, bool strict = false) {
    auto it = upper_bound(primes.begin(), primes.end(), x);
    if (!strict and isPrime[x])
      return x;
    next(it);
    return *it;
  }

  long long numDivisors(long long val) {
    auto div = primeFactorPair(val);
    long long ans = 1;
    for (const auto &p : div) {
      ans *= p.second + 1;
    }
    return ans;
  }

  set<long long> divisorsBrute(long long val) {
    set<long long> res;
    for (long long i = 1; i * i <= val; i++) {
      if (val % i == 0) {
        if ((val / i) == i) {
          res.insert(i);
        } else {
          res.insert(i);
          res.insert(val / i);
        }
      }
    }
    return res;
  }

  vector<pll> divisorPair(long long x) {
    vector<pll> res;
    for (long long i = 1; i * i <= x; i++) {
      if (x % i == 0) {
        if (x / i == i) {
          res.emplace_back(i, i);
        } else {
          res.emplace_back(i, x / i);
        }
      }
    }
    return res;
  }

  map<long long, long long> primeFactorPair(ll val) {
    if (memo.count(val))
      return memo[val];
    if (val >= maxn)
      return primeFactorPairBrute(val);
    map<long long, long long> factors;
    while (val > 1) {
      int pf = spf[val];
      ll cnt = 0;
      while (val % pf == 0) {
        cnt++;
        val /= pf;
      }
      factors[pf] = cnt;
    }
    return memo[val] = factors;
  }

  map<long long, long long> primeFactorMap(ll x) {
    if (x > maxn)
      return primeFactorMapBrute(x);
    map<long long, long long> ret;
    while (x > 1) {
      ret[spf[x]]++;
      x /= spf[x];
    }
    return ret;
  }

private:
  map<long long, long long> primeFactorMapBrute(ll val) {
    map<long long, long long> mp;
    for (ll i = 2; i * i <= val; i += 2) {
      while (val % i == 0) {
        mp[i]++;
        val /= i;
      }
    }
    if (val > 2) {
      mp[val]++;
    }
    return mp;
  }

  map<long long, long long> primeFactorPairBrute(ll val) {
    map<long long, long long> mp;
    for (long long i = 2; i * i <= val; ++i) {
      if (val % i == 0) {
        mp[i]++;
        val /= i;
      }
    }
    if (val > 1)
      mp[val]++;
    return mp;
  }
};

class DSumOfDivisors {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    int n;
    cin >> n;
    Sieve sieve(n);
    ll ans = 0;
    for (ll i = 1; i <= n; ++i) {
      ans += i * sieve.numDivisors(i);
    }
    cout << ans << '\\n';
  }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  DSumOfDivisors solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}