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

#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 __gnu_pbds;
using namespace std;
/*
         ########     ########   ########   #########   ##       #######   ##
   ####### #      #     #      #   #      #       #       # #      #     #   # #
   #     # #      #     #      #   #      #       #       #  #     #     #   #
   #     #     #
         ########     #      #   #      #       #       #   #    #     #   #   #
   #     #
         ###          #      #   #      #       #       #    #   #######   # #
   ####### #  #         #      #   #      #       #       #   #    #     #   #
   #    #     # #   #        #      #   #      #       #       #  #     #     #
   #  #     #     # #    #       #      #   #      #       #       # #      # #
   # #      #     # #     #      ########   ########       #       ##       # #
   ##       #     #
*/

#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define nrep(i, a, b) for (__typeof((b)) i = (a); i > (b); i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define ppi pair<int, int>
#define pppi pair<ppi, int>
#define vi vector<int>
#define vii vector<ppi>
#define viii vector<pppi>
#define vs vector<string>
#define pb push_back
#define bitcount __builtin_popcountll // count no's of ones in a set bit
#define prq priority_queue
#define mp make_pair
#define mem(x, val) memset((x), (val), sizeof(x));
#define sz(x) (int)x.size()
#define M_PI 3.14159265358979323846
#define rootdada                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define debug(a)                                                               \\
  cerr << #a << ": ";                                                          \\
  for (auto i : a)                                                             \\
    cerr << i << " ";                                                          \\
  cerr << '\\n';
#define trace(a) cerr << #a << ": " << a << "\\n"
#define print(ar, n)                                                           \\
  for (int u = 0; u < n; u++) {                                                \\
    cout << ar[u] << " ";                                                      \\
  }                                                                            \\
  cout << endl;
#define int long long
#define M 1000000007
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

signed main() {
  rootdada;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int n;
  cin >> n;
  int ans = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 1; j < n; j++) {
      if (n - i * j <= 0) {
        break;
      } else
        ans++;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#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 __gnu_pbds;
using namespace std;
/*
         ########     ########   ########   #########   ##       #######   ##
   ####### #      #     #      #   #      #       #       # #      #     #   # #
   #     # #      #     #      #   #      #       #       #  #     #     #   #
   #     #     #
         ########     #      #   #      #       #       #   #    #     #   #   #
   #     #
         ###          #      #   #      #       #       #    #   #######   # #
   ####### #  #         #      #   #      #       #       #   #    #     #   #
   #    #     # #   #        #      #   #      #       #       #  #     #     #
   #  #     #     # #    #       #      #   #      #       #       # #      # #
   # #      #     # #     #      ########   ########       #       ##       # #
   ##       #     #
*/

#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define nrep(i, a, b) for (__typeof((b)) i = (a); i > (b); i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define ppi pair<int, int>
#define pppi pair<ppi, int>
#define vi vector<int>
#define vii vector<ppi>
#define viii vector<pppi>
#define vs vector<string>
#define pb push_back
#define bitcount __builtin_popcountll // count no's of ones in a set bit
#define prq priority_queue
#define mp make_pair
#define mem(x, val) memset((x), (val), sizeof(x));
#define sz(x) (int)x.size()
#define M_PI 3.14159265358979323846
#define rootdada                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define debug(a)                                                               \\
  cerr << #a << ": ";                                                          \\
  for (auto i : a)                                                             \\
    cerr << i << " ";                                                          \\
  cerr << '\\n';
#define trace(a) cerr << #a << ": " << a << "\\n"
#define print(ar, n)                                                           \\
  for (int u = 0; u < n; u++) {                                                \\
    cout << ar[u] << " ";                                                      \\
  }                                                                            \\
  cout << endl;
#define int long long
#define M 1000000007
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
signed main() {
  rootdada;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int n;
  cin >> n;
  int ans = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        if (i / j == j)
          ans++;
        else
          ans += 2;
      }
    }
  }
  cout << ans << endl;
}