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

/// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
#define u64 uint64_t
#define ll long long
#define endl "\\n"
#define PI acos(-1)
#define fi first
#define si second
#define mkp make_pair
#define pb push_back
#define set0(arr) memset(arr, 0, sizeof(arr))
#define setinf(arr) memset(arr, 126, sizeof(arr))
#define all(x) (x).begin(), (x).end()
#define sz(v) ((int)(v).size())
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpll = vector<pll>;
using mll = map<ll, ll>;
using mcl = map<char, ll>;
using msl = map<string, ll>;
using sl = set<ll>;
using sc = set<char>;
using dl = deque<ll>;
const int N = 1e6 + 5;

ll mod = 1e9 + 7;
vl adj[N];
vpll adjc[N];
ll vis[N];
ll arr[N];

ll fun(ll n) {

  ll div[n + 1];
  memset(div, 0, sizeof div);

  // For every number from 1 to n
  for (ll i = 1; i <= n; i++) {

    // Increase divisors count for
    // every number divisible by i
    for (ll j = 1; j * i <= n; j++)
      div[i * j]++;
  }

  // Print the divisors
  ll sum = 0;
  for (ll i = 1; i <= n; i++)
    sum += div[i];
  return sum;
}

int main() {
  IOS;
  ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0,
                                            mul = 1, mi = 1e18, ma = -1e18, cs;
  string str;
  char ch;
  double db;
  ll l, r;
  // code

  cin >> n;
  cout << fun(n - 1) << endl;

  // code
  return 0;
}


Error: TLE

Fixed submission:

/// Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
#define u64 uint64_t
#define ll long long
#define endl "\\n"
#define PI acos(-1)
#define fi first
#define si second
#define mkp make_pair
#define pb push_back
#define set0(arr) memset(arr, 0, sizeof(arr))
#define setinf(arr) memset(arr, 126, sizeof(arr))
#define all(x) (x).begin(), (x).end()
#define sz(v) ((int)(v).size())
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpll = vector<pll>;
using mll = map<ll, ll>;
using mcl = map<char, ll>;
using msl = map<string, ll>;
using sl = set<ll>;
using sc = set<char>;
using dl = deque<ll>;
const int N = 1e6 + 5;

ll mod = 1e9 + 7;
vl adj[N];
vpll adjc[N];
ll vis[N];
ll arr[N];

ll fun(ll n) {
  ll cnt = 0;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (n / i == i)
        cnt++;
      else
        cnt += 2;
    }
  }
  return cnt;
}

int main() {
  IOS;
  ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0,
                                            mul = 1, mi = 1e18, ma = -1e18, cs;
  string str;
  char ch;
  double db;
  ll l, r;
  // code

  cin >> n;
  for (int i = 2; i < n; i++) {
    sum += fun(i);
  }
  sum++;
  cout << sum << endl;

  // code
  return 0;
}