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

#include <bits/stdc++.h>

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target("avx")

typedef long long ll;
typedef long double ld;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define endl "\\n"
#define mpq priority_queue<int, vector<int>, greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t, int64_t>
#define ftt                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int tt = 1; tt <= t; ++tt)
#define forr(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define Rev(v) reverse(v.begin(), v.end());
#define Sort(v) sort(v.begin(), v.end());
#define Sum(v) accumulate(v.begin(), v.end(), 0)
#define Input(v)                                                               \\
  for (int &x : v)                                                             \\
    cin >> x;
#define mem(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << "\\nhi" << x << "\\n"
#define pq priority_queue<int>
#define dbg cout << "\\nhi\\n"
#define sayy cout << "YES\\n"
#define sayn cout << "NO\\n"
#define int long long
#define double long double
#define pii pair<int, int>
#define mii map<int, int>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define mp make_pair

const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

using namespace std;

int fpow(int x, int y) {
  int temp;
  if (y == 0)
    return 1;
  temp = fpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void sieve(int n) {
  bool prime[5 * LIM];
  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;
  prime[1] = 0;
}

inline int read() {
  int p = 1, ans = 0;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      p = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    ans = (ans << 3) + (ans << 1) + ch - 48;
    ch = getchar();
  }
  return ans * p;
}

bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

map<int, int> primeFactors(int n) {
  map<int, int> mp;
  while (n % 2 == 0) {
    if (mp.count(2) == 1)
      mp[2]++;
    else
      mp.insert({2, 1});
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      if (mp.count(i) == 1)
        mp[i]++;
      else
        mp.insert({i, 1});
      n = n / i;
    }
  }
  if (n > 2) {
    if (mp.count(n) == 1)
      mp[n]++;
    else
      mp.insert({n, 1});
  }
  return mp;
}

//*************************************************************************************************************************

int32_t main() {
  fio;

  int n, ans = 0;
  cin >> n;
  for (int x = 1; x <= n; x++) {
    int y = n / x;
    ans += (y * (y + 1) * x) / 2;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC target("avx")

typedef long long ll;
typedef long double ld;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define endl "\\n"
#define mpq priority_queue<int, vector<int>, greater<int>>
#define debug(a) cerr << #a << ": " << a << endl
#define umii unordered_map<int64_t, int64_t>
#define ftt                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int tt = 1; tt <= t; ++tt)
#define forr(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define Rev(v) reverse(v.begin(), v.end());
#define Sort(v) sort(v.begin(), v.end());
#define Sum(v) accumulate(v.begin(), v.end(), 0)
#define Input(v)                                                               \\
  for (int &x : v)                                                             \\
    cin >> x;
#define mem(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << "\\nhi" << x << "\\n"
#define pq priority_queue<int>
#define dbg cout << "\\nhi\\n"
#define sayy cout << "YES\\n"
#define sayn cout << "NO\\n"
#define int long long
#define double long double
#define pii pair<int, int>
#define mii map<int, int>
#define vb vector<bool>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define mp make_pair

const int MOD = 1000000007;
const double PI = 3.1415926535898;
const int LIM = 100005;

using namespace std;

int fpow(int x, int y) {
  int temp;
  if (y == 0)
    return 1;
  temp = fpow(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

void sieve(int n) {
  bool prime[5 * LIM];
  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;
  prime[1] = 0;
}

inline int read() {
  int p = 1, ans = 0;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      p = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    ans = (ans << 3) + (ans << 1) + ch - 48;
    ch = getchar();
  }
  return ans * p;
}

bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

map<int, int> primeFactors(int n) {
  map<int, int> mp;
  while (n % 2 == 0) {
    if (mp.count(2) == 1)
      mp[2]++;
    else
      mp.insert({2, 1});
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      if (mp.count(i) == 1)
        mp[i]++;
      else
        mp.insert({i, 1});
      n = n / i;
    }
  }
  if (n > 2) {
    if (mp.count(n) == 1)
      mp[n]++;
    else
      mp.insert({n, 1});
  }
  return mp;
}

void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
  // 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.
  for (int i = 2; i <= n; i++)
    prime[i] = true;

  // Create a boolean array "primesquare[0..n*n+1]"
  // and initialize all entries it as false. A value
  // in squareprime[i] will finally be true if i is
  // square of prime, else false.
  for (int i = 0; i <= (n * n + 1); i++)
    primesquare[i] = false;

  // 1 is not a prime number
  prime[1] = false;

  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
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    }
  }

  int j = 0;
  for (int p = 2; p <= n; p++) {
    if (prime[p]) {
      // Storing primes in an array
      a[j] = p;

      // Update value in primesquare[p*p],
      // if p is prime.
      primesquare[p * p] = true;
      j++;
    }
  }
}

// Function to count divisors
int countDivisors(int n) {
  // If number is 1, then it will have only 1
  // as a factor. So, total factors will be 1.
  if (n == 1)
    return 1;

  bool prime[n + 1], primesquare[n * n + 1];

  int a[n]; // for storing primes upto n

  // Calling SieveOfEratosthenes to store prime
  // factors of n and to store square of prime
  // factors of n
  SieveOfEratosthenes(n, prime, primesquare, a);

  // ans will contain total number of distinct
  // divisors
  int ans = 1;

  // Loop for counting factors of n
  for (int i = 0;; i++) {
    // a[i] is not less than cube root n
    if (a[i] * a[i] * a[i] > n)
      break;

    // Calculating power of a[i] in n.
    int cnt = 1;          // cnt is power of prime a[i] in n.
    while (n % a[i] == 0) // if a[i] is a factor of n
    {
      n = n / a[i];
      cnt = cnt + 1; // incrementing power
    }

    // Calculating the number of divisors
    // If n = a^p * b^q then total divisors of n
    // are (p+1)*(q+1)
    ans = ans * cnt;
  }

  // if a[i] is greater than cube root of n

  // First case
  if (prime[n])
    ans = ans * 2;

  // Second case
  else if (primesquare[n])
    ans = ans * 3;

  // Third case
  else if (n != 1)
    ans = ans * 4;

  return ans; // Total divisors
}

//*************************************************************************************************************************

int32_t main() {
  fio;

  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int cnt = countDivisors(i);
    ans += i * cnt;
  }
  cout << ans;
  return 0;
}