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

// Hey stalker :)
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)

#define MOD 1000000007

#define FOR(a, c) for (ll(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define FORR(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)
#define all(v) (v).begin(), (v).end()
#define Sort(v) sort(all((v)))
#define rSort(v) sort((v).rbegin(), (v).rend())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (x / gcd(x, y)) * y
#define INF 9223372036854775807
// 18446744073709551615
#define abs llabs
#define by_ANIKET                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define no cout << "No\\n";
#define yes cout << "Yes\\n";

#define Clear(a, b) memset(a, b, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll

#define pr3(a, b, c)                                                           \\
  cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\t" << #c << "=" << c   \\
       << "\\n";
#define pr2(a, b) cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\n";
#define pr1(a) cerr << #a << "=" << a << "\\n";
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define F first
#define S second
#define PB push_back
#define POB pop_back

inline long long max3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}

const ll N = 1000005;
ll n;
bool hhash[1000005];
vi ps;
void s() {
  ll n = N;
  memset(hhash, true, sizeof(hhash));
  for (int p = 2; p * p < n; p++)
    if (hhash[p] == true)
      for (int i = p * 2; i < n; i += p)
        hhash[i] = false;
  ps.PB(2);
  for (int p = 3; p < n; p += 2) {
    if (hhash[p])
      ps.PB(p);
  }
}
ll divCount(ll n) {
  // sieve method for prime calculation

  // Traversing through all prime numbers
  ll total = 1;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ll c = 0;
      while (n % i == 0) {
        n /= i;
        c++;
      }
      total = total * (c + 1);
    }
  }
  if (n != 1)
    total *= 2;
  return total;
}
void solve() {
  ll m, mx, my, t = 0, k = 0, x = 0, y = 0, z = 0, a1, a2, a3, a4, a5, var = 1,
                f = INF, che = 0, ex;
  cin >> n;
  FORL(i, 1, n - 1) {
    x = n - i;
    y = divCount(x);
    z += y;
  }
  cout << z + 1 << "\\n";
}

int main() {
  s();
  // #ifndef ONLINE_JUDGE
  // freopen("Input.txt", "r", stdin);
  // freopen("Output.txt", "w", stdout);
  // freopen("Error.txt","w",stderr);
  // #endif
  by_ANIKET ll T = 1, t = 0;
  // cin>>T;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}
//   ©
//                      ______  __    __ ______ __    __ ________ ________
//                     /      \\|  \\  |  \\      \\  \\  /  \\        \\        \\
//                    |  ▓▓▓▓▓▓\\ ▓▓\\ | ▓▓\\▓▓▓▓▓▓ ▓▓ /  ▓▓ ▓▓▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓
//  ______ ______     | ▓▓__| ▓▓ ▓▓▓\\| ▓▓ | ▓▓ | ▓▓/  ▓▓| ▓▓__      | ▓▓
// |      \\      \\    | ▓▓    ▓▓ ▓▓▓▓\\ ▓▓ | ▓▓ | ▓▓  ▓▓ | ▓▓  \\     | ▓▓
//  \\▓▓▓▓▓▓\\▓▓▓▓▓▓    | ▓▓▓▓▓▓▓▓ ▓▓\\▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓\\ | ▓▓▓▓▓     | ▓▓
//                    | ▓▓  | ▓▓ ▓▓ \\▓▓▓▓_| ▓▓_| ▓▓ \\▓▓\\| ▓▓_____   | ▓▓
//                    | ▓▓  | ▓▓ ▓▓  \\▓▓▓   ▓▓ \\ ▓▓  \\▓▓\\ ▓▓     \\  | ▓▓
//                     \\▓▓   \\▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓▓▓   \\▓▓


Error: TLE

Fixed submission:

// Hey stalker :)
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)

#define MOD 1000000007

#define FOR(a, c) for (ll(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define FORR(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)
#define all(v) (v).begin(), (v).end()
#define Sort(v) sort(all((v)))
#define rSort(v) sort((v).rbegin(), (v).rend())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (x / gcd(x, y)) * y
#define INF 9223372036854775807
// 18446744073709551615
#define abs llabs
#define by_ANIKET                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define no cout << "No\\n";
#define yes cout << "Yes\\n";

#define Clear(a, b) memset(a, b, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll

#define pr3(a, b, c)                                                           \\
  cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\t" << #c << "=" << c   \\
       << "\\n";
#define pr2(a, b) cerr << #a << "=" << a << "\\t" << #b << "=" << b << "\\n";
#define pr1(a) cerr << #a << "=" << a << "\\n";
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \\
  cerr << #A << " : [";                                                        \\
  for (int i = 0; i < N; i++)                                                  \\
    cerr << A[i] << " ";                                                       \\
  cerr << "]\\n";
