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

#include <bits/stdc++.h>
using namespace std;
// long long int i,j,k,n,m,a,b,c,d,p,q,r,l,t=1;
// long long int sum,counted,ans;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
void sample() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int *read(long long int n) {
  long long int *arr = new long long int[n];

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  return arr;
}
long long int countDivisors(long long int n) {
  long long int cnt = 0;
  for (long long int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal,
      // count only one
      if (n / i == i)
        cnt++;

      else // Otherwise count both
        cnt = cnt + 2;
    }
  }
  return cnt;
}

void solve() {
  long long int n, i, j, k, ans, m, o;

  cin >> n;

  // long long int ar[n];

  // long long int* ar=read(n);

  ans = 0;

  for (i = 1; i < n; i++) {
    ans += (n / i);
    if (n % i == 0)
      ans--;
  }

  cout << ans;

  return;
}

int main() {
  fast();
  sample();
  long long int i, j, k, n, m, a, b, c, d, p, q, r, l, t = 1;
  long long int sum, count = 0, ans = 0;

  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// long long int i,j,k,n,m,a,b,c,d,p,q,r,l,t=1;
// long long int sum,counted,ans;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
void sample() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
long long int *read(long long int n) {
  long long int *arr = new long long int[n];

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  return arr;
}
long long int countDivisors(long long int n) {
  long long int cnt = 0;
  for (long long int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal,
      // count only one
      if (n / i == i)
        cnt++;

      else // Otherwise count both
        cnt = cnt + 2;
    }
  }
  return cnt;
}

int divCount(int 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
  int 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
      int count = 0;
      if (n % p == 0) {
        while (n % p == 0) {
          n = n / p;
          count++;
        }
        total = total * (count + 1);
      }
    }
  }
  return total;
}

void solve() {
  long long int n, i, j, k, ans, m, o;

  cin >> n;

  // long long int ar[n];

  // long long int* ar=read(n);

  ans = 0;

  for (i = 1; i < n; i++) {
    ans += countDivisors(i);
  }

  cout << ans;

  return;
}

int main() {
  fast();
  sample();
  long long int i, j, k, n, m, a, b, c, d, p, q, r, l, t = 1;
  long long int sum, count = 0, ans = 0;

  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}