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

// NAME-DEEPESH BHATT
// Institute-IIT Dhanbad
// MOTTO-THINK FIRST,CODE LATER

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1E9 + 7;
/*#define endl '\\n'*/
#define pb push_back
#define mp make_pair

// Check for signed int overflow
//  use __builtin_popcount(num) to calculate no.of setbits in num
// s.lower_bound() works in O(logn) whereas lower_bound(s.begin(),s.end(),x)
// works in O(n)

// Euclid's Algo
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
// Extended Euclid's Algo
ll gcd_ex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  ll g = gcd_ex(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return g;
}
// Multiplicative Modulo Inv
ll multi_mod_inv(ll a, ll m) {
  ll x, y;
  ll g = gcd_ex(a, m, x, y);
  if (g != 1) {
    return -1;
  }
  x = (x % m + m) % m;
  return x;
}
// Mod division
ll mod_div(ll a, ll b, ll m) {
  //(a/b)mod m?
  if (multi_mod_inv(b, m) != -1) {
    return (a % m * multi_mod_inv(b, m) % m) % m;
  } else {
    return -1;
  }
}
// lcm
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// Binary Exponentiation
ll power(ll a, ll n) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2);
  p = (p * p) % MOD;
  if (n % 2)
    return (p * a) % MOD;
  else
    return p % MOD;
}
// Euler Totient Function
ll phi[1000001] = {0};
void euler_totient(ll n) {
  for (ll i = 0; i <= n; i++) {
    phi[i] = i;
  }
  phi[1] = 1;
  for (ll i = 2; i <= n; i++) {
    if (i == phi[i]) {
      phi[i] = i - 1; // For a prime number phi(p)=p-1
      for (ll j = 2 * i; j <= n; j += i) {
        phi[j] = phi[j] * (i - 1) / i;
      }
    }
  }
}
/*void sieve(vector<ll>&is_prime){
    is_prime[0]=is_prime[1]=0;
    for(ll i=2;i*i<=1000000;i++){
        if(is_prime[i]){
            for(ll j=i*i;j<=1000000;j+=i){
                is_prime[j]=0;
            }
        }
    }
}*/
int32_t main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  ios_base::sync_with_stdio(false); // for fast I/O operation
  cin.tie(nullptr);
  ll t = 1;
  /*cin>>t;*/
  while (t--) {
    ll n;
    cin >> n;
    /*vector<ll>is_prime(1000001,1);
    sieve(is_prime);*/
    vector<ll> ans(n, 2);
    ans[1] = 1;
    for (ll i = 2; i * i <= n; i++) {
      for (ll j = i * i; j <= n; j += i) {
        if (i == j / i)
          ans[j]++;
        else {
          ans[j] += 2;
        }
      }
    }
    ll sum = 0;
    for (ll j = 1; j <= n - 1; j++) {
      sum += ans[j];
      /*cout<<ans[j]<<" ";*/
    }
    /*cout<<endl;*/
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// NAME-DEEPESH BHATT
// Institute-IIT Dhanbad
// MOTTO-THINK FIRST,CODE LATER

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1E9 + 7;
/*#define endl '\\n'*/
#define pb push_back
#define mp make_pair

// Check for signed int overflow
//  use __builtin_popcount(num) to calculate no.of setbits in num
// s.lower_bound() works in O(logn) whereas lower_bound(s.begin(),s.end(),x)
// works in O(n)

// Euclid's Algo
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
// Extended Euclid's Algo
ll gcd_ex(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll x1, y1;
  ll g = gcd_ex(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return g;
}
// Multiplicative Modulo Inv
ll multi_mod_inv(ll a, ll m) {
  ll x, y;
  ll g = gcd_ex(a, m, x, y);
  if (g != 1) {
    return -1;
  }
  x = (x % m + m) % m;
  return x;
}
// Mod division
ll mod_div(ll a, ll b, ll m) {
  //(a/b)mod m?
  if (multi_mod_inv(b, m) != -1) {
    return (a % m * multi_mod_inv(b, m) % m) % m;
  } else {
    return -1;
  }
}
// lcm
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// Binary Exponentiation
ll power(ll a, ll n) {
  if (n == 0)
    return 1;
  ll p = power(a, n / 2);
  p = (p * p) % MOD;
  if (n % 2)
    return (p * a) % MOD;
  else
    return p % MOD;
}
// Euler Totient Function
ll phi[1000001] = {0};
void euler_totient(ll n) {
  for (ll i = 0; i <= n; i++) {
    phi[i] = i;
  }
  phi[1] = 1;
  for (ll i = 2; i <= n; i++) {
    if (i == phi[i]) {
      phi[i] = i - 1; // For a prime number phi(p)=p-1
      for (ll j = 2 * i; j <= n; j += i) {
        phi[j] = phi[j] * (i - 1) / i;
      }
    }
  }
}
void sieve(vector<ll> &is_prime) {
  is_prime[0] = is_prime[1] = 0;
  for (ll i = 2; i * i <= 1000000; i++) {
    if (is_prime[i]) {
      for (ll j = i * i; j <= 1000000; j += i) {
        is_prime[j] = 0;
      }
    }
  }
}
int32_t main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  ios_base::sync_with_stdio(false); // for fast I/O operation
  cin.tie(nullptr);
  ll t = 1;
  /*cin>>t;*/
  while (t--) {
    ll n;
    cin >> n;
    vector<ll> is_prime(1000001, 1);
    sieve(is_prime);
    ll count = 0;
    for (ll j = 1; j <= n - 1; j++) {
      ll c = 0;
      if (is_prime[j] == 1) {
        c += 2;
      } else {
        for (ll i = 1; i * i <= j; i++) {
          if (j % i == 0) {
            if (i == j / i) {
              c++;
            } else {
              c += 2;
            }
          }
        }
      }
      /*cout<<c<<" ";*/
      count += c;
    }
    cout << endl << count << endl;
  }
  return 0;
}