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



#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define rep(i, n, z) for (int i = z; i < n; i++)
#define rrep(i, z) for (int i = z; i >= 0; i--)
#define nl cout << endl
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define vvlli vector<vlli>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair<int, int>
#define llipair pair<lli, lli>
#define pq priority_queue
#define displaymatrix(a, m, n)                                                 \\
  for (int i = 0; i < m; i++) {                                                \\
    for (int j = 0; j < n; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define printarray(a, n)                                                       \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  nl;
#define vinput(a, n)                                                           \\
  vlli a(n);                                                                   \\
  rep(i, n, 0) cin >> a[i]
#define ainput(a, n) rep(i, n, 0) cin >> a[i]
#define SO(a) sort(a.begin(), a.end())
#define all(x) (x).begin(), (x).end()
#define SOP(a, comp) sort(a.begin(), a.end(), comp)
#define inf INT_MAX
#define endl '\\n'

vector<int> vis;
vector<vector<int>> adj;
vector<long double> dis;
lli val;
lli paths;

const int MAX = 1000001;
int factor[MAX] = {0};

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

int calculateNoOFactors(int n) {
  if (n == 1)
    return 1;

  int ans = 1;

  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;
    }
    j = j / factor[j];
  }
  ans = ans * (c + 1);
  return ans;
}

void solve() {
  int n;
  cin >> n;
  lli ans = 0;
  int dp[n + 1];
  memset(dp, 0, sizeof(dp));
  generatePrimeFactors();
  for (int i = 1; i < n; i++) {
    dp[i] = calculateNoOFactors(i);
    int temp = sqrt(i);
    // if(temp * temp == i&& i > 1) dp[i]--;
  }
  for (int i = 1; i < n; i++)
    ans += dp[i];
  // printarray(dp, n+1);
  cout << ans << endl;
}

int main() {
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:



#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define lli long long int
#define rep(i, n, z) for (int i = z; i < n; i++)
#define rrep(i, z) for (int i = z; i >= 0; i--)
#define nl cout << endl
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define vvlli vector<vlli>
#define umap unordered_map
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define ipair pair<int, int>
#define llipair pair<lli, lli>
#define pq priority_queue
#define displaymatrix(a, m, n)                                                 \\
  for (int i = 0; i < m; i++) {                                                \\
    for (int j = 0; j < n; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }
#define printarray(a, n)                                                       \\
  for (int i = 0; i < n; i++) {                                                \\
    cout << a[i] << " ";                                                       \\
  }                                                                            \\
  nl;
#define vinput(a, n)                                                           \\
  vlli a(n);                                                                   \\
  rep(i, n, 0) cin >> a[i]
#define ainput(a, n) rep(i, n, 0) cin >> a[i]
#define SO(a) sort(a.begin(), a.end())
#define all(x) (x).begin(), (x).end()
#define SOP(a, comp) sort(a.begin(), a.end(), comp)
#define inf INT_MAX
#define endl '\\n'

vector<int> vis;
vector<vector<int>> adj;
vector<long double> dis;
lli val;
lli paths;

void dfs(int v, int p, int lev) {
  // vis[v] = 1;
  // cout<<"NODE STARTED "<<v<<endl;
  int flag = 0;
  for (auto u : adj[v]) {
    if (u != p) {
      flag++;
      dfs(u, v, lev + 1);
      dis[v] += dis[u];
    }
  }
  if (flag == 0) {
    dis[v] = 0;
  } else {
    dis[v] = (dis[v] / flag) + 1;
  }
}

void solve() {
  int n;
  cin >> n;
  lli ans = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= sqrt(i); j++) {
      if (i % j == 0 && i / j != j) {
        // cout<<j<<' '<<i/j<<' '<<n-i<<endl;
        ans += 2;
      } else if (i % j == 0 && i / j == j) {
        // cout<<j<<' '<<i/j<<' '<<n-i<<endl;
        ans += 1;
      }
    }
  }
  cout << ans << endl;
}

int main() {
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();
}