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


#include <bits/stdc++.h>

#define crap                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CN cout << "No\\n"
#define CY cout << "Yes\\n"

using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// #define MAXN 1000001
// ll sieve[MAXN];
// void makesieve()
// {
//     sieve[1] = 1;
//     for (int i = 2; i < MAXN; i++)
//         sieve[i] = i;
//     for (int i = 4; i < MAXN; i += 2)
//         sieve[i] = 2;
//     for (int i = 3; i * i < MAXN; i++) {
//         if (sieve[i] == i) {
//             for (int j = i * i; j < MAXN; j += i)
//                 if (sieve[j] == j)
//                     sieve[j] = i;
//         }
//     }
// }
ll powerflow(ll x, ll n) {
  ll res;
  if (n == 0) {
    return 1;
  }
  res = powerflow(x, n - 1); // recurse
  if (INT_MAX / res < x) {   // check for overflow
    return -1;               // LONG_MAX;
  }
  return x * res;
}
ll mod = 1000000007;
ll power(ll a, ll b, ll m) {
  a %= m;
  b %= m;
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = (res * a);
    b = b / 2;
    a = (a * a) % mod;
  }
  return res;
}
struct compare {
  bool operator()(const int &l, const int &r) { return l > r; }
};
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 cc(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;
}
void solve() {
  generatePrimeFactors();
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    ans += cc(i);
  }
  cout << ans;
}
int main() {
  crap;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define crap                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CN cout << "No\\n"
#define CY cout << "Yes\\n"

using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// #define MAXN 1000001
// ll sieve[MAXN];
// void makesieve()
// {
//     sieve[1] = 1;
//     for (int i = 2; i < MAXN; i++)
//         sieve[i] = i;
//     for (int i = 4; i < MAXN; i += 2)
//         sieve[i] = 2;
//     for (int i = 3; i * i < MAXN; i++) {
//         if (sieve[i] == i) {
//             for (int j = i * i; j < MAXN; j += i)
//                 if (sieve[j] == j)
//                     sieve[j] = i;
//         }
//     }
// }
ll powerflow(ll x, ll n) {
  ll res;
  if (n == 0) {
    return 1;
  }
  res = powerflow(x, n - 1); // recurse
  if (INT_MAX / res < x) {   // check for overflow
    return -1;               // LONG_MAX;
  }
  return x * res;
}
ll mod = 1000000007;
ll power(ll a, ll b, ll m) {
  a %= m;
  b %= m;
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = (res * a);
    b = b / 2;
    a = (a * a) % mod;
  }
  return res;
}
struct compare {
  bool operator()(const int &l, const int &r) { return l > r; }
};
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j * j <= i; j++) {
      if (i % j == 0) {
        ans++;
        if (i / j != j)
          ans++;
      }
    }
  }
  cout << ans;
}
int main() {
  crap;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}