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

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

#define ll long long
#define d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

int count_factors(int n) {
  int count = 0;

  for (int i = 1; i * i < n; i++) {
    if (n % i == 0)
      count += 2;
  }

  int x = sqrt(n);
  if (x * x == n)
    count++;

  return count;
}

int main() {
  int n;
  scf(n);

  int ans = 0;

  for (int i = 1; i < n; i++) {
    int x = n - i;

    ans += count_factors(x);
  }

  prin(ans);
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define d long double

#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define dscf(n) scanf("%Lf", &n)
#define pri(n) printf("%d ", (int)n)
#define lpri(n) printf("%lld ", n)
#define dpri(n) printf("%Lf ", n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define dprin(n) printf("%Lf\\n", n)

#define fast()                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define rep(i, ini, n) for (ll i = ini; i < (int)n; i++)
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define bitcount(n) __builtin_popcount(n)
#define tc                                                                     \\
  int tt;                                                                      \\
  scf(tt);                                                                     \\
  while (tt--)
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.14159265358979323846264

const ll M = 1e9 + 7;
const int N = 1e6 + 7;

ll count_factors(ll n) {
  ll count = 0;

  for (int i = 1; i * i < n; i++) {
    if (n % i == 0)
      count += 2;
  }

  ll x = sqrt(n);
  if (x * x == n)
    count++;

  return count;
}

int main() {
  ll n;
  cin >> n;

  ll ans = 0;

  for (int i = 1; i < n; i++) {
    ll x = n - i;

    ans += count_factors(x);
  }

  cout << ans;
  return 0;
}