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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename M, typename N>
istream &operator>>(istream &is, pair<M, N> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<M> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M, typename N>
ostream &operator<<(ostream &os, const pair<M, N> &p) {
  os << p.fi << ' ' << p.se << '\\n';
  return os;
}
template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) {
  for (auto it : v)
    os << it << ' ';
  cout << '\\n';
  return os;
}
template <typename M>
ostream &operator<<(ostream &os, const vector<vector<M>> &v) {
  for (auto it : v)
    os << it;
  return os;
}

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e9 + 10;
// const int mod=1000000007;
const int mod = 998244353;

int n, m, k;
const int N = 3e5 + 10;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vll a(n);
  for (int i = 1; i < n; i++) {
    for (int j = i; j < n; j += i) {
      a[j]++;
    }
  }
  cout << acc(a) << el;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename M, typename N>
istream &operator>>(istream &is, pair<M, N> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<M> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M> istream &operator>>(istream &is, vector<vector<M>> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename M, typename N>
ostream &operator<<(ostream &os, const pair<M, N> &p) {
  os << p.fi << ' ' << p.se << '\\n';
  return os;
}
template <typename M> ostream &operator<<(ostream &os, const vector<M> &v) {
  for (auto it : v)
    os << it << ' ';
  cout << '\\n';
  return os;
}
template <typename M>
ostream &operator<<(ostream &os, const vector<vector<M>> &v) {
  for (auto it : v)
    os << it;
  return os;
}

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mn(x) *min_element((x).begin(), (x).end())
#define mx(x) *max_element((x).begin(), (x).end())
#define acc(x) accumulate((x).begin(), (x).end(), 0ll)
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;

const ld pi = acos(-1);
const ll inf = (ll)1e9 + 10;
// const int mod=1000000007;
const int mod = 998244353;

int n, m, k;
const int N = 3e5 + 10;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  auto f = [&](int n) {
    int ans = 0;
    for (int i = 1; i * i <= n; i++) {
      if (n % i == 0) {
        if (i * i == n) {
          ans++;
        } else
          ans += 2;
      }
    }
    return ans;
  };
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ans += f(i);
  }
  cout << ans << el;
  return 0;
}