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

// 2020年4月17日現在

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

template <class... A> void prints() { cout << "\\n"; }
template <class... A> void prints_rest() { cout << "\\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &...rest) {
  cout << " " << first;
  prints_rest(rest...);
}
template <class T, class... A> void prints(const T &first, const A &...rest) {
  cout << first;
  prints_rest(rest...);
}
template <class T, class... A> void printd(const T &first, const A &...rest) {
  cout << fixed << setprecision(15) << first;
  prints_rest(rest...);
}

namespace {
// printElems(&a[0], &a[n]);　みたいな感じで使う。
void printElems(const int *begin, const int *end) {
  for (const int *p = begin; p != end; ++p) {
    std::cout << *p << ' ';
  }
  std::cout << std::endl;
}
} // namespace

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define souatari(i, j, n) rep1(i, n - 1) rep(j, i)
#define souatari1(i, j, n)                                                     \\
  for (int i = 2; i < n + 1; i++)                                              \\
  rep1(j, i - 1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int, 3> arr3;
typedef long double D;

const ll llinf = numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const ll BIG = 1LL << 60;

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  rep1(i, n) {
    ll y = n / i;
    ans += y * (y + 1) * i / 2;
  }
  prints(ans);

  return 0;
}


Error: TLE

Fixed submission:

// CPP program for finding number of divisor
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

// program for finding no. of divisors
int divCount(int n) {
  // sieve method for prime calculation
  bool hash[n + 1];
  memset(hash, true, sizeof(hash));
  for (int p = 2; p * p < n; p++)
    if (hash[p] == true)
      for (int i = p * 2; i < n; i += p)
        hash[i] = false;

  // Traversing through all prime numbers
  int total = 1;
  for (int p = 2; p <= n; p++) {
    if (hash[p]) {

      // calculate number of divisor
      // with formula total div =
      // (p1+1) * (p2+1) *.....* (pn+1)
      // where n = (a1^p1)*(a2^p2)....
      // *(an^pn) ai being prime divisor
      // for n and pi are their respective
      // power in factorization
      int count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }
  return total;
}

// driver program
int main() {
  int n, i, ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    ans += (i * divCount(i));
  }
  cout << ans;
  return 0;
}