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

#include <bits/stdc++.h>
#define ll long long int
#define ss second
#define ff first
#define Mem(x, val) memset(x, val, sizeof x)
#define pb push_back
#define all(v) v.begin(), v.end()
#define unik(v) v.erase(unique(v.begin(), v.end()), v.end())
#define cerr(x) cerr << __LINE__ << " th Line : " << #x << " = " << x << "\\n"
#define debug(a, b)                                                            \\
  cerr << #a << " = " << a << "   |   " << #b << " = " << b << '\\n'
#define endl '\\n'
#define SZ(a) ((int)a.size())
using namespace std;
typedef pair<int, int> pi32;
typedef pair<ll, ll> pi64;
typedef vector<int> vi32;
typedef vector<ll> vi64;
typedef vector<pi32> vp32;
typedef vector<pi64> vp64;
const ll mod = 1e9 + 7;
const int MX = 1e7 + 7;

// ll prime[MX+10];

// void sieve(){
// prime[0]=prime[1]=1;
// for(ll i=4;i<=MX;i+=2)prime[i]=1;
// for(ll i=3;i*i<=MX;i+=2){
// if(!prime[i]){
// for(ll j=i*i;j<=MX;j+=i)prime[j]=1;
//}
//}
//}

ll a[MX + 10];

void sieve1() {
  a[0] = a[1] = 1;
  for (ll i = 2; i <= MX; i++) {
    a[i] = i;
  }

  for (ll i = 4; i <= MX; i += 2)
    a[i] = 2;
  for (ll i = 3; i * i <= MX; i += 2) {
    if (a[i] == i) {
      for (ll j = i * i; j <= MX; j += i) {
        if (a[j] == j)
          a[j] = i;
      }
    }
  }
}

ll Prime_Factor(ll n) {
  vi64 ans;
  while (n != 1) {
    ans.pb(a[n]);
    n /= a[n];
  }
  map<ll, ll> mp;
  for (ll x : ans)
    mp[x]++;
  ll tmp = 1;
  for (auto it : mp)
    tmp *= (it.ss + 1);
  return tmp;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // sieve();
  sieve1();
  ll n;
  cin >> n;

  ll sum = 1;
  for (ll i = 2; i <= n; i++) {
    ll x = Prime_Factor(i) * i * 1LL;
    sum += x;
  }
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ss second
#define ff first
#define Mem(x, val) memset(x, val, sizeof x)
#define pb push_back
#define all(v) v.begin(), v.end()
#define unik(v) v.erase(unique(v.begin(), v.end()), v.end())
#define cerr(x) cerr << __LINE__ << " th Line : " << #x << " = " << x << "\\n"
#define debug(a, b)                                                            \\
  cerr << #a << " = " << a << "   |   " << #b << " = " << b << '\\n'
#define endl '\\n'
#define SZ(a) ((int)a.size())
using namespace std;
typedef pair<int, int> pi32;
typedef pair<ll, ll> pi64;
typedef vector<int> vi32;
typedef vector<ll> vi64;
typedef vector<pi32> vp32;
typedef vector<pi64> vp64;
const ll mod = 1e9 + 7;
const int MX = 1e7 + 7;

ll prime[MX + 10];

void sieve() {
  prime[0] = prime[1] = 1;
  for (ll i = 4; i <= MX; i += 2)
    prime[i] = 1;
  for (ll i = 3; i * i <= MX; i += 2) {
    if (!prime[i]) {
      for (ll j = i * i; j <= MX; j += i)
        prime[j] = 1;
    }
  }
}

ll a[MX + 10];

void sieve1() {
  a[0] = a[1] = 1;
  for (ll i = 2; i <= MX; i++) {
    a[i] = i;
  }

  for (ll i = 4; i <= MX; i += 2)
    a[i] = 2;
  for (ll i = 3; i * i <= MX; i += 2) {
    if (a[i] == i) {
      for (ll j = i * i; j <= MX; j += i) {
        if (a[j] == j)
          a[j] = i;
      }
    }
  }
}

vi64 Prime_Factor(ll n) {
  vi64 ans;
  while (n != 1) {
    ans.pb(a[n]);
    n /= a[n];
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sieve();
  sieve1();
  ll n;
  cin >> n;

  ll sum = 1;
  for (ll i = 2; i <= n; i++) {
    if (!prime[i])
      sum += 2LL * i;
    else {
      vi64 p = Prime_Factor(i);
      map<ll, ll> fr;
      for (ll x : p)
        fr[x]++;
      ll tmp = 1;
      if (SZ(fr) == 1)
        tmp = SZ(p) + 1;
      else {
        for (auto it : fr)
          tmp *= (it.ss + 1);
      }
      // cerr(tmp);
      tmp = tmp * i;

      sum += tmp;
    }
  }
  cout << sum << endl;
  return 0;
}