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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
#define fs first
#define re resize
#define sc second
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define ins insert
#define mod 1000000007
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\\n"
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll modexpo(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1 or b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return (a % mod * modexpo(a, b - 1) % mod) % mod;
  return modexpo(a % mod * a % mod, b / 2) % mod;
}
void printv(vll v) {
  for (auto &x : v) {
    cout << x << " ";
  }
  cout << endl;
}
string to_string1(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
ll to_integer(string s) {
  stringstream ss(s);
  ll x;
  ss >> x;
  return x;
}
#define tp9 1000000000
ll a, b, c, d;
void input() { cin >> a >> b >> c >> d; }
ld pi = 3.1415926535;
void printYes() { cout << "Yes\\n"; }
void printNo() { cout << "No\\n"; }
ll t, n, p;
string s;
const ll N = 10000001;
// ll spf[N+1];
// void fill_spf() {
//     memset(spf,0,sizeof spf);
//     for(ll i = 2 ; i  <= N ; i ++) {
//         if(spf[i] == 0) {
//             for(ll j = i ; j <= N ; j ++) {
//                 if(spf[j] == 0) {
//                     spf[j] = i ;
//                 }
//             }
//         }
//     }
// }
// ll f1(ll k) {
//     map<ll,ll>primefactors ;
//     ll i = k ;
//     while(spf[i]!=0) {
//         primefactors[spf[i]] ++ ;
//         i/=spf[i] ;
//     }
//     ll factors= 1 ;
//     for(auto p : primefactors) {
//         factors*=(1 + p.sc);
//     }
//     return factors;
// }
// ll f(ll k ) {
//     ll i = 2;
//     map<ll,ll>mp;
//     while(k%2==0) {
//         mp[2]++;
//         k = k / 2 ;
//     }
//     for( i = 3 ; i*i <= k ; i += 2) {
//         while(k%i==0){
//             mp[i]++;
//             k = k / i ;
//         }
//     }
//     if(k > 2) {
//         mp[k]++;
//     }
//     ll factors= 1 ;
//     for(auto p : mp) {
//         factors*=(1 + p.sc);
//     }
//     return factors;
// }
ll primeFactors(ll n) {
  // Print the number of 2s that divide n
  map<ll, ll> mp;
  while (n % 2 == 0) {
    // cout << 2 << " ";
    mp[2]++;
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (ll i = 3; i * i <= (n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      // cout << i << " ";
      mp[i]++;
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    // cout << n << " ";
    mp[n]++;
  ll ans = 1;
  for (auto p : mp) {
    ans *= (1 + p.sc);
  }
  return ans;
}
#define MAXN 10000002

// stores smallest prime factor for every number
ll spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
ll getFactorization(ll x) {
  map<ll, ll> mp;
  while (x != 1) {
    // ret.push_back(spf[x]);
    mp[spf[x]]++;
    x = x / spf[x];
  }
  ll ret = 1;
  for (auto p : mp) {
    ret *= (1 + p.sc);
  }
  return ret;
}
int main() {
  // fill_spf() ;
  sieve();
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll k = 1; k <= n; k++) {
    ans += k * getFactorization(k);
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
#define fs first
#define re resize
#define sc second
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define ins insert
#define mod 1000000007
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
  // use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\\n"
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll modexpo(ll a, ll b) {
  if (a == 0)
    return 0;
  if (a == 1 or b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b & 1)
    return (a % mod * modexpo(a, b - 1) % mod) % mod;
  return modexpo(a % mod * a % mod, b / 2) % mod;
}
void printv(vll v) {
  for (auto &x : v) {
    cout << x << " ";
  }
  cout << endl;
}
string to_string1(ll x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
ll to_integer(string s) {
  stringstream ss(s);
  ll x;
  ss >> x;
  return x;
}
#define tp9 1000000000
ll a, b, c, d;
void input() { cin >> a >> b >> c >> d; }
ld pi = 3.1415926535;
void printYes() { cout << "Yes\\n"; }
void printNo() { cout << "No\\n"; }
ll t, n, p;
string s;
const ll N = 1e7;
ll spf[N + 1];
void fill_spf() {
  memset(spf, 0, sizeof spf);
  for (ll i = 2; i <= N; i++) {
    if (spf[i] == 0) {
      for (ll j = i; j <= N; j++) {
        if (spf[j] == 0) {
          spf[j] = i;
        }
      }
    }
  }
}
ll f1(ll k) {
  map<ll, ll> primefactors;
  ll i = k;
  while (i != 1) {
    primefactors[spf[i]]++;
    i /= spf[i];
  }
  ll factors = 1;
  for (auto p : primefactors) {
    factors *= (1 + p.sc);
  }
  return factors;
}
ll f(ll k) {
  ll i = 2;
  map<ll, ll> mp;
  while (k % 2 == 0) {
    mp[2]++;
    k = k / 2;
  }
  for (i = 3; i * i <= k; i += 2) {
    while (k % i == 0) {
      mp[i]++;
      k = k / i;
    }
  }
  if (k > 2) {
    mp[k]++;
  }
  ll factors = 1;
  for (auto p : mp) {
    factors *= (1 + p.sc);
  }
  return factors;
}
ll primeFactors(ll n) {
  // Print the number of 2s that divide n
  map<ll, ll> mp;
  while (n % 2 == 0) {
    // cout << 2 << " ";
    mp[2]++;
    n = n / 2;
  }

  // n must be odd at this point. So we can skip
  // one element (Note i = i +2)
  for (ll i = 3; i * i <= (n); i = i + 2) {
    // While i divides n, print i and divide n
    while (n % i == 0) {
      // cout << i << " ";
      mp[i]++;
      n = n / i;
    }
  }

  // This condition is to handle the case when n
  // is a prime number greater than 2
  if (n > 2)
    // cout << n << " ";
    mp[n]++;
  ll ans = 1;
  for (auto p : mp) {
    ans *= (1 + p.sc);
  }
  return ans;
}
int main() {
  // fill_spf() ;
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll k = 1; k <= n; k++) {
    ans += k * primeFactors(k);
  }
  cout << ans << endl;
  return 0;
}