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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long int ll;
const ll modP = 1000000007;
#define MAX_N 10000001
vector<map<int, int>> divisors;
vector<bool> isPrime(MAX_N, true);

ll myFunc(ll X, ll MAX_NUM) {
  ll Y = MAX_NUM / X;
  return Y * (Y + 1) * X / 2;
}

void init(int N) {
  for (int i = 2; i * i <= N; i++) {
    for (int j = 2; i * j <= N; j++) {
      isPrime[i * j] = false;
    }
  }
  map<int, int> zero;
  divisors.push_back(zero);
  map<int, int> one;
  divisors.push_back(one);
  for (int i = 2; i <= N; i++) {
    map<int, int> localMap;
    if (isPrime[i] == true) {
      localMap.insert(make_pair(i, 1));
      divisors.push_back(localMap);
    } else if (isPrime[i] == false) {
      for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
          int num1 = j;
          int num2 = i / j;
          for (auto iter1 = divisors[num1].begin();
               iter1 != divisors[num1].end(); iter1++) {
            if (divisors[num2].count(iter1->first) == 0) {
              localMap.insert(make_pair(iter1->first, iter1->second));
            } else {
              localMap.insert(make_pair(
                  iter1->first, iter1->second + divisors[num2][iter1->first]));
            }
          }
          for (auto iter2 = divisors[num2].begin();
               iter2 != divisors[num2].end(); iter2++) {
            if (divisors[num1].count(iter2->first) == 0) {
              localMap.insert(make_pair(iter2->first, iter2->second));
            }
          }
        }
      }
      divisors.push_back(localMap);
    }
  }
}

vector<ll> enum_divisors(ll N) {
  vector<ll> res;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.push_back(i);
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

ll divisorsNum(ll num) { return enum_divisors(num).size(); }

int main() {
  ll N;
  cin >> N;
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += myFunc(i, N);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long int ll;
const ll modP = 1000000007;

vector<ll> enum_divisors(ll N) {
  vector<ll> res;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.push_back(i);
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

ll divisorsNum(ll num) { return enum_divisors(num).size(); }

int main() {
  ll N;
  cin >> N;
  ll ans = 0;
  // initialize
  for (int i = 1; i <= N; i++) {
    // vector<ll> divisors = enum_divisors(i);
    ans += i * divisorsNum(i);
    // cout<<i<<" "<<divisorsNum(i)<<endl;
  }
  cout << ans << endl;

  /*ll N;
  cin>>N;
  const auto &res = enum_divisors(N);
  for(int i = 0; i < res.size(); i++) cout<<res[i]<<" ";
  cout<<endl;*/

  return 0;
}