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

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

#define int long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define RFOR(i, a, b) for (i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define clr(x, f) memset(x, f, sizeof(x))
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define si set<int>
#define spii set<pii>
#define usi unordered_set<int>
#define uspii unordered_set<pii>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define tzbits(x) __builtin_ctzll(x)
#define lzbits(x) __builtin_clzll(x)
#define pbits(x) __builtin_parityll(x)
#define gcd __gcd
#define lcm(x, y) ((x) * (y)) / gcd(x, y)
#define endl '\\n'
#define sz(s) (int)s.size()
#define sp(x, y) fixed << setprecision(y) << x
const int mod = 1000000007;
const int inf = 1000000000000000000;
const ld PI = 3.1415926535897932384626;
const ld eps = 1e-12;
void __print(int32_t x) { cout << x; }
void __print(int x) { cout << x; }
void __print(double x) { cout << x; }
void __print(ld x) { cout << x; }
void __print(float x) { cout << x; }
void __print(bool x) { cout << (x ? "true" : "false"); }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#define dbg(x...)                                                              \\
  cout << "[" << #x << "]=[";                                                  \\
  _print(x)
int powerm(int base, int exp) {
  int res = 1;
  base %= mod;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp = exp >> 1;
  }
  return res;
}
int power(int base, int exp) {
  int res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = res * base;
    base = base * base;
    exp = exp >> 1;
  }
  return res;
}
float powerNeg(float base, int exp) {
  float temp;
  if (exp == 0)
    return 1;
  temp = powerNeg(base, exp / 2);
  if (exp % 2 == 0)
    return temp * temp;
  else {
    if (exp > 0)
      return base * temp * temp;
    else
      return (temp * temp) / base;
  }
}
int modinv(int exp) { return powerm(exp, mod - 2); }
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

const int N = 2e6 + 5, M = N;
vi fact(N, 2);
void sieve() {
  fact[0] = 0;
  fact[1] = 1;
  for (int i = 2; i < N; i++) {
    for (int j = 2 * i; j < N; j += i) {
      fact[j]++;
    }
  }
}
void solve() {

  int i, j;
  int n;
  cin >> n;
  sieve();
  // dbg(fact);
  int ans = 0;
  for (int c = 1; c < n; c++) {
    int x = n - c;
    ans += fact[x];
    // ans-=1;
    // for(int a=1;a*a<=x;a++){
    //     if(x%a==0){
    //         if(x/a==a) ans++;
    //         else ans+=2;
    //     }
    // }
  }

  cout << ans << endl;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t, T = 1;
  // cin>>T;
  FOR(t, 1, T) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  // cout<<endl<<"Times Elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"sec";
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define FOR(i, a, b) for (i = a; i <= b; i++)
#define RFOR(i, a, b) for (i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define clr(x, f) memset(x, f, sizeof(x))
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define si set<int>
#define spii set<pii>
#define usi unordered_set<int>
#define uspii unordered_set<pii>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define tzbits(x) __builtin_ctzll(x)
#define lzbits(x) __builtin_clzll(x)
#define pbits(x) __builtin_parityll(x)
#define gcd __gcd
#define lcm(x, y) ((x) * (y)) / gcd(x, y)
#define endl '\\n'
#define sz(s) (int)s.size()
#define sp(x, y) fixed << setprecision(y) << x
const int mod = 1000000007;
const int inf = 1000000000000000000;
const ld PI = 3.1415926535897932384626;
const ld eps = 1e-12;
void __print(int32_t x) { cout << x; }
void __print(int x) { cout << x; }
void __print(double x) { cout << x; }
void __print(ld x) { cout << x; }
void __print(float x) { cout << x; }
void __print(bool x) { cout << (x ? "true" : "false"); }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#define dbg(x...)                                                              \\
  cout << "[" << #x << "]=[";                                                  \\
  _print(x)
int powerm(int base, int exp) {
  int res = 1;
  base %= mod;
  while (exp > 0) {
    if (exp & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    exp = exp >> 1;
  }
  return res;
}
int power(int base, int exp) {
  int res = 1;
  while (exp > 0) {
    if (exp & 1)
      res = res * base;
    base = base * base;
    exp = exp >> 1;
  }
  return res;
}
float powerNeg(float base, int exp) {
  float temp;
  if (exp == 0)
    return 1;
  temp = powerNeg(base, exp / 2);
  if (exp % 2 == 0)
    return temp * temp;
  else {
    if (exp > 0)
      return base * temp * temp;
    else
      return (temp * temp) / base;
  }
}
int modinv(int exp) { return powerm(exp, mod - 2); }
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

const int N = 2e6 + 5, M = N;

void solve() {

  int i, j;
  int n;
  cin >> n;

  int ans = 0;
  for (int c = 1; c < n; c++) {
    int x = n - c;
    for (int a = 1; a * a <= x; a++) {
      if (x % a == 0) {
        if (x / a == a)
          ans++;
        else
          ans += 2;
      }
    }
  }

  cout << ans << endl;
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t, T = 1;
  // cin>>T;
  FOR(t, 1, T) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  // cout<<endl<<"Times Elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"sec";
  return 0;
}