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

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define int long long
#define mod 1000000007
#define pb push_back
#define S second
#define F first
#define INF 1e18
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)

#define MAXN 1000002

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

int getFactorization(int x) {
  vector<int> ret;
  int prev = -1;
  int cnt = 0, ans = 1ll;
  while (x != 1) {
    if (spf[x] == prev)
      cnt++;
    else {
      ans *= (cnt + 1);
      prev = spf[x];
      cnt = 1;
    }
    x = x / spf[x];
  }
  if (cnt > 0)
    ans *= (cnt + 1);
  return ans;
}

int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  sieve();
  for (int i = 1; i <= n - 1; ++i) {
    int sum = n - i;
    ans += getFactorization(sum);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define int long long
#define mod 1000000007
#define pb push_back
#define S second
#define F first
#define INF 1e18
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
vector<int> seive(int n) {
  vector<bool> isPrime(n + 1, 1);
  isPrime[1] = 1;

  for (int i = 2; i * i <= n; ++i) {
    if (isPrime[i]) {
      for (int j = i * i; j <= n; j += i) {
        isPrime[j] = 0;
      }
    }
  }
  vector<int> prime;
  for (int i = 1; i <= n; ++i)
    if (isPrime[i])
      prime.pb(i);
  return prime;
}
int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  // vector<int> primes = seive(n + 2);
  for (int i = 1; i <= n - 1; ++i) {
    int sum = n - i;

    for (int j = 1; j * j <= sum; ++j) {
      if (sum % j == 0) {
        ans++;
        if (sum / j != j)
          ans++;
      }
    }
  }
  cout << ans;
  return 0;
}