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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

vector<int> primes;
vector<bool> isprime;
vector<ll> cnt;
void init(const int N) {
  isprime = vector<bool>(N + 1, true);
  cnt = vector<ll>(N + 1, 1);
  cnt[1] = 1;
  //  for(int i = 2; i <= N; i+= 2) cnt[i] = 1;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (isprime[i]) {
      primes.push_back(i);
      for (int j = i + i; j <= N; j += i)
        isprime[j] = false;
    }
  }
  //  pp(primes, primes.size());
#ifdef DEBUG
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= primes.size(); j++) {
      int k = i * primes[j];
      if (k > N)
        break;
      cnt[k] += cnt[i];
    }
  }
#endif
}

ll g(int n) {
  ll ret = 0;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (i * i == n)
        ret++;
      else
        ret += 2;
    }
  }
  return ret;
}

ll f(int n) {
  ll ret = 1;
  if (n == 1)
    return 1LL;
  if (isprime[n])
    return 2LL;
  REP(i, primes.size()) {
    if (n == 1)
      break;
    if (isprime[n]) {
      ret *= 2LL;
      break;
    }
    int cnt = 0;
    while (n % primes[i] == 0) {
      cnt++;
      n /= primes[i];
    }
    ret *= cnt + 1;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  ll ans = 0;
  init(N);
  for (int i = 1; i <= N; i++) {
    ll tmp = f(i);
    //    cout << i << " " << tmp << endl;
    ans += tmp * (ll)i;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
  REP(i, n) cout << v[i] << ' ';
  cout << endl;
}

void inline TorF(bool tf, const string &trueMessage,
                 const string &falseMessage) {
  cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }

vector<int> primes;
vector<bool> isprime;
vector<ll> cnt;
void init(const int N) {
  isprime = vector<bool>(N + 1, true);
  cnt = vector<ll>(N + 1, 1);
  cnt[1] = 1;
  //  for(int i = 2; i <= N; i+= 2) cnt[i] = 1;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= N; i++) {
    if (isprime[i]) {
      primes.push_back(i);
      for (int j = i + i; j <= N; j += i)
        isprime[j] = false;
    }
  }
  //  pp(primes, primes.size());
#ifdef DEBUG
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= primes.size(); j++) {
      int k = i * primes[j];
      if (k > N)
        break;
      cnt[k] += cnt[i];
    }
  }
#endif
}

ll g(int n) {
  ll ret = 0;
  for (int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (i * i == n)
        ret++;
      else
        ret += 2;
    }
  }
  return ret;
}

ll f(int n) {
  ll ret = 1;
  if (n == 1)
    return 1LL;
  if (isprime[n])
    return 2LL;
  REP(i, primes.size()) {
    if (n == 1)
      break;
    int cnt = 0;
    while (n % primes[i] == 0) {
      cnt++;
      n /= primes[i];
    }
    ret *= cnt + 1;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  ll ans = 0;
  init(N);
  for (int i = 1; i <= N; i++) {
    ll tmp = f(i);
    //    cout << i << " " << tmp << endl;
    ans += tmp * (ll)i;
  }
  cout << ans << endl;
  return 0;
}