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

/*
 * zyrch - Rupanshu Yadav
 */
#ifdef CREED
#include "creed.h"
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

#define forn(i, a, b) for (int i = (a); i < ll(b); ++i)
#define fo(i, a) forn(i, 0, ll(a))
#define rforn(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ro(i, a) rforn(i, 0, a)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rsz resize
#define ins insert

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef CREED
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

const ll INF = 9e18;

ll mod = 1000000007;
inline ll fast_expo(ll base, ll power, ll modulo = mod) {
  base %= modulo;
  if (base < 0)
    base += modulo;
  ll x = base, cnt = power, ans = 1;
  while (cnt) {
    if (cnt & 1)
      ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1;
  }
  return ans;
}

ll takemod(ll a, ll mo) {
  a %= mo;
  if (a < 0)
    a += mo;
  return a;
}

// if mod is prime
ll modinv(ll a, ll mo) {
  return takemod(fast_expo(takemod(a, mo), mo - 2, mo), mo);
}

//=========================CODE STARTS HERE=============================//
const int nax = 1e7 + 17;
ll divs[nax];
ll facs[nax];
ll n;
ll ans = 0;

void pre() {
  divs[0] = 0;
  divs[1] = 1;
  for (int i = 2; i < nax; ++i) {
    for (int j = i; j < nax; j += i) {
      divs[j]++;
    }
  }
}

void pre2() {

  ans = 1;
  facs[1] = 1;
  for (int i = 2; i < n + 1; ++i) {
    //~ fs *= ;
    //~ facs[i] = fs;
    ans += (divs[i] + 1) * i;
  }
}

int main() {
  fast_cin();

  pre();

  cin >> n;

  pre2();
  //~ debug(divs[2], divs[3], divs[4]);
  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

/*
 * zyrch - Rupanshu Yadav
 */
#ifdef CREED
#include "creed.h"
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#endif

using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

#define forn(i, a, b) for (int i = (a); i < ll(b); ++i)
#define fo(i, a) forn(i, 0, ll(a))
#define rforn(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ro(i, a) rforn(i, 0, a)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rsz resize
#define ins insert

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifdef CREED
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

const ll INF = 9e18;

ll mod = 1000000007;
inline ll fast_expo(ll base, ll power, ll modulo = mod) {
  base %= modulo;
  if (base < 0)
    base += modulo;
  ll x = base, cnt = power, ans = 1;
  while (cnt) {
    if (cnt & 1)
      ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1;
  }
  return ans;
}

ll takemod(ll a, ll mo) {
  a %= mo;
  if (a < 0)
    a += mo;
  return a;
}

// if mod is prime
ll modinv(ll a, ll mo) {
  return takemod(fast_expo(takemod(a, mo), mo - 2, mo), mo);
}

//=========================CODE STARTS HERE=============================//
const int nax = 1e7 + 17;
ll divs[nax];
ll facs[nax];
ll n;
ll ans = 0;

void pre() {
  memset(divs, -1, sizeof(divs));
  divs[0] = 0;
  divs[1] = 1;

  for (int i = 2; i * i < nax; ++i) {
    if (divs[i] != -1)
      continue;
    divs[i] = i;
    for (int j = i * i; j < nax; j += i) {
      divs[j] = i;
    }
  }
}

void pre2() {

  ans = 1;
  facs[1] = 1;
  for (int i = 2; i < n + 1; ++i) {
    ll fs = 1;
    ll o = i;

    ll co = 0;
    ll p = divs[o];
    while (o % p == 0) {
      o /= p;
      ++co;
    }
    fs *= facs[o] * (co + 1);
    //~ debug(facs[i]);
    facs[i] = fs;
    ans += i * fs;
  }
}

int main() {
  fast_cin();

  pre();

  cin >> n;

  pre2();
  cout << ans << '\\n';

  return 0;
}