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

#include <bits/stdc++.h>
using namespace std;
#define fast_io                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define forp(i, a, b) for (int i = (a); i < (b); i++)
#define forn(i, a, b) for (int i = (a); i > (b); i--)
#define mod 1000000007
#define multiple_testcases_t                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--) {                                                                \\
    solve();                                                                   \\
  }
#define single_testcase_t solve();
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
vector<int> primes;

int is_prime(long long int n) {
  if (n == 1)
    return 0;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return 0;
    }
  }
  return 1;
}

void SieveOfEratosthenes(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  for (int p = 2; p <= n; p++)
    if (prime[p])
      primes.PB(p);
}

ll divisors(int n) {
  ll ans = 1;
  forp(i, 0, primes.size()) {
    if (primes[i] * primes[i] * primes[i] > n)
      break;
    int c = 1;
    while (n % primes[i] == 0) {
      n /= primes[i];
      c++;
    }
    ans = ans * c;
  }

  if (is_prime(n))
    ans *= 2;
  else if (is_prime((int)sqrt(n)) == 1 && ((int)sqrt(n)) * ((int)sqrt(n)) == n)
    ans *= 3;
  else if (n != 1)
    ans *= 4;

  return ans;
}
// <----- Actual solve() function to print output -----> //
void solve() {
  ll n, p, q, x, y, k = 0;
  cin >> n;
  vector<int> d(1000001);
  SieveOfEratosthenes(1000000);
  forp(i, 1, n) {
    p = divisors(i);
    k = k + p;
    // cout<<i<<" "<<p<<"\\n";
  }
  cout << k;
  // cout<<"\\n";      //if t>1 uncomment this
}

// MAIN function
// --------------------------------------------------------------------------//
int main() {
  fast_io
      //  freopen("input.txt", "r", stdin);
      //  freopen("output.txt", "w", stdout);

      single_testcase_t
      // multiple_testcases_t
      return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast_io                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define forp(i, a, b) for (int i = (a); i < (b); i++)
#define forn(i, a, b) for (int i = (a); i > (b); i--)
#define mod 1000000007
#define multiple_testcases_t                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--) {                                                                \\
    solve();                                                                   \\
  }
#define single_testcase_t solve();
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int divisors(int n) {
  int ans = 0;
  // cout<<sqrt(n)+1;
  forp(i, 1, (int)(sqrt(n) + 1)) {
    if (n % i == 0) {
      if (n / i == i)
        ans++;
      else
        ans += 2;
    }
  }
  return ans;
}
// <----- Actual solve() function to print output -----> //
void solve() {
  ll n, p, q, x, y, k = 0;
  cin >> n;
  k = 1;
  forp(i, 2, n) { k += divisors(i); }
  cout << k;
  // cout<<"\\n";      //if t>1 uncomment this
}

// MAIN function
// --------------------------------------------------------------------------//
int main() {
  fast_io
      //  freopen("input.txt", "r", stdin);
      //  freopen("output.txt", "w", stdout);

      single_testcase_t
      // multiple_testcases_t
      return 0;
}