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

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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define loop(i, a, b) for (int i = a; i <= b; i++)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define ms(a, x) memset(a, x, sizeof a)
#define ll long long int
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define nl cout << "\\n"
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / (gcd(a, b))
#define sq(a) (a) * (a)
#define sz(a) a.size()
#define mod 1000000007
#define PI 2 * acos(0.0)
vector<int> spf(1000005);

vector<int> modified_sieve(int n) {
  for (int i = 1; i <= n; i++)
    spf[i] = i;
  vector<int> primes;
  for (int i = 2; i * i <= n; i++)
    if (spf[i] == i)
      for (int j = i + i; j <= n; j += i)
        if (spf[j] == j)
          spf[j] = i;
  for (int i = 2; i <= n; i++)
    if (spf[i] == i)
      primes.push_back(i);
  return primes;
}

vector<int> factorize(int x) {
  vector<int> res;
  while (x > 1) {
    res.push_back(spf[x]);
    x /= spf[x];
  }
  return res;
}
int main() {
  IOS int T = 1;
  // cin>>T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> a = modified_sieve(n), b = factorize(n);
    int ans = 0;
    loop(i, 1, n - 2) {

      vector<int> y = factorize(n - i);
      int t = 1;
      map<int, int> m;
      loop(j, 0, sz(y) - 1) m[y[j]]++;
      for (auto p : m)
        t *= (p.ss + 1);
      ans += t;
    }
    cout << ans + 1;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define rloop(i, a, b) for (ll i = a; i >= b; i--)
#define ms(a, x) memset(a, x, sizeof a)
#define ll long long int
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define nl cout << "\\n"
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / (gcd(a, b))
#define sq(a) (a) * (a)
#define sz(a) a.size()
#define mod 1000000007
#define PI 2 * acos(0.0)
ll countDivisors(ll n) {
  ll cnt = 0;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal,
      // count only one
      if (n / i == i)
        cnt++;

      else // Otherwise count both
        cnt = cnt + 2;
    }
  }
  return cnt;
}
int main() {
  IOS int T = 1;
  // cin>>T;
  while (T--) {
    ll n;
    cin >> n;
    ll ans = 0;
    loop(i, 1, n - 1) {

      ll y = countDivisors(n - i);
      ans += y;
    }
    cout << ans;
  }
  return 0;
}