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

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9 + 7)
#define ceil(x, y) (x % y == 0 ? (x / y) : (x / y + 1))
#define FOR(i, N) for (int i = 0; i < N; ++i)
#define FOR1(i, N) for (int i = 1; i <= N; ++i)
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<int, int, hash<int>> ht; // while using, comment #define
int long long  */

void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
using namespace std;
#define MAX 1000009
// https://www.geeksforgeeks.org/efficient-program-print-number-factors-n-numbers/
int factor[MAX + 1];
int calc(int n) {
  if (n == 1) {
    return 1;
  }

  int ans = 1;

  int dup = factor[n];
  int c = 1;
  int j = n / factor[n];
  while (j != 1) {
    if (factor[j] == dup)
      c += 1;
    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }
    j = j / factor[j];
  }
  ans = ans * (c + 1);

  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in6.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int T = 1;
  // cin >> T;

  FOR1(tt, T) {
    // cout << "Case #" << tt << ": ";
    factor[1] = 1;
    for (int i = 2; i < MAX; i++)
      factor[i] = i;

    for (int i = 4; i < MAX; i += 2)
      factor[i] = 2;

    for (int i = 3; i * i < MAX; i++) {
      if (factor[i] == i) {
        for (int j = i * i; j < MAX; j += i) {
          if (factor[j] == j)
            factor[j] = i;
        }
      }
    }
    int n;
    cin >> n;
    int ans = 0;
    for (int num = 1; num < n; num++) {
      int cnt = calc(num);
      ans += cnt;
    }
    cout << ans;
    cout << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD (int)(1e9 + 7)
#define ceil(x, y) (x % y == 0 ? (x / y) : (x / y + 1))
#define FOR(i, N) for (int i = 0; i < N; ++i)
#define FOR1(i, N) for (int i = 1; i <= N; ++i)
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mset(a, v) memset(a, v, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define INF 2e9
#define EPS 1e-9
#define int long long
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<int, int, hash<int>> ht; // while using, comment #define
int long long  */

void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
using namespace std;
bool fun(vector<pii> &v, int n) {
  for (int i = 0; i + 2 < n; i++) {
    if (v[i].ff == v[i].ss and v[i + 1].ff == v[i + 1].ss and
        v[i + 2].ff == v[i + 2].ss)
      return true;
  }
  return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in6.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int T = 1;
  // cin >> T;

  FOR1(tt, T) {
    // cout << "Case #" << tt << ": ";
    // TLE?
    int n;
    cin >> n;
    int ans = 0;
    for (int num = 1; num < n; num++) {
      int cnt = 0;
      for (int i = 1; i * i <= num; i++) {
        if (num % i == 0) {
          cnt++;
          if (i * i != num)
            cnt++;
        }
      }
      ans += cnt;
    }
    cout << ans;
    cout << '\\n';
  }
  return 0;
}