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

#pragma optimization_level 3
#include <bits/stdc++.h>
#define f first
#define s second
#define sym(s) s = "#" + s + "#";
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define newl cout << "\\n";
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define deb1(a) cout << a << "\\n";
#define deb2(a, b) cout << a << " " << b << "\\n";
#define deb3(a, b, c) cout << a << " " << b << " " << c << "\\n";
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n";
#define debp(a) cout << a.f << " " << a.s << "\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }

using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int N = (int)2e+5 + 8;

ll n, a[N];
string s;

const ll N2 = 1e7 + 8;
ll p[N2];
void sieve() {
  for (ll i = 2; i * i <= N2; i++) {
    if (p[i] == 0) {
      for (ll j = 2 * i; j < N2; j += i) {
        if (p[j] == 0)
          p[j] = i; // for spf
      }
    }
  }

  foo(i, 1, N2 - 1) if (p[i] == 0) p[i] =
      i; // all numbers with p[i]=i, are primes
}

vector<pair<ll, ll>> prime_factorise(ll n) {
  vector<pair<ll, ll>> pf;
  while (n > 1) {
    if (pf.size() && pf.back().f == p[n]) {
      pair<ll, ll> p = pf.back();
      pf.pop_back();
      pf.push_back({p.f, p.s + 1});
    } else {
      pf.push_back({p[n], 1});
    }
    n /= p[n];
  }
  return pf;
}

void MAIN(int tc) {
  cin >> n;
  ll res = 0;
  for (int c = 1; c <= n - 1; c++) {
    ll r = n - c, cnt = 1;
    vector<pair<ll, ll>> pf = prime_factorise(r);
    for (auto [a, b] : pf)
      cnt *= (b + 1);
    res += cnt;
  }
  deb1(res);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout << setprecision(10);

  sieve();
  int test_cases = 1;
  // cin>>test_cases;
  for (int i = 1; i <= test_cases; i++) {
    MAIN(i);
  }
}

Error: TLE

Fixed submission:

#pragma optimization_level 3
#include <bits/stdc++.h>
#define f first
#define s second
#define sym(s) s = "#" + s + "#";
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define newl cout << "\\n";
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define deb1(a) cout << a << "\\n";
#define deb2(a, b) cout << a << " " << b << "\\n";
#define deb3(a, b, c) cout << a << " " << b << " " << c << "\\n";
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n";
#define debp(a) cout << a.f << " " << a.s << "\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }

using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int N = (int)2e+5 + 8;

ll n, a[N];
string s;

void MAIN(int tc) {
  cin >> n;
  ll res = 0;
  for (int c = 1; c <= n - 1; c++) {
    ll r = n - c, cnt = 0;
    for (int i = 1; i * i <= r; i++) {
      if (r % i == 0) {
        cnt++;
        if (i * i != r)
          cnt++;
      }
    }
    res += cnt;
  }
  deb1(res);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout << setprecision(10);

  int test_cases = 1;
  // cin>>test_cases;
  for (int i = 1; i <= test_cases; i++) {
    MAIN(i);
  }
}