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

/**
 *    author:  ritik_0007
 *    created:  just assume some time
 *    college :IIIT Ranchi
 *	  3rd year(5ht sem)
 **/

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define scan(arr, n)                                                           \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair<int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin(), a.end()
#define faster                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
// Constants
const int mod = 1000000007; /* 1e9 + 7*/
const int MAXN = 1000005;   /*1e6 +5 */

int spf[1000009]; // stores the smallest prime factor of a number
const int siz = 100001;
vi adj[siz];
int fac[1000001] = {0};
int depth[siz];
int diameter = 0;

void print(vi v) {
  for (auto x : v)
    cout << x << " ";
  cout << endl;
}

// to find diameter of a tree and distance between two nodes in a tree.
// dist between a and b is depth[b] , Initialise depth[a]=0 for each test case.
int dfs(int u, int par) {
  int len = 0;
  for (auto x : adj[u]) {
    if (x != par) {
      depth[x] = depth[u] + 1;
      int cur = 1 + dfs(x, u);
      diameter = max(diameter, cur + len);
      len = max(len, cur);
    }
  }
  return len;
}

void fact() {
  fac[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fac[i] = (fac[i - 1] % mod * i % mod) % mod;
}
bool prime[1000001];
void SieveOfEratosthenes() {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  int n = 1000001;
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // Print all prime numbers
}

// Function to count divisors

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;
}

//	vi v(1000001);

void solve() {
  generatePrimeFactors();
  int n;
  cin >> n;
  int ans = 0;
  // v[1]=1;

  // for(int i=2;i<=1000000;i++)
  // {
  // 	if(prime[i])
  // 		 v[i]=2;
  // 	else
  // 	{
  // 		int st=maxPrimeFactors(i);
  // 		v[i]=v[st]+v[i/st];
  // 	}
  // }

  // loop(i,1,11)
  // cout<<v[i]<<" ";
  // cout<<endl;
  for (int i = 1; i <= n - 1; i++) {
    ans += calculateNoOFactors(i);
  }

  cout << ans << endl;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fact();
  faster;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

/**
 *    author:  ritik_0007
 *    created:  just assume some time
 *    college :IIIT Ranchi
 *	  3rd year(5ht sem)
 **/

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define scan(arr, n)                                                           \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair<int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin(), a.end()
#define faster                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
// Constants
const int mod = 1000000007; /* 1e9 + 7*/
const int MAXN = 1000005;   /*1e6 +5 */

int spf[1000009]; // stores the smallest prime factor of a number
const int siz = 100001;
vi adj[siz];
int fac[1000001] = {0};
int depth[siz];
int diameter = 0;

void print(vi v) {
  for (auto x : v)
    cout << x << " ";
  cout << endl;
}

// to find diameter of a tree and distance between two nodes in a tree.
// dist between a and b is depth[b] , Initialise depth[a]=0 for each test case.
int dfs(int u, int par) {
  int len = 0;
  for (auto x : adj[u]) {
    if (x != par) {
      depth[x] = depth[u] + 1;
      int cur = 1 + dfs(x, u);
      diameter = max(diameter, cur + len);
      len = max(len, cur);
    }
  }
  return len;
}

void fact() {
  fac[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fac[i] = (fac[i - 1] % mod * i % mod) % mod;
}
bool prime[1000001];
void SieveOfEratosthenes() {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  int n = 1000001;
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // Print all prime numbers
}

// Function to count divisors
int divCount(int n) {
  // sieve method for prime calculation

  // Traversing through all prime numbers
  int total = 1;
  for (int p = 2; p <= n; p++) {
    if (prime[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;
}

// vi v(1000001);

void solve() {
  SieveOfEratosthenes();
  int n;
  cin >> n;
  int ans = 0;
  // v[1]=1;

  // for(int i=2;i<=1000000;i++)
  // {
  // 	if(prime[i])
  // 		 v[i]=2;
  // 	else
  // 	{
  // 		int st=smallestDivisor(i);
  // 		v[i]=v[i/st]+1;
  // 	}
  // }

  // loop(i,1,11)
  // cout<<v[i]<<" ";
  // cout<<endl;
  // for(int i=1;i<=n-1;i++)
  // 	ans+=v[i];
  loop(i, 1, n) { ans += divCount(i); }
  cout << ans << endl;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fact();
  faster;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}