#define debuga2(A, N, M)                                                       \\
  cerr << #A << " : \\n";                                                       \\
  for (int i = 0; i < N; i++) {                                                \\
    cerr << "[";                                                               \\
    for (int j = 0; j < M; ++j)                                                \\
      cerr << A[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugp(p)                                                              \\
  cerr << #p << " : "                                                          \\
       << "(" << (p).first << "," << (p).second << ")\\n";
#define debugv(v)                                                              \\
  cerr << #v << " : "                                                          \\
       << "[";                                                                 \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cerr << v[i] << " ";                                                       \\
  cerr << "]\\n";
#define debugv2(v)                                                             \\
  cerr << #v << " : \\n";                                                       \\
  for (int i = 0; i < v.size(); i++) {                                         \\
    cerr << "[";                                                               \\
    for (int j = 0; j < (v[0].size()); ++j)                                    \\
      cerr << v[i][j] << " ";                                                  \\
    cerr << "]\\n";                                                             \\
  }
#define debugs(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << *itr << " ";                                                       \\
  cerr << "]\\n";
#define debugm(m)                                                              \\
  cerr << #m << " : [ ";                                                       \\
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \\
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \\
  cerr << "]\\n";
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

#define F first
#define S second
#define PB push_back
#define POB pop_back

inline long long max3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}

const ll N = 100005;
ll n;
const int MAX = 1000001;
int factor[MAX] = {0};
void generatePrimeFactors() {
  factor[1] = 1;

  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 divCount(ll n) {
  // sieve method for prime calculation
  bool hash[n + 1];
  memset(hash, true, sizeof(hash));
  for (int p = 2; p * p < n; p++)
    if (hash[p] == true)
      for (int i = p * 2; i < n; i += p)
        hash[i] = false;

  // Traversing through all prime numbers
  ll total = 1;
  for (int p = 2; p <= n; p++) {
    if (hash[p]) {

      // calculate number of divisor
      // with formula total div =
      // (p1+1) * (p2+1) *.....* (pn+1)
      // where n = (a1^p1)*(a2^p2)....
      // *(an^pn) ai being prime divisor
      // for n and pi are their respective
      // power in factorization
      ll count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }
  return total;
}
void solve() {
  ll m, mx, my, t = 0, k = 0, x = 0, y = 0, z = 0, a1, a2, a3, a4, a5, var = 1,
                f = INF, che = 0, ex;
  cin >> n;
  FORL(i, 1, n - 1) {
    x = n - i;
    y = divCount(x);
    z += y;
  }
  cout << z + 1 << "\\n";
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("Input.txt", "r", stdin);
  // freopen("Output.txt", "w", stdout);
  // freopen("Error.txt","w",stderr);
  // #endif
  by_ANIKET ll T = 1, t = 0;
  // cin>>T;
  while (t++ < T) {
    // cout<<"Case #"<<t<<":"<<' ';
    solve();
    // cout<<'\\n';
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}
//   ©
//                      ______  __    __ ______ __    __ ________ ________
//                     /      \\|  \\  |  \\      \\  \\  /  \\        \\        \\
//                    |  ▓▓▓▓▓▓\\ ▓▓\\ | ▓▓\\▓▓▓▓▓▓ ▓▓ /  ▓▓ ▓▓▓▓▓▓▓▓\\▓▓▓▓▓▓▓▓
//  ______ ______     | ▓▓__| ▓▓ ▓▓▓\\| ▓▓ | ▓▓ | ▓▓/  ▓▓| ▓▓__      | ▓▓
// |      \\      \\    | ▓▓    ▓▓ ▓▓▓▓\\ ▓▓ | ▓▓ | ▓▓  ▓▓ | ▓▓  \\     | ▓▓
//  \\▓▓▓▓▓▓\\▓▓▓▓▓▓    | ▓▓▓▓▓▓▓▓ ▓▓\\▓▓ ▓▓ | ▓▓ | ▓▓▓▓▓\\ | ▓▓▓▓▓     | ▓▓
//                    | ▓▓  | ▓▓ ▓▓ \\▓▓▓▓_| ▓▓_| ▓▓ \\▓▓\\| ▓▓_____   | ▓▓
//                    | ▓▓  | ▓▓ ▓▓  \\▓▓▓   ▓▓ \\ ▓▓  \\▓▓\\ ▓▓     \\  | ▓▓
//                     \\▓▓   \\▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓\\▓▓   \\▓▓\\▓▓▓▓▓▓▓▓   \\▓▓