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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long int
#define mod 1000000007
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define REP(i, a, b) for (int i = a, i <= b; i++)
#define MAXN 200001

bool prime[1000001];
int fact[1000001];
void sieve() {
  memset(prime, false, sizeof(prime));
  for (int i = 0; i <= 1000000; i++)
    fact[i] = i;
  for (int i = 2; i <= 1000000; i++) {
    if (!prime[i]) {
      for (int j = i * i; j <= 1000000; j = j + i) {
        prime[j] = true;
        fact[j] = i;
      }
    }
  }
}

int divisorSum(int n) {
  map<int, int> mp;
  while (n != 1) {
    mp[fact[n]]++;
    n = n / fact[n];
  }
  int product = 1;
  for (auto x : mp) {
    product *= (x.S + 1);
  }
  return product;
}

int32_t main() {
  // fast;
  sieve();
  int n;
  cin >> n;
  int sum = 1;

  for (int i = 2; i <= n - 1; i++) {
    sum += divisorSum(i);
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define endl "\\n"
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long int
#define mod 1000000007
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define REP(i, a, b) for (int i = a, i <= b; i++)
#define MAXN 200001

bool prime[1000001];
void sieve() {
  memset(prime, false, sizeof(prime));
  for (int i = 2; i <= 1000000; i++) {
    if (!prime[i]) {
      for (int j = i * i; j <= 1000000; j = j + i) {
        prime[j] = true;
      }
    }
  }
}

int getVal(int n) {
  int total = 1;
  for (int p = 2; p <= n; p++) {
    if (!prime[p]) {

      int count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }

  // cout<<total<<endl;
  return total % mod;
}

int32_t main() {
  fast;
  sieve();
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n - 1; i++) {
    sum += (getVal(i)) % mod;
    sum %= mod;
  }

  cout << sum << endl;
  return 0;
}