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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

struct Sieve {
  int n;
  vector<int> f, primes;

  Sieve(int n = 1) : n(n), f(n + 1) {
    f[0] = f[1] = -1;
    for (long long i = 2; i <= n; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (long long j = i * i; j <= n; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }

  bool isPrime(int x) { return f[x] == x; }

  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }

  vector<pair<int, int>> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }

  long long divisorCalc(long long x) {
    long long res = 1;
    long long now = -1;
    long long nowCnt = 0;
    while (x != 1) {
      if (now == f[x])
        ++nowCnt;
      else {
        res *= nowCnt + 1;
        now = f[x];
        nowCnt = 1;
      }
      x /= f[x];
    }
    res *= nowCnt + 1;
    return res;
  }
};

int main() {
  int N;
  cin >> N;
  Sieve siv(N + 1);
  ll ans = 0;
  rep(i, N) { ans += (ll)(i + 1) * (ll)siv.divisorCalc(i + 1); }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

struct Sieve {
  int N;
  vector<int> f, primes;

  Sieve(int N = 1) : N(N), f(N + 1) {
    f[0] = f[1] = -1;
    for (long long i = 2; i <= N; ++i) {
      if (f[i])
        continue;
      primes.push_back(i);
      f[i] = i;
      for (long long j = i * i; j <= N; j += i) {
        if (!f[j])
          f[j] = i;
      }
    }
  }

  bool isPrime(int x) { return f[x] == x; }

  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }

  vector<pair<int, int>> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0)
      return {};
    vector<pair<int, int>> res(1, pair<int, int>(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }

  long long divisorCalc(long long x) {
    long long res = 1;
    long long now = -1;
    long long nowCnt = 0;
    while (x != 1) {
      if (now == f[x])
        ++nowCnt;
      else {
        res *= nowCnt + 1;
        now = f[x];
        nowCnt = 1;
      }
      x /= f[x];
    }
    res *= nowCnt + 1;
    return res;
  }
};

int main() {
  int N;
  cin >> N;
  Sieve siv(N);
  ll ans = 0;
  rep(i, N) { ans += (ll)i * (ll)siv.divisorCalc(i); }
  cout << ans << endl;
  return 0;
}