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

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
typedef long long int lli;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i, a, b) for (long long i = a; i < b; i++)
#define REPR(i, a, b) for (int i = a; i >= b; i--)
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
#define PI 3.14159265358979323846
const ll MAXN = 10000001;

struct LessSecond {
  template <typename T, typename U>
  bool operator()(const std::pair<T, U> &x, const std::pair<T, U> &y) const {
    return x.second < y.second;
  }
};

ll power(ll a, ll b, ll m) {
  ll ans = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  return power(a, m - 2, m);
}

ll spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
vector<bool> prime(MAXN, false);
ll s[MAXN];
ll c = 1;

void sieveOfEratosthenes() {
  // Create a boolean array "prime[0..n]" and
  // initialize all entries in it as false.

  // Initializing smallest factor equal to 2
  // for all the even numbers
  for (ll i = 2; i <= MAXN; i += 2)
    s[i] = 2;

  // For odd numbers less then equal to n
  for (ll i = 3; i <= MAXN; i += 2) {
    if (prime[i] == false) {
      // s(i) for a prime is the number itself
      s[i] = i;

      // For all multiples of current prime number
      for (ll j = i; j * i <= MAXN; j += 2) {
        if (prime[i * j] == false) {
          prime[i * j] = true;

          // i is the smallest prime factor for
          // number "i*j".
          s[i * j] = i;
        }
      }
    }
  }
}

void generatePrimeFactors(ll N) {
  // s[i] is going to store smallest prime factor
  // of i.

  ll curr = s[N]; // Current prime factor of N
  ll cnt = 1;     // Power of current prime factor

  // Printing prime factors and their powers
  while (N > 1) {
    N /= s[N];

    // N is now N/s[N].  If new N als has smallest
    // prime factor as curr, increment power
    if (curr == s[N]) {
      cnt++;
      continue;
    }

    c *= (cnt + 1);

    // Update current prime factor as s[N] and
    // initializing count as 1.
    curr = s[N];
    cnt = 1;
  }
}

int main() {

  fastIO;
  int test = 1;
  // cin >> test;
  while (test--) {

    sieveOfEratosthenes();
    ll n;
    cin >> n;
    ll ans = 0;
    REP(i, 1, n + 1) {
      c = 1;
      generatePrimeFactors(i);
      ans += i * c;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
typedef long long int lli;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i, a, b) for (long long i = a; i < b; i++)
#define REPR(i, a, b) for (int i = a; i >= b; i--)
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define endl "\\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
#define PI 3.14159265358979323846
const ll MAX = 10000000;

struct LessSecond {
  template <typename T, typename U>
  bool operator()(const std::pair<T, U> &x, const std::pair<T, U> &y) const {
    return x.second < y.second;
  }
};

ll power(ll a, ll b, ll m) {
  ll ans = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  return power(a, m - 2, m);
}

vector<ll> divisor[MAX + 1];
ll c = 0;
// Sieve based approach to pre-
// calculate all divisors of number
void sieve() {
  for (ll i = 1; i <= MAX; ++i) {
    for (ll j = i; j <= MAX; j += i)
      divisor[j].push_back(i);
  }
}

void printDivisor(ll n) {
  for (auto &div : divisor[n])
    c++;
}

int main() {

  fastIO;
  int test = 1;
  // cin >> test;
  while (test--) {

    sieve();
    ll n;
    cin >> n;
    ll ans = 0;
    REP(i, 1, n + 1) {
      c = 0;
      printDivisor(i);
      ans += i * c;
    }
    cout << ans << endl;
  }
  return 0;
}