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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);

int isPrime(LL p) {
  if (p <= 1)
    return 0;
  if (p == 2 || p == 3 || p == 5 || p == 7)
    return 1;
  if (p == 4 || p == 6 || p == 8 || p == 9)
    return 0;

  LL i;
  int ret = 1;
  for (i = 2; i * i <= p; i++) {
    if (p % i == 0 && i != p) {
      ret = 0;
      break;
    }
  }
  return ret;
}

LL calcd(map<LL, int> &mp) {
  LL ret = 1;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    int v = itr->second;
    ret *= (v + 1);
  }
  return ret;
}

LL N;
vector<LL> spf;
void init(LL n) {
  spf.assign(n + 1, 0);
  for (LL i = 0; i <= n; i++)
    spf[i] = i;
  for (LL i = 2; i * i <= n; i++) {
    if (spf[i] == i) {
      for (LL j = i * i; j <= n; j += i) {
        if (spf[j] == j) {
          spf[j] = i;
        }
      }
    }
  }
}

map<LL, int> get(LL n) {
  map<LL, int> m;
  while (n != 1) {
    m[spf[n]]++;
    n /= spf[n];
  }
  return m;
}
int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  init(N);

  LL ans = 0;
  for (LL i = 1; i < N; i++) {
    map<LL, int> mp = get(i);
    LL r = (LL)calcd(mp);
    ans += r;
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);

int isPrime(LL p) {
  if (p <= 1)
    return 0;
  if (p == 2 || p == 3 || p == 5 || p == 7)
    return 1;
  if (p == 4 || p == 6 || p == 8 || p == 9)
    return 0;

  LL i;
  int ret = 1;
  for (i = 2; i * i <= p; i++) {
    if (p % i == 0 && i != p) {
      ret = 0;
      break;
    }
  }
  return ret;
}

void factorize(long long N, map<long long, int> &mp) {
  if (N == 1) {
    return;
  }
  if (isPrime(N)) {
    mp[N] += 1;
    return;
  }

  long long i = 2, n = N;
  for (i = 2; n != 1 && i < (N / 2) + 1; i++) {
    if ((n % i) == 0) {
      if (isPrime(i)) {
        int e = 0;
        long long u = n;
        while ((u % i) == 0) {
          e++;
          u = u / i;
          n = u;
        }
        mp[i] = e;
        if (isPrime(n)) {
          mp[n] = 1;
          break;
        }
      }
    }
  }
  return;
}

LL calcd(map<LL, int> &mp) {
  LL ret = 1;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    int v = itr->second;
    ret *= (v + 1);
  }
  return ret;
}

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N;
  cin >> N;
  LL ans = 0;
  for (LL i = 1; i < N; i++) {
    map<LL, int> mp;
    factorize(i, mp);
    LL r = (LL)calcd(mp);
    ans += r;
  }
  printf("%lld\\n", ans);
  return 0;
}