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

#include <bits/stdc++.h>
using namespace std;

typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef complex<double> com;

#define MOD 1000000007
#define PI 3.14159265359
#define endl '\\n'
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define unique_sort(x)                                                         \\
  (sort(x.begin(), x.end()),                                                   \\
   x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fast_io()                                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define dbg(x) cout << #x << " = " << x << '\\n'
#define dbg2(x, y)                                                             \\
  cout << #x << " = " << x << " & " << #y << " = " << y << endl;
const int MAX = 10000007;

int factor[MAX] = {0};

void generatePrimeFactors() {
  factor[1] = 1;

  for (int i = 2; i < MAX; i++)
    factor[i] = i;

  for (int i = 4; i < MAX; i += 2)
    factor[i] = 2;

  for (int i = 3; i * i < MAX; i++) {

    if (factor[i] == i) {

      for (int j = i * i; j < MAX; j += i) {

        if (factor[j] == j)
          factor[j] = i;
      }
    }
  }
}

ll calculateNoOFactors(int n) {
  if (n == 1)
    return 1;

  ll ans = 1;

  int dup = factor[n];

  int c = 1;

  int j = n / factor[n];

  while (j != 1) {

    if (factor[j] == dup)
      c += 1;

    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }

    j = j / factor[j];
  }

  ans = ans * (c + 1);

  return ans;
}
void test() {
  generatePrimeFactors();

  int n;
  ll ans = 0;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    ans += i * calculateNoOFactors(i);
    // cout<<ans<<endl;
  }

  cout << ans << endl;
}

int main() {
  fast_io();
  cout << fixed << setprecision(15);
  int t = 1;

  // cin>>t;

  for (int i = 0; i < t; i++) {
    test();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef complex<double> com;

#define MOD 1000000007
#define PI 3.14159265359
#define endl '\\n'
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define unique_sort(x)                                                         \\
  (sort(x.begin(), x.end()),                                                   \\
   x.resize(distance(x.begin(), unique(x.begin(), x.end()))))
#define fast_io()                                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define dbg(x) cout << #x << " = " << x << '\\n'
#define dbg2(x, y)                                                             \\
  cout << #x << " = " << x << " & " << #y << " = " << y << endl;

ull divCount(ull n, bool hash[]) {
  ull total = 1;
  for (ull p = 2; p <= n; p++) {
    if (hash[p]) {

      ull count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }
  return total;
}
void test() {
  ull n;
  cin >> n;

  bool hash[n + 1];
  memset(hash, true, sizeof(hash));

  for (ull p = 2; p * p < n; p++)
    if (hash[p] == true)
      for (ull i = p * 2; i < n; i += p)
        hash[i] = false;

  unsigned long long ans = 0;

  for (ull i = 1; i <= n; i++) {
    ans += i * divCount(i, hash);
  }

  cout << ans << endl;
}

int main() {
  fast_io();
  cout << fixed << setprecision(15);
  int t = 1;

  // cin>>t;

  for (int i = 0; i < t; i++) {
    test();
  }
  return 0;
}