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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
typedef long long int ll;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define MOD 1000000007
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
//*s.find_by_order(p)   returns the value of pth largest starting from 0
// s.order_of_key(p)   returns no. of items that are strictly < p
// string bin = bitset<64>(m).to_string();     convert decimal m to binary
#define MAXN 1000005
int spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)
    spf[i] = i;
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;
  for (int i = 3; i * i < MAXN; i++) {
    if (spf[i] == i) {
      for (int j = i * i; j < MAXN; j += i)
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}
int main() {
  fast;
  ll t1;
  t1 = 1;
  while (t1--) {
    sieve();
    int n, i, a, j, b, ans;
    cin >> n;
    ans = 0;
    for (i = 1; i <= n; i++) {
      a = n - i;
      if (a < 1) {
        continue;
      }
      b = sqrt(a);
      if (a == 1) {
        ans++;
        continue;
      }
      // cout<<a<<" ";
      vector<int> v = getFactorization(a);
      sort(v.begin(), v.end());
      int p = 1;
      int s = 1;
      if (v.size() == 0) {
        continue;
      }
      for (j = 1; j < v.size(); j++) {
        if (v[j] == v[j - 1]) {
          p++;
        } else {
          s *= (p + 1);
          p = 1;
        }
      }
      s *= (p + 1);
      ans += s;
    }
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
typedef long long int ll;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define MOD 1000000007
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
//*s.find_by_order(p)   returns the value of pth largest starting from 0
// s.order_of_key(p)   returns no. of items that are strictly < p
// string bin = bitset<64>(m).to_string();     convert decimal m to binary
ll countWays(ll n) {
  ll count = 0;
  for (ll i = 1; i * i < n; i++)
    if (n % i == 0)
      count++;

  return count;
}
int main() {
  fast;
  ll t1;
  t1 = 1;
  while (t1--) {
    ll n, i, a, b, ans;
    cin >> n;
    ans = 0;
    for (i = 1; i <= n; i++) {
      a = n - i;
      if (a < 1) {
        continue;
      }
      b = sqrt(a);
      if (b * b == a) {
        ans++;
      }
      ans += 2 * countWays(a);
    }
    cout << ans << "\\n";
  }
}