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


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <typename T> class PrimeFact {
public:
  std::vector<T> spf;
  std::map<T, T> m;

  PrimeFact(T N) : spf(N + 1, 0) { init(N); }

  void init(T N) {
    for (T i = 0; i <= N; i++)
      spf[i] = i;
    for (T i = 2; i * i <= N; i++) {
      if (spf[i] != i)
        continue;
      for (T j = i * i; j <= N; j += i) {
        if (spf[j] == j)
          spf[j] = i;
      }
    }
    /*
    spf[1] = 1;
    for(int i=2; i<=N; i++) {
      if ( spf[i] == 0 ) {
        for(int j=i; j <= N; j += i ) {
          spf[j] = i;
        }
      }
    }
    */
    return;
  }

  auto get(T n) {
    m.clear();
    while (n != 1) {
      m[spf[n]]++;
      n /= spf[n];
    }
    ll count = 1;
    for (auto &&s : m)
      count *= (1 + s.second);
    return count;
  }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  PrimeFact<int> pf(n);
  ll ans = 0;
  for (int k = 1; k <= n; k++)
    ans += k * pf.get(k);
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

template <typename T> struct PrimeFact {
  std::vector<T> spf;
  PrimeFact(T N) { init(N); }
  void init(T N) {
    spf.assign(N + 1, 0);
    for (T i = 0; i <= N; i++)
      spf[i] = i;
    for (T i = 2; i * i <= N; i++) {
      if (spf[i] == i) {
        for (T j = i * i; j <= N; j += i) {
          if (spf[j] == j) {
            spf[j] = i;
          }
        }
      }
    }
  }

  auto get(T n) {
    std::unordered_map<T, T> m;
    while (n != 1) {
      m[spf[n]]++;
      n /= spf[n];
    }
    return m;
  }
};

ll divisor_count(ll n) {
  int j, c = 1;
  for (int i = 2; i * i <= n; i++, c *= j)
    for (j = 1; !(n % i); j++)
      n /= i;
  return (1 < n) ? c * 2 : c;
}

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll n;
  std::cin >> n;
  PrimeFact<ll> pf(n);

  ll ans = 0;
  for (int k = 1; k <= n; k++) {
    auto m = pf.get(k);
    ll tmp = 1;
    for (auto &&s : m)
      tmp *= (1 + s.second);
    // std::cout << k << " " << m << std::endl;
    ans += k * tmp;
  }
  std::cout << ans << std::endl;
  return 0;
}