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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (ll) c.size()
#define fr first
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
const ll INF = __LONG_LONG_MAX__;
#define rall(a) (a).rbegin(), (a).rend()
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

ll power(ll b, ll e, ll m) {
  if (b == 0)
    return 0;
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (b == 0)
    return 0;
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
bool isPowerOfTwo(ll x) {
  // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
  // or not
  return (x && !(x & (x - 1)));
}

const int MAX = 1000001;

// array to store prime factors
int factor[MAX] = {0};

// function to generate all prime factors
// of numbers from 1 to 10^6
void generatePrimeFactors() {
  factor[1] = 1;

  // Initializes all the positions with their value.
  for (int i = 2; i < MAX; i++)
    factor[i] = i;

  // Initializes all multiples of 2 with 2
  for (int i = 4; i < MAX; i += 2)
    factor[i] = 2;

  // A modified version of Sieve of Eratosthenes to
  // store the smallest prime factor that divides
  // every number.
  for (int i = 3; i * i < MAX; i++) {
    // check if it has no prime factor.
    if (factor[i] == i) {
      // Initializes of j starting from i*i
      for (int j = i * i; j < MAX; j += i) {
        // if it has no prime factor before, then
        // stores the smallest prime divisor
        if (factor[j] == j)
          factor[j] = i;
      }
    }
  }
}

// function to calculate number of factors
int calculateNoOFactors(int n) {
  if (n == 1)
    return 1;

  int ans = 1;

  // stores the smallest prime number
  // that divides n
  int dup = factor[n];

  // stores the count of number of times
  // a prime number divides n.
  int c = 1;

  // reduces to the next number after prime
  // factorization of n
  int j = n / factor[n];

  // false when prime factorization is done
  while (j != 1) {
    // if the same prime number is dividing n,
    // then we increase the count
    if (factor[j] == dup)
      c += 1;

    /* if its a new prime factor that is factorizing n,
       then we again set c=1 and change dup to the new
       prime factor, and apply the formula explained
       above. */
    else {
      dup = factor[j];
      ans = ans * (c + 1);
      c = 1;
    }

    // prime factorizes a number
    j = j / factor[j];
  }

  // for the last prime factor
  ans = ans * (c + 1);

  return ans;
}

ll calc(ll x) {
  ll ans = 0;
  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      if ((x / i) != i)
        ans += 2;
      else
        ans += 1;
    }
  }
  return ans;
}

void solve() {
  fastio generatePrimeFactors();
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll x = n - i;
    ans += calculateNoOFactors(x);
  }
  cout << ans;
}

int main() {
  fastio ll tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
    cout << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define ull unsigned long long
#define si set<ll>
#define vi vector<ll>
#define popcount(x) __builtin_popcountll(x)
#define mii map<ll, ll>
#define vpi vector<pair<ll, ll>>
#define sz(c) (ll) c.size()
#define fr first
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define ld long double
const ll INF = __LONG_LONG_MAX__;
#define rall(a) (a).rbegin(), (a).rend()
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

ll power(ll b, ll e, ll m) {
  if (b == 0)
    return 0;
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b % m, e / 2, m) % m;
  return power(b * b % m, e / 2, m);
}
ll power(ll b, ll e) {
  if (b == 0)
    return 0;
  if (e == 0)
    return 1;
  if (e & 1)
    return b * power(b * b, e / 2);
  return power(b * b, e / 2);
}
bool isPowerOfTwo(ll x) {
  // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2
  // or not
  return (x && !(x & (x - 1)));
}

ll calc(ll x) {
  ll ans = 0;
  for (int i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      if ((x / i) != i)
        ans += 2;
      else
        ans += 1;
    }
  }
  return ans;
}

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll x = n - i;
    ans += calc(x);
  }
  cout << ans;
}

int main() {
  fastio ll tt = 1;
  // cin>>tt;
  while (tt--) {
    solve();
    cout << "\\n";
  }
}