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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define _GLIBCXX_DEBUG   // check []
#define DIVISOR 1000000007

using namespace std;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pII;

template <typename T> void cins(vector<T> &arr) {
  for (T &e : arr)
    cin >> e;
}

#ifdef DEBUG
#define debug(fmt, ...) printf("[debug: %s] " fmt, __func__, __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif

int main(void) {
  int num;
  cin >> num;
  vector<int> primes(num + 1);
  for (int i = 1; i <= num; i++) {
    primes[i] = i;
  }

  vector<int> cnt(num + 1, 1);
  for (int i = 2; i <= num; i++) {
    if (primes[i] == 1)
      continue;
    int p = primes[i];
    for (int j = i; j <= num; j += i) {
      int m = 0;
      while (1) {
        if (primes[j] % p)
          break;
        primes[j] /= p;
        m++;
      }
      cnt[j] *= (m + 1);
    }
  }
  int64_t sum = 0;
  for (int i = 1; i <= num; i++) {
    sum += (int64_t)i * cnt[i];
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define _GLIBCXX_DEBUG   // check []
#define DIVISOR 1000000007

using namespace std;
typedef pair<int, int> pii;
typedef pair<int64_t, int64_t> pII;

template <typename T> void cins(vector<T> &arr) {
  for (T &e : arr)
    cin >> e;
}

#ifdef DEBUG
#define debug(fmt, ...) printf("[debug: %s] " fmt, __func__, __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif

int main(void) {
  int num;
  cin >> num;
  vector<char> is_prime(num + 1, 1);
  for (int i = 2; i <= num; i++) {
    int limit = num / i;
    if (!is_prime[i])
      continue;
    for (int j = 2; j <= limit; j++) {
      is_prime[i * j] = 0;
    }
  }
  debug("%d ok\\n", num);
  int64_t sum = 0;
  for (int i = 1; i <= num; i++) {
    int val = i;
    map<int, int> cnt;
    int prime = 2;
    while (val > 1) {
      if (is_prime[val]) {
        cnt[val]++;
        break;
      }
      if (val % prime) {
        prime++;
        continue;
      }
      val /= prime;
      cnt[prime]++;
    }
    int res = 1;
    for (pii p : cnt) {
      res *= (p.second + 1);
    }
    sum += (int64_t)i * res;
  }
  cout << sum << endl;
  return 0;
}